diff --git a/pkg/redis/RedisProducer.php b/pkg/redis/RedisProducer.php index 3721ed20a..cc7d63230 100644 --- a/pkg/redis/RedisProducer.php +++ b/pkg/redis/RedisProducer.php @@ -64,7 +64,7 @@ public function send(Destination $destination, Message $message): void $payload = $this->context->getSerializer()->toString($message); if ($message->getDeliveryDelay()) { - $deliveryAt = time() + $message->getDeliveryDelay(); + $deliveryAt = time() + $message->getDeliveryDelay() / 1000; $this->context->getRedis()->zadd($destination->getName().':delayed', $payload, $deliveryAt); } else { $this->context->getRedis()->lpush($destination->getName(), $payload); diff --git a/pkg/redis/Tests/RedisProducerTest.php b/pkg/redis/Tests/RedisProducerTest.php index 12082734c..daef46b61 100644 --- a/pkg/redis/Tests/RedisProducerTest.php +++ b/pkg/redis/Tests/RedisProducerTest.php @@ -88,6 +88,45 @@ public function testShouldCallLPushOnSend() $producer->send($destination, new RedisMessage()); } + /** + * Tests if Redis::zadd is called with the expected 'score' (used as delivery timestamp). + * + * @depends testShouldCallLPushOnSend + */ + public function testShouldCallZaddOnSendWithDeliveryDelay() + { + $destination = new RedisDestination('aDestination'); + + $redisMock = $this->createRedisMock(); + $redisMock + ->expects($this->once()) + ->method('zadd') + ->with( + 'aDestination:delayed', + $this->isJson(), + $this->equalTo(time() + 5) + ) + ; + + $context = $this->createContextMock(); + $context + ->expects($this->once()) + ->method('getRedis') + ->willReturn($redisMock) + ; + $context + ->expects($this->once()) + ->method('getSerializer') + ->willReturn(new JsonSerializer()) + ; + + $message = new RedisMessage(); + $message->setDeliveryDelay(5000); // 5 seconds in milliseconds + + $producer = new RedisProducer($context); + $producer->send($destination, $message); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|RedisContext */