PoolTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. use GuzzleHttp\Exception\ClientException;
  4. use GuzzleHttp\Handler\MockHandler;
  5. use GuzzleHttp\HandlerStack;
  6. use GuzzleHttp\Pool;
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Psr7\Request;
  9. use GuzzleHttp\Psr7\Response;
  10. use GuzzleHttp\Promise\Promise;
  11. use Psr\Http\Message\RequestInterface;
  12. use PHPUnit\Framework\TestCase;
  13. class PoolTest extends TestCase
  14. {
  15. /**
  16. * @expectedException \InvalidArgumentException
  17. */
  18. public function testValidatesIterable()
  19. {
  20. $p = new Pool(new Client(), 'foo');
  21. $p->promise()->wait();
  22. }
  23. /**
  24. * @expectedException \InvalidArgumentException
  25. */
  26. public function testValidatesEachElement()
  27. {
  28. $c = new Client();
  29. $requests = ['foo'];
  30. $p = new Pool($c, new \ArrayIterator($requests));
  31. $p->promise()->wait();
  32. }
  33. /**
  34. * @doesNotPerformAssertions
  35. */
  36. public function testSendsAndRealizesFuture()
  37. {
  38. $c = $this->getClient();
  39. $p = new Pool($c, [new Request('GET', 'http://example.com')]);
  40. $p->promise()->wait();
  41. }
  42. /**
  43. * @doesNotPerformAssertions
  44. */
  45. public function testExecutesPendingWhenWaiting()
  46. {
  47. $r1 = new Promise(function () use (&$r1) { $r1->resolve(new Response()); });
  48. $r2 = new Promise(function () use (&$r2) { $r2->resolve(new Response()); });
  49. $r3 = new Promise(function () use (&$r3) { $r3->resolve(new Response()); });
  50. $handler = new MockHandler([$r1, $r2, $r3]);
  51. $c = new Client(['handler' => $handler]);
  52. $p = new Pool($c, [
  53. new Request('GET', 'http://example.com'),
  54. new Request('GET', 'http://example.com'),
  55. new Request('GET', 'http://example.com'),
  56. ], ['pool_size' => 2]);
  57. $p->promise()->wait();
  58. }
  59. public function testUsesRequestOptions()
  60. {
  61. $h = [];
  62. $handler = new MockHandler([
  63. function (RequestInterface $request) use (&$h) {
  64. $h[] = $request;
  65. return new Response();
  66. }
  67. ]);
  68. $c = new Client(['handler' => $handler]);
  69. $opts = ['options' => ['headers' => ['x-foo' => 'bar']]];
  70. $p = new Pool($c, [new Request('GET', 'http://example.com')], $opts);
  71. $p->promise()->wait();
  72. $this->assertCount(1, $h);
  73. $this->assertTrue($h[0]->hasHeader('x-foo'));
  74. }
  75. public function testCanProvideCallablesThatReturnResponses()
  76. {
  77. $h = [];
  78. $handler = new MockHandler([
  79. function (RequestInterface $request) use (&$h) {
  80. $h[] = $request;
  81. return new Response();
  82. }
  83. ]);
  84. $c = new Client(['handler' => $handler]);
  85. $optHistory = [];
  86. $fn = function (array $opts) use (&$optHistory, $c) {
  87. $optHistory = $opts;
  88. return $c->request('GET', 'http://example.com', $opts);
  89. };
  90. $opts = ['options' => ['headers' => ['x-foo' => 'bar']]];
  91. $p = new Pool($c, [$fn], $opts);
  92. $p->promise()->wait();
  93. $this->assertCount(1, $h);
  94. $this->assertTrue($h[0]->hasHeader('x-foo'));
  95. }
  96. public function testBatchesResults()
  97. {
  98. $requests = [
  99. new Request('GET', 'http://foo.com/200'),
  100. new Request('GET', 'http://foo.com/201'),
  101. new Request('GET', 'http://foo.com/202'),
  102. new Request('GET', 'http://foo.com/404'),
  103. ];
  104. $fn = function (RequestInterface $request) {
  105. return new Response(substr($request->getUri()->getPath(), 1));
  106. };
  107. $mock = new MockHandler([$fn, $fn, $fn, $fn]);
  108. $handler = HandlerStack::create($mock);
  109. $client = new Client(['handler' => $handler]);
  110. $results = Pool::batch($client, $requests);
  111. $this->assertCount(4, $results);
  112. $this->assertSame([0, 1, 2, 3], array_keys($results));
  113. $this->assertSame(200, $results[0]->getStatusCode());
  114. $this->assertSame(201, $results[1]->getStatusCode());
  115. $this->assertSame(202, $results[2]->getStatusCode());
  116. $this->assertInstanceOf(ClientException::class, $results[3]);
  117. }
  118. public function testBatchesResultsWithCallbacks()
  119. {
  120. $requests = [
  121. new Request('GET', 'http://foo.com/200'),
  122. new Request('GET', 'http://foo.com/201')
  123. ];
  124. $mock = new MockHandler([
  125. function (RequestInterface $request) {
  126. return new Response(substr($request->getUri()->getPath(), 1));
  127. }
  128. ]);
  129. $client = new Client(['handler' => $mock]);
  130. $results = Pool::batch($client, $requests, [
  131. 'fulfilled' => function ($value) use (&$called) { $called = true; }
  132. ]);
  133. $this->assertCount(2, $results);
  134. $this->assertTrue($called);
  135. }
  136. public function testUsesYieldedKeyInFulfilledCallback()
  137. {
  138. $r1 = new Promise(function () use (&$r1) { $r1->resolve(new Response()); });
  139. $r2 = new Promise(function () use (&$r2) { $r2->resolve(new Response()); });
  140. $r3 = new Promise(function () use (&$r3) { $r3->resolve(new Response()); });
  141. $handler = new MockHandler([$r1, $r2, $r3]);
  142. $c = new Client(['handler' => $handler]);
  143. $keys = [];
  144. $requests = [
  145. 'request_1' => new Request('GET', 'http://example.com'),
  146. 'request_2' => new Request('GET', 'http://example.com'),
  147. 'request_3' => new Request('GET', 'http://example.com'),
  148. ];
  149. $p = new Pool($c, $requests, [
  150. 'pool_size' => 2,
  151. 'fulfilled' => function($res, $index) use (&$keys) { $keys[] = $index; }
  152. ]);
  153. $p->promise()->wait();
  154. $this->assertCount(3, $keys);
  155. $this->assertSame($keys, array_keys($requests));
  156. }
  157. private function getClient($total = 1)
  158. {
  159. $queue = [];
  160. for ($i = 0; $i < $total; $i++) {
  161. $queue[] = new Response();
  162. }
  163. $handler = new MockHandler($queue);
  164. return new Client(['handler' => $handler]);
  165. }
  166. }