From 38c6f699a6f619f406ff19decc4a59048018f964 Mon Sep 17 00:00:00 2001 From: Kazuki Yamada Date: Wed, 21 Aug 2024 13:09:46 +0900 Subject: [PATCH] fix: Fix ExponentialStrategy calculation and update tests --- src/Strategies/ExponentialStrategy.php | 5 +---- tests/Strategies/ExponentialStrategyTest.php | 23 ++++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Strategies/ExponentialStrategy.php b/src/Strategies/ExponentialStrategy.php index bb55344..fedf91b 100644 --- a/src/Strategies/ExponentialStrategy.php +++ b/src/Strategies/ExponentialStrategy.php @@ -14,9 +14,6 @@ class ExponentialStrategy extends AbstractStrategy */ public function getWaitTime($attempt) { - return (int) ($attempt == 1 - ? $this->base - : pow(2, $attempt) * $this->base - ); + return (int) ($this->base * (pow(2, $attempt - 1))); } } diff --git a/tests/Strategies/ExponentialStrategyTest.php b/tests/Strategies/ExponentialStrategyTest.php index 898c81a..743020c 100644 --- a/tests/Strategies/ExponentialStrategyTest.php +++ b/tests/Strategies/ExponentialStrategyTest.php @@ -17,12 +17,21 @@ public function testWaitTimes() $s = new ExponentialStrategy(200); $this->assertEquals(200, $s->getWaitTime(1)); - $this->assertEquals(800, $s->getWaitTime(2)); - $this->assertEquals(1600, $s->getWaitTime(3)); - $this->assertEquals(3200, $s->getWaitTime(4)); - $this->assertEquals(6400, $s->getWaitTime(5)); - $this->assertEquals(12800, $s->getWaitTime(6)); - $this->assertEquals(25600, $s->getWaitTime(7)); - $this->assertEquals(51200, $s->getWaitTime(8)); + $this->assertEquals(400, $s->getWaitTime(2)); + $this->assertEquals(800, $s->getWaitTime(3)); + $this->assertEquals(1600, $s->getWaitTime(4)); + $this->assertEquals(3200, $s->getWaitTime(5)); + $this->assertEquals(6400, $s->getWaitTime(6)); + $this->assertEquals(12800, $s->getWaitTime(7)); + $this->assertEquals(25600, $s->getWaitTime(8)); + } + + public function testWaitTimesWithDefault() + { + $strategy = new ExponentialStrategy(); + $base = $strategy->getBase(); + $this->assertEquals((int) ($base * pow(2, 0)), $strategy->getWaitTime(1)); + $this->assertEquals((int) ($base * pow(2, 1)), $strategy->getWaitTime(2)); + $this->assertEquals((int) ($base * pow(2, 2)), $strategy->getWaitTime(3)); } }