Skip to content

[13.x] Fix flaky QueueWorkerTest by freezing time before computing retryUntil#59727

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
bipinks:fix/queue-worker-test-race
Apr 16, 2026
Merged

[13.x] Fix flaky QueueWorkerTest by freezing time before computing retryUntil#59727
taylorotwell merged 1 commit into
laravel:13.xfrom
bipinks:fix/queue-worker-test-race

Conversation

@bipinks

@bipinks bipinks commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Problem

QueueWorkerTest::testJobIsNotReleasedIfItHasExpired intermittently fails on CI (observed on Windows runners):

MaxAttemptsExceededException: "WorkerFakeJob has been attempted too many times."
is not instance of expected class "RuntimeException"

Root Cause

The test computes retryUntil and the frozen test time from two separate Carbon::now() calls against the real wall clock (no setTestNow is active yet):

$job->retryUntil = Carbon::now()->addSeconds(1)->getTimestamp();  // line 253
Carbon::setTestNow(Carbon::now()->addSeconds(1));                 // line 257-258

If a real-time second boundary passes between these two lines (more likely on slow CI), the frozen now ends up 1 second ahead of retryUntil. This causes markJobAsFailedIfAlreadyExceedsMaxAttempts to see the job as already expired (now > retryUntil) and throw MaxAttemptsExceededException before the job fires, instead of letting the job run, throw RuntimeException, and then fail via markJobAsFailedIfWillExceedMaxAttempts.

Fix

Freeze time with Carbon::setTestNow() before computing retryUntil, so both timestamps derive from the same deterministic base:

Carbon::setTestNow($now = Carbon::create(2026, 1, 1, 0, 0, 0));
$job->retryUntil = $now->copy()->addSecond()->getTimestamp();
Carbon::setTestNow($now->copy()->addSecond());

This eliminates the real-time race entirely. The frozen now equals retryUntil, so the <= check in markJobAsFailedIfAlreadyExceedsMaxAttempts passes (returns early), the job fires normally, and the test exercises its intended path.

1 file changed, 4 insertions, 4 deletions.

@taylorotwell
taylorotwell merged commit a3f860e into laravel:13.x Apr 16, 2026
54 checks passed
jonagoldman pushed a commit to deplox/laravel-framework that referenced this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants