Skip to content

Commit be3b5dd

Browse files
committed
delay strategy
1 parent 6d70adf commit be3b5dd

10 files changed

Lines changed: 46 additions & 55 deletions

‎pkg/amqp-bunny/Symfony/RabbitMqAmqpBunnyTransportFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
3131
$builder
3232
->children()
3333
->scalarNode('delay_strategy')
34-
->defaultNull()
34+
->defaultValue('dlx')
3535
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3636
->end()
3737
;

‎pkg/amqp-bunny/Tests/Symfony/RabbitMqAmqpBunnyTransportFactoryTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testShouldAllowAddConfiguration()
5858
'user' => 'guest',
5959
'pass' => 'guest',
6060
'vhost' => '/',
61-
'delay_strategy' => null,
61+
'delay_strategy' => 'dlx',
6262
'lazy' => true,
6363
'receive_method' => 'basic_get',
6464
'heartbeat' => 0,

‎pkg/amqp-ext/Symfony/RabbitMqAmqpTransportFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
3131
$builder
3232
->children()
3333
->scalarNode('delay_strategy')
34-
->defaultNull()
34+
->defaultValue('dlx')
3535
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3636
->end()
3737
;

‎pkg/amqp-ext/Tests/Symfony/RabbitMqAmqpTransportFactoryTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testShouldAllowAddConfiguration()
5959
'pass' => 'guest',
6060
'vhost' => '/',
6161
'persisted' => false,
62-
'delay_strategy' => null,
62+
'delay_strategy' => 'dlx',
6363
'lazy' => true,
6464
'receive_method' => 'basic_get',
6565
], $config);

‎pkg/amqp-lib/Symfony/RabbitMqAmqpLibTransportFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
3131
$builder
3232
->children()
3333
->scalarNode('delay_strategy')
34-
->defaultNull()
34+
->defaultValue('dlx')
3535
->info('The delay strategy to be used. Possible values are "dlx", "delayed_message_plugin" or service id')
3636
->end()
3737
;

‎pkg/amqp-lib/Tests/AmqpConsumerTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function testShouldReturnMessageOnReceiveNoWait()
102102
$amqpMessage = new \PhpAmqpLib\Message\AMQPMessage('body');
103103
$amqpMessage->delivery_info['delivery_tag'] = 'delivery-tag';
104104
$amqpMessage->delivery_info['redelivered'] = true;
105+
$amqpMessage->delivery_info['routing_key'] = 'routing-key';
105106

106107
$channel = $this->createChannelMock();
107108
$channel
@@ -120,13 +121,15 @@ public function testShouldReturnMessageOnReceiveNoWait()
120121
$this->assertInstanceOf(AmqpMessage::class, $message);
121122
$this->assertSame('body', $message->getBody());
122123
$this->assertSame('delivery-tag', $message->getDeliveryTag());
124+
$this->assertSame('routing-key', $message->getRoutingKey());
123125
$this->assertTrue($message->isRedelivered());
124126
}
125127

126128
public function testShouldReturnMessageOnReceiveWithReceiveMethodBasicGet()
127129
{
128130
$amqpMessage = new \PhpAmqpLib\Message\AMQPMessage('body');
129131
$amqpMessage->delivery_info['delivery_tag'] = 'delivery-tag';
132+
$amqpMessage->delivery_info['routing_key'] = 'routing-key';
130133
$amqpMessage->delivery_info['redelivered'] = true;
131134

132135
$channel = $this->createChannelMock();
@@ -146,6 +149,7 @@ public function testShouldReturnMessageOnReceiveWithReceiveMethodBasicGet()
146149
$this->assertInstanceOf(AmqpMessage::class, $message);
147150
$this->assertSame('body', $message->getBody());
148151
$this->assertSame('delivery-tag', $message->getDeliveryTag());
152+
$this->assertSame('routing-key', $message->getRoutingKey());
149153
$this->assertTrue($message->isRedelivered());
150154
}
151155

