Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
denisyukphp committed Sep 7, 2021
1 parent 8f5c38c commit 4418dd8
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 73 deletions.
3 changes: 0 additions & 3 deletions tests/Facade/ConstantBackOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orangesoft\BackOff\Tests\Facade;

use PHPUnit\Framework\TestCase;
use Orangesoft\BackOff\BackOffInterface;
use Orangesoft\BackOff\Facade\ConstantBackOff;
use Orangesoft\BackOff\Jitter\DummyJitter;
use Orangesoft\BackOff\Sleeper\Sleeper;
Expand All @@ -20,8 +19,6 @@ public function testBackOff(): void

$backOff = new ConstantBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $jitter, $sleeper);

$this->assertInstanceOf(BackOffInterface::class, $backOff);

$this->expectException(\RuntimeException::class);

$backOff->backOff(0, new \RuntimeException());
Expand Down
3 changes: 0 additions & 3 deletions tests/Facade/DecorrelationJitterBackOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orangesoft\BackOff\Tests\Facade;

use PHPUnit\Framework\TestCase;
use Orangesoft\BackOff\BackOffInterface;
use Orangesoft\BackOff\Facade\DecorrelationJitterBackOff;
use Orangesoft\BackOff\Sleeper\Sleeper;

Expand All @@ -19,8 +18,6 @@ public function testBackOff(): void

$backOff = new DecorrelationJitterBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $multiplier, $sleeper);

$this->assertInstanceOf(BackOffInterface::class, $backOff);

$this->expectException(\RuntimeException::class);

$backOff->backOff(0, new \RuntimeException());
Expand Down
3 changes: 0 additions & 3 deletions tests/Facade/ExponentialBackOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orangesoft\BackOff\Tests\Facade;

use PHPUnit\Framework\TestCase;
use Orangesoft\BackOff\BackOffInterface;
use Orangesoft\BackOff\Facade\ExponentialBackOff;
use Orangesoft\BackOff\Jitter\DummyJitter;
use Orangesoft\BackOff\Sleeper\Sleeper;
Expand All @@ -21,8 +20,6 @@ public function testBackOff(): void

$backOff = new ExponentialBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $multiplier, $jitter, $sleeper);

$this->assertInstanceOf(BackOffInterface::class, $backOff);

$this->expectException(\RuntimeException::class);

$backOff->backOff(0, new \RuntimeException());
Expand Down
3 changes: 0 additions & 3 deletions tests/Facade/LinearBackOffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orangesoft\BackOff\Tests\Facade;

use PHPUnit\Framework\TestCase;
use Orangesoft\BackOff\BackOffInterface;
use Orangesoft\BackOff\Facade\LinearBackOff;
use Orangesoft\BackOff\Jitter\DummyJitter;
use Orangesoft\BackOff\Sleeper\Sleeper;
Expand All @@ -20,8 +19,6 @@ public function testBackOff(): void

$backOff = new LinearBackOff($maxAttempts, $baseTimeMs, $capTimeMs, $jitter, $sleeper);

$this->assertInstanceOf(BackOffInterface::class, $backOff);

$this->expectException(\RuntimeException::class);

$backOff->backOff(0, new \RuntimeException());
Expand Down
51 changes: 24 additions & 27 deletions tests/Generator/GeneratorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,77 @@
use Orangesoft\BackOff\Generator\Generator;
use Orangesoft\BackOff\Generator\GeneratorBuilder;
use Orangesoft\BackOff\Duration\Milliseconds;
use Orangesoft\BackOff\Duration\DurationInterface;
use Orangesoft\BackOff\Strategy\ConstantStrategy;
use Orangesoft\BackOff\Strategy\ExponentialStrategy;
use Orangesoft\BackOff\Strategy\LinearStrategy;
use Orangesoft\BackOff\Strategy\StrategyInterface;
use Orangesoft\BackOff\Jitter\DummyJitter;
use Orangesoft\BackOff\Jitter\FullJitter;
use Orangesoft\BackOff\Jitter\JitterInterface;

