|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | + * Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 15 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | + */ |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Unit\Grphp\Client\Interceptors; |
| 21 | + |
| 22 | +use Grphp\Client\Config; |
| 23 | +use Grphp\Client\Error; |
| 24 | +use Grphp\Client\Interceptors\Retry; |
| 25 | +use Grphp\Client\Response; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 28 | + |
| 29 | +class RetryTest extends TestCase |
| 30 | +{ |
| 31 | + use ProphecyTrait; |
| 32 | + |
| 33 | + private Retry $subject; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $this->subject = new Retry([ |
| 40 | + 'max_retries' => 3, |
| 41 | + 'delay_milliseconds' => 1, |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + public function testCallSuccessAfterRetries(): void |
| 46 | + { |
| 47 | + $callback = function () { |
| 48 | + static $attempts = 0; |
| 49 | + $attempts++; |
| 50 | + if ($attempts < 2) { |
| 51 | + throw new Error( |
| 52 | + new Config(), |
| 53 | + new Error\Status(14, 'test') |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return $this->prophesize(Response::class)->reveal(); |
| 58 | + }; |
| 59 | + |
| 60 | + $this->assertInstanceOf(Response::class, $this->subject->call($callback)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testCallFailureAfterRetries(): void |
| 64 | + { |
| 65 | + $callback = function () { |
| 66 | + throw new Error(new Config(), new Error\Status(14, 'test')); |
| 67 | + }; |
| 68 | + |
| 69 | + $this->expectException(Error::class); |
| 70 | + $this->subject->call($callback); |
| 71 | + } |
| 72 | + |
| 73 | + public function testCallFailureOnNonRetriableError(): void |
| 74 | + { |
| 75 | + $callback = function () { |
| 76 | + throw new Error(new Config(), new Error\Status(0, 'non-retriable error')); |
| 77 | + }; |
| 78 | + |
| 79 | + $this->expectException(Error::class); |
| 80 | + $this->subject->call($callback); |
| 81 | + } |
| 82 | + |
| 83 | + public function testCallWithCustomBackoffFunc(): void |
| 84 | + { |
| 85 | + $callback = function () { |
| 86 | + static $attempts = 0; |
| 87 | + if ($attempts < 2) { |
| 88 | + $attempts++; |
| 89 | + throw new Error( |
| 90 | + new Config(), |
| 91 | + new Error\Status(14, 'test') |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->prophesize(Response::class)->reveal(); |
| 96 | + }; |
| 97 | + |
| 98 | + $backoffCalled = 0; |
| 99 | + |
| 100 | + $this->subject = new Retry([ |
| 101 | + 'max_retries' => 3, |
| 102 | + 'delay_milliseconds' => 1, |
| 103 | + 'backoff_func' => function (int $attempt, int $delayMilliseconds) use (&$backoffCalled) { |
| 104 | + $this->assertSame(1, $delayMilliseconds); |
| 105 | + $backoffCalled++; |
| 106 | + }, |
| 107 | + ]); |
| 108 | + |
| 109 | + $this->assertInstanceOf(Response::class, $this->subject->call($callback)); |
| 110 | + $this->assertSame(2, $backoffCalled); |
| 111 | + } |
| 112 | +} |
0 commit comments