From dcecf0cb68c161447c5794f61a4cc2ea98ed57d6 Mon Sep 17 00:00:00 2001 From: Rick Newton-Rogers Date: Fri, 13 Mar 2026 17:27:45 +0000 Subject: [PATCH] Fix flakey `testScheduledCallbackNotExecutedBeforeDeadline` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation * `MTELGScheduledCallbackTests.testScheduledCallbackNotExecutedBeforeDeadline` is flakey on macOS CI. It schedules a callback with a 1ms deadline, sleeps for 1µs via `Task.sleep`, then asserts the callback hasn't fired. On a loaded CI runner, `Task.sleep` can overshoot well past 1ms, causing the callback to fire before the assertion. Modifications * Increase the scheduled callback deadline from `.milliseconds(1)` to `.hours(1)` in `_BaseScheduledCallbackTests`. This makes it impossible for the short sleep to overshoot past the deadline. The `EmbeddedEventLoop` and `NIOAsyncTestingEventLoop` subclasses use virtual time so this change is harmless to them. Result * The test reliably asserts that a callback is not executed before its deadline without depending on real-time sleep precision. --- Tests/NIOPosixTests/NIOScheduledCallbackTests.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/NIOPosixTests/NIOScheduledCallbackTests.swift b/Tests/NIOPosixTests/NIOScheduledCallbackTests.swift index 0a97beba0..13cf0dc6e 100644 --- a/Tests/NIOPosixTests/NIOScheduledCallbackTests.swift +++ b/Tests/NIOPosixTests/NIOScheduledCallbackTests.swift @@ -241,7 +241,9 @@ extension _BaseScheduledCallbackTests { func testScheduledCallbackNotExecutedBeforeDeadline() async throws { let handler = MockScheduledCallbackHandler() - _ = try self.loop.scheduleCallback(in: .milliseconds(1), handler: handler) + // Use a long deadline so that even if the short sleep overshoots on a loaded CI machine, + // it cannot reach the callback deadline. + _ = try self.loop.scheduleCallback(in: .hours(1), handler: handler) handler.assert(callbackCount: 0, cancelCount: 0) try await self.advanceTime(by: .microseconds(1))