Skip to content

Commit

Permalink
Merge pull request #19 from yamadashy/fix/exponential-strategy
Browse files Browse the repository at this point in the history
Fix ExponentialStrategy calculation and update tests
  • Loading branch information
jszobody authored Aug 29, 2024
2 parents 1758371 + 38c6f69 commit cfbc47e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/Strategies/ExponentialStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
23 changes: 16 additions & 7 deletions tests/Strategies/ExponentialStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit cfbc47e

Please sign in to comment.