Skip to content

Commit 962f404

Browse files
authored
Merge pull request #738 from niels-nijens/fix-redis-delivery-delay
[Redis] Fix messages sent with incorrect delivery delay
2 parents d811e2f + 01e35cd commit 962f404

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

Diff for: pkg/redis/RedisProducer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function send(Destination $destination, Message $message): void
6464
$payload = $this->context->getSerializer()->toString($message);
6565

6666
if ($message->getDeliveryDelay()) {
67-
$deliveryAt = time() + $message->getDeliveryDelay();
67+
$deliveryAt = time() + $message->getDeliveryDelay() / 1000;
6868
$this->context->getRedis()->zadd($destination->getName().':delayed', $payload, $deliveryAt);
6969
} else {
7070
$this->context->getRedis()->lpush($destination->getName(), $payload);

Diff for: pkg/redis/Tests/RedisProducerTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ public function testShouldCallLPushOnSend()
8888
$producer->send($destination, new RedisMessage());
8989
}
9090

91+
/**
92+
* Tests if Redis::zadd is called with the expected 'score' (used as delivery timestamp).
93+
*
94+
* @depends testShouldCallLPushOnSend
95+
*/
96+
public function testShouldCallZaddOnSendWithDeliveryDelay()
97+
{
98+
$destination = new RedisDestination('aDestination');
99+
100+
$redisMock = $this->createRedisMock();
101+
$redisMock
102+
->expects($this->once())
103+
->method('zadd')
104+
->with(
105+
'aDestination:delayed',
106+
$this->isJson(),
107+
$this->equalTo(time() + 5)
108+
)
109+
;
110+
111+
$context = $this->createContextMock();
112+
$context
113+
->expects($this->once())
114+
->method('getRedis')
115+
->willReturn($redisMock)
116+
;
117+
$context
118+
->expects($this->once())
119+
->method('getSerializer')
120+
->willReturn(new JsonSerializer())
121+
;
122+
123+
$message = new RedisMessage();
124+
$message->setDeliveryDelay(5000); // 5 seconds in milliseconds
125+
126+
$producer = new RedisProducer($context);
127+
$producer->send($destination, $message);
128+
}
129+
91130
/**
92131
* @return \PHPUnit_Framework_MockObject_MockObject|RedisContext
93132
*/

0 commit comments

Comments
 (0)