class GeneratorBuilderTest extends TestCase
{
public function testCreate(): void
{
$builder = GeneratorBuilder::create();
$generatorBuilder = GeneratorBuilder::create();

$this->assertInstanceOf(GeneratorBuilder::class, $builder);
$this->assertInstanceOf(GeneratorBuilder::class, $generatorBuilder);
}

public function testDefaults(): void
public function testDefault(): void
{
$builder = GeneratorBuilder::create();
$generatorBuilder = GeneratorBuilder::create();

$this->assertSame(INF, $builder->getMaxAttempts());
$this->assertEquals(1000, $builder->getBaseTime()->asMilliseconds());
$this->assertEquals(60 * 1000, $builder->getCapTime()->asMilliseconds());
$this->assertInstanceOf(ConstantStrategy::class, $builder->getStrategy());
$this->assertInstanceOf(DummyJitter::class, $builder->getJitter());
$this->assertSame(INF, $generatorBuilder->getMaxAttempts());
$this->assertEquals(1000, $generatorBuilder->getBaseTime()->asMilliseconds());
$this->assertEquals(60 * 1000, $generatorBuilder->getCapTime()->asMilliseconds());
$this->assertInstanceOf(ExponentialStrategy::class, $generatorBuilder->getStrategy());
$this->assertInstanceOf(DummyJitter::class, $generatorBuilder->getJitter());
}

public function testMaxAttempts(): void
{
$builder = GeneratorBuilder::create()->setMaxAttempts(3);
$generatorBuilder = GeneratorBuilder::create()->setMaxAttempts(3);

$this->assertEquals(3, $builder->getMaxAttempts());
$this->assertEquals(3, $generatorBuilder->getMaxAttempts());
}

public function testMaxAttemptsInf(): void
{
$builder = GeneratorBuilder::create()->setMaxAttempts(INF);
$generatorBuilder = GeneratorBuilder::create()->setMaxAttempts(INF);

$this->assertEquals(INF, $builder->getMaxAttempts());
$this->assertEquals(INF, $generatorBuilder->getMaxAttempts());
}

public function testBaseTime(): void
{
$builder = GeneratorBuilder::create()->setBaseTime(new Milliseconds(1000));
$generatorBuilder = GeneratorBuilder::create()->setBaseTime(new Milliseconds(1000));

$this->assertInstanceOf(DurationInterface::class, $builder->getBaseTime());
$this->assertInstanceOf(Milliseconds::class, $generatorBuilder->getBaseTime());
}

public function testCapTime(): void
{
$builder = GeneratorBuilder::create()->setCapTime(new Milliseconds(1000));
$generatorBuilder = GeneratorBuilder::create()->setCapTime(new Milliseconds(1000));

$this->assertInstanceOf(DurationInterface::class, $builder->getCapTime());
$this->assertInstanceOf(Milliseconds::class, $generatorBuilder->getCapTime());
}

public function testStrategy(): void
{
$builder = GeneratorBuilder::create()->setStrategy(new LinearStrategy());
$generatorBuilder = GeneratorBuilder::create()->setStrategy(new LinearStrategy());

$this->assertInstanceOf(StrategyInterface::class, $builder->getStrategy());
$this->assertInstanceOf(LinearStrategy::class, $generatorBuilder->getStrategy());
}

public function testJitter(): void
{
$builder = GeneratorBuilder::create()->setJitter(new FullJitter());
$generatorBuilder = GeneratorBuilder::create()->setJitter(new FullJitter());

$this->assertInstanceOf(JitterInterface::class, $builder->getJitter());
$this->assertInstanceOf(FullJitter::class, $generatorBuilder->getJitter());
}

public function testBuild(): void
{
$generator = GeneratorBuilder::create()->build();
$generatorBuilder = GeneratorBuilder::create()->build();

$this->assertInstanceOf(Generator::class, $generator);
$this->assertInstanceOf(Generator::class, $generatorBuilder);
}
}
3 changes: 0 additions & 3 deletions tests/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit\Framework\TestCase;
use Orangesoft\BackOff\Generator\GeneratorBuilder;
use Orangesoft\BackOff\Duration\Milliseconds;
use Orangesoft\BackOff\Duration\DurationInterface;
use Orangesoft\BackOff\Strategy\LinearStrategy;
use Orangesoft\BackOff\Generator\Exception\MaxAttemptsException;

Expand All @@ -22,8 +21,6 @@ public function testGenerate(): void

$duration = $generator->generate(3);

$this->assertInstanceOf(DurationInterface::class, $duration);

