CurlMultiHandlerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Client;
  3. use GuzzleHttp\Ring\Client\CurlMultiHandler;
  4. class CurlMultiHandlerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testSendsRequest()
  7. {
  8. Server::enqueue([['status' => 200]]);
  9. $a = new CurlMultiHandler();
  10. $response = $a([
  11. 'http_method' => 'GET',
  12. 'headers' => ['host' => [Server::$host]],
  13. ]);
  14. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  15. $this->assertEquals(200, $response['status']);
  16. $this->assertArrayHasKey('transfer_stats', $response);
  17. $realUrl = trim($response['transfer_stats']['url'], '/');
  18. $this->assertEquals(trim(Server::$url, '/'), $realUrl);
  19. $this->assertArrayHasKey('effective_url', $response);
  20. $this->assertEquals(
  21. trim(Server::$url, '/'),
  22. trim($response['effective_url'], '/')
  23. );
  24. }
  25. public function testCreatesErrorResponses()
  26. {
  27. $url = 'http://localhost:123/';
  28. $a = new CurlMultiHandler();
  29. $response = $a([
  30. 'http_method' => 'GET',
  31. 'headers' => ['host' => ['localhost:123']],
  32. ]);
  33. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  34. $this->assertNull($response['status']);
  35. $this->assertNull($response['reason']);
  36. $this->assertEquals([], $response['headers']);
  37. $this->assertArrayHasKey('error', $response);
  38. $this->assertContains('cURL error ', $response['error']->getMessage());
  39. $this->assertArrayHasKey('transfer_stats', $response);
  40. $this->assertEquals(
  41. trim($url, '/'),
  42. trim($response['transfer_stats']['url'], '/')
  43. );
  44. $this->assertArrayHasKey('effective_url', $response);
  45. $this->assertEquals(
  46. trim($url, '/'),
  47. trim($response['effective_url'], '/')
  48. );
  49. }
  50. public function testSendsFuturesWhenDestructed()
  51. {
  52. Server::enqueue([['status' => 200]]);
  53. $a = new CurlMultiHandler();
  54. $response = $a([
  55. 'http_method' => 'GET',
  56. 'headers' => ['host' => [Server::$host]],
  57. ]);
  58. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  59. $a->__destruct();
  60. $this->assertEquals(200, $response['status']);
  61. }
  62. public function testCanSetMaxHandles()
  63. {
  64. $a = new CurlMultiHandler(['max_handles' => 2]);
  65. $this->assertEquals(2, $this->readAttribute($a, 'maxHandles'));
  66. }
  67. public function testCanSetSelectTimeout()
  68. {
  69. $a = new CurlMultiHandler(['select_timeout' => 2]);
  70. $this->assertEquals(2, $this->readAttribute($a, 'selectTimeout'));
  71. }
  72. public function testSendsFuturesWhenMaxHandlesIsReached()
  73. {
  74. $request = [
  75. 'http_method' => 'PUT',
  76. 'headers' => ['host' => [Server::$host]],
  77. 'future' => 'lazy', // passing this to control the test
  78. ];
  79. $response = ['status' => 200];
  80. Server::flush();
  81. Server::enqueue([$response, $response, $response]);
  82. $a = new CurlMultiHandler(['max_handles' => 3]);
  83. for ($i = 0; $i < 5; $i++) {
  84. $responses[] = $a($request);
  85. }
  86. $this->assertCount(3, Server::received());
  87. $responses[3]->cancel();
  88. $responses[4]->cancel();
  89. }
  90. public function testCanCancel()
  91. {
  92. Server::flush();
  93. $response = ['status' => 200];
  94. Server::enqueue(array_fill_keys(range(0, 10), $response));
  95. $a = new CurlMultiHandler();
  96. $responses = [];
  97. for ($i = 0; $i < 10; $i++) {
  98. $response = $a([
  99. 'http_method' => 'GET',
  100. 'headers' => ['host' => [Server::$host]],
  101. 'future' => 'lazy',
  102. ]);
  103. $response->cancel();
  104. $responses[] = $response;
  105. }
  106. $this->assertCount(0, Server::received());
  107. foreach ($responses as $response) {
  108. $this->assertTrue($this->readAttribute($response, 'isRealized'));
  109. }
  110. }
  111. public function testCannotCancelFinished()
  112. {
  113. Server::flush();
  114. Server::enqueue([['status' => 200]]);
  115. $a = new CurlMultiHandler();
  116. $response = $a([
  117. 'http_method' => 'GET',
  118. 'headers' => ['host' => [Server::$host]],
  119. ]);
  120. $response->wait();
  121. $response->cancel();
  122. }
  123. public function testDelaysInParallel()
  124. {
  125. Server::flush();
  126. Server::enqueue([['status' => 200]]);
  127. $a = new CurlMultiHandler();
  128. $expected = microtime(true) + (100 / 1000);
  129. $response = $a([
  130. 'http_method' => 'GET',
  131. 'headers' => ['host' => [Server::$host]],
  132. 'client' => ['delay' => 100],
  133. ]);
  134. $response->wait();
  135. $this->assertGreaterThanOrEqual($expected, microtime(true));
  136. }
  137. public function testSendsNonLazyFutures()
  138. {
  139. $request = [
  140. 'http_method' => 'GET',
  141. 'headers' => ['host' => [Server::$host]],
  142. 'future' => true,
  143. ];
  144. Server::flush();
  145. Server::enqueue([['status' => 202]]);
  146. $a = new CurlMultiHandler();
  147. $response = $a($request);
  148. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  149. $this->assertEquals(202, $response['status']);
  150. }
  151. public function testExtractsErrors()
  152. {
  153. $request = [
  154. 'http_method' => 'GET',
  155. 'headers' => ['host' => ['127.0.0.1:123']],
  156. 'future' => true,
  157. ];
  158. Server::flush();
  159. Server::enqueue([['status' => 202]]);
  160. $a = new CurlMultiHandler();
  161. $response = $a($request);
  162. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  163. $this->assertEquals(CURLE_COULDNT_CONNECT, $response['curl']['errno']);
  164. $this->assertNotEmpty($response['curl']['error']);
  165. }
  166. }