diff --git a/CHANGELOG.md b/CHANGELOG.md index 76ce766ef..1bf593d88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- The lost-lease schema check caches its result for 60 seconds by the framework + clock instead of PHP's `time()`, so a frozen test clock holds the window and + query-count tests no longer see an extra schema query on slow runs. + ## 2.34.0 - 2026-11-19 2.34.0 adds one migration (#1231): workflow tasks gain `lost_lease_count` and diff --git a/src/V2/Support/WorkflowTaskLeaseLoss.php b/src/V2/Support/WorkflowTaskLeaseLoss.php index e0b10ae8e..8adb28f72 100644 --- a/src/V2/Support/WorkflowTaskLeaseLoss.php +++ b/src/V2/Support/WorkflowTaskLeaseLoss.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\QueryException; +use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\Log; use InvalidArgumentException; use Workflow\V2\Enums\TaskStatus; @@ -359,10 +360,15 @@ private static function hasColumn(Model $model, string $column, bool $fresh = fa { $key = $model->getConnectionName() . '|' . $model->getTable() . '|' . $column; $cached = self::$schemas[$key] ?? null; - $now = time(); + // The framework clock, so a frozen test clock holds the window. + $now = Date::now()->getTimestamp(); - if (! $fresh && $cached !== null && $now - $cached['checked_at'] < self::SCHEMA_RECHECK_SECONDS) { - return $cached['tracked']; + if (! $fresh && $cached !== null) { + $elapsed = $now - $cached['checked_at']; + + if ($elapsed >= 0 && $elapsed < self::SCHEMA_RECHECK_SECONDS) { + return $cached['tracked']; + } } $tracked = $model->getConnection() diff --git a/tests/Feature/V2/V2WorkflowTaskLostLeaseTest.php b/tests/Feature/V2/V2WorkflowTaskLostLeaseTest.php index bf333a217..c0c3394d5 100644 --- a/tests/Feature/V2/V2WorkflowTaskLostLeaseTest.php +++ b/tests/Feature/V2/V2WorkflowTaskLostLeaseTest.php @@ -344,6 +344,37 @@ public function testASummaryColumnRollbackDegradesSummaryWrites(): void } } + public function testTheSchemaCheckWindowFollowsTheFrameworkClock(): void + { + $this->travelTo(now()->startOfSecond()); + $this->assertTrue(WorkflowTaskLeaseLoss::summaryTracked(true)); + $queries = 0; + DB::listen(static function () use (&$queries): void { + $queries++; + }); + + // A frozen clock holds the cached check however long the test runs. + $this->travel(59) + ->seconds(); + $this->assertTrue(WorkflowTaskLeaseLoss::summaryTracked()); + $this->assertSame(0, $queries); + + $this->travel(1) + ->seconds(); + $this->assertTrue(WorkflowTaskLeaseLoss::summaryTracked()); + $this->assertGreaterThan(0, $queries); + + // A clock moved backwards re-checks once, then holds again. + $queries = 0; + $this->travel(-120) + ->seconds(); + $this->assertTrue(WorkflowTaskLeaseLoss::summaryTracked()); + $this->assertGreaterThan(0, $queries); + $queries = 0; + $this->assertTrue(WorkflowTaskLeaseLoss::summaryTracked()); + $this->assertSame(0, $queries); + } + protected function startQueueWorkersAutomatically(): bool { return false;