Skip to content

Commit 613de56

Browse files
committed
[client] GenericDriver should init delivery delay, prioirt, time to live.
1 parent 570d34d commit 613de56

2 files changed

Lines changed: 328 additions & 6 deletions

File tree

‎pkg/enqueue/Client/Driver/GenericDriver.php‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Enqueue\Client\RouteCollection;
1313
use Interop\Queue\PsrContext;
1414
use Interop\Queue\PsrMessage;
15+
use Interop\Queue\PsrProducer;
1516
use Interop\Queue\PsrQueue;
1617
use Interop\Queue\PsrTopic;
1718
use Psr\Log\LoggerInterface;
@@ -54,8 +55,9 @@ public function sendToRouter(Message $message): void
5455

5556
$topic = $this->createRouterTopic();
5657
$transportMessage = $this->createTransportMessage($message);
58+
$producer = $this->getContext()->createProducer();
5759

58-
$this->doSendToRouter($topic, $transportMessage);
60+
$this->doSendToRouter($producer, $topic, $transportMessage);
5961
}
6062

6163
public function sendToProcessor(Message $message): void
@@ -91,7 +93,23 @@ public function sendToProcessor(Message $message): void
9193
$transportMessage = $this->createTransportMessage($message);
9294
$queue = $this->createRouteQueue($route);
9395

94-
$this->doSendToProcessor($queue, $transportMessage);
96+
$producer = $this->context->createProducer();
97+
98+
if ($delay = $transportMessage->getProperty('X-Enqueue-Delay')) {
99+
$producer->setDeliveryDelay($delay * 1000);
100+
}
101+
102+
if ($expire = $transportMessage->getProperty('X-Enqueue-Expire')) {
103+
$producer->setTimeToLive($expire * 1000);
104+
}
105+
106+
if ($priority = $transportMessage->getProperty('X-Enqueue-Priority')) {
107+
$priorityMap = $this->getPriorityMap();
108+
109+
$producer->setPriority($priorityMap[$priority]);
110+
}
111+
112+
$this->doSendToProcessor($producer, $queue, $transportMessage);
95113
}
96114

97115
public function setupBroker(LoggerInterface $logger = null): void
@@ -186,14 +204,14 @@ public function getRouteCollection(): RouteCollection
186204
return $this->routeCollection;
187205
}
188206

189-
protected function doSendToRouter(PsrTopic $topic, PsrMessage $transportMessage): void
207+
protected function doSendToRouter(PsrProducer $producer, PsrTopic $topic, PsrMessage $transportMessage): void
190208
{
191-
$this->context->createProducer()->send($topic, $transportMessage);
209+
$producer->send($topic, $transportMessage);
192210
}
193211

194-
protected function doSendToProcessor(PsrQueue $queue, PsrMessage $transportMessage): void
212+
protected function doSendToProcessor(PsrProducer $producer, PsrQueue $queue, PsrMessage $transportMessage): void
195213
{
196-
$this->context->createProducer()->send($queue, $transportMessage);
214+
$producer->send($queue, $transportMessage);
197215
}
198216

199217
protected function createRouterTopic(): PsrTopic
@@ -227,4 +245,20 @@ protected function createTransportQueueName(string $name, bool $prefix): string
227245

228246
return strtolower(implode($this->config->getSeparator(), array_filter([$clientPrefix, $clientAppName, $name])));
229247
}
248+
249+
/**
250+
* [client message priority => transport message priority].
251+
*
252+
* @return int[]
253+
*/
254+
protected function getPriorityMap(): array
255+
{
256+
return [
257+
MessagePriority::VERY_LOW => 0,
258+
MessagePriority::LOW => 1,
259+
MessagePriority::NORMAL => 2,
260+
MessagePriority::HIGH => 3,
261+
MessagePriority::VERY_HIGH => 4,
262+
];
263+
}
230264
}

‎pkg/enqueue/Tests/Client/Driver/GenericDriverTestsTrait.php‎

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,144 @@ public function testShouldSendMessageToRouter()
288288
$driver->sendToRouter($message);
289289
}
290290

