zenstruck / messenger-test

Assertions and helpers for testing your symfony/messenger queues.
MIT License
218 stars 15 forks source link

feat: support delay stamp #66

Closed nikophil closed 9 months ago

nikophil commented 10 months ago

Hi!

here is a suggestion for supporting the DelayStamp in this lib.

Please, consider this PR as fully WIP, I'll add tests, and cleaner code once we're OK on some approach.

Why is it needed?

we have some kind of home-made scheduler which permits to program some actions in the future, based on delay stamp, and we would like to be able to test this functionally by making "time jumps"

example:

// somewhere in the app
$bus->dispatch(new Enevelope(new DoSomething1(), [new DelayStamp('+3 days')]));
$bus->dispatch(new Enevelope(new DoSomething2(), [new DelayStamp('+5 days')]));
// somewhere in the tests

// 1. trigger the action that will dispatch the two previous messages by calling

// 2. assert no thing was done yet
$this->transport('scheduler')->process();
$this->transport('scheduler')->queue()->assertCount(2);

// 3. sleep and assert messages have been handled
$clock->sleep('3 days');
$this->transport('scheduler')->acknowledged()->assertContains(DoSomething1::class);
$this->asssertDoSomething1IsHandled();

$clock->sleep('2 days');
$this->transport('scheduler')->acknowledged()->assertContains(DoSomething2::class);
$this->asssertDoSomething2IsHandled();

Some problem it may raise

WDYT?

kbond commented 9 months ago

Should we have TestTransport::ignoreDelayStamp()|supportDelayStamp() methods?

nikophil commented 9 months ago

Should we have TestTransport::ignoreDelayStamp()|supportDelayStamp() methods?

because I wanted support_delay_stamp to be the de facto behavior, I didn't wanted the feature to be disabled. Since the IMO tests with delay stamp are wrongly handled for now, the support_delay_stamp was only a BC layer for me, waiting all user to migrate. But it seems harder that I was thinking

kbond commented 9 months ago

Should we have TestTransport::ignoreDelayStamp()|supportDelayStamp()

Let's not add until asked and re-evaluate when/if that happens.

nikophil commented 9 months ago

@kbond the PR is ready I think

the last problem is that I required symfony/clock as a dev dep in order to test the behavior, but this component has never been compatible with php 8.0, so tests are failing in some cases

any idea how to mitigate this?

kbond commented 9 months ago

any idea how to mitigate this?

Not without some CI craziness (requiring symfony/clock on 8.1+). I'd be fine requiring 8.1 for this package. 8.0 is almost EOL. WDYT?

nikophil commented 9 months ago

let's go for this!

nikophil commented 9 months ago

@kbond most of the runs are green, any idea how to fix the deprecation?

on my computer, even when using php 8.1 + --prefer-lowest, this deprecation is not a direct one... :shrug:

kbond commented 9 months ago

on my computer, even when using php 8.1 + --prefer-lowest, this deprecation is not a direct one...

I was able to recreate locally, needed to bump phpunit min to 9.6+ (I pushed this to your branch)