|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JVelasco\CircuitBreaker\AvailabilityStrategy; |
|
4
|
|
|
|
|
5
|
|
|
use JVelasco\CircuitBreaker\StorageException; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
final class FixWaitTimeToRetryTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
const A_SERVICE = "a service"; |
|
11
|
|
|
const MAX_FAILURES = 1; |
|
12
|
|
|
const ONE_SECOND = 1; |
|
13
|
|
|
|
|
14
|
|
|
/** @test */ |
|
15
|
|
|
public function it_report_as_available() |
|
16
|
|
|
{ |
|
17
|
|
|
$storage = $this->prophesize(Storage::class); |
|
18
|
|
|
$strategy = new FixedWaitTimeToRetry( |
|
19
|
|
|
$storage->reveal(), |
|
20
|
|
|
self::MAX_FAILURES, |
|
21
|
|
|
self::ONE_SECOND |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
$storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES-1); |
|
25
|
|
|
$this->assertTrue($strategy->isAvailable(self::A_SERVICE)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @test */ |
|
29
|
|
|
public function it_reports_as_non_available() |
|
30
|
|
|
{ |
|
31
|
|
|
$storage = $this->prophesize(Storage::class); |
|
32
|
|
|
$strategy = new FixedWaitTimeToRetry( |
|
33
|
|
|
$storage->reveal(), |
|
34
|
|
|
self::MAX_FAILURES, |
|
35
|
|
|
self::ONE_SECOND |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES); |
|
39
|
|
|
$storage->getStrategyData($strategy, "last_try"); |
|
40
|
|
|
$this->assertFalse($strategy->isAvailable(self::A_SERVICE)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** @test */ |
|
44
|
|
|
public function it_close_the_circuit_after_timeout() |
|
45
|
|
|
{ |
|
46
|
|
|
$storage = $this->prophesize(Storage::class); |
|
47
|
|
|
$strategy = new FixedWaitTimeToRetry( |
|
48
|
|
|
$storage->reveal(), |
|
49
|
|
|
self::MAX_FAILURES, |
|
50
|
|
|
self::ONE_SECOND |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES); |
|
54
|
|
|
$twoSecondsAgo = new \DateTime(); |
|
55
|
|
|
$twoSecondsAgo->modify("-2 sec"); |
|
56
|
|
|
$storage->getStrategyData($strategy, "last_try") |
|
57
|
|
|
->willReturn((string) $twoSecondsAgo->getTimestamp()); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertTrue($strategy->isAvailable(self::A_SERVICE)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @test */ |
|
63
|
|
|
public function it_reports_as_available_on_storage_failures() |
|
64
|
|
|
{ |
|
65
|
|
|
$storage = $this->prophesize(Storage::class); |
|
66
|
|
|
$strategy = new FixedWaitTimeToRetry( |
|
67
|
|
|
$storage->reveal(), |
|
68
|
|
|
self::MAX_FAILURES, |
|
69
|
|
|
self::ONE_SECOND |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$storage->numberOfFailures(self::A_SERVICE)->willThrow(StorageException::class); |
|
73
|
|
|
$this->assertTrue($strategy->isAvailable(self::A_SERVICE)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|