Skip to content

Commit

Permalink
Strict code, code standards and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 18, 2021
1 parent 079ed77 commit b2f5634
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Strategies/ExponentialStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ExponentialStrategy extends AbstractStrategy
*/
public function getWaitTime(int $attempt): int
{
return $attempt === 1 ? $this->base : (2 ** $attempt) * $this->base;
$value = $attempt === 1 ? $this->base : (2 ** $attempt) * $this->base;
return (int)$value;
}
}
3 changes: 2 additions & 1 deletion src/Strategies/PolynomialStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public function __construct(int $base = self::DEFAULT_BASE, int $degree = self::
*/
public function getWaitTime(int $attempt): int
{
return ($attempt ** $this->degree) * $this->base;
$value = ($attempt ** $this->degree) * $this->base;
return (int)$value;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/RetryGeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use JBZoo\Retry\Retry;
use JBZoo\Retry\Strategies\ConstantStrategy;
use JBZoo\Retry\Strategies\ExponentialStrategy;
use STS\Backoff\Backoff;

use function JBZoo\Retry\retry;
Expand Down Expand Up @@ -114,6 +115,24 @@ public function testClassAlias()
isSame(123, $result);
}

public function testJitter()
{
$retry = new Retry();
$retry->setStrategy(new ExponentialStrategy(10));
$retry->setWaitCap(1000000);

isFalse($retry->jitterEnabled());
isSame($retry->getWaitTime(3), $retry->getWaitTime(3));

$retry->enableJitter();
isTrue($retry->jitterEnabled());
isNotSame($retry->getWaitTime(3), $retry->getWaitTime(3));

$retry->disableJitter();
isFalse($retry->jitterEnabled());
isSame($retry->getWaitTime(3), $retry->getWaitTime(3));
}

public function testFunctionAlias()
{
$result = \backoff(function () {
Expand Down

0 comments on commit b2f5634

Please sign in to comment.