$this->assertEquals(4000, $duration->asMilliseconds());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Jitter/FullJitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class FullJitterTest extends TestCase
{
public function testJitter(): void
{
$equalJitter = new FullJitter();
$fullJitter = new FullJitter();

$duration = $equalJitter->jitter(new Milliseconds(1000));
$duration = $fullJitter->jitter(new Milliseconds(1000));

$this->assertGreaterThanOrEqual(0, $duration->asMilliseconds());
$this->assertLessThanOrEqual(1000, $duration->asMilliseconds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

use Orangesoft\BackOff\BackOffInterface;

class BackOffCounter implements BackOffInterface
class AttemptsCounter implements BackOffInterface
{
/**
* @var int
*/
private $maxAttempts;
private $lastAttempt;
/**
* @var int
*/
private $lastAttempt;
private $maxAttempts;

public function __construct(int $maxAttempts, int $lastAttempt = 0)
public function __construct(int $maxAttempts)
{
$this->lastAttempt = 0;
$this->maxAttempts = $maxAttempts;
$this->lastAttempt = $lastAttempt;
}

/**
Expand Down
28 changes: 18 additions & 10 deletions tests/Retry/ExceptionClassifier/ExceptionClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@

class ExceptionClassifierTest extends TestCase
{
public function testDefaults(): void
public function testDefault(): void
{
$classifier = new ExceptionClassifier();
$exceptionClassifier = new ExceptionClassifier();

$this->assertTrue($classifier->classify(new \Error()));
$this->assertTrue($classifier->classify(new \Exception()));
$this->assertTrue($exceptionClassifier->classify(new \Error()));
$this->assertTrue($exceptionClassifier->classify(new \Exception()));
}

public function testClassify(): void
public function testInherited(): void
{
$classifier = new ExceptionClassifier([
$exceptionClassifier = new ExceptionClassifier([
\RuntimeException::class,
]);

$this->assertTrue($classifier->classify(new \RuntimeException()));
$this->assertTrue($classifier->classify(new \UnexpectedValueException()));
$this->assertFalse($classifier->classify(new \InvalidArgumentException()));
$this->assertFalse($classifier->classify(new \Exception()));
$this->assertTrue($exceptionClassifier->classify(new \RuntimeException()));
$this->assertTrue($exceptionClassifier->classify(new \UnexpectedValueException()));
}

public function testUnsupported(): void
{
$exceptionClassifier = new ExceptionClassifier([
\RuntimeException::class,
]);

$this->assertFalse($exceptionClassifier->classify(new \InvalidArgumentException()));
$this->assertFalse($exceptionClassifier->classify(new \Exception()));
}
}
12 changes: 6 additions & 6 deletions tests/Retry/RetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function testThrowException(): void
{
$backOff = new ConstantBackOff(0, 0);

$classifier = new ExceptionClassifier([
$exceptionClassifier = new ExceptionClassifier([
\RuntimeException::class,
]);

$retry = new Retry($backOff, $classifier);
$retry = new Retry($backOff, $exceptionClassifier);

$this->expectException(\RuntimeException::class);

Expand All @@ -46,20 +46,20 @@ public function testThrowException(): void

public function testAttemptsCounter(): void
{
$counter = new BackOffCounter(3);
$attemptsCounter = new AttemptsCounter(3);

$classifier = new ExceptionClassifier([
$exceptionClassifier = new ExceptionClassifier([
\RuntimeException::class,
]);

$retry = new Retry($counter, $classifier);
$retry = new Retry($attemptsCounter, $exceptionClassifier);

try {
$retry->call(function () {
throw new \RuntimeException();
});
} catch (\RuntimeException $e) {
$this->assertSame(3, $counter->getLastAttempt());
$this->assertSame(3, $attemptsCounter->getLastAttempt());
}
}
}
4 changes: 2 additions & 2 deletions tests/Strategy/ConstantStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ConstantStrategyTest extends TestCase
{
public function testCalculate(): void
{
$strategy = new ConstantStrategy();
$constantStrategy = new ConstantStrategy();

$duration = $strategy->calculate(new Milliseconds(1000), 3);
$duration = $constantStrategy->calculate(new Milliseconds(1000), 3);

$this->assertEquals(1000, $duration->asMilliseconds());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Strategy/DecorrelationJitterStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class DecorrelationJitterStrategyTest extends TestCase
{
public function testCalculate(): void
{
$strategy = new DecorrelationJitterStrategy(3);
$decorrelationJitterStrategy = new DecorrelationJitterStrategy(3);

$duration = $strategy->calculate(new Milliseconds(1000), 0);
$duration = $decorrelationJitterStrategy->calculate(new Milliseconds(1000), 0);

$this->assertGreaterThanOrEqual(1000, $duration->asMilliseconds());
$this->assertLessThanOrEqual(3000, $duration->asMilliseconds());
Expand Down
4 changes: 2 additions & 2 deletions tests/Strategy/ExponentialStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ExponentialStrategyTest extends TestCase
{
public function testCalculate(): void
{
$strategy = new ExponentialStrategy(2);
$exponentialStrategy = new ExponentialStrategy(2);

$duration = $strategy->calculate(new Milliseconds(1000), 3);
$duration = $exponentialStrategy->calculate(new Milliseconds(1000), 3);

$this->assertEquals(8000, $duration->asMilliseconds());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Strategy/LinearStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class LinearStrategyTest extends TestCase
{
public function testCalculate(): void
{
$strategy = new LinearStrategy();
$linearStrategy = new LinearStrategy();

$duration = $strategy->calculate(new Milliseconds(1000), 3);
$duration = $linearStrategy->calculate(new Milliseconds(1000), 3);

$this->assertEquals(4000, $duration->asMilliseconds());
}
Expand Down

0 comments on commit 4418dd8

Please sign in to comment.