‎pkg/amqp-lib/Tests/AmqpContextTest.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Interop\Amqp\Impl\AmqpTopic;
99
use PhpAmqpLib\Channel\AMQPChannel;
1010
use PhpAmqpLib\Connection\AbstractConnection;
11+
use PhpAmqpLib\Wire\AMQPTable;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class AmqpContextTest extends TestCase
@@ -26,7 +27,7 @@ public function testShouldDeclareTopic()
2627
$this->isTrue(),
2728
$this->isTrue(),
2829
$this->isTrue(),
29-
$this->identicalTo(['key' => 'value']),
30+
$this->isInstanceOf(AMQPTable::class),
3031
$this->isNull()
3132
)
3233
;
@@ -94,7 +95,7 @@ public function testShouldDeclareQueue()
9495
$this->isTrue(),
9596
$this->isTrue(),
9697
$this->isTrue(),
97-
$this->identicalTo(['key' => 'value']),
98+
$this->isInstanceOf(AMQPTable::class),
9899
$this->isNull()
99100
)
100101
;

‎pkg/amqp-lib/Tests/AmqpProducerTest.php‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\AmqpLib\Tests;
44

5+
use Enqueue\AmqpLib\AmqpContext;
56
use Enqueue\AmqpLib\AmqpProducer;
67
use Enqueue\Test\ClassExtensionTrait;
78
use Interop\Amqp\Impl\AmqpMessage;
@@ -23,7 +24,7 @@ class AmqpProducerTest extends TestCase
2324

2425
public function testCouldBeConstructedWithRequiredArguments()
2526
{
26-
new AmqpProducer($this->createAmqpChannelMock());
27+
new AmqpProducer($this->createAmqpChannelMock(), $this->createContextMock());
2728
}
2829

2930
public function testShouldImplementPsrProducerInterface()
@@ -33,7 +34,7 @@ public function testShouldImplementPsrProducerInterface()
3334

3435
public function testShouldThrowExceptionWhenDestinationTypeIsInvalid()
3536
{
36-
$producer = new AmqpProducer($this->createAmqpChannelMock());
37+
$producer = new AmqpProducer($this->createAmqpChannelMock(), $this->createContextMock());
3738

3839
$this->expectException(InvalidDestinationException::class);
3940
$this->expectExceptionMessage('The destination must be an instance of Interop\Amqp\AmqpQueue but got');
@@ -43,7 +44,7 @@ public function testShouldThrowExceptionWhenDestinationTypeIsInvalid()
4344

4445
public function testShouldThrowExceptionWhenMessageTypeIsInvalid()
4546
{
46-
$producer = new AmqpProducer($this->createAmqpChannelMock());
47+
$producer = new AmqpProducer($this->createAmqpChannelMock(), $this->createContextMock());
4748

4849
$this->expectException(InvalidMessageException::class);
4950
$this->expectExceptionMessage('The message must be an instance of Interop\Amqp\AmqpMessage but it is');
@@ -70,7 +71,7 @@ public function testShouldPublishMessageToTopic()
7071
$message = new AmqpMessage('body');
7172
$message->setRoutingKey('routing-key');
7273

73-
$producer = new AmqpProducer($channel);
74+
$producer = new AmqpProducer($channel, $this->createContextMock());
7475
$producer->send($topic, $message);
7576

7677
$this->assertEquals('body', $amqpMessage->getBody());
@@ -92,7 +93,7 @@ public function testShouldPublishMessageToQueue()
9293

9394
$queue = new AmqpQueue('queue');
9495

95-
$producer = new AmqpProducer($channel);
96+
$producer = new AmqpProducer($channel, $this->createContextMock());
9697
$producer->send($queue, new AmqpMessage('body'));
9798

9899
$this->assertEquals('body', $amqpMessage->getBody());
@@ -111,7 +112,7 @@ public function testShouldSetMessageHeaders()
111112
}))
112113
;
113114

114-
$producer = new AmqpProducer($channel);
115+
$producer = new AmqpProducer($channel, $this->createContextMock());
115116
$producer->send(new AmqpTopic('name'), new AmqpMessage('body', [], ['content_type' => 'text/plain']));
116117

117118
$this->assertEquals(['content_type' => 'text/plain'], $amqpMessage->get_properties());
@@ -130,7 +131,7 @@ public function testShouldSetMessageProperties()
130131
}))
131132
;
132133

