1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eljam\CircuitBreaker; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
6
|
|
|
use Eljam\CircuitBreaker\Circuit; |
7
|
|
|
use Eljam\CircuitBreaker\Event\CircuitEvents; |
8
|
|
|
use Eljam\CircuitBreaker\Exception\CircuitOpenException; |
9
|
|
|
use Eljam\CircuitBreaker\Exception\CustomException; |
10
|
|
|
use Symfony\Component\EventDispatcher\Event; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BreakerTest. |
14
|
|
|
*/ |
15
|
|
|
class BreakerTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
protected $dir; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'store'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* testMultiProcess. |
26
|
|
|
*/ |
27
|
|
|
public function testMultiProcess() |
28
|
|
|
{ |
29
|
|
|
$fileCache = new FilesystemCache($this->dir, 'txt'); |
30
|
|
|
$breaker = new Breaker('github_api', ['ignore_exceptions' => true], $fileCache); |
31
|
|
|
$breaker2 = new Breaker('github_api', ['ignore_exceptions' => true], $fileCache); |
32
|
|
|
|
33
|
|
|
$breaker1FailureCount = 0; |
34
|
|
|
|
35
|
|
|
$breaker->addListener(CircuitEvents::FAILURE, function (Event $event) use (&$breaker1FailureCount) { |
36
|
|
|
$breaker1FailureCount = $event->getCircuit()->getFailures(); |
|
|
|
|
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$breaker2->addListener(CircuitEvents::FAILURE, function (Event $event) use (&$breaker1FailureCount) { |
40
|
|
|
$this->assertEquals($breaker1FailureCount, $event->getCircuit()->getFailures()); |
|
|
|
|
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$fn = function () { |
44
|
|
|
throw new CustomException("An error as occured"); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$breaker->protect($fn); |
48
|
|
|
|
49
|
|
|
$breaker2->protect($fn); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* testOpenBehavior. |
55
|
|
|
*/ |
56
|
|
|
public function testOpenBehavior() |
57
|
|
|
{ |
58
|
|
|
$breaker = new Breaker( |
59
|
|
|
'exception breaker', |
60
|
|
|
['exclude_exceptions' => [CustomException::class]] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$breaker->addListener(CircuitEvents::OPEN, function (Event $event) { |
64
|
|
|
$this->assertInstanceOf(Circuit::class, $event->getCircuit()); |
|
|
|
|
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$this->setExpectedException('Eljam\CircuitBreaker\Exception\CircuitOpenException'); |
68
|
|
|
|
69
|
|
|
$fn = function () { |
70
|
|
|
throw new CustomException("An error as occured"); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
for ($i = 0; $i <= 5; $i++) { |
74
|
|
|
$breaker->protect($fn); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* testHalfOpenBehavior. |
80
|
|
|
*/ |
81
|
|
|
public function testHalfOpenBehavior() |
82
|
|
|
{ |
83
|
|
|
$breaker = new Breaker( |
84
|
|
|
'exception breaker', |
85
|
|
|
[ |
86
|
|
|
'reset_timeout' => 1, |
87
|
|
|
'ignore_exceptions' => true, |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$breaker->addListener(CircuitEvents::HALF_OPEN, function (Event $event) { |
92
|
|
|
$this->assertInstanceOf(Circuit::class, $event->getCircuit()); |
|
|
|
|
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$fn = function () { |
96
|
|
|
throw new CustomException("An error as occured"); |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
for ($i = 0; $i <= 5; $i++) { |
101
|
|
|
$breaker->protect($fn); |
102
|
|
|
} |
103
|
|
|
} catch (CircuitOpenException $e) { |
104
|
|
|
$this->assertSame(CircuitOpenException::class, get_class($e)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
sleep(2); |
108
|
|
|
|
109
|
|
|
$fnPass = function () { |
110
|
|
|
return 'ok'; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
$breaker->protect($fnPass); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* testGetTheResult. |
118
|
|
|
*/ |
119
|
|
|
public function testGetTheResult() |
120
|
|
|
{ |
121
|
|
|
$breaker = new Breaker('simple_echo'); |
122
|
|
|
$hello = 'eljam'; |
123
|
|
|
|
124
|
|
|
$fn = function () use ($hello) { |
125
|
|
|
return $hello; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
$result = $breaker->protect($fn); |
129
|
|
|
|
130
|
|
|
$this->assertSame($hello, $result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* testIgnoreException. |
135
|
|
|
*/ |
136
|
|
|
public function testIgnoreAllException() |
137
|
|
|
{ |
138
|
|
|
$breaker = new Breaker( |
139
|
|
|
'simple_echo', |
140
|
|
|
['ignore_exceptions' => true] |
141
|
|
|
); |
142
|
|
|
$hello = 'eljam'; |
143
|
|
|
|
144
|
|
|
$fn = function () use ($hello) { |
145
|
|
|
throw new CustomException("An error as occured"); |
146
|
|
|
|
147
|
|
|
return $hello; |
|
|
|
|
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
$result = $breaker->protect($fn); |
151
|
|
|
|
152
|
|
|
$this->assertNull($result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* testThrowCustomException. |
157
|
|
|
*/ |
158
|
|
|
public function testThrowCustomException() |
159
|
|
|
{ |
160
|
|
|
$breaker = new Breaker( |
161
|
|
|
'custom_exception' |
162
|
|
|
); |
163
|
|
|
$hello = 'eljam'; |
164
|
|
|
|
165
|
|
|
$this->setExpectedException('Eljam\CircuitBreaker\Exception\CustomException'); |
166
|
|
|
|
167
|
|
|
$fn = function () use ($hello) { |
168
|
|
|
throw new CustomException("An error as occured"); |
169
|
|
|
|
170
|
|
|
return $hello; |
|
|
|
|
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
$breaker->protect($fn); |
174
|
|
|
|
175
|
|
|
$this->assertInstanceOf(CustomException::class, $result); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function tearDown() |
179
|
|
|
{ |
180
|
|
|
@unlink($this->dir); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: