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 fe85e79 commit 079ed77
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ php:
- 7.3
- 7.4
- 8.0
- 8.1

matrix:
fast_finish: true
allow_failures:
- php: 8.0
- php: 8.1

env:
matrix:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},

"require-dev" : {
"jbzoo/toolbox-dev" : "^2.6.2"
"jbzoo/toolbox-dev" : "^2.6.2",
"jbzoo/jbdump" : ">=1.5.4"
},

"autoload" : {
Expand Down
9 changes: 1 addition & 8 deletions src/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Retry
*
* @var int In milliseconds
*/
protected $waitCap = 0;
protected $waitCap = self::DEFAULT_WAIT_CAP;

/**
* @var bool
Expand Down Expand Up @@ -159,7 +159,6 @@ public function setJitter(bool $useJitter): self
public function enableJitter(): self
{
$this->setJitter(true);

return $this;
}

Expand Down Expand Up @@ -216,10 +215,6 @@ protected function buildStrategy($strategy): callable
return $strategy;
}

if (\is_int($strategy)) {
return new ConstantStrategy($strategy);
}

throw new \InvalidArgumentException("Invalid strategy: {$strategy}");
}

Expand Down Expand Up @@ -321,7 +316,6 @@ public function wait(int $attempt): self

/**
* @param int $attempt
*
* @return int
*/
public function getWaitTime(int $attempt): int
Expand All @@ -341,7 +335,6 @@ protected function cap(int $waitTime): int

/**
* @param int $waitTime
*
* @return int
*/
protected function jitter(int $waitTime): int
Expand Down
39 changes: 39 additions & 0 deletions tests/RetryGeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace JBZoo\PHPUnit;

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

Expand Down Expand Up @@ -101,6 +102,11 @@ public function testClassAlias()
{
$backoff = new Backoff();

$backoff->enableJitter();
isTrue($backoff->jitterEnabled());
$backoff->disableJitter();
isFalse($backoff->jitterEnabled());

$result = $backoff->run(function () {
return 123;
});
Expand All @@ -116,4 +122,37 @@ public function testFunctionAlias()

isSame("success", $result);
}

public function testUndefinedStrategy()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage("Invalid strategy: UndefinedStrategy");

$backoff = new Backoff();
$backoff->setStrategy('UndefinedStrategy');
}

public function testErrorHandler()
{
$messages = [];

$retry = new Retry();
$retry->setErrorHandler(function ($exception, $attempt, $maxAttempts) use (&$messages) {
$messages[] = "On run {$attempt}/{$maxAttempts} we hit a problem: {$exception->getMessage()}";
});

try {
$retry->run(function () {
throw new \Error("failure");
});
} catch (\Exception $exception) {
}

isSame([
'On run 1/5 we hit a problem: failure',
'On run 2/5 we hit a problem: failure',
'On run 3/5 we hit a problem: failure',
'On run 4/5 we hit a problem: failure'
], $messages);
}
}

0 comments on commit 079ed77

Please sign in to comment.