133-
$producer = new AmqpProducer($channel);
134+
$producer = new AmqpProducer($channel, $this->createContextMock());
134135
$producer->send(new AmqpTopic('name'), new AmqpMessage('body', ['key' => 'value']));
135136

136137
$properties = $amqpMessage->get_properties();
@@ -163,4 +164,12 @@ private function createAmqpChannelMock()
163164
{
164165
return $this->createMock(AMQPChannel::class);
165166
}
167+
168+
/**
169+
* @return \PHPUnit_Framework_MockObject_MockObject|AmqpContext
170+
*/
171+
private function createContextMock()
172+
{
173+
return $this->createMock(AmqpContext::class);
174+
}
166175
}

‎pkg/amqp-lib/Tests/Symfony/RabbitMqAmqpLibTransportFactoryTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testShouldAllowAddConfiguration()
5858
'user' => 'guest',
5959
'pass' => 'guest',
6060
'vhost' => '/',
61-
'delay_strategy' => null,
61+
'delay_strategy' => 'dlx',
6262
'lazy' => true,
6363
'receive_method' => 'basic_get',
6464
'connection_timeout' => 3.0,

‎pkg/enqueue/Tests/Client/Amqp/RabbitMqDriverTest.php‎

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testShouldConvertTransportMessageToClientMessage()
9797
$transportMessage->setBody('body');
9898
$transportMessage->setHeaders(['hkey' => 'hval']);
9999
$transportMessage->setProperties(['key' => 'val']);
100-
$transportMessage->setProperty('x-delay', '5678000');
100+
$transportMessage->setProperty('enqueue-delay', '5678000');
101101
$transportMessage->setHeader('content_type', 'ContentType');
102102
$transportMessage->setHeader('expiration', '12345000');
103103
$transportMessage->setHeader('priority', 3);
@@ -108,7 +108,7 @@ public function testShouldConvertTransportMessageToClientMessage()
108108

109109
$driver = new RabbitMqDriver(
110110
$this->createAmqpContextMock(),
111-
new Config('', '', '', '', '', '', ['delay_plugin_installed' => true]),
111+
new Config('', '', '', '', '', '', ['delay_strategy' => 'dlx']),
112112
$this->createDummyQueueMetaRegistry()
113113
);
114114

@@ -128,7 +128,7 @@ public function testShouldConvertTransportMessageToClientMessage()
128128
], $clientMessage->getHeaders());
129129
$this->assertSame([
130130
'key' => 'val',
131-
'x-delay' => '5678000',
131+
'enqueue-delay' => '5678000',
132132
], $clientMessage->getProperties());
133133
$this->assertSame('MessageId', $clientMessage->getMessageId());
134134
$this->assertSame(12345, $clientMessage->getExpire());
@@ -143,7 +143,7 @@ public function testShouldConvertTransportMessageToClientMessage()
143143
public function testShouldThrowExceptionIfXDelayIsNotNumeric()
144144
{
145145
$transportMessage = new AmqpMessage();
146-
$transportMessage->setProperty('x-delay', 'is-not-numeric');
146+
$transportMessage->setProperty('enqueue-delay', 'is-not-numeric');
147147

148148
$driver = new RabbitMqDriver(
149149
$this->createAmqpContextMock(),
@@ -152,7 +152,7 @@ public function testShouldThrowExceptionIfXDelayIsNotNumeric()
152152
);
153153

154154
$this->expectException(\LogicException::class);
155-
$this->expectExceptionMessage('x-delay header is not numeric. "is-not-numeric"');
155+
$this->expectExceptionMessage('"enqueue-delay" header is not numeric. "is-not-numeric"');
156156

157157
$driver->createClientMessage($transportMessage);
158158
}
@@ -239,7 +239,7 @@ public function testShouldConvertClientMessageToTransportMessage()
239239

240240
$driver = new RabbitMqDriver(
241241
$context,
242-
new Config('', '', '', '', '', '', ['delay_plugin_installed' => true]),
242+
new Config('', '', '', '', '', '', ['delay_strategy' => 'dlx']),
243243
$this->createDummyQueueMetaRegistry()
244244
);
245245

