Skip to content

Commit 3ef8ab7

Browse files
authored
Merge pull request php-vcr#151 from thormeier/fix-parallel-curl-requests
Fix parallel cURL requests
2 parents 1a10017 + 92c9221 commit 3ef8ab7

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/VCR/LibraryHooks/CurlHook.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class CurlHook implements LibraryHook
4646
protected static $multiHandles = array();
4747

4848
/**
49-
* @var resource Last active curl_multi_exec() handle.
49+
* @var array Last active curl_multi_exec() handles.
5050
*/
51-
protected static $multiExecLastCh;
51+
protected static $multiExecLastChs = array();
5252

5353
/**
5454
* @var AbstractCodeTransform
@@ -253,7 +253,7 @@ public static function curlMultiExec($multiHandle, &$stillRunning)
253253
if (isset(self::$multiHandles[(int) $multiHandle])) {
254254
foreach (self::$multiHandles[(int) $multiHandle] as $curlHandle) {
255255
if (!isset(self::$responses[(int) $curlHandle])) {
256-
self::$multiExecLastCh = $curlHandle;
256+
self::$multiExecLastChs[] = $curlHandle;
257257
self::curlExec($curlHandle);
258258
}
259259
}
@@ -271,13 +271,12 @@ public static function curlMultiExec($multiHandle, &$stillRunning)
271271
*/
272272
public static function curlMultiInfoRead()
273273
{
274-
if (self::$multiExecLastCh) {
274+
if (!empty(self::$multiExecLastChs)) {
275275
$info = array(
276276
'msg' => CURLMSG_DONE,
277-
'handle' => self::$multiExecLastCh,
277+
'handle' => array_pop(self::$multiExecLastChs),
278278
'result' => CURLE_OK
279279
);
280-
self::$multiExecLastCh = null;
281280

282281
return $info;
283282
}

tests/VCR/LibraryHooks/CurlHookTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ function (Request $request) use ($testClass, &$callCount) {
266266
curl_multi_add_handle($curlMultiHandle, $curlHandle2);
267267

268268
$mh = curl_multi_exec($curlMultiHandle);
269-
$lastInfo = curl_multi_info_read($mh);
270-
$afterLastInfo = curl_multi_info_read($mh);
269+
270+
$lastInfo = curl_multi_info_read($mh);
271+
$secondLastInfo = curl_multi_info_read($mh);
272+
$afterLastInfo = curl_multi_info_read($mh);
271273

272274
curl_multi_remove_handle($curlMultiHandle, $curlHandle1);
273275
curl_multi_remove_handle($curlMultiHandle, $curlHandle2);
@@ -281,6 +283,13 @@ function (Request $request) use ($testClass, &$callCount) {
281283
$lastInfo,
282284
'When called the first time curl_multi_info_read should return last curl info.'
283285
);
286+
287+
$this->assertEquals(
288+
array('msg' => 1, 'result' => 0, 'handle' => $curlHandle1),
289+
$secondLastInfo,
290+
'When called the second time curl_multi_info_read should return second to last curl info.'
291+
);
292+
284293
$this->assertFalse($afterLastInfo, 'Multi info called the last time should return false.');
285294
}
286295

0 commit comments

Comments
 (0)