From d10162108d713a3506a18a9b946a793f7627ef3f Mon Sep 17 00:00:00 2001 From: Amp Date: Tue, 1 Dec 2026 06:39:59 +0000 Subject: [PATCH] test(zero-swift): make the lifecycle tests' process connect once before the first test AppLifecycleTests.hiddenDisconnectsAndShownReconnects (and once hiddenWhileConnecting) failed in the iOS Simulator step of "Swift end-to-end (macOS)": the first connect never reached the test's server within connectWait. On CI the step runs while the simulator's first boot keeps the 3-core runner's load average at 150-470 for minutes. A process's first URLSession connect waits, before its socket opens, on what the system's services (cfprefsd, containermanagerd, runningboardd, configd_sim) set up once per process, and the simulator's unified log on CI showed that taking from about a second to about 30 s. The process's later connects don't wait for it: the second test took 0.15-0.25 s in 37 of 38 probe runs. Waiting for the simulator's boot to finish, stopping zero-cache and Postgres, or a job of its own didn't take the first connect's delay away. The suite now makes the process's first connect once, before its first test, and the tests' waits start after it. URLSession's request timeout (60 s) bounds it, within the first test's 2-minute time limit. Stopping the simulator's configd_sim for the first 30 s of the test process failed main 3 of 3 times (the first test at 12.5 s) and passes now 3 of 3, with a 45 s stop too. Amp-Thread-ID: https://ampcode.com/threads/T-01a22e8b-3956-7364-87ad-1c52739ad938 Co-authored-by: tmm --- .../Tests/AppleTests/AppLifecycleTests.swift | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/swift/Tests/AppleTests/AppLifecycleTests.swift b/swift/Tests/AppleTests/AppLifecycleTests.swift index e5b4697cda..981609a6d4 100644 --- a/swift/Tests/AppleTests/AppLifecycleTests.swift +++ b/swift/Tests/AppleTests/AppLifecycleTests.swift @@ -73,9 +73,27 @@ } let log = RecordingLogSink() - let stops = Stops() + let stops: Stops let connectTimeouts = ConnectTimeouts() + /// Each test starts once the process has made its first connect (`firstConnect`), and counts the stops from there. + init() async throws { + try await Self.firstConnect.value + stops = Stops() + } + + /// The process's first URLSession connect, made once, before the first test. Before its socket opens, it waits on the system's services (cfprefsd, containermanagerd, runningboardd, configd_sim) for what a process sets up once. In the iOS Simulator on CI, while the simulator's first boot keeps the runner busy, that has taken from about a second to about 30 s, which the tests' waits for a connection (`connectWait`) would count; the process's later connects don't wait for it. URLSession's request timeout (60 s) bounds it, within the first test's time limit; when it fails, every test fails with its error. + static let firstConnect = Task { + let server = try WebSocketServer() + defer { server.stop() } + let port = try await server.start() + let start = ContinuousClock.now + let socket = try await URLSessionTransport().connect( + url: "ws://127.0.0.1:\(port)/", secProtocol: nil) + print("[lifecycle] the process's first connect took \(ContinuousClock.now - start)") + await socket.close(code: 1000, reason: "") + } + /// Whether the process was stopped during the test: a timer on a queue of its own came back more than a second late, as `within`'s polls do. final class Stops: @unchecked Sendable { private let queue = DispatchQueue(label: "AppLifecycleTests.Stops")