@@ -260,7 +260,7 @@ public function testShouldConvertClientMessageToTransportMessage()
260260
], $transportMessage->getHeaders());
261261
$this->assertSame([
262262
'key' => 'val',
263-
'x-delay' => '432000',
263+
'enqueue-delay' => 432000,
264264
], $transportMessage->getProperties());
265265
$this->assertSame('MessageId', $transportMessage->getMessageId());
266266
$this->assertSame(1000, $transportMessage->getTimestamp());
@@ -282,12 +282,12 @@ public function testThrowIfDelayNotSupportedOnConvertClientMessageToTransportMes
282282

283283
$driver = new RabbitMqDriver(
284284
$context,
285-
new Config('', '', '', '', '', '', ['delay_plugin_installed' => false]),
285+
new Config('', '', '', '', '', '', ['delay_strategy' => null]),
286286
$this->createDummyQueueMetaRegistry()
287287
);
288288

289289
$this->expectException(\LogicException::class);
290-
$this->expectExceptionMessage('The message delaying is not supported. In order to use delay feature install RabbitMQ delay plugin.');
290+
$this->expectExceptionMessage('The message delaying is not supported. In order to use delay feature install RabbitMQ delay strategy.');
291291
$driver->createTransportMessage($clientMessage);
292292
}
293293

@@ -386,29 +386,28 @@ public function testShouldSendMessageToProcessor()
386386
$driver->sendToProcessor($message);
387387
}
388388

389-
public function testShouldSendMessageToDelayExchangeIfDelaySet()
389+
public function testShouldSendMessageToProcessorWithDeliveryDelay()
390390
{
391391
$queue = new AmqpQueue('');
392-
$delayTopic = new AmqpTopic('');
393392
$transportMessage = new AmqpMessage();
394393

395394
$producer = $this->createAmqpProducerMock();
396395
$producer
397396
->expects($this->once())
398397
->method('send')
399-
->with($this->identicalTo($delayTopic), $this->identicalTo($transportMessage))
398+
->with($this->identicalTo($queue), $this->identicalTo($transportMessage))
399+
;
400+
$producer
401+
->expects($this->once())
402+
->method('setDeliveryDelay')
403+
->with($this->identicalTo(10000))
400404
;
401405
$context = $this->createAmqpContextMock();
402406
$context
403407
->expects($this->once())
404408
->method('createQueue')
405409
->willReturn($queue)
406410
;
407-
$context
408-
->expects($this->once())
409-
->method('createTopic')
410-
->willReturn($delayTopic)
411-
;
412411
$context
413412
->expects($this->once())
414413
->method('createProducer')
@@ -422,7 +421,7 @@ public function testShouldSendMessageToDelayExchangeIfDelaySet()
422421

423422
$driver = new RabbitMqDriver(
424423
$context,
425-
new Config('', '', '', '', '', '', ['delay_plugin_installed' => true]),
424+
new Config('', '', '', '', '', '', ['delay_strategy' => 'dlx']),
426425
$this->createDummyQueueMetaRegistry()
427426
);
428427

@@ -521,7 +520,6 @@ public function testShouldSetupBroker()
521520
$routerQueue = new AmqpQueue('');
522521

523522
$processorQueue = new AmqpQueue('');
524-
$delayTopic = new AmqpTopic('');
525523

526524
$context = $this->createAmqpContextMock();
527525
// setup router
@@ -561,27 +559,6 @@ public function testShouldSetupBroker()
561559
->method('declareQueue')
562560
->with($this->identicalTo($processorQueue))
563561
;
564-
$context
565-
->expects($this->at(7))
566-
->method('createQueue')
567-
->willReturn($processorQueue)
568-
;
569-
$context
570-
->expects($this->at(8))
571-
->method('createTopic')
572-
->willReturn($delayTopic)
573-
;
574-
$context
575-
->expects($this->at(9))
576-
->method('declareTopic')
577-
->with($this->identicalTo($delayTopic))
578-
;
579-
580-
$context
581-
->expects($this->at(10))
582-
->method('bind')
583-
->with($this->isInstanceOf(AmqpBind::class))
584-
;
585562

586563
$config = Config::create('', '', '', '', '', '', ['delay_plugin_installed' => true]);
587564

0 commit comments

Comments
 (0)