From eefd6593fa59b1a6828dd006edf8584f15919d23 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 28 May 2014 10:08:02 -0700 Subject: [PATCH] Fix #61: Ensure curl error is correctly set when making GET calls in parallel --- src/Curl/Curl.php | 7 ++----- tests/PHPCurlClass/PHPCurlClassTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index ea606f5..9772821 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -82,9 +82,8 @@ class Curl $status = curl_multi_exec($curl_multi, $active); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); - //getting curl_error_codes of children - while (($info_array = curl_multi_info_read($curl_multi)) !== FALSE) { - if ($info_array['msg'] !== CURLMSG_DONE) { + while (!($info_array = curl_multi_info_read($curl_multi)) === false) { + if (!($info_array['msg'] === CURLMSG_DONE)) { continue; } foreach ($this->curls as $ch) { @@ -400,8 +399,6 @@ class Curl if ($ch->multi_child) { $ch->response = curl_multi_getcontent($ch->curl); - //curl_errno() cannot be used with multi child handle - //Alternatively, use curl_multi_info_read() on parent handle (see get()) } else { $ch->response = curl_exec($ch->curl); $ch->curl_error_code = curl_errno($ch->curl); diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 0394a43..33e55f8 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -674,6 +674,23 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertTrue(substr($curl->curls['2']->response, - $len) === '/c/?foo=bar'); } + public function testParallelRequestErrors() + { + $test = new Test(); + $curl = $test->curl; + $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 4000); + $curl->complete(function ($instance) use (&$success_called, &$error_called, &$complete_called) { + PHPUnit_Framework_Assert::assertTrue($instance->error); + PHPUnit_Framework_Assert::assertTrue($instance->curl_error); + PHPUnit_Framework_Assert::assertTrue($instance->curl_error_code === CURLE_OPERATION_TIMEOUTED); + }); + $curl->get(array( + Test::ERROR_URL . 'a/', + Test::ERROR_URL . 'b/', + Test::ERROR_URL . 'c/', + )); + } + public function testParallelSetOptions() { $test = new Test();