From 9fe4c867060ca33d3ed4d182bc0404ebc784ee44 Mon Sep 17 00:00:00 2001 From: Eric Dahl Date: Thu, 3 Dec 2026 02:41:14 -0500 Subject: [PATCH] Fake X-Air: a failed listen is tried again instead of waited on forever (ShowRecorder #1215) After a power-off the fake Mixer listens on the same port again. When a Link is still asking, that first listen often fails with EADDRINUSE, and the fake only watched for .ready, so powerOn() never returned: the power-cycle test hung and the package tests ran into the CI time limit (ShowRecorder run 43502838420). The test normally waits a timeout for the Link to notice the power-off, which hid this. When its first connect failed on a busy runner, the Link was already down, the power-on followed the power-off at once, and the listen failed. - The fake tries a failed listen again, 25 ms apart, and throws after 10 s instead of waiting forever. - The power-cycle test waits for the Link to come up after connect: a connect that fails is retried by the Link itself (#1209). - A new test power-cycles the fake at once while the Link keeps trying. Test code only. Refs ericdahl-dev/ShowRecorder#1215 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01VCgWWseQZtUzcSRzjqqQJQ --- .../MixerLinkEndToEndTests.swift | 29 +++++++++-- .../Support/FakeXAirMixer.swift | 49 +++++++++++++------ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Tests/MixerLinkTests/MixerLinkEndToEndTests.swift b/Tests/MixerLinkTests/MixerLinkEndToEndTests.swift index 4b2b7e8..c95c17b 100644 --- a/Tests/MixerLinkTests/MixerLinkEndToEndTests.swift +++ b/Tests/MixerLinkTests/MixerLinkEndToEndTests.swift @@ -14,11 +14,12 @@ struct MixerLinkEndToEndTests { state.channels[0] = .init(name: "Kick", color: 1) state.usbRouting = XAirDriverTests.defaultRouting // answered, so no read waits out its timeout let mixer = try await FakeXAirMixer(state) - // Connecting asks the Mixer about 100 things one after another; each must answer in time even on a busy Mac, - // or the Link never comes up and every wait below runs out (#464). Noticing the power-off takes one timeout. + // Connecting asks the Mixer about 100 things one after another, and on a busy Mac one answer can come after the + // timeout (#464): that connect fails and the Link keeps trying (#1209). So wait for the Link to come up rather + // than check at once (ShowRecorder #1215). Noticing the power-off takes one timeout. let link = MixerLinkController(timeout: Loopback.mixedTimeout, renewEvery: .milliseconds(100)) await link.connect(to: "127.0.0.1:\(mixer.port)", usbChannelCount: 18) - #expect(link.sources[safe: 0]?.name == "Kick") + #expect(await eventually(30) { link.sources[safe: 0]?.name == "Kick" }, "\(link.status)") mixer.powerOff() #expect(await eventually(30) { if case .down = link.status { true } else { false } }) @@ -29,5 +30,27 @@ struct MixerLinkEndToEndTests { mixer.rename(channel: 2, to: "Snare") #expect(await eventually(30) { link.sources[safe: 1]?.name == "Snare" }) } + + /// With the Link already down at power-off, nothing waits between the power-off and the power-on, as when the + /// connect failed on a busy CI runner. The fake's first try to listen on its port again then often fails + /// (EADDRINUSE); it used to wait forever for that listener, and the package tests ran into their 40-minute limit + /// (ShowRecorder #1215). + @Test("Switched off and on again at once while the Link keeps trying, the Mixer answers again and the Link comes up") + func powerCycleAtOnce() async throws { + var state = FakeXAirMixer.State() + state.channels[0] = .init(name: "Kick", color: 1) + state.usbRouting = XAirDriverTests.defaultRouting + state.answers = false // the connect fails, and the Link keeps trying (#1209) + let mixer = try await FakeXAirMixer(state) + let link = MixerLinkController(timeout: .seconds(1), renewEvery: .milliseconds(100)) + await link.connect(to: "127.0.0.1:\(mixer.port)", usbChannelCount: 18) + #expect(await eventually(30) { if case .down = link.status { true } else { false } }) + + mixer.powerOff() + mixer.update { $0.answers = true } + try await mixer.powerOn() + + #expect(await eventually(30) { link.sources[safe: 0]?.name == "Kick" }, "\(link.status)") + } } #endif diff --git a/Tests/MixerLinkTests/Support/FakeXAirMixer.swift b/Tests/MixerLinkTests/Support/FakeXAirMixer.swift index 4291d79..93dba8b 100644 --- a/Tests/MixerLinkTests/Support/FakeXAirMixer.swift +++ b/Tests/MixerLinkTests/Support/FakeXAirMixer.swift @@ -117,24 +117,45 @@ final class FakeXAirMixer: Sendable { deinit { listener.withLock { $0?.cancel() } } /// Opens a loopback UDP listener on `port` that answers with `responder`, and returns it with its port. + /// + /// A listen that fails is tried again. Right after `powerOff()`, with a Link still asking, the port is often still + /// taken (EADDRINUSE) for a moment; this used to wait for a `.ready` that never came, which hung the whole test run + /// (ShowRecorder #1215). private static func listen(on port: NWEndpoint.Port, responder: Responder) async throws -> (NWListener, UInt16) { - let parameters = NWParameters.udp - parameters.requiredLocalEndpoint = .hostPort(host: "127.0.0.1", port: port) - parameters.allowLocalEndpointReuse = true - let listener = try NWListener(using: parameters) - let ready = AsyncStream.makeStream() - listener.stateUpdateHandler = { [listener] update in - if case .ready = update, let port = listener.port?.rawValue { ready.continuation.yield(port) } - } - listener.newConnectionHandler = { connection in - connection.start(queue: .global()) - responder.serve(connection) + var failure: NWError? + for _ in 0..>.makeStream() + listener.stateUpdateHandler = { [listener] update in + switch update { + case .ready: if let port = listener.port?.rawValue { started.continuation.yield(.success(port)) } + case .failed(let error), .waiting(let error): started.continuation.yield(.failure(error)) + default: break + } + } + listener.newConnectionHandler = { connection in + connection.start(queue: .global()) + responder.serve(connection) + } + listener.start(queue: .global()) + var iterator = started.stream.makeAsyncIterator() + switch await iterator.next() { + case .success(let port)?: return (listener, port) + case .failure(let error)?: failure = error + case nil: break // the test was canceled + } + listener.cancel() + try await Task.sleep(for: .milliseconds(25)) } - listener.start(queue: .global()) - var iterator = ready.stream.makeAsyncIterator() - return (listener, await iterator.next() ?? 0) + throw failure ?? NWError.posix(.ETIMEDOUT) } + /// How many times a listen is tried, 25 ms apart: 10 s in all. One more try was enough in every run seen. + private static let listenTries = 400 + /// Switches the Mixer off, as a power cycle does: nothing listens on its port any more. (On loopback macOS doesn't /// report the bounce to the Link's connection, so this doesn't make it fail; `OSCUDPClientRecoveryTests` covers that.) func powerOff() {