1 | <?php |
||
15 | final class QueueTest extends TestCase |
||
16 | { |
||
17 | private $collection; |
||
18 | private $mongoUrl; |
||
19 | private $queue; |
||
20 | |||
21 | public function setUp() |
||
22 | { |
||
23 | $this->mongoUrl = getenv('TESTING_MONGO_URL') ?: 'mongodb://localhost:27017'; |
||
24 | $mongo = new Client( |
||
25 | $this->mongoUrl, |
||
26 | [], |
||
27 | ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
||
28 | ); |
||
29 | $this->collection = $mongo->selectDatabase('testing')->selectCollection('messages'); |
||
30 | $this->collection->deleteMany([]); |
||
31 | |||
32 | $this->queue = new Queue($this->collection); |
||
33 | } |
||
34 | |||
35 | public function tearDown() |
||
39 | |||
40 | /** |
||
41 | * @test |
||
42 | * @covers ::__construct |
||
43 | * @expectedException \InvalidArgumentException |
||
44 | */ |
||
45 | public function constructWithNonStringUrl() |
||
49 | |||
50 | /** |
||
51 | * @test |
||
52 | * @covers ::ensureGetIndex |
||
53 | */ |
||
54 | public function ensureGetIndex() |
||
55 | { |
||
56 | $collection = (new Client($this->mongoUrl))->selectDatabase('testing')->selectCollection(uniqid()); |
||
57 | $queue = new Queue($collection); |
||
58 | $queue->ensureGetIndex(['type' => 1], ['boo' => -1]); |
||
59 | $queue->ensureGetIndex(['another.sub' => 1]); |
||
60 | |||
61 | $indexes = iterator_to_array($collection->listIndexes()); |
||
62 | $this->assertSame(3, count($indexes)); |
||
63 | |||
64 | $expectedOne = [ |
||
65 | 'earliestGet' => 1, |
||
66 | 'payload.type' => 1, |
||
67 | 'priority' => 1, |
||
68 | 'created' => 1, |
||
69 | 'payload.boo' => -1, |
||
70 | ]; |
||
71 | $this->assertSame($expectedOne, $indexes[1]['key']); |
||
72 | |||
73 | $expectedTwo = [ |
||
74 | 'earliestGet' => 1, |
||
75 | 'payload.another.sub' => 1, |
||
76 | 'priority' => 1, |
||
77 | 'created' => 1, |
||
78 | ]; |
||
79 | $this->assertSame($expectedTwo, $indexes[2]['key']); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @test |
||
84 | * @covers ::ensureGetIndex |
||
85 | * @expectedException \Exception |
||
86 | */ |
||
87 | public function ensureGetIndexWithTooLongCollectionName() |
||
95 | |||
96 | /** |
||
97 | * @test |
||
98 | * @covers ::ensureGetIndex |
||
99 | * @expectedException \InvalidArgumentException |
||
100 | */ |
||
101 | public function ensureGetIndexWithNonStringBeforeSortKey() |
||
105 | |||
106 | /** |
||
107 | * @test |
||
108 | * @covers ::ensureGetIndex |
||
109 | * @expectedException \InvalidArgumentException |
||
110 | */ |
||
111 | public function ensureGetIndexWithNonStringAfterSortKey() |
||
115 | |||
116 | /** |
||
117 | * @test |
||
118 | * @covers ::ensureGetIndex |
||
119 | * @expectedException \InvalidArgumentException |
||
120 | */ |
||
121 | public function ensureGetIndexWithBadBeforeSortValue() |
||
125 | |||
126 | /** |
||
127 | * @test |
||
128 | * @covers ::ensureGetIndex |
||
129 | * @expectedException \InvalidArgumentException |
||
130 | */ |
||
131 | public function ensureGetIndexWithBadAfterSortValue() |
||
135 | |||
136 | /** |
||
137 | * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts. |
||
138 | * |
||
139 | * @test |
||
140 | * @covers ::ensureGetIndex |
||
141 | * @expectedException \Exception |
||
142 | * @expectedExceptionMessage couldnt create index after 5 attempts |
||
143 | */ |
||
144 | public function ensureIndexCannotBeCreatedAfterFiveAttempts() |
||
153 | |||
154 | /** |
||
155 | * @test |
||
156 | * @covers ::ensureCountIndex |
||
157 | */ |
||
158 | public function ensureCountIndex() |
||
174 | |||
175 | /** |
||
176 | * @test |
||
177 | * @covers ::ensureCountIndex |
||
178 | */ |
||
179 | public function ensureCountIndexWithPrefixOfPrevious() |
||
190 | |||
191 | /** |
||
192 | * @test |
||
193 | * @covers ::ensureCountIndex |
||
194 | * @expectedException \InvalidArgumentException |
||
195 | */ |
||
196 | public function ensureCountIndexWithNonStringKey() |
||
200 | |||
201 | /** |
||
202 | * @test |
||
203 | * @covers ::ensureCountIndex |
||
204 | * @expectedException \InvalidArgumentException |
||
205 | */ |
||
206 | public function ensureCountIndexWithBadValue() |
||
210 | |||
211 | /** |
||
212 | * @test |
||
213 | * @covers ::get |
||
214 | */ |
||
215 | public function getByBadQuery() |
||
224 | |||
225 | /** |
||
226 | * @test |
||
227 | * @covers ::get |
||
228 | */ |
||
229 | public function getWithNegativePollDuration() |
||
234 | |||
235 | /** |
||
236 | * @test |
||
237 | * @covers ::get |
||
238 | * @expectedException \InvalidArgumentException |
||
239 | */ |
||
240 | public function getWithNonStringKey() |
||
241 | { |
||
242 | $this->queue->get([0 => 'a value']); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @test |
||
247 | * @covers ::get |
||
248 | */ |
||
249 | public function getByFullQuery() |
||
250 | { |
||
251 | $messageOne = $this->getMessage(['key1' => 0, 'key2' => true]); |
||
252 | |||
253 | $this->queue->send($messageOne); |
||
254 | $this->queue->send($this->getMessage(['key' => 'value'])); |
||
255 | |||
256 | $result = $this->queue->get($messageOne->getPayload())[0]; |
||
257 | |||
258 | $this->assertSame((string)$messageOne->getId(), (string)$result->getId()); |
||
259 | $this->assertSame($messageOne->getPayload(), $result->getPayload()); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @test |
||
264 | * @covers ::get |
||
265 | */ |
||
266 | public function getBySubDocQuery() |
||
267 | { |
||
268 | $expected = $this->getMessage( |
||
269 | [ |
||
270 | 'one' => [ |
||
271 | 'two' => [ |
||
272 | 'three' => 5, |
||
273 | 'notused' => 'notused', |
||
274 | ], |
||
275 | 'notused' => 'notused', |
||
276 | ], |
||
277 | 'notused' => 'notused', |
||
278 | ] |
||
279 | ); |
||
280 | |||
281 | $this->queue->send($this->getMessage(['key1' => 0, 'key2' => true])); |
||
282 | $this->queue->send($expected); |
||
283 | |||
284 | $actual = $this->queue->get(['one.two.three' => ['$gt' => 4]])[0]; |
||
285 | $this->assertSameMessage($expected, $actual); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @test |
||
290 | * @covers ::get |
||
291 | */ |
||
292 | public function getBeforeAck() |
||
293 | { |
||
294 | $messageOne = $this->getMessage(['key1' => 0, 'key2' => true]); |
||
295 | |||
296 | $this->queue->send($messageOne); |
||
297 | $this->queue->send($this->getMessage(['key' => 'value'])); |
||
298 | |||
299 | $this->queue->get($messageOne->getPayload()); |
||
300 | |||
301 | //try get message we already have before ack |
||
302 | $result = $this->queue->get($messageOne->getPayload()); |
||
303 | $this->assertSame([], $result); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @test |
||
308 | * @covers ::get |
||
309 | */ |
||
310 | public function getWithCustomPriority() |
||
326 | |||
327 | /** |
||
328 | * @test |
||
329 | * @covers ::get |
||
330 | */ |
||
331 | public function getWithTimeBasedPriority() |
||
347 | |||
348 | /** |
||
349 | * @test |
||
350 | * @covers ::get |
||
351 | */ |
||
352 | public function getWait() |
||
363 | |||
364 | /** |
||
365 | * @test |
||
366 | * @covers ::get |
||
367 | */ |
||
368 | public function earliestGet() |
||
383 | |||
384 | /** |
||
385 | * @test |
||
386 | * @covers ::get |
||
387 | */ |
||
388 | public function resetStuck() |
||
389 | { |
||
390 | $messageOne = $this->getMessage(['key' => 0]); |
||
391 | $messageTwo = $this->getMessage(['key' => 1]); |
||
392 | |||
393 | $this->queue->send($messageOne); |
||
394 | $this->queue->send($messageTwo); |
||
395 | |||
396 | //sets to running |
||
397 | $this->collection->updateOne( |
||
398 | ['payload.key' => 0], |
||
399 | ['$set' => ['earliestGet' => new UTCDateTime(time() * 1000)]] |
||
400 | ); |
||
401 | $this->collection->updateOne( |
||
402 | ['payload.key' => 1], |
||
403 | ['$set' => ['earliestGet' => new UTCDateTime(time() * 1000)]] |
||
404 | ); |
||
405 | |||
406 | $this->assertSame( |
||
407 | 2, |
||
408 | $this->collection->count( |
||
409 | ['earliestGet' => ['$lte' => new UTCDateTime((int)(microtime(true) * 1000))]] |
||
410 | ) |
||
411 | ); |
||
412 | |||
413 | //resets and gets messageOne |
||
414 | $this->assertNotNull($this->queue->get($messageOne->getPayload())); |
||
415 | |||
416 | $this->assertSame( |
||
417 | 1, |
||
418 | $this->collection->count( |
||
419 | ['earliestGet' => ['$lte' => new UTCDateTime((int)(microtime(true) * 1000))]] |
||
420 | ) |
||
421 | ); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @test |
||
426 | * @covers ::count |
||
427 | * @expectedException \InvalidArgumentException |
||
428 | */ |
||
429 | public function countWithNonStringKey() |
||
433 | |||
434 | /** |
||
435 | * @test |
||
436 | * @covers ::count |
||
437 | */ |
||
438 | public function testCount() |
||
456 | |||
457 | /** |
||
458 | * @test |
||
459 | * @covers ::ack |
||
460 | */ |
||
461 | public function ack() |
||
474 | |||
475 | /** |
||
476 | * @test |
||
477 | * @covers ::requeue |
||
478 | */ |
||
479 | public function requeue() |
||
480 | { |
||
481 | $messages = [ |
||
482 | $this->getMessage(['id' => 1, 'key' => 'value']), |
||
483 | $this->getMessage(['id' => 2, 'key' => 'value']), |
||
484 | $this->getMessage(['id' => 3, 'key' => 'value']), |
||
485 | ]; |
||
486 | |||
487 | foreach ($messages as $message) { |
||
488 | $this->queue->send($message); |
||
489 | } |
||
490 | |||
491 | $query = ['key' => 'value']; |
||
492 | |||
493 | $message = $this->queue->get($query)[0]; |
||
494 | |||
495 | $this->assertSameMessage($messages[0], $message); |
||
496 | |||
497 | $this->queue->requeue($message->withEarliestGet(new UTCDateTime((int)(microtime(true) * 1000)))); |
||
498 | |||
499 | $actual = $this->queue->get([], ['maxNumberOfMessages' => 10]); |
||
500 | |||
501 | $this->assertSameMessage($messages[1], $actual[0]); |
||
502 | $this->assertSameMessage($messages[2], $actual[1]); |
||
503 | $this->assertSameMessage($messages[0], $actual[2]); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @test |
||
508 | * @covers ::send |
||
509 | */ |
||
510 | public function send() |
||
511 | { |
||
512 | $message = $this->getMessage(['key1' => 0, 'key2' => true], new UTCDateTime(34 * 1000), 0.8); |
||
513 | $this->queue->send($message); |
||
514 | $this->assertSingleMessage($message); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * @test |
||
519 | * @covers ::send |
||
520 | */ |
||
521 | public function sendWithHighEarliestGet() |
||
527 | |||
528 | /** |
||
529 | * @test |
||
530 | * @covers ::send |
||
531 | */ |
||
532 | public function sendWithLowEarliestGet() |
||
533 | { |
||
534 | $message = $this->getMessage([], new UTCDateTime(0)); |
||
535 | $this->queue->send($message); |
||
536 | $this->assertSingleMessage($message); |
||
537 | } |
||
538 | |||
539 | private function assertSameMessage(Message $expected, Message $actual) |
||
545 | |||
546 | private function assertSingleMessage(Message $expected) |
||
560 | |||
561 | private function getMessage(array $payload = [], UTCDateTime $earliestGet = null, float $priority = 0.0) |
||
565 | } |
||
566 |