Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Tests/MixerLinkTests/MixerLinkEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })
Expand All @@ -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
49 changes: 35 additions & 14 deletions Tests/MixerLinkTests/Support/FakeXAirMixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt16>.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..<listenTries {
let parameters = NWParameters.udp
parameters.requiredLocalEndpoint = .hostPort(host: "127.0.0.1", port: port)
parameters.allowLocalEndpointReuse = true
let listener = try NWListener(using: parameters)
let started = AsyncStream<Result<UInt16, NWError>>.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() {
Expand Down
Loading