Repository navigation
Expand file tree
/
Copy pathSubscriptionQueueTest.php
More file actions
624 lines (507 loc) · 23.6 KB
/
Copy pathSubscriptionQueueTest.php
File metadata and controls
624 lines (507 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
declare(strict_types=1);
namespace IDCT\NATS\Tests\Unit;
use Amp\Cancellation;
use Amp\DeferredFuture;
use Amp\Future;
use Amp\TimeoutCancellation;
use IDCT\NATS\Connection\Enum\SlowConsumerPolicy;
use IDCT\NATS\Connection\NatsOptions;
use IDCT\NATS\Core\NatsClient;
use IDCT\NATS\Core\NatsMessage;
use IDCT\NATS\Core\SubscriptionQueue;
use IDCT\NATS\Tests\Support\FakeTransport;
use IDCT\NATS\Transport\TransportInterface;
use PHPUnit\Framework\TestCase;
use function Amp\async;
use function Amp\delay;
final class SubscriptionQueueTest extends TestCase
{
private function makeConnectedClient(FakeTransport $transport): NatsClient
{
$client = new NatsClient(new NatsOptions(), $transport);
$client->connect()->await();
return $client;
}
/** @return list<string> */
private function infoAndPong(): array
{
return [
'INFO {"server_id":"S1","server_name":"n1","version":"2.12.0","jetstream":true,"max_payload":1048576,"headers":true}' . "\r\n",
"PONG\r\n",
];
}
public function testSubscribeQueueReturnsSidAndFetchesMessage(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG events 1 5\r\nhello\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
self::assertInstanceOf(SubscriptionQueue::class, $queue);
self::assertSame(1, $queue->sid);
$msg = $queue->fetch();
self::assertNotNull($msg);
self::assertSame('hello', $msg->payload);
self::assertSame('events', $msg->subject);
}
/**
* A message dispatched for the new sid while subscribe()'s SUB write is still suspended (the
* handler is registered before the write, so the sid is already routable) must reach the
* returned queue instead of being silently dropped in the pre-construction window (#129).
*/
public function testMessageDeliveredDuringSubscribeAwaitReachesTheQueue(): void
{
$info = 'INFO {"server_id":"S1","server_name":"n1","version":"2.12.0","jetstream":true,"max_payload":1048576,"headers":true}' . "\r\n";
$release = new DeferredFuture();
$transport = new class ($info, $release) implements TransportInterface {
/** @var list<string> */
public array $writes = [];
/** @var list<string> */
private array $reads;
/** @param DeferredFuture<void> $release */
public function __construct(string $info, private DeferredFuture $release)
{
$this->reads = [$info, "PONG\r\n", "MSG updates 1 5\r\nearly\r\n"];
}
public function connect(string $dsn, int $timeoutMs): Future
{
return async(static fn(): mixed => null);
}
public function upgradeTls(): Future
{
return async(static fn(): mixed => null);
}
public function write(string $bytes): Future
{
return async(function () use ($bytes): void {
$this->writes[] = $bytes;
// Hold the SUB write open: the handler is registered before the write, so a
// concurrent read can deliver for the new sid while subscribeQueue() is still
// suspended here and its SubscriptionQueue object does not exist yet.
if (str_starts_with($bytes, 'SUB ')) {
$this->release->getFuture()->await();
}
});
}
public function readLine(?Cancellation $cancellation = null): Future
{
return async(function (): string {
return array_shift($this->reads) ?? '';
});
}
public function close(): Future
{
return async(static fn(): mixed => null);
}
};
$client = new NatsClient(new NatsOptions(), $transport);
$client->connect()->await();
$queueFuture = $client->subscribeQueue('updates');
delay(0.01); // subscribeQueue() is now suspended on the held SUB write
// A concurrent read dispatches the message for sid 1; the queue object does not exist yet.
$client->processIncoming()->await();
$release->complete();
$queue = $queueFuture->await();
$message = $queue->fetch();
self::assertNotNull($message, 'a delivery during the construction window must not be dropped');
self::assertSame('early', $message->payload);
}
public function testFetchReturnsNullWhenNoMessages(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
$msg = $queue->fetch();
self::assertNull($msg);
}
public function testNextReturnsBufferedMessageImmediately(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG data 1 3\r\nabc\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('data')->await();
// Pre-load the message into the internal buffer via processIncoming.
$client->processIncoming()->await();
$msg = $queue->next();
self::assertNotNull($msg);
self::assertSame('abc', $msg->payload);
}
public function testNextReturnsNullOnTimeout(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('empty')->await();
$queue->setTimeout(0.01);
$msg = $queue->next();
self::assertNull($msg);
}
public function testNextWithoutTimeoutRunsSingleCycleAndReturnsMessage(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG data 1 3\r\nxyz\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('data')->await();
// No timeout configured (default 0): a single processIncoming cycle should surface the message.
$msg = $queue->next();
self::assertNotNull($msg);
self::assertSame('xyz', $msg->payload);
}
public function testNextWithoutTimeoutReturnsNullWhenEmpty(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('empty')->await();
// Default timeout 0 must NOT block indefinitely; a single empty cycle returns null.
$msg = $queue->next();
self::assertNull($msg);
}
public function testFetchAllCollectsMultipleMessages(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG items 1 1\r\na\r\n",
"MSG items 1 1\r\nb\r\n",
"MSG items 1 1\r\nc\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('items')->await();
$queue->setTimeout(0.1);
$messages = $queue->fetchAll();
self::assertCount(3, $messages);
self::assertSame('a', $messages[0]->payload);
self::assertSame('b', $messages[1]->payload);
self::assertSame('c', $messages[2]->payload);
}
public function testFetchAllRespectsLimit(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG items 1 1\r\na\r\n",
"MSG items 1 1\r\nb\r\n",
"MSG items 1 1\r\nc\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('items')->await();
$queue->setTimeout(0.1);
$messages = $queue->fetchAll(2);
self::assertCount(2, $messages);
self::assertSame('a', $messages[0]->payload);
self::assertSame('b', $messages[1]->payload);
}
public function testSubscribeQueueWithQueueGroup(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG work 1 4\r\njob1\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('work', 'workers')->await();
// Verify the SUB command includes the queue group.
$allWrites = implode('||', $transport->writes);
self::assertStringContainsString('SUB work workers 1', $allWrites);
$msg = $queue->fetch();
self::assertNotNull($msg);
self::assertSame('job1', $msg->payload);
}
public function testSetTimeoutReturnsSelf(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('x')->await();
$result = $queue->setTimeout(5.0);
self::assertSame($queue, $result);
}
public function testFetchReturnsAlreadyBufferedMessage(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG data 1 2\r\nhi\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('data')->await();
// Buffer the message first so fetch() returns it directly without another read.
$client->processIncoming()->await();
$msg = $queue->fetch();
self::assertNotNull($msg);
self::assertSame('hi', $msg->payload);
}
public function testNextWithTimeoutReturnsMessageArrivingDuringWait(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG data 1 3\r\nabc\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('data')->await();
$queue->setTimeout(0.2);
// Not pre-buffered: next() enters the bounded wait loop and breaks as soon as the message
// is read, rather than running to the timeout.
$msg = $queue->next();
self::assertNotNull($msg);
self::assertSame('abc', $msg->payload);
}
public function testFetchAllWithoutTimeoutCollectsBufferedMessages(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG items 1 1\r\na\r\n",
"MSG items 1 1\r\nb\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('items')->await();
// No timeout configured exercises the unbounded (null cancellation) collection path.
$messages = $queue->fetchAll();
self::assertCount(2, $messages);
self::assertSame('a', $messages[0]->payload);
self::assertSame('b', $messages[1]->payload);
}
public function testFetchAllReturnsEarlyWhenBufferedMeetsLimit(): void
{
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG items 1 1\r\na\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('items')->await();
// Pre-buffer one message, then request a limit of one: the buffered drain alone satisfies it.
$client->processIncoming()->await();
$messages = $queue->fetchAll(1);
self::assertCount(1, $messages);
self::assertSame('a', $messages[0]->payload);
}
public function testFetchDoesNotBlockOnIdleSubject(): void
{
// blockWhenEmpty mirrors a live but idle socket: readLine() suspends until cancelled. The
// outer 2s bound fails the test if fetch() ever parks the caller (it must return ~instantly).
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('idle.subject')->await();
$result = \Amp\Future\await([async(static fn(): ?\IDCT\NATS\Core\NatsMessage => $queue->fetch())], new TimeoutCancellation(2.0))[0];
self::assertNull($result);
self::assertTrue($transport->lastReadHadCancellation, 'fetch() must bound the read with a cancellation');
}
public function testNextWithDefaultTimeoutDoesNotBlockOnIdleSubject(): void
{
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('idle.subject')->await();
// Default timeout is 0 (the <= 0 branch); it must not block the calling fiber.
$result = \Amp\Future\await([async(static fn(): ?\IDCT\NATS\Core\NatsMessage => $queue->next())], new TimeoutCancellation(2.0))[0];
self::assertNull($result);
}
public function testNextWithNegativeTimeoutDoesNotBlockOnIdleSubject(): void
{
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('idle.subject')->await();
$queue->setTimeout(-1.0);
$result = \Amp\Future\await([async(static fn(): ?\IDCT\NATS\Core\NatsMessage => $queue->next())], new TimeoutCancellation(2.0))[0];
self::assertNull($result);
}
public function testFetchAllWithDefaultTimeoutDoesNotBlockOnIdleSubject(): void
{
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('idle.subject')->await();
// No setTimeout(): fetchAll() must still bound its read and return [] rather than parking.
$result = \Amp\Future\await([async(static fn(): array => $queue->fetchAll())], new TimeoutCancellation(2.0))[0];
self::assertSame([], $result);
}
public function testFetchAllDoesNotBailOnTransientEmptyReadWithinTimeout(): void
{
// A transient 0-frame read (e.g. the heartbeat self-read briefly owning the socket) between
// two deliveries must NOT end fetchAll early while a timeout window remains.
$transport = new FakeTransport([
...$this->infoAndPong(),
"MSG events 1 4\r\nmsg1\r\n",
'', // empty read -> processIncoming returns 0 frames mid-window
"MSG events 1 4\r\nmsg2\r\n",
]);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
$queue->setTimeout(0.5);
$messages = $queue->fetchAll(2);
self::assertCount(2, $messages);
self::assertSame('msg1', $messages[0]->payload);
self::assertSame('msg2', $messages[1]->payload);
}
public function testEnqueueBoundsBacklogWithDropOldest(): void
{
$transport = new FakeTransport($this->infoAndPong());
$client = $this->makeConnectedClient($transport);
$queue = new SubscriptionQueue($client, 99, 2, SlowConsumerPolicy::DropOldest);
$queue->enqueue(new NatsMessage('events', 99, null, 'a'));
$queue->enqueue(new NatsMessage('events', 99, null, 'b'));
$queue->enqueue(new NatsMessage('events', 99, null, 'c')); // over cap (2) -> drop oldest ('a')
$messages = $queue->fetchAll();
self::assertCount(2, $messages);
self::assertSame('b', $messages[0]->payload);
self::assertSame('c', $messages[1]->payload);
}
public function testEnqueueDropsNewestWhenPolicyIsDropNewest(): void
{
$transport = new FakeTransport($this->infoAndPong());
$client = $this->makeConnectedClient($transport);
$queue = new SubscriptionQueue($client, 99, 2, SlowConsumerPolicy::DropNewest);
$queue->enqueue(new NatsMessage('events', 99, null, 'a'));
$queue->enqueue(new NatsMessage('events', 99, null, 'b'));
$queue->enqueue(new NatsMessage('events', 99, null, 'c')); // over cap -> drop the newest ('c')
$messages = $queue->fetchAll();
self::assertCount(2, $messages);
self::assertSame('a', $messages[0]->payload);
self::assertSame('b', $messages[1]->payload);
}
/**
* A DropOldest overflow must not be silent (#134): droppedCount() increments monotonically and
* the client's errorListener receives a slow-consumer error, mirroring the connection layer.
*/
public function testEnqueueDropOldestIncrementsDroppedCountAndNotifiesErrorListener(): void
{
$errors = [];
$options = new NatsOptions(errorListener: static function (\Throwable $e) use (&$errors): void {
$errors[] = $e;
});
$transport = new FakeTransport($this->infoAndPong());
$client = new NatsClient($options, $transport);
$client->connect()->await();
$queue = new SubscriptionQueue($client, 99, 2, SlowConsumerPolicy::DropOldest);
$queue->enqueue(new NatsMessage('events', 99, null, 'a'));
$queue->enqueue(new NatsMessage('events', 99, null, 'b'));
self::assertSame(0, $queue->droppedCount(), 'no drop below the cap');
$queue->enqueue(new NatsMessage('events', 99, null, 'c')); // over cap (2) -> drops oldest ('a')
self::assertSame(1, $queue->droppedCount());
$queue->enqueue(new NatsMessage('events', 99, null, 'd')); // drops 'b' -> counter is monotonic
self::assertSame(2, $queue->droppedCount());
self::assertCount(2, $errors);
self::assertStringContainsString('Slow consumer on sid 99', $errors[0]->getMessage());
self::assertStringContainsString('dropped oldest message', $errors[0]->getMessage());
}
/**
* A DropNewest overflow must not be silent (#134): the discarded (never-enqueued) message still
* increments droppedCount() and reaches the client's errorListener.
*/
public function testEnqueueDropNewestIncrementsDroppedCountAndNotifiesErrorListener(): void
{
$errors = [];
$options = new NatsOptions(errorListener: static function (\Throwable $e) use (&$errors): void {
$errors[] = $e;
});
$transport = new FakeTransport($this->infoAndPong());
$client = new NatsClient($options, $transport);
$client->connect()->await();
$queue = new SubscriptionQueue($client, 99, 2, SlowConsumerPolicy::DropNewest);
$queue->enqueue(new NatsMessage('events', 99, null, 'a'));
$queue->enqueue(new NatsMessage('events', 99, null, 'b'));
self::assertSame(0, $queue->droppedCount(), 'no drop below the cap');
$queue->enqueue(new NatsMessage('events', 99, null, 'c')); // over cap (2) -> 'c' is discarded
self::assertSame(1, $queue->droppedCount());
self::assertCount(1, $errors);
self::assertStringContainsString('Slow consumer on sid 99', $errors[0]->getMessage());
self::assertStringContainsString('dropped newest message', $errors[0]->getMessage());
}
public function testUnsubscribeSendsUnsubForOwnSid(): void
{
$transport = new FakeTransport($this->infoAndPong());
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
$queue->unsubscribe()->await();
self::assertStringContainsString("UNSUB {$queue->sid}\r\n", implode('', $transport->writes));
}
/**
* SlowConsumerPolicy::Error throws NatsException when the queue is full, and the drop is
* observable: droppedCount() increments and the client's errorListener is notified before the
* throw, mirroring the DropOldest/DropNewest paths so the loss is never silent (#134/#159).
*/
public function testEnqueueThrowsOnOverflowWhenPolicyIsError(): void
{
$errors = [];
$options = new NatsOptions(errorListener: static function (\Throwable $e) use (&$errors): void {
$errors[] = $e;
});
$transport = new FakeTransport($this->infoAndPong());
$client = new NatsClient($options, $transport);
$client->connect()->await();
$queue = new SubscriptionQueue($client, 99, 2, SlowConsumerPolicy::Error);
$queue->enqueue(new NatsMessage('events', 99, null, 'a'));
$queue->enqueue(new NatsMessage('events', 99, null, 'b'));
self::assertSame(0, $queue->droppedCount(), 'no drop below the cap');
// Third enqueue exceeds the cap of 2 and must throw with the Error policy.
try {
$queue->enqueue(new NatsMessage('events', 99, null, 'c'));
self::fail('the Error-policy overflow must throw');
} catch (\IDCT\NATS\Exception\NatsException $e) {
self::assertStringContainsString('Subscription queue overflow for sid 99', $e->getMessage());
}
// The drop is observable even though it threw: counted and surfaced through the error listener.
self::assertSame(1, $queue->droppedCount());
self::assertCount(1, $errors);
self::assertStringContainsString('Subscription queue overflow for sid 99', $errors[0]->getMessage());
}
/**
* close() is an alias of unsubscribe() and sends UNSUB for the queue's own sid.
*/
public function testCloseSendsUnsubForOwnSid(): void
{
$transport = new FakeTransport($this->infoAndPong());
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
$queue->close()->await();
self::assertStringContainsString("UNSUB {$queue->sid}\r\n", implode('', $transport->writes));
}
/**
* next() with a positive timeout times out (CancelledException caught) and returns null.
*
* The blocking transport means processIncoming() suspends the fiber until the TimeoutCancellation
* fires, which exercises the catch(CancelledException) clause.
*/
public function testNextWithTimeoutReturnsNullWhenNoMessageArrivesBeforeDeadline(): void
{
// blockWhenEmpty suspends processIncoming() so the TimeoutCancellation fires inside it,
// propagates as CancelledException caught, and next() returns null.
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('idle')->await();
$queue->setTimeout(0.02);
$msg = $queue->next();
self::assertNull($msg);
}
/**
* the final-drain body in fetchAll() collects a message that was enqueued into the
* queue by a concurrent fiber while fetchAll() was blocked inside processIncoming().
*
* Flow:
* 1. fetchAll() blocks in processIncoming() (blockWhenEmpty transport).
* 2. A parallel async fiber waits briefly then calls enqueue() directly.
* 3. The TimeoutCancellation fires; processIncoming() throws CancelledException.
* 4. The final drain loop finds the concurrently-enqueued message and returns it.
*/
public function testFetchAllFinalDrainCollectsConcurrentlyEnqueuedMessage(): void
{
$transport = new FakeTransport($this->infoAndPong(), blockWhenEmpty: true);
$client = $this->makeConnectedClient($transport);
$queue = $client->subscribeQueue('events')->await();
$queue->setTimeout(0.1);
// Schedule a direct enqueue to fire after fetchAll() has suspended in processIncoming().
async(static function () use ($queue): void {
\Amp\delay(0.02);
$queue->enqueue(new NatsMessage('events', 1, null, 'late'));
});
$messages = $queue->fetchAll();
self::assertCount(1, $messages);
self::assertSame('late', $messages[0]->payload);
}
}