j-velasco /
php-circuit-breaker
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace JVelasco\CircuitBreaker\AvailabilityStrategy; |
||||||
| 4 | |||||||
| 5 | use PHPUnit\Framework\TestCase; |
||||||
| 6 | use Prophecy\Argument; |
||||||
| 7 | use Prophecy\Prophecy\ObjectProphecy; |
||||||
| 8 | |||||||
| 9 | final class TimeBackoffTest extends TestCase |
||||||
| 10 | { |
||||||
| 11 | const SERVICE_NAME = 'service name'; |
||||||
| 12 | const LAST_ATTEMPT_KEY = "last_attempt"; |
||||||
| 13 | const MAX_FAILURES = 1; |
||||||
| 14 | const BASE_WAIT_TIME = 200; // 200 ms |
||||||
| 15 | const MAX_WAIT_TIME = 30000; // 30 secs |
||||||
| 16 | |||||||
| 17 | /** @var InMemoryStorage */ |
||||||
| 18 | private $storage; |
||||||
| 19 | /** @var TimeBackoff */ |
||||||
| 20 | private $sut; |
||||||
| 21 | /** @var BackoffStrategy|ObjectProphecy */ |
||||||
| 22 | private $backoffStrategy; |
||||||
| 23 | |||||||
| 24 | protected function setUp() |
||||||
| 25 | { |
||||||
| 26 | $this->storage = new InMemoryStorage(); |
||||||
| 27 | $this->backoffStrategy = $this->prophesize(BackoffStrategy::class); |
||||||
| 28 | $this->backoffStrategy->id()->willReturn("test_backoff"); |
||||||
| 29 | $this->sut = new TimeBackoff( |
||||||
| 30 | $this->storage, |
||||||
| 31 | $this->backoffStrategy->reveal(), |
||||||
| 32 | self::MAX_FAILURES, |
||||||
| 33 | self::BASE_WAIT_TIME, |
||||||
| 34 | self::MAX_WAIT_TIME |
||||||
| 35 | ); |
||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | /** @test */ |
||||||
| 39 | public function it_report_as_available_when_failures_is_under_threshold() |
||||||
| 40 | { |
||||||
| 41 | $this->failuresAreUnderThreshold(); |
||||||
| 42 | $this->assertTrue($this->sut->isAvailable(self::SERVICE_NAME)); |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | /** @test */ |
||||||
| 46 | public function it_reports_as_non_available_between_attempts() |
||||||
| 47 | { |
||||||
| 48 | $this->storage->saveStrategyData( |
||||||
| 49 | $this->sut, |
||||||
| 50 | self::SERVICE_NAME, |
||||||
| 51 | self::LAST_ATTEMPT_KEY, |
||||||
| 52 | floor(microtime(true) * 1000) |
||||||
| 53 | ); |
||||||
| 54 | $this->setFailuresToMaxAllowed($this->storage); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 55 | $this->backoffStrategy->waitTime(Argument::any(), self::BASE_WAIT_TIME) |
||||||
|
0 ignored issues
–
show
Prophecy\Argument::any() of type Prophecy\Argument\Token\AnyValueToken is incompatible with the type integer expected by parameter $attempt of JVelasco\CircuitBreaker\...offStrategy::waitTime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 56 | ->willReturn(100); |
||||||
| 57 | $this->assertFalse($this->sut->isAvailable(self::SERVICE_NAME)); |
||||||
| 58 | } |
||||||
| 59 | |||||||
| 60 | /** @test */ |
||||||
| 61 | public function it_closes_the_circuit_after_timeout() |
||||||
| 62 | { |
||||||
| 63 | $this->setFailuresToMaxAllowed(); |
||||||
| 64 | $oneSecAndOneMillisecond = floor((microtime(true) * 1000)) - self::BASE_WAIT_TIME - 1; |
||||||
| 65 | $this->storage->saveStrategyData( |
||||||
| 66 | $this->sut, |
||||||
| 67 | self::SERVICE_NAME, |
||||||
| 68 | self::LAST_ATTEMPT_KEY, |
||||||
| 69 | (string) $oneSecAndOneMillisecond |
||||||
| 70 | ); |
||||||
| 71 | |||||||
| 72 | $this->backoffStrategy->waitTime(Argument::any(), self::BASE_WAIT_TIME) |
||||||
|
0 ignored issues
–
show
Prophecy\Argument::any() of type Prophecy\Argument\Token\AnyValueToken is incompatible with the type integer expected by parameter $attempt of JVelasco\CircuitBreaker\...offStrategy::waitTime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 73 | ->willReturn(self::BASE_WAIT_TIME); |
||||||
| 74 | |||||||
| 75 | $this->assertTrue($this->sut->isAvailable(self::SERVICE_NAME)); |
||||||
| 76 | $this->assertEquals(0, $this->storage->numberOfFailures(self::SERVICE_NAME)); |
||||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /** @test */ |
||||||
| 80 | public function it_reports_as_available_on_storage_failures() |
||||||
| 81 | { |
||||||
| 82 | $this->storage->throwExceptionInNumberOfFailures(); |
||||||
| 83 | $this->assertTrue($this->sut->isAvailable(self::SERVICE_NAME)); |
||||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | |||||||
| 87 | /** @test */ |
||||||
| 88 | public function it_tracks_attempts() |
||||||
| 89 | { |
||||||
| 90 | $this->setFailuresToMaxAllowed(); |
||||||
| 91 | $this->waitTimeHaveOccurred(); |
||||||
| 92 | |||||||
| 93 | $this->sut->isAvailable(self::SERVICE_NAME); |
||||||
| 94 | $this->assertEquals(1, $this->storage->getStrategyData($this->sut, self::SERVICE_NAME, "attempts")); |
||||||
| 95 | |||||||
| 96 | $this->setFailuresToMaxAllowed(); |
||||||
| 97 | $this->waitTimeHaveOccurred(); |
||||||
| 98 | |||||||
| 99 | $this->sut->isAvailable(self::SERVICE_NAME); |
||||||
| 100 | $this->assertEquals(2, $this->storage->getStrategyData($this->sut, self::SERVICE_NAME, "attempts")); |
||||||
| 101 | } |
||||||
| 102 | |||||||
| 103 | /** @test */ |
||||||
| 104 | public function it_no_wait_longer_than_max_wait_time() |
||||||
| 105 | { |
||||||
| 106 | $this->setFailuresToMaxAllowed(); |
||||||
| 107 | $this->maxWaitTimeHaveOccurred(); |
||||||
| 108 | $this->backoffStrategy->waitTime(Argument::any(), self::BASE_WAIT_TIME) |
||||||
|
0 ignored issues
–
show
Prophecy\Argument::any() of type Prophecy\Argument\Token\AnyValueToken is incompatible with the type integer expected by parameter $attempt of JVelasco\CircuitBreaker\...offStrategy::waitTime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 109 | ->willReturn(self::MAX_WAIT_TIME + 1); |
||||||
| 110 | |||||||
| 111 | $this->assertTrue($this->sut->isAvailable(self::SERVICE_NAME)); |
||||||
| 112 | } |
||||||
| 113 | |||||||
| 114 | private function setFailuresToMaxAllowed() |
||||||
| 115 | { |
||||||
| 116 | $this->storage->setNumberOfFailures( |
||||||
| 117 | self::SERVICE_NAME, |
||||||
| 118 | self::MAX_FAILURES |
||||||
| 119 | ); |
||||||
| 120 | } |
||||||
| 121 | |||||||
| 122 | private function failuresAreUnderThreshold() |
||||||
| 123 | { |
||||||
| 124 | $this->storage->setNumberOfFailures( |
||||||
| 125 | self::SERVICE_NAME, |
||||||
| 126 | self::MAX_FAILURES - 1 |
||||||
| 127 | ); |
||||||
| 128 | } |
||||||
| 129 | |||||||
| 130 | private function waitTimeHaveOccurred() |
||||||
| 131 | { |
||||||
| 132 | $this->backoffStrategy->waitTime(Argument::any(), Argument::any()) |
||||||
|
0 ignored issues
–
show
Prophecy\Argument::any() of type Prophecy\Argument\Token\AnyValueToken is incompatible with the type integer expected by parameter $baseTime of JVelasco\CircuitBreaker\...offStrategy::waitTime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
Prophecy\Argument::any() of type Prophecy\Argument\Token\AnyValueToken is incompatible with the type integer expected by parameter $attempt of JVelasco\CircuitBreaker\...offStrategy::waitTime().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 133 | ->willReturn(0); |
||||||
| 134 | $this->storage->saveStrategyData( |
||||||
| 135 | $this->sut, |
||||||
| 136 | self::SERVICE_NAME, |
||||||
| 137 | self::LAST_ATTEMPT_KEY, |
||||||
| 138 | floor(microtime(true) * 1000) - 1); |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | private function maxWaitTimeHaveOccurred() |
||||||
| 142 | { |
||||||
| 143 | return $this->storage->saveStrategyData( |
||||||
|
0 ignored issues
–
show
Are you sure the usage of
$this->storage->saveStra... - self::MAX_WAIT_TIME) targeting JVelasco\CircuitBreaker\...age::saveStrategyData() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 144 | $this->sut, |
||||||
| 145 | self::SERVICE_NAME, |
||||||
| 146 | self::LAST_ATTEMPT_KEY, |
||||||
| 147 | floor(microtime(true) * 1000) - self::MAX_WAIT_TIME); |
||||||
| 148 | } |
||||||
| 149 | } |
||||||
| 150 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.