Skip to content

Commit

Permalink
[Messenger] Draw transport remove + in delay due to php bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed May 25, 2024
1 parent b44e267 commit caa6dd6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 28 additions & 0 deletions packages/messenger/Tests/Transport/DrawTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
12 changes: 6 additions & 6 deletions packages/messenger/Transport/DrawTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit caa6dd6

Please sign in to comment.