291+
public function testShouldNotInitDeliveryDelayOnSendMessageToRouter()
292+
{
293+
$topic = $this->createTopic('');
294+
$transportMessage = $this->createMessage();
295+
296+
$producer = $this->createProducerMock();
297+
$producer
298+
->expects($this->once())
299+
->method('send')
300+
->with($this->identicalTo($topic), $this->identicalTo($transportMessage))
301+
;
302+
$producer
303+
->expects($this->never())
304+
->method('setDeliveryDelay')
305+
;
306+
307+
$context = $this->createContextMock();
308+
$context
309+
->expects($this->once())
310+
->method('createTopic')
311+
->willReturn($topic)
312+
;
313+
$context
314+
->expects($this->once())
315+
->method('createProducer')
316+
->willReturn($producer)
317+
;
318+
$context
319+
->expects($this->once())
320+
->method('createMessage')
321+
->willReturn($transportMessage)
322+
;
323+
324+
$driver = $this->createDriver(
325+
$context,
326+
$this->createDummyConfig(),
327+
new RouteCollection([])
328+
);
329+
330+
$message = new Message();
331+
$message->setDelay(456);
332+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
333+
334+
$driver->sendToRouter($message);
335+
}
336+
337+
public function testShouldNotInitTimeToLiveOnSendMessageToRouter()
338+
{
339+
$topic = $this->createTopic('');
340+
$transportMessage = $this->createMessage();
341+
342+
$producer = $this->createProducerMock();
343+
$producer
344+
->expects($this->once())
345+
->method('send')
346+
->with($this->identicalTo($topic), $this->identicalTo($transportMessage))
347+
;
348+
$producer
349+
->expects($this->never())
350+
->method('setTimeToLive')
351+
;
352+
353+
$context = $this->createContextMock();
354+
$context
355+
->expects($this->once())
356+
->method('createTopic')
357+
->willReturn($topic)
358+
;
359+
$context
360+
->expects($this->once())
361+
->method('createProducer')
362+
->willReturn($producer)
363+
;
364+
$context
365+
->expects($this->once())
366+
->method('createMessage')
367+
->willReturn($transportMessage)
368+
;
369+
370+
$driver = $this->createDriver(
371+
$context,
372+
$this->createDummyConfig(),
373+
new RouteCollection([])
374+
);
375+
376+
$message = new Message();
377+
$message->setExpire(456);
378+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
379+
380+
$driver->sendToRouter($message);
381+
}
382+
383+
public function testShouldNotInitPriorityOnSendMessageToRouter()
384+
{
385+
$topic = $this->createTopic('');
386+
$transportMessage = $this->createMessage();
387+
388+
$producer = $this->createProducerMock();
389+
$producer
390+
->expects($this->once())
391+
->method('send')
392+
->with($this->identicalTo($topic), $this->identicalTo($transportMessage))
393+
;
394+
$producer
395+
->expects($this->never())
396+
->method('setPriority')
397+
;
398+
399+
$context = $this->createContextMock();
400+
$context
401+
->expects($this->once())
402+
->method('createTopic')
403+
->willReturn($topic)
404+
;
405+
$context
406+
->expects($this->once())
407+
->method('createProducer')
408+
->willReturn($producer)
409+
;
410+
$context
411+
->expects($this->once())
412+
->method('createMessage')
413+
->willReturn($transportMessage)
414+
;
415+
416+
$driver = $this->createDriver(
417+
$context,
418+
$this->createDummyConfig(),
419+
new RouteCollection([])
420+
);
421+
422+
$message = new Message();
423+
$message->setPriority(MessagePriority::HIGH);
424+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
425+
426+
$driver->sendToRouter($message);
427+
}
428+
291429
public function testThrowIfTopicIsNotSetOnSendToRouter()
292430
{
293431
$driver = $this->createDriver(
@@ -407,6 +545,156 @@ public function testShouldSendTopicMessageToProcessorToCustomQueue()
407545
$driver->sendToProcessor($message);
408546
}
409547

548+
public function testShouldInitDeliveryDelayIfDelayPropertyOnSendToProcessor()
549+
{
550+
$queue = $this->createQueue('');
551+
$transportMessage = $this->createMessage();
552+
553+
$producer = $this->createProducerMock();
554+
$producer
555+
->expects($this->once())
556+
->method('setDeliveryDelay')
557+
->with(456000)
558+
;
559+
$producer
560+
->expects($this->once())
561+
->method('send')
562+
->with($this->identicalTo($queue), $this->identicalTo($transportMessage))
563+
;
564+
$context = $this->createContextMock();
565+
$context
566+
->expects($this->once())
567+
->method('createQueue')
568+
->with('default')
569+
->willReturn($queue)
570+
;
571+
$context
572+
->expects($this->once())
573+
->method('createProducer')
574+
->willReturn($producer)
575+
;
576+
$context
577+
->expects($this->once())
578+
->method('createMessage')
579+
->willReturn($transportMessage)
580+
;
581+
582+
$driver = $this->createDriver(
583+
$context,
584+
$this->createDummyConfig(),
585+
new RouteCollection([
586+
new Route('topic', Route::TOPIC, 'processor'),
587+
])
588+
);
589+
590+
$message = new Message();
591+
$message->setDelay(456);
592+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
593+
$message->setProperty(Config::PARAMETER_PROCESSOR_NAME, 'processor');
594+
595+
$driver->sendToProcessor($message);
596+
}
597+
598+
public function testShouldSetInitTimeToLiveIfExpirePropertyOnSendToProcessor()
599+
{
600+
$queue = $this->createQueue('');
601+
$transportMessage = $this->createMessage();
602+
603+
$producer = $this->createProducerMock();
604+
$producer
605+
->expects($this->once())
606+
->method('setTimeToLive')
607+
->with(678000)
608+
;
609+
$producer
610+
->expects($this->once())
611+
->method('send')
612+
->with($this->identicalTo($queue), $this->identicalTo($transportMessage))
613+
;
614+
$context = $this->createContextMock();
615+
$context
616+
->expects($this->once())
617+
->method('createQueue')
618+
->with('default')
619+
->willReturn($queue)
620+
;
621+
$context
622+
->expects($this->once())
623+
->method('createProducer')
624+
->willReturn($producer)
625+
;
626+
$context
627+
->expects($this->once())
628+
->method('createMessage')
629+
->willReturn($transportMessage)
630+
;
631+
632+
$driver = $this->createDriver(
633+
$context,
634+
$this->createDummyConfig(),
635+
new RouteCollection([
636+
new Route('topic', Route::TOPIC, 'processor'),
637+
])
638+
);
639+
640+
$message = new Message();
641+
$message->setExpire(678);
642+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
643+
$message->setProperty(Config::PARAMETER_PROCESSOR_NAME, 'processor');
644+
645+
$driver->sendToProcessor($message);
646+
}
647+
648+
public function testShouldSetInitPriorityIfPriorityPropertyOnSendToProcessor()
649+
{
650+
$queue = $this->createQueue('');
651+
$transportMessage = $this->createMessage();
652+
653+
$producer = $this->createProducerMock();
654+
$producer
655+
->expects($this->once())
656+
->method('setPriority')
657+
->with(3)
658+
;
659+
$producer
660+
->expects($this->once())
661+
->method('send')
662+
->with($this->identicalTo($queue), $this->identicalTo($transportMessage))
663+
;
664+
$context = $this->createContextMock();
665+
$context
666+
->expects($this->once())
667+
->method('createQueue')
668+
->with('default')
669+
->willReturn($queue)
670+
;
671+
$context
672+
->expects($this->once())
673+
->method('createProducer')
674+
->willReturn($producer)
675+
;
676+
$context
677+
->expects($this->once())
678+
->method('createMessage')
679+
->willReturn($transportMessage)
680+
;
681+
682+
$driver = $this->createDriver(
683+
$context,
684+
$this->createDummyConfig(),
685+
new RouteCollection([
686+
new Route('topic', Route::TOPIC, 'processor'),
687+
])
688+
);
689+
690+
$message = new Message();
691+
$message->setPriority(MessagePriority::HIGH);
692+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, 'topic');
693+
$message->setProperty(Config::PARAMETER_PROCESSOR_NAME, 'processor');
694+
695+
$driver->sendToProcessor($message);
696+
}
697+
410698
public function testThrowIfNoRouteFoundForTopicMessageOnSendToProcessor()
411699
{
412700
$context = $this->createContextMock();

0 commit comments

Comments
 (0)