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")