Skip to content

Commit 01bcb21

Browse files
committed
delay strategy
1 parent 20c030d commit 01bcb21

4 files changed

Lines changed: 423 additions & 4 deletions

File tree

‎pkg/amqp-tools/RabbitMQDelayPluginDelayStrategy.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Interop\Amqp\Impl\AmqpBind;
1111
use Interop\Queue\InvalidDestinationException;
1212

13-
class RabbitMQDelayPluginDelayStrategy implements DelayStrategy
13+
class RabbitMqDelayPluginDelayStrategy implements DelayStrategy
1414
{
1515
/**
1616
* {@inheritdoc}
@@ -47,7 +47,10 @@ public function delayMessage(AmqpContext $context, AmqpDestination $dest, AmqpMe
4747
}
4848

4949
$producer = $context->createProducer();
50-
$producer->setDelayStrategy(null);
50+
51+
if ($producer instanceof DelayStrategyAware) {
52+
$producer->setDelayStrategy(null);
53+
}
5154

5255
$producer->send($delayTopic, $delayMessage);
5356
}

‎pkg/amqp-tools/RabbitMQDlxDelayStrategy.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Interop\Amqp\AmqpTopic;
1010
use Interop\Queue\InvalidDestinationException;
1111

12-
class RabbitMQDlxDelayStrategy implements DelayStrategy
12+
class RabbitMqDlxDelayStrategy implements DelayStrategy
1313
{
1414
/**
1515
* {@inheritdoc}
@@ -24,7 +24,6 @@ public function delayMessage(AmqpContext $context, AmqpDestination $dest, AmqpMe
2424
unset($properties['x-death']);
2525

2626
$delayMessage = $context->createMessage($message->getBody(), $properties, $message->getHeaders());
27-
$delayMessage->setExpiration((int) $delayMsec);
2827
$delayMessage->setRoutingKey($message->getRoutingKey());
2928

3029
if ($dest instanceof AmqpTopic) {
@@ -33,11 +32,13 @@ public function delayMessage(AmqpContext $context, AmqpDestination $dest, AmqpMe
3332

3433
$delayQueue = $context->createQueue($name);
3534
$delayQueue->addFlag(AmqpTopic::FLAG_DURABLE);
35+
$delayQueue->setArgument('x-message-ttl', $delayMsec);
3636
$delayQueue->setArgument('x-dead-letter-exchange', $dest->getTopicName());
3737
$delayQueue->setArgument('x-dead-letter-routing-key', (string) $delayMessage->getRoutingKey());
3838
} elseif ($dest instanceof AmqpQueue) {
3939
$delayQueue = $context->createQueue('enqueue.'.$dest->getQueueName().'.'.$delayMsec.'.delayed');
4040
$delayQueue->addFlag(AmqpTopic::FLAG_DURABLE);
41+
$delayQueue->setArgument('x-message-ttl', $delayMsec);
4142
$delayQueue->setArgument('x-dead-letter-exchange', '');
4243
$delayQueue->setArgument('x-dead-letter-routing-key', $dest->getQueueName());
4344
} else {
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpTools\Tests;
4+
5+
use Enqueue\AmqpTools\DelayStrategy;
6+
use Enqueue\AmqpTools\RabbitMqDelayPluginDelayStrategy;
7+
use Enqueue\Test\ClassExtensionTrait;
8+
use Interop\Amqp\AmqpBind;
9+
use Interop\Amqp\AmqpContext;
10+
use Interop\Amqp\AmqpDestination;
11+
use Interop\Amqp\AmqpProducer;
12+
use Interop\Amqp\Impl\AmqpMessage;
13+
use Interop\Amqp\Impl\AmqpQueue;
14+
use Interop\Amqp\Impl\AmqpTopic;
15+
use Interop\Queue\InvalidDestinationException;
16+
use Interop\Queue\PsrDestination;
17+
use Interop\Queue\PsrMessage;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class RabbitMqDelayPluginDelayStrategyTest extends TestCase
21+
{
22+
use ClassExtensionTrait;
23+
24+
public function testShouldImplementDelayStrategyInterface()
25+
{
26+
$this->assertClassImplements(DelayStrategy::class, RabbitMqDelayPluginDelayStrategy::class);
27+
}
28+
29+
public function testShouldSendDelayedMessageToTopic()
30+
{
31+
$delayedTopic = new AmqpTopic('the-topic');
32+
$delayedMessage = new AmqpMessage();
33+
34+
$producer = $this->createProducerMock();
35+
$producer
36+
->expects($this->once())
37+
->method('send')
38+
->with($this->identicalTo($delayedTopic), $this->identicalTo($delayedMessage))
39+
;
40+
41+
$context = $this->createContextMock();
42+
$context
43+
->expects($this->once())
44+
->method('createTopic')
45+
->with($this->identicalTo('enqueue.the-topic.delayed'))
46+
->willReturn($delayedTopic)
47+
;
48+
$context
49+
->expects($this->once())
50+
->method('declareTopic')
51+
->with($this->identicalTo($delayedTopic))
52+
;
53+
$context
54+
->expects($this->once())
55+
->method('createProducer')
56+
->willReturn($producer)
57+
;
58+
$context
59+
->expects($this->once())
60+
->method('createMessage')
61+
->with($this->identicalTo('the body'), $this->identicalTo(['key' => 'value']), $this->identicalTo(['hkey' => 'hvalue']))
62+
->willReturn($delayedMessage)
63+
;
64+
$context
65+
->expects($this->once())
66+
->method('bind')
67+
->with($this->isInstanceOf(AmqpBind::class))
68+
;
69+
70+
$message = new AmqpMessage('the body', ['key' => 'value'], ['hkey' => 'hvalue']);
71+
$message->setRoutingKey('the-routing-key');
72+
73+
$dest = new AmqpTopic('the-topic');
74+
$dest->setFlags(12345);
75+
76+
$strategy = new RabbitMqDelayPluginDelayStrategy();
77+
$strategy->delayMessage($context, $dest, $message, 10000);
78+
79+
$this->assertSame(12345, $delayedTopic->getFlags());
80+
$this->assertSame('x-delayed-message', $delayedTopic->getType());
81+
$this->assertSame([
82+
'x-delayed-type' => 'direct',
83+
], $delayedTopic->getArguments());
84+
85+
$this->assertSame(['x-delay' => 10000], $delayedMessage->getProperties());
86+
$this->assertSame('the-routing-key', $delayedMessage->getRoutingKey());
87+
}
88+
89+
public function testShouldSendDelayedMessageToQueue()
90+
{
91+
$delayedTopic = new AmqpTopic('the-topic');
92+
$delayedMessage = new AmqpMessage();
93+
94+
$producer = $this->createProducerMock();
95+
$producer
96+
->expects($this->once())
97+
->method('send')
98+
->with($this->identicalTo($delayedTopic), $this->identicalTo($delayedMessage))
99+
;
100+
101+
$context = $this->createContextMock();
102+
$context
103+
->expects($this->once())
104+
->method('createTopic')
105+
->with($this->identicalTo('enqueue.queue.delayed'))
106+
->willReturn($delayedTopic)
107+
;
108+
$context
109+
->expects($this->once())
110+
->method('declareTopic')
111+
->with($this->identicalTo($delayedTopic))
112+
;
113+
$context
114+
->expects($this->once())
115+
->method('createProducer')
116+
->willReturn($producer)
117+
;
118+
$context
119+
->expects($this->once())
120+
->method('createMessage')
121+
->with($this->identicalTo('the body'), $this->identicalTo(['key' => 'value']), $this->identicalTo(['hkey' => 'hvalue']))
122+
->willReturn($delayedMessage)
123+
;
124+
$context
125+
->expects($this->once())
126+
->method('bind')
127+
->with($this->isInstanceOf(AmqpBind::class))
128+
;
129+
130+
$message = new AmqpMessage('the body', ['key' => 'value'], ['hkey' => 'hvalue']);
131+
$message->setRoutingKey('the-routing-key');
132+
133+
$dest = new AmqpQueue('the-queue');
134+
135+
$strategy = new RabbitMqDelayPluginDelayStrategy();
136+
$strategy->delayMessage($context, $dest, $message, 10000);
137+
138+
$this->assertSame(AmqpQueue::FLAG_DURABLE, $delayedTopic->getFlags());
139+
$this->assertSame('x-delayed-message', $delayedTopic->getType());
140+
$this->assertSame([
141+
'x-delayed-type' => 'direct',
142+
], $delayedTopic->getArguments());
143+
144+
$this->assertSame(['x-delay' => 10000], $delayedMessage->getProperties());
145+
$this->assertSame('the-queue', $delayedMessage->getRoutingKey());
146+
}
147+
148+
public function testShouldThrowExceptionIfInvalidDestination()
149+
{
150+
$delayedMessage = new AmqpMessage();
151+
152+
$context = $this->createContextMock();
153+
$context
154+
->expects($this->once())
155+
->method('createMessage')
156+
->willReturn($delayedMessage)
157+
;
158+
159+
$strategy = new RabbitMqDelayPluginDelayStrategy();
160+
161+
$this->expectException(InvalidDestinationException::class);
162+
$this->expectExceptionMessage('The destination must be an instance of Interop\Amqp\AmqpTopic|Interop\Amqp\AmqpQueue but got');
163+
164+
$strategy->delayMessage($context, $this->createMock(AmqpDestination::class), new AmqpMessage(), 10000);
165+
}
166+
167+
/**
168+
* @return \PHPUnit_Framework_MockObject_MockObject|AmqpContext
169+
*/
170+
private function createContextMock()
171+
{
172+
return $this->createMock(AmqpContext::class);
173+
}
174+
175+
/**
176+
* @return \PHPUnit_Framework_MockObject_MockObject|TestProducer
177+
*/
178+
private function createProducerMock()
179+
{
180+
return $this->createMock(TestProducer::class);
181+
}
182+
}
183+
184+
class TestProducer implements AmqpProducer, DelayStrategy
185+
{
186+
public function delayMessage(AmqpContext $context, AmqpDestination $dest, \Interop\Amqp\AmqpMessage $message, $delayMsec)
187+
{
188+
}
189+
190+
public function send(PsrDestination $destination, PsrMessage $message)
191+
{
192+
}
193+
194+
public function setDeliveryDelay($deliveryDelay)
195+
{
196+
}
197+
198+
public function getDeliveryDelay()
199+
{
200+
}
201+
202+
public function setPriority($priority)
203+
{
204+
}
205+
206+
public function getPriority()
207+
{
208+
}
209+
210+
public function setTimeToLive($timeToLive)
211+
{
212+
}
213+
214+
public function getTimeToLive()
215+
{
216+
}
217+
}

0 commit comments

Comments
 (0)