Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/V2/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,16 @@ public static function runReadyTasks(int $limit = 100): int
$processed = 0;

while ($processed < $limit) {
$query = self::taskQuery();
$dueAt = $query->getModel()
->fromDateTime(now());

/** @var WorkflowTask|null $task */
$task = self::taskQuery()
$task = $query
->where('status', TaskStatus::Ready->value)
->where(static function ($query): void {
->where(static function ($query) use ($dueAt): void {
$query->whereNull('available_at')
->orWhere('available_at', '<=', now());
->orWhere('available_at', '<=', $dueAt);
})
->orderBy('available_at')
->orderBy('created_at')
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/V2/V2WorkflowStubFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
use Tests\Fixtures\V2\TestGreetingActivity;
use Tests\Fixtures\V2\TestGreetingWorkflow;
Expand Down Expand Up @@ -358,6 +359,34 @@ public function testFakeModeCanDrainDueTimerTasksAfterTimeTravel(): void
->all());
}

#[DataProvider('dueTaskTimestampCases')]
public function testFakeModePreservesTaskTimestampPrecision(string $startedAt, string $observedAt, bool $due): void
{
$this->travelTo(CarbonImmutable::parse($startedAt, 'UTC'));
WorkflowStub::fake();

$workflow = WorkflowStub::make(TestTimerWorkflow::class, 'fake-precise-timer');
$workflow->start(1);
$this->assertFalse($workflow->refresh()->completed());

$this->travelTo(CarbonImmutable::parse($observedAt, 'UTC'));

$this->assertSame($due ? 1 : 0, WorkflowStub::runReadyTasks());
$this->assertSame($due, $workflow->refresh()->completed());
$this->assertSame($due ? TimerStatus::Fired : TimerStatus::Pending, WorkflowTimer::query()->sole()->status);
}

/**
* @return iterable<string, array{string, string, bool}>
*/
public static function dueTaskTimestampCases(): iterable
{
yield 'whole-second equality' => ['2026-01-02 00:00:00.000000', '2026-01-02 00:00:01.000000', true];
yield 'fractional equality' => ['2026-01-02 00:00:00.500000', '2026-01-02 00:00:01.500000', true];
yield 'elapsed within the same second' => ['2026-01-02 00:00:00.100000', '2026-01-02 00:00:01.400000', true];
yield 'future fractional deadline' => ['2026-01-02 00:00:00.500000', '2026-01-02 00:00:01.499999', false];
}

public function testFakeModeRunsActivityAfterTimerWithImmutableApplicationDates(): void
{
Date::use(CarbonImmutable::class);
Expand Down
Loading