|
25 | 25 | use GuzzleHttp\Exception\BadResponseException; |
26 | 26 | use GuzzleHttp\Exception\ClientException; |
27 | 27 | use GuzzleHttp\Exception\ConnectException; |
| 28 | +use GuzzleHttp\Exception\GuzzleException; |
28 | 29 | use GuzzleHttp\Exception\ServerException; |
29 | 30 | use GuzzleHttp\Exception\TransferException; |
30 | 31 | use GuzzleHttp\Handler\MockHandler; |
@@ -58,6 +59,7 @@ public function testInstantiation(): void |
58 | 59 | * @dataProvider providerForRetryOccursWhenStatusCodeMatches |
59 | 60 | * @param Response $response |
60 | 61 | * @param bool $retryShouldOccur |
| 62 | + * @throws GuzzleException |
61 | 63 | */ |
62 | 64 | public function testRetryOccursWhenStatusCodeMatches(Response $response, bool $retryShouldOccur): void |
63 | 65 | { |
@@ -101,6 +103,7 @@ public function providerForRetryOccursWhenStatusCodeMatches(): array |
101 | 103 | * |
102 | 104 | * @dataProvider retriesFailAfterSpecifiedLimitProvider |
103 | 105 | * @param array<int,Response> $responses |
| 106 | + * @throws GuzzleException |
104 | 107 | */ |
105 | 108 | public function testRetriesFailAfterSpecifiedLimit(array $responses): void |
106 | 109 | { |
@@ -529,7 +532,7 @@ public function testDelayTakesIntoAccountGiveUpAfterTime(): void |
529 | 532 | $client->request('GET', '/'); |
530 | 533 | $this->fail('Should have timed out'); |
531 | 534 | } catch (ClientException $e) { |
532 | | - $this->assertEquals([3.0, 7.0], $delayTimes); |
| 535 | + $this->assertEquals([3.0, 10.0], $delayTimes); |
533 | 536 | } |
534 | 537 | } |
535 | 538 |
|
@@ -762,24 +765,39 @@ public function testGiveUpAfterSecsFailsWhenTimeLimitExceeded(): void |
762 | 765 | $client->request('GET', '/'); |
763 | 766 | } |
764 | 767 |
|
765 | | - public function testGiveUpAfterSecsWithLongerTimes(): void |
| 768 | + public function testRespectGiveUpAfterSecs(): void |
766 | 769 | { |
767 | 770 | $responses = [ |
768 | | - new Response(429, [], 'Wait 1'), |
769 | | - new Response(429, [], 'Wait 2'), |
770 | | - new Response(503, [], 'Wait 3'), |
771 | | - new Response(429, [], 'Wait 4'), |
| 771 | + new Response(503, ['Retry-After' => '1'], 'Resp 1'), |
| 772 | + new Response(503, ['Retry-After' => '1'], 'Resp 2'), |
| 773 | + new Response(503, ['Retry-After' => '1'], 'Resp 3'), |
| 774 | + new Response(503, ['Retry-After' => '360'], 'Resp 4'), |
| 775 | + new Response(503, ['Retry-After' => '10'], 'Resp 5'), |
772 | 776 | new Response(200, [], 'Good') |
773 | 777 | ]; |
774 | 778 |
|
| 779 | + $numRetries = 0; |
| 780 | + |
775 | 781 | $stack = HandlerStack::create(new MockHandler($responses)); |
776 | 782 | $stack->push(GuzzleRetryMiddleware::factory([ |
777 | | - 'default_retry_multiplier' => 1.5, |
778 | | - 'give_up_after_secs' => 1 |
| 783 | + 'default_retry_multiplier' => 2, |
| 784 | + 'give_up_after_secs' => 5, |
| 785 | + 'on_retry_callback' => function ($retryCount) use (&$numRetries) { |
| 786 | + $numRetries = $retryCount; |
| 787 | + } |
779 | 788 | ])); |
780 | 789 |
|
781 | | - $client = new Client(['handler' => $stack]); |
782 | | - $client->request('GET', '/'); |
| 790 | + $respBody = null; |
| 791 | + |
| 792 | + try { |
| 793 | + $client = new Client(['handler' => $stack]); |
| 794 | + $client->request('GET', '/'); |
| 795 | + } catch (ServerException $e) { |
| 796 | + $respBody = $e->getResponse()->getBody()->getContents(); |
| 797 | + } |
| 798 | + |
| 799 | + $this->assertEquals(3, $numRetries); |
| 800 | + $this->assertEquals('Resp 4', $respBody); |
783 | 801 | } |
784 | 802 |
|
785 | 803 | public function testGiveUpAfterSecsSucceedsWhenTimeLimitNotExceeded(): void |
@@ -896,7 +914,7 @@ public function testRetryCallbackReferenceModification(): void |
896 | 914 | $client->request('GET', '/'); |
897 | 915 |
|
898 | 916 | $this->assertEquals('GoodHeader', $testRequest->getHeaderLine('TestHeader')); |
899 | | - $this->assertArrayHasKey('TestOption', $testOptions); |
| 917 | + $this->assertArrayHasKey('TestOption', $testOptions ?? []); |
900 | 918 | $this->assertEquals('GoodOption', $testOptions['TestOption']); |
901 | 919 | } |
902 | 920 |
|
|
0 commit comments