-
Notifications
You must be signed in to change notification settings - Fork 4
feat(http): retry requests on transient error status codes #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| <?php | ||
|
|
||
| namespace Descope\Tests; | ||
|
|
||
| use Descope\SDK\API; | ||
| use Descope\SDK\Exception\AuthException; | ||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\Exception\RequestException; | ||
| use GuzzleHttp\Handler\MockHandler; | ||
| use GuzzleHttp\HandlerStack; | ||
| use GuzzleHttp\Psr7\Request; | ||
| use GuzzleHttp\Psr7\Response; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionClass; | ||
|
|
||
| final class APIRetryTest extends TestCase | ||
| { | ||
| private function apiWithMockedClient(MockHandler $mockHandler): API | ||
| { | ||
| $handlerStack = HandlerStack::create($mockHandler); | ||
| $client = new Client(['handler' => $handlerStack]); | ||
|
dorsha marked this conversation as resolved.
|
||
|
|
||
| $api = new API('project', null, false); | ||
| $reflection = new ReflectionClass(API::class); | ||
|
|
||
| $httpClientProp = $reflection->getProperty('httpClient'); | ||
| $httpClientProp->setAccessible(true); | ||
| $httpClientProp->setValue($api, $client); | ||
|
|
||
| $retryDelaysProp = $reflection->getProperty('retryDelaysUs'); | ||
| $retryDelaysProp->setAccessible(true); | ||
| $retryDelaysProp->setValue($api, [0, 0, 0]); | ||
|
|
||
| return $api; | ||
| } | ||
|
|
||
| private function retryableException(int $statusCode): RequestException | ||
| { | ||
| $request = new Request('GET', 'https://example.com/test'); | ||
| $response = new Response($statusCode, [], ''); | ||
| return new RequestException('transient error', $request, $response); | ||
| } | ||
|
|
||
| private function successResponse(): Response | ||
| { | ||
| return new Response(200, [], json_encode(['ok' => true])); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider retryableStatusCodeProvider | ||
| */ | ||
| public function testRetriesOnRetryableStatusCodeAndSucceedsOnSecondAttempt(int $statusCode): void | ||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->retryableException($statusCode), | ||
| $this->successResponse(), | ||
| ])); | ||
|
|
||
| $result = $api->doGet('https://example.com/test', false); | ||
| $this->assertSame(['ok' => true], $result); | ||
| } | ||
|
|
||
| public static function retryableStatusCodeProvider(): array | ||
| { | ||
| return [[503], [521], [522], [524], [530]]; | ||
| } | ||
|
|
||
| public function testRetriesUpToThreeTimesAndThrowsOnExhaustion(): void | ||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->retryableException(503), | ||
| $this->retryableException(503), | ||
| $this->retryableException(503), | ||
| $this->retryableException(503), | ||
| ])); | ||
|
|
||
| $this->expectException(AuthException::class); | ||
| $api->doGet('https://example.com/test', false); | ||
| } | ||
|
|
||
| public function testDoesNotRetryOnNonRetryableStatusCodes(): void | ||
| { | ||
| foreach ([400, 401, 403, 404, 500, 502] as $statusCode) { | ||
| $request = new Request('GET', 'https://example.com/test'); | ||
| $response = new Response($statusCode, [], ''); | ||
| $exception = new RequestException('error', $request, $response); | ||
|
|
||
| $api = $this->apiWithMockedClient(new MockHandler([$exception])); | ||
|
|
||
| try { | ||
| $api->doGet('https://example.com/test', false); | ||
| $this->fail("Expected exception for status $statusCode"); | ||
| } catch (AuthException $e) { | ||
| $this->assertStringContainsString((string) $statusCode, (string) $e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function testRetryWorksForDoPost(): void | ||
|
dorsha marked this conversation as resolved.
|
||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->retryableException(503), | ||
| $this->successResponse(), | ||
| ])); | ||
|
|
||
| $result = $api->doPost('https://example.com/test', []); | ||
| $this->assertSame(['ok' => true], $result); | ||
| } | ||
|
|
||
| public function testRetryWorksForDoDelete(): void | ||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->retryableException(503), | ||
| $this->successResponse(), | ||
| ])); | ||
|
|
||
| $result = $api->doDelete('https://example.com/test'); | ||
| $this->assertSame(['ok' => true], $result); | ||
| } | ||
|
|
||
| public function testSucceedsImmediatelyWithNoRetry(): void | ||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->successResponse(), | ||
| ])); | ||
|
|
||
| $result = $api->doGet('https://example.com/test', false); | ||
| $this->assertSame(['ok' => true], $result); | ||
| } | ||
|
|
||
| public function testSucceedsOnThirdRetry(): void | ||
| { | ||
| $api = $this->apiWithMockedClient(new MockHandler([ | ||
| $this->retryableException(503), | ||
| $this->retryableException(522), | ||
| $this->retryableException(530), | ||
| $this->successResponse(), | ||
| ])); | ||
|
|
||
| $result = $api->doGet('https://example.com/test', false); | ||
| $this->assertSame(['ok' => true], $result); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.