From 74bb8f49cef67c21445e8e8f0cc8038a0254f99a Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Wed, 3 Dec 2025 10:22:09 +0100 Subject: [PATCH] Fix: Correct time inconsistency in ServerStorageCheckIndependenceTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move Carbon::setTestNow() to the beginning of each test before creating test data. Previously, tests created servers using now() (real current time) and only afterwards called Carbon::setTestNow(), making sentinel_updated_at inconsistent with the test clock. This caused staleness calculations to use different timelines: - sentinel_updated_at was based on real time (e.g., Dec 2024) - Test execution time was frozen at 2025-01-15 Now all timestamps use the same frozen test time, making staleness checks predictable and tests reliable regardless of when they run. Affected tests (all 7 test cases in the file): - does not dispatch storage check when sentinel is in sync - dispatches storage check when sentinel is out of sync - dispatches storage check when sentinel is disabled - respects custom hourly storage check frequency when sentinel is out of sync - handles VALID_CRON_STRINGS mapping correctly when sentinel is out of sync - respects server timezone for storage checks when sentinel is out of sync - does not dispatch storage check outside schedule 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../ServerStorageCheckIndependenceTest.php | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/Feature/ServerStorageCheckIndependenceTest.php b/tests/Feature/ServerStorageCheckIndependenceTest.php index d5b8b79f6..57b392e2f 100644 --- a/tests/Feature/ServerStorageCheckIndependenceTest.php +++ b/tests/Feature/ServerStorageCheckIndependenceTest.php @@ -20,6 +20,9 @@ }); it('does not dispatch storage check when sentinel is in sync', function () { + // When: ServerManagerJob runs at 11 PM + Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); + // Given: A server with Sentinel recently updated (in sync) $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -31,9 +34,6 @@ 'server_disk_usage_check_frequency' => '0 23 * * *', 'server_timezone' => 'UTC', ]); - - // When: ServerManagerJob runs at 11 PM - Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -42,6 +42,9 @@ }); it('dispatches storage check when sentinel is out of sync', function () { + // When: ServerManagerJob runs at 11 PM + Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); + // Given: A server with Sentinel out of sync (last update 10 minutes ago) $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -53,9 +56,6 @@ 'server_disk_usage_check_frequency' => '0 23 * * *', 'server_timezone' => 'UTC', ]); - - // When: ServerManagerJob runs at 11 PM - Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -67,6 +67,9 @@ }); it('dispatches storage check when sentinel is disabled', function () { + // When: ServerManagerJob runs at 11 PM + Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); + // Given: A server with Sentinel disabled $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -79,9 +82,6 @@ 'server_timezone' => 'UTC', 'is_metrics_enabled' => false, ]); - - // When: ServerManagerJob runs at 11 PM - Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -92,6 +92,9 @@ }); it('respects custom hourly storage check frequency when sentinel is out of sync', function () { + // When: ServerManagerJob runs at the top of the hour (23:00) + Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); + // Given: A server with hourly storage check frequency and Sentinel out of sync $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -103,9 +106,6 @@ 'server_disk_usage_check_frequency' => '0 * * * *', 'server_timezone' => 'UTC', ]); - - // When: ServerManagerJob runs at the top of the hour (23:00) - Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -116,6 +116,9 @@ }); it('handles VALID_CRON_STRINGS mapping correctly when sentinel is out of sync', function () { + // When: ServerManagerJob runs at the top of the hour + Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); + // Given: A server with 'hourly' string (should be converted to '0 * * * *') and Sentinel out of sync $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -127,9 +130,6 @@ 'server_disk_usage_check_frequency' => 'hourly', 'server_timezone' => 'UTC', ]); - - // When: ServerManagerJob runs at the top of the hour - Carbon::setTestNow(Carbon::parse('2025-01-15 23:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -140,6 +140,9 @@ }); it('respects server timezone for storage checks when sentinel is out of sync', function () { + // When: ServerManagerJob runs at 11 PM New York time (4 AM UTC next day) + Carbon::setTestNow(Carbon::parse('2025-01-16 04:00:00', 'UTC')); + // Given: A server in America/New_York timezone (UTC-5) configured for 11 PM local time and Sentinel out of sync $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -151,9 +154,6 @@ 'server_disk_usage_check_frequency' => '0 23 * * *', 'server_timezone' => 'America/New_York', ]); - - // When: ServerManagerJob runs at 11 PM New York time (4 AM UTC next day) - Carbon::setTestNow(Carbon::parse('2025-01-16 04:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle(); @@ -164,6 +164,9 @@ }); it('does not dispatch storage check outside schedule', function () { + // When: ServerManagerJob runs at 10 PM (not 11 PM) + Carbon::setTestNow(Carbon::parse('2025-01-15 22:00:00', 'UTC')); + // Given: A server with daily storage check at 11 PM $team = Team::factory()->create(); $server = Server::factory()->create([ @@ -175,9 +178,6 @@ 'server_disk_usage_check_frequency' => '0 23 * * *', 'server_timezone' => 'UTC', ]); - - // When: ServerManagerJob runs at 10 PM (not 11 PM) - Carbon::setTestNow(Carbon::parse('2025-01-15 22:00:00', 'UTC')); $job = new ServerManagerJob; $job->handle();