1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CruftFlake\Config; |
4
|
|
|
|
5
|
|
|
use Gendoria\CruftFlake\Config\ConsulConfig; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Description of ConsulConfigTest |
11
|
|
|
* |
12
|
|
|
* @author Tomasz Struczyński <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ConsulConfigTest extends PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
public function testGetMachineIdOnEmptyCurrent() |
17
|
|
|
{ |
18
|
|
|
$kvPrefix = 'test/'; |
19
|
|
|
$sessionId = 'test'; |
20
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
21
|
|
|
'TTL' => '600s', |
22
|
|
|
"Behavior" => "delete", |
23
|
|
|
'LockDelay' => '300s', |
24
|
|
|
); |
25
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
26
|
|
|
$curl->expects($this->any()) |
27
|
|
|
->method('performPutRequest') |
28
|
|
|
->will($this->returnValueMap(array( |
29
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
30
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
31
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 0, true), |
32
|
|
|
))); |
33
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
34
|
|
|
$machine = $config->getMachine(); |
35
|
|
|
$this->assertEquals(0, $machine); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetMachineIdException() |
39
|
|
|
{ |
40
|
|
|
$this->setExpectedException('\RuntimeException', 'Could not register machine ID on consul'); |
|
|
|
|
41
|
|
|
$kvPrefix = 'test/'; |
42
|
|
|
$sessionId = 'test'; |
43
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
44
|
|
|
'TTL' => '600s', |
45
|
|
|
"Behavior" => "delete", |
46
|
|
|
'LockDelay' => '300s', |
47
|
|
|
); |
48
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
49
|
|
|
$curl->expects($this->any()) |
50
|
|
|
->method('performPutRequest') |
51
|
|
|
->will($this->returnValueMap(array( |
52
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
53
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
54
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 0, false), |
55
|
|
|
))); |
56
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
57
|
|
|
$machine = $config->getMachine(); |
58
|
|
|
$this->assertEquals(0, $machine); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetMachineIdConsuleTimeout() |
62
|
|
|
{ |
63
|
|
|
$kvPrefix = 'test/'; |
64
|
|
|
$sessionId = 'test'; |
65
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
66
|
|
|
'TTL' => '600s', |
67
|
|
|
"Behavior" => "delete", |
68
|
|
|
'LockDelay' => '300s', |
69
|
|
|
); |
70
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
71
|
|
|
$curl->expects($this->any()) |
72
|
|
|
->method('performPutRequest') |
73
|
|
|
->will($this->returnValueMap(array( |
74
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
75
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId."&flags=0", $sessionId, false), |
76
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId."&flags=1", $sessionId, true), |
77
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 0, true), |
78
|
|
|
))); |
79
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
80
|
|
|
$machine = $config->getMachine(); |
81
|
|
|
$this->assertEquals(0, $machine); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetMachineIdOnExistingCurrent() |
85
|
|
|
{ |
86
|
|
|
$kvPrefix = 'test/'; |
87
|
|
|
$sessionId = 'test'; |
88
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
89
|
|
|
'TTL' => '600s', |
90
|
|
|
"Behavior" => "delete", |
91
|
|
|
'LockDelay' => '300s', |
92
|
|
|
); |
93
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
94
|
|
|
$curl->expects($this->any()) |
95
|
|
|
->method('performPutRequest') |
96
|
|
|
->will($this->returnValueMap(array( |
97
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
98
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
99
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 2, true), |
100
|
|
|
))); |
101
|
|
|
$curl->expects($this->any()) |
102
|
|
|
->method('performGetRequest') |
103
|
|
|
->will($this->returnValueMap(array( |
104
|
|
|
array('/kv/'.$kvPrefix.'?recurse', array( |
105
|
|
|
array( |
106
|
|
|
'Key' => $kvPrefix, |
107
|
|
|
'Value' => $sessionId, |
108
|
|
|
), |
109
|
|
|
array( |
110
|
|
|
'Key' => 'testold', |
111
|
|
|
'Value' => base64_encode(0), |
112
|
|
|
), |
113
|
|
|
array( |
114
|
|
|
'Key' => 'testold', |
115
|
|
|
'Value' => base64_encode(1), |
116
|
|
|
), |
117
|
|
|
array( |
118
|
|
|
'Key' => 'testold', |
119
|
|
|
'Value' => base64_encode(3), |
120
|
|
|
), |
121
|
|
|
)), |
122
|
|
|
))); |
123
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
124
|
|
|
$machine = $config->getMachine(); |
125
|
|
|
$this->assertEquals(2, $machine); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetMachineIdExisting() |
129
|
|
|
{ |
130
|
|
|
$kvPrefix = 'test/'; |
131
|
|
|
$sessionId = 'test'; |
132
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
133
|
|
|
'TTL' => '600s', |
134
|
|
|
"Behavior" => "delete", |
135
|
|
|
'LockDelay' => '300s', |
136
|
|
|
); |
137
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
138
|
|
|
$curl->expects($this->any()) |
139
|
|
|
->method('performPutRequest') |
140
|
|
|
->will($this->returnValueMap(array( |
141
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
142
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
143
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 10, true), |
144
|
|
|
))); |
145
|
|
|
$curl->expects($this->any()) |
146
|
|
|
->method('performGetRequest') |
147
|
|
|
->will($this->returnValueMap(array( |
148
|
|
|
array('/kv/'.$kvPrefix.$sessionId, array( |
149
|
|
|
'Key' => $sessionId, |
150
|
|
|
'Value' => base64_encode(10), |
151
|
|
|
)), |
152
|
|
|
array('/kv/'.$kvPrefix.'?recurse', array( |
153
|
|
|
array( |
154
|
|
|
'Key' => $kvPrefix, |
155
|
|
|
'Value' => $sessionId, |
156
|
|
|
), |
157
|
|
|
)), |
158
|
|
|
))); |
159
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
160
|
|
|
$machine = $config->getMachine(); |
161
|
|
|
$this->assertEquals(10, $machine); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetMachineIdInCurrent() |
165
|
|
|
{ |
166
|
|
|
$kvPrefix = 'test/'; |
167
|
|
|
$sessionId = 'test'; |
168
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
169
|
|
|
'TTL' => '600s', |
170
|
|
|
"Behavior" => "delete", |
171
|
|
|
'LockDelay' => '300s', |
172
|
|
|
); |
173
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
174
|
|
|
$curl->expects($this->any()) |
175
|
|
|
->method('performPutRequest') |
176
|
|
|
->will($this->returnValueMap(array( |
177
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
178
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
179
|
|
|
array('/kv/'.$kvPrefix.$sessionId.'?acquire='.$sessionId, 10, true), |
180
|
|
|
))); |
181
|
|
|
$curl->expects($this->any()) |
182
|
|
|
->method('performGetRequest') |
183
|
|
|
->will($this->returnValueMap(array( |
184
|
|
|
array('/kv/'.$kvPrefix.'?recurse', array( |
185
|
|
|
array( |
186
|
|
|
'Key' => $kvPrefix, |
187
|
|
|
'Value' => $sessionId, |
188
|
|
|
), |
189
|
|
|
array( |
190
|
|
|
'Key' => 'test', |
191
|
|
|
'Value' => base64_encode(10), |
192
|
|
|
), |
193
|
|
|
)), |
194
|
|
|
))); |
195
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
196
|
|
|
$machine = $config->getMachine(); |
197
|
|
|
$this->assertEquals(10, $machine); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testGetMachineIdImpossible() |
201
|
|
|
{ |
202
|
|
|
$this->setExpectedException('RuntimeException', 'Cannot acquire machine ID'); |
|
|
|
|
203
|
|
|
$kvPrefix = 'test/'; |
204
|
|
|
$sessionId = 'test'; |
205
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
206
|
|
|
'TTL' => '600s', |
207
|
|
|
"Behavior" => "delete", |
208
|
|
|
'LockDelay' => '300s', |
209
|
|
|
); |
210
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
211
|
|
|
$curl->expects($this->any()) |
212
|
|
|
->method('performPutRequest') |
213
|
|
|
->will($this->returnValueMap(array( |
214
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
215
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
216
|
|
|
))); |
217
|
|
|
|
218
|
|
|
$filledValues = array_map(function($val) { |
219
|
|
|
return array( |
220
|
|
|
'Key' => 'test'.$val, |
221
|
|
|
'Value' => base64_encode($val), |
222
|
|
|
); |
223
|
|
|
}, range(0, 1023)); |
224
|
|
|
|
225
|
|
|
$curl->expects($this->any()) |
226
|
|
|
->method('performGetRequest') |
227
|
|
|
->will($this->returnValueMap(array( |
228
|
|
|
array('/kv/'.$kvPrefix.'?recurse', $filledValues), |
229
|
|
|
))); |
230
|
|
|
$config = new ConsulConfig($curl, 600, $kvPrefix); |
231
|
|
|
$config->getMachine(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testHeartbeatNothingToDo() |
235
|
|
|
{ |
236
|
|
|
$kvPrefix = 'test/'; |
237
|
|
|
$sessionId = 'test'; |
238
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
239
|
|
|
'TTL' => '200s', |
240
|
|
|
"Behavior" => "delete", |
241
|
|
|
'LockDelay' => '100s', |
242
|
|
|
); |
243
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
244
|
|
|
$curl->expects($this->any()) |
245
|
|
|
->method('performPutRequest') |
246
|
|
|
->will($this->returnValueMap(array( |
247
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
248
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
249
|
|
|
))); |
250
|
|
|
|
251
|
|
|
$config = new ConsulConfig($curl, 200, $kvPrefix); |
252
|
|
|
$this->assertFalse($config->heartbeat()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testHeartbeatSessionRenevalSuccessfull() |
256
|
|
|
{ |
257
|
|
|
$kvPrefix = 'test/'; |
258
|
|
|
$sessionId = 'test'; |
259
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
260
|
|
|
'TTL' => '0s', |
261
|
|
|
"Behavior" => "delete", |
262
|
|
|
'LockDelay' => '0s', |
263
|
|
|
); |
264
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
265
|
|
|
$curl->expects($this->any()) |
266
|
|
|
->method('performPutRequest') |
267
|
|
|
->will($this->returnValueMap(array( |
268
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
269
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
270
|
|
|
array("/session/renew/".$sessionId, null, true) |
271
|
|
|
))); |
272
|
|
|
|
273
|
|
|
$config = new ConsulConfig($curl, 0, $kvPrefix); |
274
|
|
|
$this->assertFalse($config->heartbeat()); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function testHeartbeatSessionRenevalUnsuccessfull() |
278
|
|
|
{ |
279
|
|
|
$kvPrefix = 'test/'; |
280
|
|
|
$sessionId = 'test'; |
281
|
|
|
$sessionCreatePayload = $payload = array( |
|
|
|
|
282
|
|
|
'TTL' => '0s', |
283
|
|
|
"Behavior" => "delete", |
284
|
|
|
'LockDelay' => '0s', |
285
|
|
|
); |
286
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
287
|
|
|
$curl->expects($this->any()) |
288
|
|
|
->method('performPutRequest') |
289
|
|
|
->will($this->returnValueMap(array( |
290
|
|
|
array('/session/create', json_encode($sessionCreatePayload), array('ID' => $sessionId)), |
291
|
|
|
array('/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0', $sessionId, true), |
292
|
|
|
array("/session/renew/".$sessionId, null, false) |
293
|
|
|
))); |
294
|
|
|
|
295
|
|
|
$config = new ConsulConfig($curl, 0, $kvPrefix); |
296
|
|
|
$this->assertTrue($config->heartbeat()); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testHeartbeatSessionRenevalAndCreationUnsuccessfull() |
300
|
|
|
{ |
301
|
|
|
$this->setExpectedException('RuntimeException', 'Cannot create session'); |
|
|
|
|
302
|
|
|
$kvPrefix = 'test/'; |
303
|
|
|
$sessionId = 'test'; |
304
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
305
|
|
|
$curl->expects($this->any()) |
306
|
|
|
->method('performPutRequest') |
307
|
|
|
->will($this->returnCallback(function($url) use ($sessionId, $kvPrefix) { |
308
|
|
|
static $invCount = 0; |
309
|
|
|
if ($url == '/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0') { |
310
|
|
|
return true; |
311
|
|
|
} elseif($url == '/session/renew') { |
312
|
|
|
return false; |
313
|
|
|
} elseif ($url == '/session/create') { |
314
|
|
|
if ($invCount == 0) { |
315
|
|
|
$invCount++; |
316
|
|
|
return array('ID' => $sessionId); |
317
|
|
|
} else { |
318
|
|
|
return null; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
})); |
322
|
|
|
|
323
|
|
|
try { |
324
|
|
|
$config = new ConsulConfig($curl, 0, $kvPrefix); |
325
|
|
|
} catch (RuntimeException $e) { |
326
|
|
|
$this->fail('Failed too quickly on '.$e->getMessage()); |
327
|
|
|
} |
328
|
|
|
$config->heartbeat(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function testHeartbeatSessionRenevalAndCreationUnsuccessfullNoUpdateNeeded() |
332
|
|
|
{ |
333
|
|
|
$kvPrefix = 'test/'; |
334
|
|
|
$sessionId = 'test'; |
335
|
|
|
$curl = $this->getMock('\Gendoria\CruftFlake\Config\ConsulCurl', array(), array('')); |
336
|
|
|
$curl->expects($this->any()) |
337
|
|
|
->method('performPutRequest') |
338
|
|
|
->will($this->returnCallback(function($url) use ($sessionId, $kvPrefix) { |
339
|
|
|
static $invCount = 0; |
340
|
|
|
if ($url == '/kv/'.$kvPrefix.'?acquire='.$sessionId.'&flags=0') { |
341
|
|
|
return true; |
342
|
|
|
} elseif($url == '/session/renew') { |
343
|
|
|
return false; |
344
|
|
|
} elseif ($url == '/session/create') { |
345
|
|
|
if ($invCount == 0) { |
346
|
|
|
$invCount++; |
347
|
|
|
return array('ID' => $sessionId); |
348
|
|
|
} else { |
349
|
|
|
return null; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
})); |
353
|
|
|
|
354
|
|
|
try { |
355
|
|
|
$config = new ConsulConfig($curl, 10, $kvPrefix); |
356
|
|
|
sleep(5); |
357
|
|
|
} catch (RuntimeException $e) { |
358
|
|
|
$this->fail('Failed too quickly on '.$e->getMessage()); |
359
|
|
|
} |
360
|
|
|
$this->assertFalse($config->heartbeat()); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.