From 10db4bd81df796c0a7316e5b687802487517fe58 Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Mon, 1 Jul 2024 11:05:15 +0300 Subject: [PATCH 1/5] Redesin package --- src/BackOff.php | 11 +- src/BackOffInterface.php | 4 +- src/CallbackBackOff.php | 6 +- src/ConstantBackOff.php | 30 ++++ src/DecorrelatedJitterBackOff.php | 13 +- src/ExponentialBackOff.php | 14 +- src/FibonacciBackOff.php | 11 +- src/LinearBackOff.php | 11 +- src/NullBackOff.php | 15 ++ src/PermanentBackOff.php | 25 --- src/Retry/BackOffRetry.php | 45 ------ ...iatelyThrowableRetry.php => NullRetry.php} | 2 +- src/Retry/Retry.php | 10 +- src/Sleeper/CallbackSleeper.php | 23 +++ ...anentStrategy.php => ConstantStrategy.php} | 2 +- src/Strategy/DecorrelatedJitterStrategy.php | 6 +- src/Strategy/ExponentialStrategy.php | 6 +- tests/ConstantBackOffTest.php | 145 ++++++++++++++++++ tests/DecorrelatedJitterBackOffTest.php | 32 ++-- tests/ExponentialBackOffTest.php | 88 ++++++----- tests/FibonacciBackOffTest.php | 78 ++++++---- tests/Jitter/EqualJitterTest.php | 4 +- tests/Jitter/FullJitterTest.php | 4 +- tests/Jitter/ScatteredJitterTest.php | 4 +- tests/LinearBackOffTest.php | 78 ++++++---- tests/PermanentBackOffTest.php | 133 ---------------- tests/Retry/BackOffRetryTest.php | 61 -------- ...rowableRetryTest.php => NullRetryTest.php} | 8 +- tests/Retry/RetryTest.php | 19 ++- ...ategyTest.php => ConstantStrategyTest.php} | 8 +- .../DecorrelatedJitterStrategyTest.php | 6 +- tests/Strategy/ExponentialStrategyTest.php | 6 +- tests/Strategy/FibonacciStrategyTest.php | 4 +- tests/Strategy/LinearStrategyTest.php | 4 +- 34 files changed, 468 insertions(+), 448 deletions(-) create mode 100644 src/ConstantBackOff.php create mode 100644 src/NullBackOff.php delete mode 100644 src/PermanentBackOff.php delete mode 100644 src/Retry/BackOffRetry.php rename src/Retry/{ImmediatelyThrowableRetry.php => NullRetry.php} (72%) create mode 100644 src/Sleeper/CallbackSleeper.php rename src/Strategy/{PermanentStrategy.php => ConstantStrategy.php} (88%) create mode 100644 tests/ConstantBackOffTest.php delete mode 100644 tests/PermanentBackOffTest.php delete mode 100644 tests/Retry/BackOffRetryTest.php rename tests/Retry/{ImmediatelyThrowableRetryTest.php => NullRetryTest.php} (54%) rename tests/Strategy/{PermanentStrategyTest.php => ConstantStrategyTest.php} (73%) diff --git a/src/BackOff.php b/src/BackOff.php index b5bc723..6188294 100644 --- a/src/BackOff.php +++ b/src/BackOff.php @@ -13,16 +13,23 @@ abstract class BackOff implements BackOffInterface { public function __construct( + private Duration $baseTime, + private Duration $capTime, private GeneratorInterface $generator, private SleeperInterface $sleeper, ) { } - public function backOff(int $attempt, Duration $baseTime, Duration $capTime): void + public function backOff(int $attempt): void { Assertion::greaterThan($attempt, 0); // @codeCoverageIgnore - $sleepTime = $this->generator->generate($attempt, $baseTime->asNanoseconds(), $capTime->asNanoseconds()); + $sleepTime = $this->generator->generate( + attempt: $attempt, + baseTime: $this->baseTime->asNanoseconds(), + capTime: $this->capTime->asNanoseconds(), + ); + $this->sleeper->sleep(new Nanoseconds($sleepTime)); } } diff --git a/src/BackOffInterface.php b/src/BackOffInterface.php index 237dce8..3dbc73c 100644 --- a/src/BackOffInterface.php +++ b/src/BackOffInterface.php @@ -4,9 +4,7 @@ namespace Orangesoft\BackOff; -use Orangesoft\BackOff\Duration\Duration; - interface BackOffInterface { - public function backOff(int $attempt, Duration $baseTime, Duration $capTime): void; + public function backOff(int $attempt): void; } diff --git a/src/CallbackBackOff.php b/src/CallbackBackOff.php index 4b02840..9ffada8 100644 --- a/src/CallbackBackOff.php +++ b/src/CallbackBackOff.php @@ -4,8 +4,6 @@ namespace Orangesoft\BackOff; -use Orangesoft\BackOff\Duration\Duration; - /** * @codeCoverageIgnore */ @@ -16,8 +14,8 @@ public function __construct( ) { } - public function backOff(int $attempt, Duration $baseTime, Duration $capTime): void + public function backOff(int $attempt): void { - ($this->callback)($attempt, $baseTime, $capTime); + ($this->callback)($attempt); } } diff --git a/src/ConstantBackOff.php b/src/ConstantBackOff.php new file mode 100644 index 0000000..d9fc4ad --- /dev/null +++ b/src/ConstantBackOff.php @@ -0,0 +1,30 @@ +maxAttempts, 0); // @codeCoverageIgnore - - $attempt = 0; - - retrying: - - try { - return $callback(); - } catch (\Throwable $throwable) { - ++$attempt; - - $this->backOff->backOff($attempt, $this->baseTime, $this->capTime); - - if ($attempt >= $this->maxAttempts || !$this->exceptionClassifier->classify($throwable)) { - throw $throwable; - } - - goto retrying; - } - } -} diff --git a/src/Retry/ImmediatelyThrowableRetry.php b/src/Retry/NullRetry.php similarity index 72% rename from src/Retry/ImmediatelyThrowableRetry.php rename to src/Retry/NullRetry.php index 04d32b9..9b1d996 100644 --- a/src/Retry/ImmediatelyThrowableRetry.php +++ b/src/Retry/NullRetry.php @@ -4,7 +4,7 @@ namespace Orangesoft\BackOff\Retry; -final class ImmediatelyThrowableRetry implements RetryInterface +final class NullRetry implements RetryInterface { public function call(callable $callback): mixed { diff --git a/src/Retry/Retry.php b/src/Retry/Retry.php index 676d9b4..a2cf8ae 100644 --- a/src/Retry/Retry.php +++ b/src/Retry/Retry.php @@ -5,14 +5,20 @@ namespace Orangesoft\BackOff\Retry; use Assert\Assertion; +use Orangesoft\BackOff\BackOffInterface; +use Orangesoft\BackOff\NullBackOff; +use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier; use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifierInterface; final class Retry implements RetryInterface { public function __construct( private int $maxAttempts, - private ExceptionClassifierInterface $exceptionClassifier, + private ?BackOffInterface $backOff = null, + private ?ExceptionClassifierInterface $exceptionClassifier = null, ) { + $this->backOff ??= new NullBackOff(); + $this->exceptionClassifier ??= new ExceptionClassifier(); } public function call(callable $callback): mixed @@ -28,6 +34,8 @@ public function call(callable $callback): mixed } catch (\Throwable $throwable) { ++$attempt; + $this->backOff->backOff($attempt); + if ($attempt >= $this->maxAttempts || !$this->exceptionClassifier->classify($throwable)) { throw $throwable; } diff --git a/src/Sleeper/CallbackSleeper.php b/src/Sleeper/CallbackSleeper.php new file mode 100644 index 0000000..9875c30 --- /dev/null +++ b/src/Sleeper/CallbackSleeper.php @@ -0,0 +1,23 @@ +callback)($sleepTime); + } +} diff --git a/src/Strategy/PermanentStrategy.php b/src/Strategy/ConstantStrategy.php similarity index 88% rename from src/Strategy/PermanentStrategy.php rename to src/Strategy/ConstantStrategy.php index e7fec32..fcdb170 100644 --- a/src/Strategy/PermanentStrategy.php +++ b/src/Strategy/ConstantStrategy.php @@ -6,7 +6,7 @@ use Assert\Assertion; -final class PermanentStrategy implements StrategyInterface +final class ConstantStrategy implements StrategyInterface { public function calculate(int $attempt, float $duration): float { diff --git a/src/Strategy/DecorrelatedJitterStrategy.php b/src/Strategy/DecorrelatedJitterStrategy.php index 7315824..e1f433c 100644 --- a/src/Strategy/DecorrelatedJitterStrategy.php +++ b/src/Strategy/DecorrelatedJitterStrategy.php @@ -9,14 +9,14 @@ final class DecorrelatedJitterStrategy implements StrategyInterface { public function __construct( - private float $multiplier, + private float $factor, ) { } public function calculate(int $attempt, float $duration): float { // @codeCoverageIgnoreStart - Assertion::greaterOrEqualThan($this->multiplier, 0); + Assertion::greaterOrEqualThan($this->factor, 0); Assertion::greaterOrEqualThan($attempt, 0); Assertion::greaterOrEqualThan($duration, 0); // @codeCoverageIgnoreEnd @@ -25,6 +25,6 @@ public function calculate(int $attempt, float $duration): float return 0; } - return random_float($duration, $duration * $this->multiplier * $attempt); + return random_float($duration, $duration * $this->factor * $attempt); } } diff --git a/src/Strategy/ExponentialStrategy.php b/src/Strategy/ExponentialStrategy.php index 3704bd6..d66f4f5 100644 --- a/src/Strategy/ExponentialStrategy.php +++ b/src/Strategy/ExponentialStrategy.php @@ -9,14 +9,14 @@ final class ExponentialStrategy implements StrategyInterface { public function __construct( - private float $multiplier, + private float $factor, ) { } public function calculate(int $attempt, float $duration): float { // @codeCoverageIgnoreStart - Assertion::greaterOrEqualThan($this->multiplier, 0); + Assertion::greaterOrEqualThan($this->factor, 0); Assertion::greaterOrEqualThan($attempt, 0); Assertion::greaterOrEqualThan($duration, 0); // @codeCoverageIgnoreEnd @@ -25,6 +25,6 @@ public function calculate(int $attempt, float $duration): float return 0; } - return $duration * $this->multiplier ** ($attempt - 1); + return $duration * $this->factor ** ($attempt - 1); } } diff --git a/tests/ConstantBackOffTest.php b/tests/ConstantBackOffTest.php new file mode 100644 index 0000000..5cbd9bb --- /dev/null +++ b/tests/ConstantBackOffTest.php @@ -0,0 +1,145 @@ +backOff($attempt); + + $this->assertEquals($expectedSleepTime, $sleeperSpy->getSleepTime()?->asMicroseconds()); + } + + public function getConstantData(): array + { + return [ + [1, 1_000], + [2, 1_000], + [3, 1_000], + [4, 1_000], + [5, 1_000], + [6, 1_000], + ]; + } + + /** + * @param float[] $expectedSleepTime + * + * @dataProvider getConstantDataWithEqualJitter + */ + public function testConstantBackOffWithEqualJitter(int $attempt, array $expectedSleepTime): void + { + $sleeperSpy = new SleeperSpy(); + $backOff = new ConstantBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new EqualJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); + } + + public function getConstantDataWithEqualJitter(): array + { + return [ + [1, [500, 1_000]], + [2, [500, 1_000]], + [3, [500, 1_000]], + [4, [500, 1_000]], + [5, [500, 1_000]], + [6, [500, 1_000]], + ]; + } + + /** + * @param float[] $expectedSleepTime + * + * @dataProvider getConstantDataWithFullJitter + */ + public function testConstantBackOffWithFullJitter(int $attempt, array $expectedSleepTime): void + { + $sleeperSpy = new SleeperSpy(); + $backOff = new ConstantBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new FullJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); + } + + public function getConstantDataWithFullJitter(): array + { + return [ + [1, [0, 1_000]], + [2, [0, 1_000]], + [3, [0, 1_000]], + [4, [0, 1_000]], + [5, [0, 1_000]], + [6, [0, 1_000]], + ]; + } + + /** + * @param float[] $expectedSleepTime + * + * @dataProvider getConstantDataWithScatteredJitter + */ + public function testConstantBackOffWithScatteredJitter(int $attempt, float $range, array $expectedSleepTime): void + { + $sleeperSpy = new SleeperSpy(); + $backOff = new ConstantBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new ScatteredJitter($range), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); + } + + public function getConstantDataWithScatteredJitter(): array + { + return [ + [1, 0.5, [500, 1_500]], + [2, 0.5, [500, 1_500]], + [3, 0.5, [500, 1_500]], + [4, 0.5, [500, 1_500]], + [5, 0.5, [500, 1_500]], + [6, 0.5, [500, 1_500]], + ]; + } +} diff --git a/tests/DecorrelatedJitterBackOffTest.php b/tests/DecorrelatedJitterBackOffTest.php index b094272..e4b0e03 100644 --- a/tests/DecorrelatedJitterBackOffTest.php +++ b/tests/DecorrelatedJitterBackOffTest.php @@ -5,25 +5,11 @@ namespace Orangesoft\BackOff\Tests; use Orangesoft\BackOff\DecorrelatedJitterBackOff; -use Orangesoft\BackOff\Duration\Duration; use Orangesoft\BackOff\Duration\Microseconds; use PHPUnit\Framework\TestCase; final class DecorrelatedJitterBackOffTest extends TestCase { - private float $multiplier; - private Duration $baseTime; - private Duration $capTime; - private SleeperSpy $sleeperSpy; - - protected function setUp(): void - { - $this->multiplier = 3; - $this->baseTime = new Microseconds(1_000); - $this->capTime = new Microseconds(15_000); - $this->sleeperSpy = new SleeperSpy(); - } - /** * @param float[] $expectedSleepTime * @@ -31,12 +17,18 @@ protected function setUp(): void */ public function testDecorrelatedJitterBackOff(int $attempt, array $expectedSleepTime): void { - $decorrelatedJitterBackOff = new DecorrelatedJitterBackOff($this->multiplier, $this->sleeperSpy); - - $decorrelatedJitterBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new DecorrelatedJitterBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(15_000), + factor: 3.0, + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getDecorrelatedJitterData(): array diff --git a/tests/ExponentialBackOffTest.php b/tests/ExponentialBackOffTest.php index e9887ac..4a6ef65 100644 --- a/tests/ExponentialBackOffTest.php +++ b/tests/ExponentialBackOffTest.php @@ -4,7 +4,6 @@ namespace Orangesoft\BackOff\Tests; -use Orangesoft\BackOff\Duration\Duration; use Orangesoft\BackOff\Duration\Microseconds; use Orangesoft\BackOff\ExponentialBackOff; use Orangesoft\BackOff\Jitter\EqualJitter; @@ -15,29 +14,23 @@ final class ExponentialBackOffTest extends TestCase { - private float $multiplier; - private Duration $baseTime; - private Duration $capTime; - private SleeperSpy $sleeperSpy; - - protected function setUp(): void - { - $this->multiplier = 2; - $this->baseTime = new Microseconds(1_000); - $this->capTime = new Microseconds(16_000); - $this->sleeperSpy = new SleeperSpy(); - } - /** * @dataProvider getExponentialData */ public function testExponentialBackOff(int $attempt, int $expectedSleepTime): void { - $exponentialBackOff = new ExponentialBackOff($this->multiplier, new NullJitter(), $this->sleeperSpy); - - $exponentialBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertEquals($expectedSleepTime, $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new ExponentialBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(16_000), + factor: 2.0, + jitter: new NullJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertEquals($expectedSleepTime, $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getExponentialData(): array @@ -59,12 +52,19 @@ public function getExponentialData(): array */ public function testExponentialBackOffWithEqualJitter(int $attempt, array $expectedSleepTime): void { - $exponentialBackOff = new ExponentialBackOff($this->multiplier, new EqualJitter(), $this->sleeperSpy); - - $exponentialBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new ExponentialBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(16_000), + factor: 2.0, + jitter: new EqualJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getExponentialDataWithEqualJitter(): array @@ -86,12 +86,19 @@ public function getExponentialDataWithEqualJitter(): array */ public function testExponentialBackOffWithFullJitter(int $attempt, array $expectedSleepTime): void { - $exponentialBackOff = new ExponentialBackOff($this->multiplier, new FullJitter(), $this->sleeperSpy); - - $exponentialBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new ExponentialBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(16_000), + factor: 2.0, + jitter: new FullJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getExponentialDataWithFullJitter(): array @@ -113,12 +120,19 @@ public function getExponentialDataWithFullJitter(): array */ public function testExponentialBackOffWithScatteredJitter(int $attempt, float $range, array $expectedSleepTime): void { - $exponentialBackOff = new ExponentialBackOff($this->multiplier, new ScatteredJitter($range), $this->sleeperSpy); - - $exponentialBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new ExponentialBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(16_000), + factor: 2.0, + jitter: new ScatteredJitter($range), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getExponentialDataWithScatteredJitter(): array diff --git a/tests/FibonacciBackOffTest.php b/tests/FibonacciBackOffTest.php index c5ed500..fd9de70 100644 --- a/tests/FibonacciBackOffTest.php +++ b/tests/FibonacciBackOffTest.php @@ -4,7 +4,6 @@ namespace Orangesoft\BackOff\Tests; -use Orangesoft\BackOff\Duration\Duration; use Orangesoft\BackOff\Duration\Microseconds; use Orangesoft\BackOff\FibonacciBackOff; use Orangesoft\BackOff\Jitter\EqualJitter; @@ -15,27 +14,22 @@ final class FibonacciBackOffTest extends TestCase { - private Duration $baseTime; - private Duration $capTime; - private SleeperSpy $sleeperSpy; - - protected function setUp(): void - { - $this->baseTime = new Microseconds(1_000); - $this->capTime = new Microseconds(21_000); - $this->sleeperSpy = new SleeperSpy(); - } - /** * @dataProvider getFibonacciData */ public function testFibonacciBackOff(int $attempt, int $expectedSleepTime): void { - $fibonacciBackOff = new FibonacciBackOff(new NullJitter(), $this->sleeperSpy); + $sleeperSpy = new SleeperSpy(); + $backOff = new FibonacciBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(21_000), + jitter: new NullJitter(), + sleeper: $sleeperSpy, + ); - $fibonacciBackOff->backOff($attempt, $this->baseTime, $this->capTime); + $backOff->backOff($attempt); - $this->assertEquals($expectedSleepTime, (int) $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertEquals($expectedSleepTime, (int) $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getFibonacciData(): array @@ -60,12 +54,18 @@ public function getFibonacciData(): array */ public function testFibonacciBackOffWithEqualJitter(int $attempt, array $expectedSleepTime): void { - $fibonacciBackOff = new FibonacciBackOff(new EqualJitter(), $this->sleeperSpy); - - $fibonacciBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new FibonacciBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(21_000), + jitter: new EqualJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getFibonacciDataWithEqualJitter(): array @@ -90,12 +90,18 @@ public function getFibonacciDataWithEqualJitter(): array */ public function testFibonacciBackOffWithFullJitter(int $attempt, array $expectedSleepTime): void { - $fibonacciBackOff = new FibonacciBackOff(new FullJitter(), $this->sleeperSpy); - - $fibonacciBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new FibonacciBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(21_000), + jitter: new FullJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getFibonacciDataWithFullJitter(): array @@ -120,12 +126,18 @@ public function getFibonacciDataWithFullJitter(): array */ public function testFibonacciBackOffWithScatteredJitter(int $attempt, float $range, array $expectedSleepTime): void { - $fibonacciBackOff = new FibonacciBackOff(new ScatteredJitter($range), $this->sleeperSpy); - - $fibonacciBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new FibonacciBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(21_000), + jitter: new ScatteredJitter($range), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getFibonacciDataWithScatteredJitter(): array diff --git a/tests/Jitter/EqualJitterTest.php b/tests/Jitter/EqualJitterTest.php index b12d836..0063c23 100644 --- a/tests/Jitter/EqualJitterTest.php +++ b/tests/Jitter/EqualJitterTest.php @@ -16,9 +16,9 @@ final class EqualJitterTest extends TestCase */ public function testFullJitter(int $time, array $expectedTime): void { - $equalJitter = new EqualJitter(); + $jitter = new EqualJitter(); - $actualTime = $equalJitter->jitter($time); + $actualTime = $jitter->jitter($time); $this->assertGreaterThanOrEqual($expectedTime[0], $actualTime); $this->assertLessThanOrEqual($expectedTime[1], $actualTime); diff --git a/tests/Jitter/FullJitterTest.php b/tests/Jitter/FullJitterTest.php index 2295ade..59c98f5 100644 --- a/tests/Jitter/FullJitterTest.php +++ b/tests/Jitter/FullJitterTest.php @@ -16,9 +16,9 @@ final class FullJitterTest extends TestCase */ public function testFullJitter(int $time, array $expectedTime): void { - $fullJitter = new FullJitter(); + $jitter = new FullJitter(); - $actualTime = $fullJitter->jitter($time); + $actualTime = $jitter->jitter($time); $this->assertGreaterThanOrEqual($expectedTime[0], $actualTime); $this->assertLessThanOrEqual($expectedTime[1], $actualTime); diff --git a/tests/Jitter/ScatteredJitterTest.php b/tests/Jitter/ScatteredJitterTest.php index a96bbe0..61169e5 100644 --- a/tests/Jitter/ScatteredJitterTest.php +++ b/tests/Jitter/ScatteredJitterTest.php @@ -16,9 +16,9 @@ final class ScatteredJitterTest extends TestCase */ public function testFullJitter(float $range, int $time, array $expectedTime): void { - $scatteredJitter = new ScatteredJitter($range); + $jitter = new ScatteredJitter($range); - $actualTime = $scatteredJitter->jitter($time); + $actualTime = $jitter->jitter($time); $this->assertGreaterThanOrEqual($expectedTime[0], $actualTime); $this->assertLessThanOrEqual($expectedTime[1], $actualTime); diff --git a/tests/LinearBackOffTest.php b/tests/LinearBackOffTest.php index ce601de..c18174c 100644 --- a/tests/LinearBackOffTest.php +++ b/tests/LinearBackOffTest.php @@ -4,7 +4,6 @@ namespace Orangesoft\BackOff\Tests; -use Orangesoft\BackOff\Duration\Duration; use Orangesoft\BackOff\Duration\Microseconds; use Orangesoft\BackOff\Jitter\EqualJitter; use Orangesoft\BackOff\Jitter\FullJitter; @@ -15,27 +14,22 @@ final class LinearBackOffTest extends TestCase { - private Duration $baseTime; - private Duration $capTime; - private SleeperSpy $sleeperSpy; - - protected function setUp(): void - { - $this->baseTime = new Microseconds(1_000); - $this->capTime = new Microseconds(5_000); - $this->sleeperSpy = new SleeperSpy(); - } - /** * @dataProvider getLinearData */ public function testLinearBackOff(int $attempt, int $expectedSleepTime): void { - $linearBackOff = new LinearBackOff(new NullJitter(), $this->sleeperSpy); + $sleeperSpy = new SleeperSpy(); + $backOff = new LinearBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new NullJitter(), + sleeper: $sleeperSpy, + ); - $linearBackOff->backOff($attempt, $this->baseTime, $this->capTime); + $backOff->backOff($attempt); - $this->assertEquals($expectedSleepTime, $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertEquals($expectedSleepTime, $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getLinearData(): array @@ -57,12 +51,18 @@ public function getLinearData(): array */ public function testLinearBackOffWithEqualJitter(int $attempt, array $expectedSleepTime): void { - $linearBackOff = new LinearBackOff(new EqualJitter(), $this->sleeperSpy); - - $linearBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new LinearBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new EqualJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getLinearDataWithEqualJitter(): array @@ -84,12 +84,18 @@ public function getLinearDataWithEqualJitter(): array */ public function testLinearBackOffWithFullJitter(int $attempt, array $expectedSleepTime): void { - $linearBackOff = new LinearBackOff(new FullJitter(), $this->sleeperSpy); - - $linearBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new LinearBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new FullJitter(), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getLinearDataWithFullJitter(): array @@ -111,12 +117,18 @@ public function getLinearDataWithFullJitter(): array */ public function testLinearBackOffWithScatteredJitter(int $attempt, float $range, array $expectedSleepTime): void { - $linearBackOff = new LinearBackOff(new ScatteredJitter($range), $this->sleeperSpy); - - $linearBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); + $sleeperSpy = new SleeperSpy(); + $backOff = new LinearBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new ScatteredJitter($range), + sleeper: $sleeperSpy, + ); + + $backOff->backOff($attempt); + + $this->assertGreaterThanOrEqual($expectedSleepTime[0], $sleeperSpy->getSleepTime()?->asMicroseconds()); + $this->assertLessThanOrEqual($expectedSleepTime[1], $sleeperSpy->getSleepTime()?->asMicroseconds()); } public function getLinearDataWithScatteredJitter(): array diff --git a/tests/PermanentBackOffTest.php b/tests/PermanentBackOffTest.php deleted file mode 100644 index 0edf595..0000000 --- a/tests/PermanentBackOffTest.php +++ /dev/null @@ -1,133 +0,0 @@ -baseTime = new Microseconds(1_000); - $this->capTime = new Microseconds(5_000); - $this->sleeperSpy = new SleeperSpy(); - } - - /** - * @dataProvider getPermanentData - */ - public function testPermanentBackOff(int $attempt, int $expectedSleepTime): void - { - $permanentBackOff = new PermanentBackOff(new NullJitter(), $this->sleeperSpy); - - $permanentBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertEquals($expectedSleepTime, $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - } - - public function getPermanentData(): array - { - return [ - [1, 1_000], - [2, 1_000], - [3, 1_000], - [4, 1_000], - [5, 1_000], - [6, 1_000], - ]; - } - - /** - * @param float[] $expectedSleepTime - * - * @dataProvider getPermanentDataWithEqualJitter - */ - public function testPermanentBackOffWithEqualJitter(int $attempt, array $expectedSleepTime): void - { - $permanentBackOff = new PermanentBackOff(new EqualJitter(), $this->sleeperSpy); - - $permanentBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - } - - public function getPermanentDataWithEqualJitter(): array - { - return [ - [1, [500, 1_000]], - [2, [500, 1_000]], - [3, [500, 1_000]], - [4, [500, 1_000]], - [5, [500, 1_000]], - [6, [500, 1_000]], - ]; - } - - /** - * @param float[] $expectedSleepTime - * - * @dataProvider getPermanentDataWithFullJitter - */ - public function testPermanentBackOffWithFullJitter(int $attempt, array $expectedSleepTime): void - { - $permanentBackOff = new PermanentBackOff(new FullJitter(), $this->sleeperSpy); - - $permanentBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - } - - public function getPermanentDataWithFullJitter(): array - { - return [ - [1, [0, 1_000]], - [2, [0, 1_000]], - [3, [0, 1_000]], - [4, [0, 1_000]], - [5, [0, 1_000]], - [6, [0, 1_000]], - ]; - } - - /** - * @param float[] $expectedSleepTime - * - * @dataProvider getPermanentDataWithScatteredJitter - */ - public function testPermanentBackOffWithScatteredJitter(int $attempt, float $range, array $expectedSleepTime): void - { - $permanentBackOff = new PermanentBackOff(new ScatteredJitter($range), $this->sleeperSpy); - - $permanentBackOff->backOff($attempt, $this->baseTime, $this->capTime); - - $this->assertGreaterThanOrEqual($expectedSleepTime[0], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - $this->assertLessThanOrEqual($expectedSleepTime[1], $this->sleeperSpy->getSleepTime()?->asMicroseconds()); - } - - public function getPermanentDataWithScatteredJitter(): array - { - return [ - [1, 0.5, [500, 1_500]], - [2, 0.5, [500, 1_500]], - [3, 0.5, [500, 1_500]], - [4, 0.5, [500, 1_500]], - [5, 0.5, [500, 1_500]], - [6, 0.5, [500, 1_500]], - ]; - } -} diff --git a/tests/Retry/BackOffRetryTest.php b/tests/Retry/BackOffRetryTest.php deleted file mode 100644 index 6f5bad8..0000000 --- a/tests/Retry/BackOffRetryTest.php +++ /dev/null @@ -1,61 +0,0 @@ -call(static fn (): float => 1.618); - - $this->assertEquals(1.618, $result); - } - - public function testFailureRetryableCall(): void - { - $sleeperSpy = new SleeperSpy(); - $backOffRetry = new BackOffRetry( - maxAttempts: 3, - baseTime: new Microseconds(1_000), - capTime: new Microseconds(5_000), - backOff: new LinearBackOff( - jitter: new NullJitter(), - sleeper: $sleeperSpy, - ), - exceptionClassifier: new ExceptionClassifier( - classNames: [ - \RuntimeException::class, - ], - ), - ); - - try { - $backOffRetry->call(new CallbackSpy(function (int $counter): never { - throw new \RuntimeException(sprintf('Exception thrown %d times.', $counter)); - })); - } catch (\RuntimeException $e) { - $this->assertSame('Exception thrown 2 times.', $e->getMessage()); - $this->assertEquals(3_000, $sleeperSpy->getSleepTime()?->asMicroseconds()); - } - } -} diff --git a/tests/Retry/ImmediatelyThrowableRetryTest.php b/tests/Retry/NullRetryTest.php similarity index 54% rename from tests/Retry/ImmediatelyThrowableRetryTest.php rename to tests/Retry/NullRetryTest.php index 6f2de0f..5dfdd25 100644 --- a/tests/Retry/ImmediatelyThrowableRetryTest.php +++ b/tests/Retry/NullRetryTest.php @@ -4,18 +4,18 @@ namespace Orangesoft\BackOff\Tests\Retry; -use Orangesoft\BackOff\Retry\ImmediatelyThrowableRetry; +use Orangesoft\BackOff\Retry\NullRetry; use PHPUnit\Framework\TestCase; -final class ImmediatelyThrowableRetryTest extends TestCase +final class NullRetryTest extends TestCase { public function testImmediatelyThrowable(): void { - $immediatelyThrowableRetry = new ImmediatelyThrowableRetry(); + $retry = new NullRetry(); $this->expectException(\RuntimeException::class); - $immediatelyThrowableRetry->call(static function () { + $retry->call(static function (): never { throw new \RuntimeException(); }); } diff --git a/tests/Retry/RetryTest.php b/tests/Retry/RetryTest.php index 9c3f13a..d11145a 100644 --- a/tests/Retry/RetryTest.php +++ b/tests/Retry/RetryTest.php @@ -4,19 +4,20 @@ namespace Orangesoft\BackOff\Tests\Retry; +use Orangesoft\BackOff\Duration\Microseconds; +use Orangesoft\BackOff\Jitter\NullJitter; +use Orangesoft\BackOff\LinearBackOff; use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier; use Orangesoft\BackOff\Retry\Retry; use Orangesoft\BackOff\Tests\CallbackSpy; +use Orangesoft\BackOff\Tests\SleeperSpy; use PHPUnit\Framework\TestCase; final class RetryTest extends TestCase { public function testSuccessfulCall(): void { - $retry = new Retry( - maxAttempts: 3, - exceptionClassifier: new ExceptionClassifier(), - ); + $retry = new Retry(3); /** @var float $result */ $result = $retry->call(static fn (): float => 1.618); @@ -26,8 +27,15 @@ public function testSuccessfulCall(): void public function testFailureRetryableCall(): void { + $sleeperSpy = new SleeperSpy(); $retry = new Retry( maxAttempts: 3, + backOff: new LinearBackOff( + baseTime: new Microseconds(1_000), + capTime: new Microseconds(5_000), + jitter: new NullJitter(), + sleeper: $sleeperSpy, + ), exceptionClassifier: new ExceptionClassifier( classNames: [ \RuntimeException::class, @@ -36,11 +44,12 @@ classNames: [ ); try { - $retry->call(new CallbackSpy(function (int $counter): never { + $retry->call(new CallbackSpy(static function (int $counter): never { throw new \RuntimeException(sprintf('Exception thrown %d times.', $counter)); })); } catch (\RuntimeException $e) { $this->assertSame('Exception thrown 2 times.', $e->getMessage()); + $this->assertEquals(3_000, $sleeperSpy->getSleepTime()?->asMicroseconds()); } } } diff --git a/tests/Strategy/PermanentStrategyTest.php b/tests/Strategy/ConstantStrategyTest.php similarity index 73% rename from tests/Strategy/PermanentStrategyTest.php rename to tests/Strategy/ConstantStrategyTest.php index 4145715..5264009 100644 --- a/tests/Strategy/PermanentStrategyTest.php +++ b/tests/Strategy/ConstantStrategyTest.php @@ -4,19 +4,19 @@ namespace Orangesoft\BackOff\Tests\Strategy; -use Orangesoft\BackOff\Strategy\PermanentStrategy; +use Orangesoft\BackOff\Strategy\ConstantStrategy; use PHPUnit\Framework\TestCase; -final class PermanentStrategyTest extends TestCase +final class ConstantStrategyTest extends TestCase { /** * @dataProvider getConstantStrategyData */ public function testConstantStrategy(int $attempt, float $time, float $expectedTime): void { - $permanentStrategy = new PermanentStrategy(); + $strategy = new ConstantStrategy(); - $actualTime = $permanentStrategy->calculate($attempt, $time); + $actualTime = $strategy->calculate($attempt, $time); $this->assertEquals($expectedTime, $actualTime); } diff --git a/tests/Strategy/DecorrelatedJitterStrategyTest.php b/tests/Strategy/DecorrelatedJitterStrategyTest.php index 2f497ec..111b86c 100644 --- a/tests/Strategy/DecorrelatedJitterStrategyTest.php +++ b/tests/Strategy/DecorrelatedJitterStrategyTest.php @@ -14,11 +14,11 @@ final class DecorrelatedJitterStrategyTest extends TestCase * * @dataProvider getDecorrelatedJitterStrategyData */ - public function testDecorrelatedJitterStrategy(float $multiplier, int $attempt, float $time, array $expectedTime): void + public function testDecorrelatedJitterStrategy(float $factor, int $attempt, float $time, array $expectedTime): void { - $decorrelatedJitterStrategy = new DecorrelatedJitterStrategy($multiplier); + $strategy = new DecorrelatedJitterStrategy($factor); - $actualTime = $decorrelatedJitterStrategy->calculate($attempt, $time); + $actualTime = $strategy->calculate($attempt, $time); $this->assertGreaterThanOrEqual($expectedTime[0], $actualTime); $this->assertLessThanOrEqual($expectedTime[1], $actualTime); diff --git a/tests/Strategy/ExponentialStrategyTest.php b/tests/Strategy/ExponentialStrategyTest.php index 7c3ce6c..b207390 100644 --- a/tests/Strategy/ExponentialStrategyTest.php +++ b/tests/Strategy/ExponentialStrategyTest.php @@ -12,11 +12,11 @@ final class ExponentialStrategyTest extends TestCase /** * @dataProvider getExponentialStrategyData */ - public function testExponentialStrategy(float $multiplier, int $attempt, float $time, float $expectedTime): void + public function testExponentialStrategy(float $factor, int $attempt, float $time, float $expectedTime): void { - $exponentialStrategy = new ExponentialStrategy($multiplier); + $strategy = new ExponentialStrategy($factor); - $actualTime = $exponentialStrategy->calculate($attempt, $time); + $actualTime = $strategy->calculate($attempt, $time); $this->assertEquals($expectedTime, $actualTime); } diff --git a/tests/Strategy/FibonacciStrategyTest.php b/tests/Strategy/FibonacciStrategyTest.php index 7ced8a0..9ebec06 100644 --- a/tests/Strategy/FibonacciStrategyTest.php +++ b/tests/Strategy/FibonacciStrategyTest.php @@ -14,9 +14,9 @@ final class FibonacciStrategyTest extends TestCase */ public function testFibonacciStrategy(int $attempt, float $time, float $expectedTime): void { - $fibonacciStrategy = new FibonacciStrategy(); + $strategy = new FibonacciStrategy(); - $actualTime = (int) $fibonacciStrategy->calculate($attempt, $time); + $actualTime = (int) $strategy->calculate($attempt, $time); $this->assertEquals($expectedTime, $actualTime); } diff --git a/tests/Strategy/LinearStrategyTest.php b/tests/Strategy/LinearStrategyTest.php index dc59fe5..7196ecc 100644 --- a/tests/Strategy/LinearStrategyTest.php +++ b/tests/Strategy/LinearStrategyTest.php @@ -14,9 +14,9 @@ final class LinearStrategyTest extends TestCase */ public function testLinearStrategy(int $attempt, int $time, int $expectedTime): void { - $linearStrategy = new LinearStrategy(); + $strategy = new LinearStrategy(); - $actualTime = $linearStrategy->calculate($attempt, $time); + $actualTime = $strategy->calculate($attempt, $time); $this->assertEquals($expectedTime, $actualTime); } From 7094a13ff28bc2d6be72e7acd0ceadfa8caa069c Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Mon, 1 Jul 2024 11:05:35 +0300 Subject: [PATCH 2/5] Update .gitattributes --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitattributes b/.gitattributes index 9fb2638..0f4f3ff 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,3 +12,5 @@ /psalm-baseline.xml export-ignore /psalm.xml export-ignore /tests/ export-ignore +/.Dockerfile export-ignore +/.Makefile export-ignore From ba7ad0761474e1608f676ff474f2c09d8b30a28c Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Mon, 1 Jul 2024 11:05:55 +0300 Subject: [PATCH 3/5] Update php-cs-fixer configuration --- .php-cs-fixer.dist.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 54072da..52dfa59 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -16,6 +16,8 @@ ->setRules([ '@Symfony' => true, '@Symfony:risky' => true, + 'phpdoc_separation' => false, + 'phpdoc_to_comment' => false, ]) ->setFinder($finder) ; From 2558ebdc23ae6acbeb125b605a01b147ed78f302 Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Mon, 1 Jul 2024 11:06:08 +0300 Subject: [PATCH 4/5] Update psalm baseline --- psalm-baseline.xml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8d47fef..d8c63be 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,13 +1,12 @@ - - - mixed - - mixed + + backOff + classify + From ba3a5170c039671a35e68fb25cf3fb9ac45693a7 Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Mon, 1 Jul 2024 11:06:17 +0300 Subject: [PATCH 5/5] Update README.md --- README.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index eb55cd8..2ed554c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This package requires PHP 8.1 or later. ## Quick usage -Configure `Orangesoft\BackOff\Retry\BackOffRetry::class`, any of back-off classes, and `Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier::class` to retry a business logic when an exception is thrown: +Configure `Orangesoft\BackOff\Retry\Retry::class`, any of back-off classes, and `Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier::class` to retry a business logic when an exception is thrown: ```php call(static function (): int { - $random = mt_rand(5, 10); +$result = $retry->call(static function (): int { + $random = mt_rand(0, 1); if (0 === $random % 2) { throw new \RuntimeException(); @@ -63,11 +63,11 @@ $result = $backOffRetry->call(static function (): int { The following back-off strategies are available: - [Orangesoft\BackOff\CallbackBackOff](./src/CallbackBackOff.php) +- [Orangesoft\BackOff\ConstantBackOff](./src/ConstantBackOff.php) - [Orangesoft\BackOff\DecorrelatedJitterBackOff](./src/DecorrelatedJitterBackOff.php) - [Orangesoft\BackOff\ExponentialBackOff](./src/ExponentialBackOff.php) - [Orangesoft\BackOff\FibonacciBackOff](./src/FibonacciBackOff.php) - [Orangesoft\BackOff\LinearBackOff](./src/LinearBackOff.php) -- [Orangesoft\BackOff\PermanentBackOff](./src/PermanentBackOff.php) ## Enable Jitter @@ -80,19 +80,21 @@ use Orangesoft\BackOff\ExponentialBackOff; use Orangesoft\BackOff\Duration\Microseconds; use Orangesoft\BackOff\Jitter\EqualJitter; -$exponentialBackOff = new ExponentialBackOff( - multiplier: 2.0, - jitter: new EqualJitter(), -); - -$exponentialBackOff->backOff( - attempt: 1, +$backOff = new ExponentialBackOff( baseTime: new Microseconds(1_000), capTime: new Microseconds(512_000), + factor: 2.0, + jitter: new EqualJitter(), ); + +for ($i = 1; $i <= 10; $i++) { + $backOff->backOff( + attempt: $i, + ); +} ``` -Below you can see the time intervals in microseconds for exponential back-off with a multiplier of 2.0 and equal jitter, where the base time is 1000 μs and the cap time is 512000 μs: +Below you can see the time intervals in microseconds for exponential back-off with a multiplier of `2.0` and equal jitter, where the base time is `1_000` μs and the cap time is `512_000` μs: ```text +---------+---------------------------+--------------------+