From 8073eb69f830813891dc29284ee6da09eb8039dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Sat, 25 May 2024 13:33:08 -0400 Subject: [PATCH] [Messenger] Draw transport remove + in delay due to php bug --- .../Tests/Transport/DrawTransportTest.php | 28 +++++++++++++++++++ .../messenger/Transport/DrawTransport.php | 12 ++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/messenger/Tests/Transport/DrawTransportTest.php b/packages/messenger/Tests/Transport/DrawTransportTest.php index aea201f70..197045dd7 100644 --- a/packages/messenger/Tests/Transport/DrawTransportTest.php +++ b/packages/messenger/Tests/Transport/DrawTransportTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\Attributes\Depends; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\TransportException; +use Symfony\Component\Messenger\Stamp\DelayStamp; use Symfony\Component\Messenger\Stamp\RedeliveryStamp; use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp; use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface; @@ -117,6 +118,33 @@ public function testSendException(): void $this->service->send(new Envelope(new \stdClass())); } + #[Depends('testSetup')] + public function testSendNegativeDelay(): void + { + $envelope = $this->service->send(new Envelope( + new \stdClass(), + [new DelayStamp(-10000)] + )); + + $id = $envelope->last(TransportMessageIdStamp::class)->getId(); + + static::assertNotNull($id); + + $result = static::loadDefaultConnection() + ->executeQuery( + 'SELECT available_at FROM draw_messenger__message WHERE id = ?', + [ + $id, + ] + )->fetchAllAssociative(); + + static::assertEqualsWithDelta( + strtotime('now - 10 seconds'), + strtotime($result[0]['available_at']), + 1 + ); + } + #[Depends('testSetup')] public function testSend(): Envelope { diff --git a/packages/messenger/Transport/DrawTransport.php b/packages/messenger/Transport/DrawTransport.php index 9aef28ef0..e19f820e4 100644 --- a/packages/messenger/Transport/DrawTransport.php +++ b/packages/messenger/Transport/DrawTransport.php @@ -137,10 +137,10 @@ private function insert( ?string $messageClass = null, ): string { $id = Uuid::uuid6()->toString(); - $now = new \DateTime(); + $now = new \DateTimeImmutable(); $availableAt = null; if (null !== $delay) { - $availableAt = (clone $now)->modify(sprintf('+%d seconds', $delay / 1000)); + $availableAt = $now->modify(sprintf('%d seconds', $delay / 1000)); } $queryBuilder = $this->driverConnection->createQueryBuilder() @@ -164,8 +164,8 @@ private function insert( json_encode($headers, \JSON_THROW_ON_ERROR), $this->connection->getConfiguration()['queue_name'], self::formatDateTime($now), - $availableAt ? self::formatDateTime($availableAt) : null, - $expiresAt ? self::formatDateTime($expiresAt) : null, + self::formatDateTime($availableAt), + self::formatDateTime($expiresAt), ]); if ($tags) { @@ -182,9 +182,9 @@ private function insert( return $id; } - private static function formatDateTime(\DateTimeInterface $dateTime): string + private static function formatDateTime(?\DateTimeInterface $dateTime): ?string { - return $dateTime->format('Y-m-d\TH:i:s'); + return $dateTime?->format('Y-m-d\TH:i:s'); } public function setup(): void