1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JVelasco\CircuitBreaker; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class CircuitBreakerTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** @test */ |
11
|
|
|
public function it_delegates_to_strategy_for_availability() |
12
|
|
|
{ |
13
|
|
|
$strategy = $this->prophesize(AvailabilityStrategy::class); |
14
|
|
|
$anAvailableService = "available"; |
15
|
|
|
$strategy->isAvailable($anAvailableService)->willReturn(true); |
16
|
|
|
$aNotAvailableService = "not available"; |
17
|
|
|
$strategy->isAvailable($aNotAvailableService)->willReturn(false); |
18
|
|
|
|
19
|
|
|
$circuitBreaker = new CircuitBreaker( |
20
|
|
|
$strategy->reveal(), |
21
|
|
|
$this->prophesize(FailuresCounterStorage::class)->reveal() |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
$this->assertTrue($circuitBreaker->isAvailable($anAvailableService)); |
25
|
|
|
$this->assertFalse($circuitBreaker->isAvailable($aNotAvailableService)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @test */ |
29
|
|
|
public function it_delegates_to_storage_counter_for_failures() |
30
|
|
|
{ |
31
|
|
|
$storage = $this->prophesize(FailuresCounterStorage::class); |
32
|
|
|
$aService = "a service"; |
33
|
|
|
$storage->incrementFailures($aService)->shouldBeCalledTimes(1); |
34
|
|
|
|
35
|
|
|
$circuitBreaker = new CircuitBreaker( |
36
|
|
|
$this->prophesize(AvailabilityStrategy::class)->reveal(), |
37
|
|
|
$storage->reveal() |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$circuitBreaker->reportFailure($aService); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @test */ |
44
|
|
|
public function it_delegates_to_storage_counter_for_successes() |
45
|
|
|
{ |
46
|
|
|
$storage = $this->prophesize(FailuresCounterStorage::class); |
47
|
|
|
$aService = "a service"; |
48
|
|
|
$storage->decrementFailures($aService)->shouldBeCalledTimes(1); |
49
|
|
|
|
50
|
|
|
$circuitBreaker = new CircuitBreaker( |
51
|
|
|
$this->prophesize(AvailabilityStrategy::class)->reveal(), |
52
|
|
|
$storage->reveal() |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$circuitBreaker->reportSuccess($aService); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @test */ |
59
|
|
|
public function it_throw_custom_exceptions_from_storage_increment_failures() |
60
|
|
|
{ |
61
|
|
|
$storage = $this->prophesize(FailuresCounterStorage::class); |
62
|
|
|
|
63
|
|
|
$circuitBreaker = new CircuitBreaker( |
64
|
|
|
$this->prophesize(AvailabilityStrategy::class)->reveal(), |
65
|
|
|
$storage->reveal() |
66
|
|
|
); |
67
|
|
|
$aService = "a service"; |
68
|
|
|
|
69
|
|
|
$storage->incrementFailures($aService)->willThrow(new Exception); |
70
|
|
|
$this->expectException(StorageException::class); |
71
|
|
|
$circuitBreaker->reportFailure($aService); |
72
|
|
|
|
73
|
|
|
$storage->decrementFailures($aService)->willThrow(new Exception); |
74
|
|
|
$this->expectException(StorageException::class); |
75
|
|
|
$this->expectExceptionMessage("Error incrementing failures"); |
76
|
|
|
$circuitBreaker->reportSuccess($aService); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @test */ |
80
|
|
|
public function it_throw_custom_exceptions_from_storage_decrement_failures() |
81
|
|
|
{ |
82
|
|
|
$storage = $this->prophesize(FailuresCounterStorage::class); |
83
|
|
|
|
84
|
|
|
$circuitBreaker = new CircuitBreaker( |
85
|
|
|
$this->prophesize(AvailabilityStrategy::class)->reveal(), |
86
|
|
|
$storage->reveal() |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$aService = "a service"; |
90
|
|
|
|
91
|
|
|
$storage->decrementFailures($aService)->willThrow(new Exception); |
92
|
|
|
$this->expectException(StorageException::class); |
93
|
|
|
$this->expectExceptionMessage("Error decrementing failures"); |
94
|
|
|
$circuitBreaker->reportSuccess($aService); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|