1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Cronus; |
4
|
|
|
|
5
|
|
|
use MongoDB\BSON\ObjectID; |
6
|
|
|
|
7
|
|
|
//encoded hostname from the replaced builtin function |
8
|
|
|
const HOSTNAME = 'my_DOT_host_DOLLAR_name'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @coversDefaultClass \TraderInteractive\Cronus\ProcessRegistry |
12
|
|
|
* @covers ::<private> |
13
|
|
|
* @covers ::__construct |
14
|
|
|
*/ |
15
|
|
|
final class ProcessRegistryTest extends \PHPUnit\Framework\TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Mongo collection to use in each test. |
19
|
|
|
* |
20
|
|
|
* @var \MongoDB\Collection |
21
|
|
|
*/ |
22
|
|
|
private $collection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Registry instance to use in each test. |
26
|
|
|
* |
27
|
|
|
* @var ProcessRegistry |
28
|
|
|
*/ |
29
|
|
|
private $registry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Prepare each test. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
$mongo = new \MongoDB\Client( |
39
|
|
|
'mongodb://localhost:27017', |
40
|
|
|
[], |
41
|
|
|
['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
42
|
|
|
); |
43
|
|
|
$this->collection = $mongo->selectDatabase('testing')->selectCollection('processes'); |
44
|
|
|
$this->collection->drop(); |
45
|
|
|
$this->registry = new ProcessRegistry($this->collection); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
* @covers ::add |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function addEmptyCollection() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId'))); |
57
|
|
|
|
58
|
|
|
$this->assertSame(1, $this->collection->count()); |
59
|
|
|
$result = $this->collection->findOne(); |
60
|
|
|
|
61
|
|
|
$expected = [ |
62
|
|
|
'_id' => 'testId', |
63
|
|
|
'hosts' => [HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
64
|
|
|
'version' => $result['version'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->assertSame($expected, $result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @test |
72
|
|
|
* @covers ::add |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function addExistingDifferentHost() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$expireSecs = time() + 60; |
79
|
|
|
$initialVersion = new ObjectID(); |
80
|
|
|
$initalTask = [ |
81
|
|
|
'_id' => 'testId', |
82
|
|
|
'hosts' => ['a host' => ['1' => $expireSecs]], |
83
|
|
|
'version' => $initialVersion, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$this->collection->insertOne($initalTask); |
87
|
|
|
|
88
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 2, 1))); |
89
|
|
|
|
90
|
|
|
$this->assertSame(1, $this->collection->count()); |
91
|
|
|
$result = $this->collection->findOne(); |
92
|
|
|
|
93
|
|
|
$expected = [ |
94
|
|
|
'_id' => 'testId', |
95
|
|
|
'hosts' => [ |
96
|
|
|
'a host' => ['1' => $expireSecs], |
97
|
|
|
HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX], |
98
|
|
|
], |
99
|
|
|
'version' => $result['version'], |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$this->assertSame($expected, $result); |
103
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
* @covers ::add |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function addOverMaxGlobalProcessesOnDifferentHost() |
113
|
|
|
{ |
114
|
|
|
$initalTask = [ |
115
|
|
|
'_id' => 'testId', |
116
|
|
|
'hosts' => ['different host' => ['1' => time() + 60]], |
117
|
|
|
'version' => new ObjectID(), |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$this->collection->insertOne($initalTask); |
121
|
|
|
|
122
|
|
|
$this->assertFalse($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 1, PHP_INT_MAX))); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @test |
127
|
|
|
* @covers ::add |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function addOverMaxGlobalProcessesOnSameHost() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$pipes = []; |
134
|
|
|
$process = proc_open('sleep 10 &', self::getDevNullProcOpenDescriptors(), $pipes); |
135
|
|
|
$status = proc_get_status($process); |
136
|
|
|
if (!$status['running']) { |
137
|
|
|
$this->markTestSkipped('Unable to start sleep process.'); |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$initalTask = [ |
142
|
|
|
'_id' => 'testId', |
143
|
|
|
'hosts' => [HOSTNAME => [$status['pid'] => time() + 60]], |
144
|
|
|
'version' => new ObjectID(), |
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
$this->collection->insertOne($initalTask); |
148
|
|
|
|
149
|
|
|
$this->assertFalse($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 1, PHP_INT_MAX))); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @test |
154
|
|
|
* @covers ::add |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function addOverMaxHostProcesses() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$pipes = []; |
161
|
|
|
$process = proc_open('sleep 10 &', self::getDevNullProcOpenDescriptors(), $pipes); |
162
|
|
|
$status = proc_get_status($process); |
163
|
|
|
if (!$status['running']) { |
164
|
|
|
$this->markTestSkipped('Unable to start sleep process.'); |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$initalTask = [ |
169
|
|
|
'_id' => 'testId', |
170
|
|
|
'hosts' => [HOSTNAME => [$status['pid'] => time() + 60]], |
171
|
|
|
'version' => new ObjectID(), |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$this->collection->insertOne($initalTask); |
175
|
|
|
|
176
|
|
|
$this->assertFalse($this->registry->add($this->getProcess('testId', PHP_INT_MAX, PHP_INT_MAX, 1))); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @test |
181
|
|
|
* @covers ::add |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function addCleaningNotRunningProcessWithoutExtra() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$initialVersion = new ObjectID(); |
188
|
|
|
$initalTask = [ |
189
|
|
|
'_id' => 'testId', |
190
|
|
|
'hosts' => [HOSTNAME => ['9999999' => time() + 60]], |
191
|
|
|
'version' => $initialVersion, |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
$this->collection->insertOne($initalTask); |
195
|
|
|
|
196
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId'))); |
197
|
|
|
|
198
|
|
|
$this->assertSame(1, $this->collection->count()); |
199
|
|
|
$result = $this->collection->findOne(); |
200
|
|
|
|
201
|
|
|
$expected = [ |
202
|
|
|
'_id' => 'testId', |
203
|
|
|
'hosts' => [HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
204
|
|
|
'version' => $result['version'], |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
$this->assertSame($expected, $result); |
208
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @test |
213
|
|
|
* @covers ::add |
214
|
|
|
* |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
View Code Duplication |
public function addCleaningNotRunningProcessWithExtra() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$pipes = []; |
220
|
|
|
$process = proc_open('sleep 3 &', self::getDevNullProcOpenDescriptors(), $pipes); |
221
|
|
|
$status = proc_get_status($process); |
222
|
|
|
if (!$status['running']) { |
223
|
|
|
$this->markTestSkipped('Unable to start sleep process.'); |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$extraPid = $status['pid']; |
228
|
|
|
|
229
|
|
|
$expireSecs = time() + 60; |
230
|
|
|
$initialVersion = new ObjectID(); |
231
|
|
|
$initalTask = [ |
232
|
|
|
'_id' => 'testId', |
233
|
|
|
'hosts' => [ |
234
|
|
|
HOSTNAME => [ |
235
|
|
|
$extraPid => $expireSecs, |
236
|
|
|
'9999999' => $expireSecs, |
237
|
|
|
], |
238
|
|
|
], |
239
|
|
|
'version' => $initialVersion, |
240
|
|
|
]; |
241
|
|
|
|
242
|
|
|
$this->collection->insertOne($initalTask); |
243
|
|
|
|
244
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 2, 2))); |
245
|
|
|
|
246
|
|
|
$this->assertSame(1, $this->collection->count()); |
247
|
|
|
$result = $this->collection->findOne(); |
248
|
|
|
|
249
|
|
|
$expected = [ |
250
|
|
|
'_id' => 'testId', |
251
|
|
|
'hosts' => [HOSTNAME => [$extraPid => $expireSecs, getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
252
|
|
|
'version' => $result['version'], |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
$this->assertSame($expected, $result); |
256
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @test |
261
|
|
|
* @covers ::add |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
View Code Duplication |
public function addCleaningExpiredProcessWithoutExtra() |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$initialVersion = new ObjectID(); |
268
|
|
|
$initalTask = [ |
269
|
|
|
'_id' => 'testId', |
270
|
|
|
'hosts' => ['different host' => ['1' => strtotime('-1 hour')]], |
271
|
|
|
'version' => $initialVersion, |
272
|
|
|
]; |
273
|
|
|
|
274
|
|
|
$this->collection->insertOne($initalTask); |
275
|
|
|
|
276
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId'))); |
277
|
|
|
|
278
|
|
|
$this->assertSame(1, $this->collection->count()); |
279
|
|
|
$result = $this->collection->findOne(); |
280
|
|
|
|
281
|
|
|
$expected = [ |
282
|
|
|
'_id' => 'testId', |
283
|
|
|
'hosts' => [HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
284
|
|
|
'version' => $result['version'], |
285
|
|
|
]; |
286
|
|
|
|
287
|
|
|
$this->assertSame($expected, $result); |
288
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @test |
293
|
|
|
* @covers ::add |
294
|
|
|
* |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
View Code Duplication |
public function addCleaningExpiredProcessWithExtra() |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$expireSecs = time() + 60; |
300
|
|
|
$initialVersion = new ObjectID(); |
301
|
|
|
$initalTask = [ |
302
|
|
|
'_id' => 'testId', |
303
|
|
|
'hosts' => [ |
304
|
|
|
'a host' => [ |
305
|
|
|
'2' => time() - 1, |
306
|
|
|
'1' => $expireSecs, |
307
|
|
|
], |
308
|
|
|
], |
309
|
|
|
'version' => $initialVersion, |
310
|
|
|
]; |
311
|
|
|
|
312
|
|
|
$this->collection->insertOne($initalTask); |
313
|
|
|
|
314
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 2, 2))); |
315
|
|
|
|
316
|
|
|
$this->assertSame(1, $this->collection->count()); |
317
|
|
|
$result = $this->collection->findOne(); |
318
|
|
|
|
319
|
|
|
$expected = [ |
320
|
|
|
'_id' => 'testId', |
321
|
|
|
'hosts' => [ |
322
|
|
|
'a host' => ['1' => $expireSecs], |
323
|
|
|
HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX] |
324
|
|
|
], |
325
|
|
|
'version' => $result['version'], |
326
|
|
|
]; |
327
|
|
|
|
328
|
|
|
$this->assertSame($expected, $result); |
329
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @test |
334
|
|
|
* @covers ::add |
335
|
|
|
* |
336
|
|
|
* @return void |
337
|
|
|
*/ |
338
|
|
View Code Duplication |
public function addCleaningRecycledProcessWithoutExtra() |
|
|
|
|
339
|
|
|
{ |
340
|
|
|
$initialVersion = new ObjectID(); |
341
|
|
|
$initalTask = [ |
342
|
|
|
'_id' => 'testId', |
343
|
|
|
'hosts' => [HOSTNAME => [getmypid() => time() + 60]], |
344
|
|
|
'version' => $initialVersion, |
345
|
|
|
]; |
346
|
|
|
|
347
|
|
|
$this->collection->insertOne($initalTask); |
348
|
|
|
|
349
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId'))); |
350
|
|
|
|
351
|
|
|
$this->assertSame(1, $this->collection->count()); |
352
|
|
|
$result = $this->collection->findOne(); |
353
|
|
|
|
354
|
|
|
$expected = [ |
355
|
|
|
'_id' => 'testId', |
356
|
|
|
'hosts' => [HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
357
|
|
|
'version' => $result['version'], |
358
|
|
|
]; |
359
|
|
|
|
360
|
|
|
$this->assertSame($expected, $result); |
361
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @test |
366
|
|
|
* @covers ::add |
367
|
|
|
* |
368
|
|
|
* @return void |
369
|
|
|
*/ |
370
|
|
View Code Duplication |
public function addCleaningRecycledProcessWithExtra() |
|
|
|
|
371
|
|
|
{ |
372
|
|
|
$pipes = []; |
373
|
|
|
$process = proc_open('sleep 3 &', self::getDevNullProcOpenDescriptors(), $pipes); |
374
|
|
|
$status = proc_get_status($process); |
375
|
|
|
if (!$status['running']) { |
376
|
|
|
$this->markTestSkipped('Unable to start sleep process.'); |
377
|
|
|
return; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$extraPid = $status['pid']; |
381
|
|
|
|
382
|
|
|
$expireSecs = time() + 60; |
383
|
|
|
$initialVersion = new ObjectID(); |
384
|
|
|
$initalTask = [ |
385
|
|
|
'_id' => 'testId', |
386
|
|
|
'hosts' => [ |
387
|
|
|
HOSTNAME => [ |
388
|
|
|
$extraPid => $expireSecs, |
389
|
|
|
getmypid() => $expireSecs, |
390
|
|
|
], |
391
|
|
|
], |
392
|
|
|
'version' => $initialVersion, |
393
|
|
|
]; |
394
|
|
|
|
395
|
|
|
$this->collection->insertOne($initalTask); |
396
|
|
|
|
397
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId', PHP_INT_MAX, 2, 2))); |
398
|
|
|
|
399
|
|
|
$this->assertSame(1, $this->collection->count()); |
400
|
|
|
$result = $this->collection->findOne(); |
401
|
|
|
|
402
|
|
|
$expected = [ |
403
|
|
|
'_id' => 'testId', |
404
|
|
|
'hosts' => [HOSTNAME => [$extraPid => $expireSecs, getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
405
|
|
|
'version' => $result['version'], |
406
|
|
|
]; |
407
|
|
|
|
408
|
|
|
$this->assertSame($expected, $result); |
409
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @test |
414
|
|
|
* @covers ::add |
415
|
|
|
* |
416
|
|
|
* @return void |
417
|
|
|
*/ |
418
|
|
View Code Duplication |
public function addUnderflowMinsBeforeExpire() |
|
|
|
|
419
|
|
|
{ |
420
|
|
|
$this->assertTrue($this->registry->add($this->getProcess('testId', ~PHP_INT_MAX))); |
421
|
|
|
|
422
|
|
|
$this->assertSame(1, $this->collection->count()); |
423
|
|
|
$result = $this->collection->findOne(); |
424
|
|
|
|
425
|
|
|
$this->assertSame( |
426
|
|
|
['_id' => 'testId', 'hosts' => [HOSTNAME => [getmypid() => 0]], 'version' => $result['version']], |
427
|
|
|
$result |
428
|
|
|
); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @test |
433
|
|
|
* @covers ::remove |
434
|
|
|
* |
435
|
|
|
* @return void |
436
|
|
|
*/ |
437
|
|
|
public function removeWithExistingProcess() |
438
|
|
|
{ |
439
|
|
|
$initialVersion = new ObjectID(); |
440
|
|
|
$initalTask = [ |
441
|
|
|
'_id' => 'testId', |
442
|
|
|
'hosts' => [ |
443
|
|
|
HOSTNAME => ['1' => 0, getmypid() => time() + 60], |
444
|
|
|
], |
445
|
|
|
'version' => $initialVersion, |
446
|
|
|
]; |
447
|
|
|
|
448
|
|
|
$this->collection->insertOne($initalTask); |
449
|
|
|
|
450
|
|
|
$this->registry->remove($this->getProcess('testId')); |
451
|
|
|
|
452
|
|
|
$this->assertSame(1, $this->collection->count()); |
453
|
|
|
$result = $this->collection->findOne(); |
454
|
|
|
|
455
|
|
|
$this->assertSame( |
456
|
|
|
['_id' => 'testId', 'hosts' => [HOSTNAME => ['1' => 0]], 'version' => $result['version']], |
457
|
|
|
$result |
458
|
|
|
); |
459
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @test |
464
|
|
|
* @covers ::remove |
465
|
|
|
* |
466
|
|
|
* @return void |
467
|
|
|
*/ |
468
|
|
View Code Duplication |
public function removeWithoutExistingProcess() |
|
|
|
|
469
|
|
|
{ |
470
|
|
|
$initialVersion = new ObjectID(); |
471
|
|
|
$initalTask = [ |
472
|
|
|
'_id' => 'testId', |
473
|
|
|
'hosts' => [HOSTNAME => [getmypid() => time() + 60]], |
474
|
|
|
'version' => $initialVersion, |
475
|
|
|
]; |
476
|
|
|
|
477
|
|
|
$this->collection->insertOne($initalTask); |
478
|
|
|
|
479
|
|
|
$this->registry->remove($this->getProcess('testId')); |
480
|
|
|
|
481
|
|
|
$this->assertSame(1, $this->collection->count()); |
482
|
|
|
$result = $this->collection->findOne(); |
483
|
|
|
|
484
|
|
|
$this->assertSame(['_id' => 'testId', 'hosts' => [HOSTNAME => []], 'version' => $result['version']], $result); |
485
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @test |
490
|
|
|
* @covers ::reset |
491
|
|
|
* |
492
|
|
|
* @return void |
493
|
|
|
*/ |
494
|
|
|
public function resetWithoutExtra() |
495
|
|
|
{ |
496
|
|
|
$initialExpireSecs = time() + 60; |
497
|
|
|
$initialVersion = new ObjectID(); |
498
|
|
|
$initalTask = [ |
499
|
|
|
'_id' => 'testId', |
500
|
|
|
'hosts' => [HOSTNAME => [getmypid() => $initialExpireSecs]], |
501
|
|
|
'version' => $initialVersion, |
502
|
|
|
]; |
503
|
|
|
|
504
|
|
|
$this->collection->insertOne($initalTask); |
505
|
|
|
|
506
|
|
|
$this->registry->reset($this->getProcess('testId', 2)); |
507
|
|
|
|
508
|
|
|
$this->assertSame(1, $this->collection->count()); |
509
|
|
|
$result = $this->collection->findOne(); |
510
|
|
|
|
511
|
|
|
$result['hosts'][HOSTNAME][getmypid()] = null; |
512
|
|
|
|
513
|
|
|
$this->assertSame( |
514
|
|
|
['_id' => 'testId', 'hosts' => [HOSTNAME => [getmypid() => null]], 'version' => $result['version']], |
515
|
|
|
$result |
516
|
|
|
); |
517
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @test |
522
|
|
|
* @covers ::reset |
523
|
|
|
* |
524
|
|
|
* @return void |
525
|
|
|
*/ |
526
|
|
|
public function resetWithExtra() |
527
|
|
|
{ |
528
|
|
|
$initialExpireSecs = time() + 60; |
529
|
|
|
$initialVersion = new ObjectID(); |
530
|
|
|
$initalTask = [ |
531
|
|
|
'_id' => 'testId', |
532
|
|
|
'hosts' => [ |
533
|
|
|
HOSTNAME => [ |
534
|
|
|
getmypid() => $initialExpireSecs, |
535
|
|
|
'extra pid' => 0, |
536
|
|
|
], |
537
|
|
|
], |
538
|
|
|
'version' => $initialVersion, |
539
|
|
|
]; |
540
|
|
|
|
541
|
|
|
$this->collection->insertOne($initalTask); |
542
|
|
|
|
543
|
|
|
$this->registry->reset($this->getProcess('testId', 2)); |
544
|
|
|
|
545
|
|
|
$this->assertSame(1, $this->collection->count()); |
546
|
|
|
$result = $this->collection->findOne(); |
547
|
|
|
|
548
|
|
|
$result['hosts'][HOSTNAME][getmypid()] = null; |
549
|
|
|
|
550
|
|
|
$expected = [ |
551
|
|
|
'_id' => 'testId', |
552
|
|
|
'hosts' => [HOSTNAME => [getmypid() => null, 'extra pid' => 0]], |
553
|
|
|
'version' => $result['version'], |
554
|
|
|
]; |
555
|
|
|
|
556
|
|
|
$this->assertSame($expected, $result); |
557
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* @test |
562
|
|
|
* @covers ::reset |
563
|
|
|
* |
564
|
|
|
* @return void |
565
|
|
|
*/ |
566
|
|
View Code Duplication |
public function resetUnderflowMinsBeforeExpire() |
|
|
|
|
567
|
|
|
{ |
568
|
|
|
$initialVersion = new ObjectID(); |
569
|
|
|
$initalTask = [ |
570
|
|
|
'_id' => 'testId', |
571
|
|
|
'hosts' => [HOSTNAME => [getmypid() => time() + 60]], |
572
|
|
|
'version' => $initialVersion, |
573
|
|
|
]; |
574
|
|
|
|
575
|
|
|
$this->collection->insertOne($initalTask); |
576
|
|
|
|
577
|
|
|
$this->registry->reset($this->getProcess('testId', ~PHP_INT_MAX)); |
578
|
|
|
|
579
|
|
|
$this->assertSame(1, $this->collection->count()); |
580
|
|
|
$result = $this->collection->findOne(); |
581
|
|
|
|
582
|
|
|
$this->assertSame( |
583
|
|
|
['_id' => 'testId', 'hosts' => [HOSTNAME => [getmypid() => 0]], 'version' => $result['version']], |
584
|
|
|
$result |
585
|
|
|
); |
586
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @test |
591
|
|
|
* @covers ::reset |
592
|
|
|
* |
593
|
|
|
* @return void |
594
|
|
|
*/ |
595
|
|
View Code Duplication |
public function resetOverflowMinsBeforeExpire() |
|
|
|
|
596
|
|
|
{ |
597
|
|
|
$initialVersion = new ObjectID(); |
598
|
|
|
$initalTask = [ |
599
|
|
|
'_id' => 'testId', |
600
|
|
|
'hosts' => [HOSTNAME => [getmypid() => time() + 60]], |
601
|
|
|
'version' => $initialVersion, |
602
|
|
|
]; |
603
|
|
|
|
604
|
|
|
$this->collection->insertOne($initalTask); |
605
|
|
|
|
606
|
|
|
$this->registry->reset($this->getProcess('testId', PHP_INT_MAX)); |
607
|
|
|
|
608
|
|
|
$this->assertSame(1, $this->collection->count()); |
609
|
|
|
$result = $this->collection->findOne(); |
610
|
|
|
|
611
|
|
|
$expected = [ |
612
|
|
|
'_id' => 'testId', |
613
|
|
|
'hosts' => [HOSTNAME => [getmypid() => ProcessRegistry::MONGO_INT32_MAX]], |
614
|
|
|
'version' => $result['version'], |
615
|
|
|
]; |
616
|
|
|
|
617
|
|
|
$this->assertSame($expected, $result); |
618
|
|
|
$this->assertNotSame((string)$initialVersion, (string)$result['version']); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Verify behavior of add() with too much concurrency. |
623
|
|
|
* |
624
|
|
|
* @test |
625
|
|
|
* @covers ::add |
626
|
|
|
* |
627
|
|
|
* @return void |
628
|
|
|
*/ |
629
|
|
|
public function addTooMuchConcurrency() |
630
|
|
|
{ |
631
|
|
|
$mockUpdateResult = $this->getMockBuilder('\\MongoDB\\UpdateResult')->disableOriginalConstructor()->getMock(); |
632
|
|
|
$mockUpdateResult->method('getMatchedCount')->willReturn(0); |
633
|
|
|
$entry = ['_id' => 'myTask', 'hosts' => [], 'version' => new ObjectID()]; |
634
|
|
|
$mockCollection = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->getMock(); |
635
|
|
|
$mockCollection->method('findOne')->willReturn($entry); |
636
|
|
|
$mockCollection->method('replaceOne')->willReturn($mockUpdateResult); |
637
|
|
|
|
638
|
|
|
$registry = new ProcessRegistry($mockCollection); |
|
|
|
|
639
|
|
|
$this->assertFalse($registry->add($this->getProcess('myTask'))); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Returns array where the key represents the descriptor number and the value represents how PHP will pass that |
644
|
|
|
* descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr. |
645
|
|
|
* |
646
|
|
|
* @return array |
647
|
|
|
*/ |
648
|
|
|
private static function getDevNullProcOpenDescriptors() : array |
649
|
|
|
{ |
650
|
|
|
return [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['file', '/dev/null', 'w']]; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
private function getProcess( |
654
|
|
|
string $name = 'testId', |
655
|
|
|
int $expire = PHP_INT_MAX, |
656
|
|
|
int $global = 1, |
657
|
|
|
int $host = 1 |
658
|
|
|
) : ProcessInterface { |
659
|
|
|
$mock = $this->getMockBuilder('\\TraderInteractive\\Cronus\\ProcessInterface')->getMock(); |
660
|
|
|
$mock->method('getName')->willReturn($name); |
661
|
|
|
$mock->method('getMinsBeforeExpire')->willReturn($expire); |
662
|
|
|
$mock->method('getMaxGlobalProcesses')->willReturn($global); |
663
|
|
|
$mock->method('getMaxHostProcesses')->willReturn($host); |
664
|
|
|
return $mock; |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Override of global gethostname(). |
670
|
|
|
* |
671
|
|
|
* @return string |
672
|
|
|
*/ |
673
|
|
|
function gethostname() |
674
|
|
|
{ |
675
|
|
|
return 'my.host$name'; |
676
|
|
|
} |
677
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.