From 2988633b02ab4e2ab2d4c6550f5a4593ce0e2639 Mon Sep 17 00:00:00 2001 From: Jeroen Bouwmans Date: Fri, 15 Dec 2023 13:22:31 +0100 Subject: [PATCH 1/5] [Messenger] Fix using negative delay in Doctrine Bridge --- .../Transport/DoctrineIntegrationTest.php | 19 +++++++++++++++++++ .../Bridge/Doctrine/Transport/Connection.php | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php index b900fa86d5774..4f3a4813e05bf 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php @@ -73,6 +73,25 @@ public function testSendWithDelay() $this->assertGreaterThan($now, $available_at); } + public function testSendWithNegativeDelay() + { + $this->connection->send('{"message": "Hi, I am not actually delayed"}', ['type' => DummyMessage::class], -600000); + + $qb = $this->driverConnection->createQueryBuilder() + ->select('m.available_at') + ->from('messenger_messages', 'm') + ->where('m.body = :body') + ->setParameter('body', '{"message": "Hi, I am not actually delayed"}'); + + // DBAL 2 compatibility + $result = method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute(); + + $available_at = new \DateTimeImmutable($result->fetchOne()); + + $now = new \DateTimeImmutable('now - 60 seconds'); + $this->assertLessThan($now, $available_at); + } + public function testItRetrieveTheFirstAvailableMessage() { $this->connection->setup(); diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php index 2c787e9cca0c3..36875da9023b7 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php @@ -127,7 +127,7 @@ public static function buildConfiguration(#[\SensitiveParameter] string $dsn, ar public function send(string $body, array $headers, int $delay = 0): string { $now = new \DateTimeImmutable('UTC'); - $availableAt = $now->modify(sprintf('+%d seconds', $delay / 1000)); + $availableAt = $now->modify(sprintf('%+d seconds', $delay / 1000)); $queryBuilder = $this->driverConnection->createQueryBuilder() ->insert($this->configuration['table_name']) From 42aa96ea9b1cfbc9921f2289b6b52b39472d7dfd Mon Sep 17 00:00:00 2001 From: Jeroen Bouwmans Date: Fri, 15 Dec 2023 13:22:55 +0100 Subject: [PATCH 2/5] [Messenger] Fix using negative delay in `InMemoryTransport` --- .../Tests/Transport/InMemory/InMemoryTransportTest.php | 9 +++++++++ .../Messenger/Transport/InMemory/InMemoryTransport.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php b/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php index 0a4b218623a2e..db6b14c455c98 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php @@ -85,6 +85,15 @@ public function testQueueWithDelay() $this->assertSame([$envelope1], $this->transport->get()); } + public function testQueueWithNegativeDelay() + { + $envelope1 = new Envelope(new \stdClass()); + $envelope1 = $this->transport->send($envelope1); + $envelope2 = (new Envelope(new \stdClass()))->with(new DelayStamp(-10_000)); + $envelope2 = $this->transport->send($envelope2); + $this->assertSame([$envelope1, $envelope2], $this->transport->get()); + } + public function testQueueWithSerialization() { $envelope = new Envelope(new \stdClass()); diff --git a/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php b/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php index 4937c4b325786..b03ddd4448b93 100644 --- a/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php +++ b/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php @@ -102,7 +102,7 @@ public function send(Envelope $envelope): Envelope /** @var DelayStamp|null $delayStamp */ if ($delayStamp = $envelope->last(DelayStamp::class)) { $now = $this->clock?->now() ?? new \DateTimeImmutable(); - $this->availableAt[$id] = $now->modify(sprintf('+%d seconds', $delayStamp->getDelay() / 1000)); + $this->availableAt[$id] = $now->modify(sprintf('%+d seconds', $delayStamp->getDelay() / 1000)); } return $envelope; From 840d7e320682e0177cff7efb46f59ba9a21ac1af Mon Sep 17 00:00:00 2001 From: J-roen Date: Fri, 15 Dec 2023 20:25:00 +0100 Subject: [PATCH 3/5] [Messenger] Fix code style Co-authored-by: Oskar Stark --- .../Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php index 4f3a4813e05bf..de46ac6726b78 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php @@ -86,7 +86,7 @@ public function testSendWithNegativeDelay() // DBAL 2 compatibility $result = method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute(); - $available_at = new \DateTimeImmutable($result->fetchOne()); + $availableAt = new \DateTimeImmutable($result->fetchOne()); $now = new \DateTimeImmutable('now - 60 seconds'); $this->assertLessThan($now, $available_at); From 72e3803331e5ae3ff7b8c4d08e9b6629eb0911b3 Mon Sep 17 00:00:00 2001 From: Jeroen Bouwmans Date: Fri, 15 Dec 2023 20:33:01 +0100 Subject: [PATCH 4/5] [Messenger] Fix variable name --- .../Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php index de46ac6726b78..138ff24ce43dc 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php @@ -89,7 +89,7 @@ public function testSendWithNegativeDelay() $availableAt = new \DateTimeImmutable($result->fetchOne()); $now = new \DateTimeImmutable('now - 60 seconds'); - $this->assertLessThan($now, $available_at); + $this->assertLessThan($now, $availableAt); } public function testItRetrieveTheFirstAvailableMessage() From 6e419d84e8b4d65fd6a0ac416c18b8d2c894d495 Mon Sep 17 00:00:00 2001 From: Jeroen Bouwmans Date: Sat, 16 Dec 2023 14:31:58 +0100 Subject: [PATCH 5/5] [Messenger] Fix code style --- .../Doctrine/Tests/Transport/DoctrineIntegrationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php index 138ff24ce43dc..0a34c86bc48f0 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php @@ -67,10 +67,10 @@ public function testSendWithDelay() // DBAL 2 compatibility $result = method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute(); - $available_at = new \DateTimeImmutable($result->fetchOne()); + $availableAt = new \DateTimeImmutable($result->fetchOne()); $now = new \DateTimeImmutable('now + 60 seconds'); - $this->assertGreaterThan($now, $available_at); + $this->assertGreaterThan($now, $availableAt); } public function testSendWithNegativeDelay()