From 359648943eebc104bbfbfebfb5b0358bffd82ab2 Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Wed, 4 Nov 2026 21:31:03 -0500 Subject: [PATCH] Synchronize callback fixture readiness on a native thread Signed-off-by: Vincent Taverna --- Tests/ParcelAppTests/AppCallbackTests.swift | 66 +++++++++++++++------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/Tests/ParcelAppTests/AppCallbackTests.swift b/Tests/ParcelAppTests/AppCallbackTests.swift index c64044e1..3fd2f901 100644 --- a/Tests/ParcelAppTests/AppCallbackTests.swift +++ b/Tests/ParcelAppTests/AppCallbackTests.swift @@ -10,6 +10,7 @@ import Testing @testable import ParcelTestSupport @MainActor +@Suite(.timeLimit(.minutes(1))) struct AppCallbackTests { private func invocation( _ arguments: [String], event: CallbackEvent = .windowOpened, environment: [String: String] = [:] @@ -189,25 +190,51 @@ struct AppCallbackTests { private nonisolated func callbackContents( _ file: URL, ready: @escaping @Sendable (String) -> Bool ) async throws -> String { - // Fixture readiness is not a process deadline, and must not depend on main-actor polling. - let task = Task.detached { - let deadline = ContinuousClock.now.advanced(by: .seconds(30)) - while true { - try Task.checkCancellation() - if FileManager.default.fileExists(atPath: file.path) { - let contents = try String(contentsOf: file, encoding: .utf8) - if ready(contents) { return contents } - } - guard ContinuousClock.now < deadline else { - throw E2EError(description: "Timed out waiting for callback fixture output") + // Fixture progress must not race executor scheduling; the suite watchdog owns the hang guard. + let cancelled = Mutex(false) + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { continuation in + let reader = Thread { + do { + while true { + if cancelled.withLock({ $0 }) { throw CancellationError() } + if FileManager.default.fileExists(atPath: file.path) { + let contents = try String(contentsOf: file, encoding: .utf8) + if ready(contents) { + continuation.resume(returning: contents) + return + } + } + Thread.sleep(forTimeInterval: 0.02) + } + } catch { continuation.resume(throwing: error) } } - try await Task.sleep(for: .milliseconds(20)) + reader.name = "Parcel callback output fixture" + reader.start() } - } - return try await withTaskCancellationHandler { - try await task.value } onCancel: { + cancelled.withLock { $0 = true } + } + } + + @Test func fixtureReadinessWaitCancelsAfterRegistrationAndAllowsANewRead() async throws { + try await withDirectory { directory in + let file = directory.appendingPathComponent("output") + try Data("waiting".utf8).write(to: file) + let registered = AsyncStream.makeStream() + let task = Task { + try await callbackContents(file) { contents in + registered.continuation.yield(()) + return contents == "ready" + } + } + defer { task.cancel() } + var registrations = registered.stream.makeAsyncIterator() + try #require(await registrations.next() != nil) task.cancel() + await #expect(throws: CancellationError.self) { try await task.value } + try Data("ready".utf8).write(to: file) + #expect(try await callbackContents(file) { $0 == "ready" } == "ready") } } @@ -954,7 +981,7 @@ struct AppCallbackTests { func advance(by duration: Duration) { instant.withLock { $0 = $0.advanced(by: duration) } } } - @Test(.timeLimit(.minutes(1))) + @Test func advancingInjectedCallbackTimeToTheDeadlineKillsTheRealForkedGroup() async throws { try await withDirectory { directory in let started = directory.appendingPathComponent("started") @@ -999,7 +1026,7 @@ struct AppCallbackTests { } } - @Test(.timeLimit(.minutes(1))) + @Test func overflowFeedbackArrivesWhileTheActiveCallbackIsStillBlocked() async throws { try await withDirectory { directory in let started = directory.appendingPathComponent("started") @@ -1041,7 +1068,7 @@ struct AppCallbackTests { } } - @Test(.timeLimit(.minutes(1))) + @Test func stoppingKillsForkedChildrenDiscardsQueuedWorkAndAllowsRestart() async throws { try await withDirectory { directory in let started = directory.appendingPathComponent("started") @@ -1114,11 +1141,12 @@ struct AppCallbackTests { let space = write("space") let replacement = write("new") var failures: [String] = [] + let clock = CallbackTestTiming() let runner = AppCallbackRunner( configuration: CallbackConfiguration(commands: [ .windowOpened: active, .windowClosed: removed, .focusChanged: edited, .layoutChanged: layout, .spaceChanged: space, - ]) + ]), timing: clock.timing ) { failures.append($0.localizedDescription) } runner.submit(invocation(active)) do { -- knotmirror 0.0.1