Skip to content

Commit

Permalink
fix: handle DelayStamp in the past (#75)
Browse files Browse the repository at this point in the history
Fix an issue when the envelope was created with a DelayStamp in the past,
the availability was generated in the future.
  • Loading branch information
benito103e authored Mar 21, 2024
1 parent 1403dd6 commit 9f31c86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Stamp/AvailableAtStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function __construct(private \DateTimeImmutable $availableAt)
public static function fromDelayStamp(DelayStamp $delayStamp, \DateTimeImmutable $now): self
{
return new self(
$now->modify(\sprintf('+%d seconds', $delayStamp->getDelay() / 1000)),
$now->modify(\sprintf('%s%d seconds',
$delayStamp->getDelay() > 0 ? '+' : '-',
\abs($delayStamp->getDelay() / 1000)
))
);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/DelayStampTestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageA;
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageB;
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageC;
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageD;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
Expand Down Expand Up @@ -88,6 +89,26 @@ public function it_handles_messages_depending_on_delay_stamp(): void
$transport->process()->acknowledged()->assertCount(3)->assertContains(MessageA::class);
}

/**
* @test
*/
public function it_handles_directly_messages_with_negative_delay_stamp(): void
{
$clock = self::mockTime();

$transport = $this->transport('async');
$transport->send(new Envelope(new MessageA(), [new DelayStamp(5_000)]));
$transport->send(new Envelope(new MessageB()));
$transport->send(new Envelope(new MessageC(), [new DelayStamp(-5_000)]));

$transport->acknowledged()->assertCount(0);

$transport->process()->acknowledged()->assertCount(2)->assertContains(MessageB::class)->assertContains(MessageC::class);

$clock->sleep(10);
$transport->process()->acknowledged()->assertCount(3)->assertContains(MessageA::class);
}

protected static function bootKernel(array $options = []): KernelInterface // @phpstan-ignore-line
{
return parent::bootKernel(\array_merge(['environment' => 'single_transport'], $options));
Expand Down

0 comments on commit 9f31c86

Please sign in to comment.