Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
// OSCLoopbackTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
#if canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#endif
|
||||
|
||||
// End-to-end OSC over real UDP loopback: an `OSCServer` bound to 127.0.0.1 and an
|
||||
// `OSCClient` sending to it. On Apple this drives `Network.framework`; off Apple
|
||||
// it drives the POSIX-socket transport added for Linux — and, unlike the MIDI and
|
||||
// audio backends, needs no hardware, so it runs everywhere and is the actual
|
||||
// acceptance check for OSC on Linux.
|
||||
@MainActor
|
||||
@Suite("OSC UDP loopback")
|
||||
struct OSCLoopbackTests {
|
||||
|
||||
private func makeEngine() -> SynthesisEngine {
|
||||
SynthesisEngine(
|
||||
blockSize: 64, sampleRate: 44100,
|
||||
numOutputChannels: 2, numInputChannels: 0,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
}
|
||||
|
||||
/// Bind a server on some free port in a high range, retrying past a port
|
||||
/// already in use. Returns the started server and its port.
|
||||
private func startServer(
|
||||
_ engine: SynthesisEngine,
|
||||
configure: (OSCServer) -> Void
|
||||
) throws -> OSCServer {
|
||||
var lastError: (any Error)?
|
||||
for candidate: UInt16 in stride(from: UInt16(9800), through: UInt16(9840), by: 1) {
|
||||
let server: OSCServer = OSCServer(port: candidate, engine: engine)
|
||||
configure(server)
|
||||
do {
|
||||
try server.start()
|
||||
return server
|
||||
} catch {
|
||||
lastError = error // port in use — try the next
|
||||
}
|
||||
}
|
||||
throw lastError ?? OSCTestError.noFreePort
|
||||
}
|
||||
|
||||
private enum OSCTestError: Error { case noFreePort }
|
||||
|
||||
/// Fire the client repeatedly until the handler signals or the deadline
|
||||
/// passes — covers both the listener-not-yet-ready startup race and UDP's
|
||||
/// own permission to drop a datagram.
|
||||
private func waitForDelivery(
|
||||
send: () -> Void,
|
||||
arrived: DispatchSemaphore,
|
||||
timeout: TimeInterval = 3.0
|
||||
) -> Bool {
|
||||
let deadline: Date = Date(timeIntervalSinceNow: timeout)
|
||||
while Date() < deadline {
|
||||
send()
|
||||
if arrived.wait(timeout: .now() + 0.05) == .success { return true }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@Test func messageRoundTripsWithArguments() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let received: LockedBox = LockedBox()
|
||||
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
s.addHandler(address: "/synth/freq") { msg, _ in
|
||||
received.set(address: msg.address,
|
||||
int: msg[0]?.intValue,
|
||||
float: msg[1]?.floatValue,
|
||||
string: msg[2]?.stringValue)
|
||||
arrived.signal()
|
||||
}
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
let delivered: Bool = waitForDelivery(
|
||||
send: {
|
||||
client.send(address: "/synth/freq",
|
||||
args: [.int32(440), .float32(1.5), .string("hi")])
|
||||
},
|
||||
arrived: arrived
|
||||
)
|
||||
|
||||
#expect(delivered, "the message never arrived over loopback")
|
||||
#expect(received.address == "/synth/freq")
|
||||
#expect(received.int == 440)
|
||||
#expect(received.float == 1.5)
|
||||
#expect(received.string == "hi")
|
||||
}
|
||||
|
||||
@Test func patternAddressMatchesOnDelivery() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let hitCount: LockedCounter = LockedCounter()
|
||||
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
// Glob pattern: should match "/synth/cutoff".
|
||||
s.addHandler(address: "/synth/*") { _, _ in
|
||||
hitCount.increment()
|
||||
arrived.signal()
|
||||
}
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
let delivered: Bool = waitForDelivery(
|
||||
send: { client.send(address: "/synth/cutoff", args: [.float32(0.5)]) },
|
||||
arrived: arrived
|
||||
)
|
||||
#expect(delivered, "the wildcard handler never fired")
|
||||
#expect(hitCount.value >= 1)
|
||||
}
|
||||
|
||||
@Test func removedHandlerStopsReceiving() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let hits: LockedCounter = LockedCounter()
|
||||
|
||||
var token: OSCHandlerToken?
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
token = s.addHandler(address: "/x") { _, _ in
|
||||
hits.increment()
|
||||
arrived.signal()
|
||||
}
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
// Confirm the handler works, then remove it and confirm silence.
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
#expect(waitForDelivery(send: { client.send(address: "/x") }, arrived: arrived),
|
||||
"handler should fire before removal")
|
||||
if let token: OSCHandlerToken = token { server.removeHandler(token) }
|
||||
|
||||
let countAfterRemoval: Int = hits.value
|
||||
// Send several more; none should reach the removed handler.
|
||||
for _ in 0..<10 { client.send(address: "/x") }
|
||||
Thread.sleep(forTimeInterval: 0.3)
|
||||
#expect(hits.value == countAfterRemoval, "a removed handler must not keep receiving")
|
||||
}
|
||||
|
||||
// The POSIX receive loop must survive a zero-length UDP datagram — a valid
|
||||
// packet any peer can send. The bug this guards (recv==0 treated as a close,
|
||||
// killing the listener) lived only in the non-Network transport, so the test
|
||||
// does too. On Apple the empty datagram is filtered by the `!data.isEmpty`
|
||||
// guard in the NWListener path.
|
||||
#if !canImport(Network)
|
||||
@Test func emptyDatagramDoesNotKillListener() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
s.addHandler(address: "/ping") { _, _ in arrived.signal() }
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
// Fire a raw zero-length datagram straight at the port.
|
||||
let raw: Int32 = socket(AF_INET, Int32(SOCK_DGRAM.rawValue), 0)
|
||||
#expect(raw >= 0)
|
||||
if raw >= 0 {
|
||||
var addr: sockaddr_in = sockaddr_in()
|
||||
addr.sin_family = sa_family_t(AF_INET)
|
||||
addr.sin_port = server.port.bigEndian
|
||||
_ = inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr)
|
||||
_ = withUnsafePointer(to: &addr) { ptr in
|
||||
ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) { sa in
|
||||
connect(raw, sa, socklen_t(MemoryLayout<sockaddr_in>.size))
|
||||
}
|
||||
}
|
||||
// A couple of empty datagrams — the old code broke on the first.
|
||||
for _ in 0..<3 { _ = sendto(raw, nil, 0, Int32(MSG_NOSIGNAL), nil, 0) }
|
||||
close(raw)
|
||||
}
|
||||
|
||||
// The listener must still be alive: a normal message gets through.
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
let delivered: Bool = waitForDelivery(
|
||||
send: { client.send(address: "/ping") },
|
||||
arrived: arrived
|
||||
)
|
||||
#expect(delivered, "an empty datagram killed the listener")
|
||||
}
|
||||
#endif
|
||||
|
||||
@Test func doubleStartIsIdempotent() throws {
|
||||
// Calling start() on an already-started server returns without binding a
|
||||
// second socket or launching a second thread, and the server keeps
|
||||
// working. On Apple this is NWListener's duplicate-cancel; off Apple it
|
||||
// is the atomic `isStarted` claim.
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
s.addHandler(address: "/twice") { _, _ in arrived.signal() }
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
try server.start() // second start — must be a clean no-op
|
||||
try server.start() // and a third
|
||||
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
#expect(waitForDelivery(send: { client.send(address: "/twice") }, arrived: arrived),
|
||||
"server should still deliver after redundant start() calls")
|
||||
}
|
||||
|
||||
// Linux-only: the atomic `isStarted` claim being hardened lives in the POSIX
|
||||
// branch. On Apple, `NWListener` reports a bind failure asynchronously, so a
|
||||
// fixed-port concurrent-start test cannot assert deterministically there —
|
||||
// and that path was already duplicate-safe via NWListener's own cancel.
|
||||
#if !canImport(Network)
|
||||
@Test func concurrentStartsAreSerializedByTheClaim() throws {
|
||||
// Start the server on a known-free port, then fire many start() calls at
|
||||
// it concurrently. Each must return cleanly (the claim sees `isStarted`
|
||||
// already set) — no second bind, no second thread, no crash — and the
|
||||
// server must keep delivering.
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let arrived: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let server: OSCServer = try startServer(engine) { s in
|
||||
s.addHandler(address: "/race") { _, _ in arrived.signal() }
|
||||
}
|
||||
defer { server.stop() }
|
||||
|
||||
let group: DispatchGroup = DispatchGroup()
|
||||
let failures: LockedCounter = LockedCounter()
|
||||
for _ in 0..<8 {
|
||||
DispatchQueue.global().async(group: group) {
|
||||
do { try server.start() } catch { failures.increment() }
|
||||
}
|
||||
}
|
||||
group.wait()
|
||||
#expect(failures.value == 0, "\(failures.value) redundant concurrent start()s threw")
|
||||
|
||||
let client: OSCClient = OSCClient(host: "127.0.0.1", port: server.port)
|
||||
#expect(waitForDelivery(send: { client.send(address: "/race") }, arrived: arrived),
|
||||
"server should still deliver after concurrent start() calls")
|
||||
}
|
||||
#endif
|
||||
|
||||
@Test func stopIsIdempotentAndSafeBeforeStart() {
|
||||
let server: OSCServer = OSCServer(port: 9899, engine: makeEngine())
|
||||
server.stop()
|
||||
server.stop()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Small thread-safe test boxes
|
||||
|
||||
private final class LockedBox: @unchecked Sendable {
|
||||
private let lock: NSLock = NSLock()
|
||||
private var _address: String?
|
||||
private var _int: Int32?
|
||||
private var _float: Float?
|
||||
private var _string: String?
|
||||
|
||||
func set(address: String?, int: Int32?, float: Float?, string: String?) {
|
||||
lock.withLock { _address = address; _int = int; _float = float; _string = string }
|
||||
}
|
||||
var address: String? { lock.withLock { _address } }
|
||||
var int: Int32? { lock.withLock { _int } }
|
||||
var float: Float? { lock.withLock { _float } }
|
||||
var string: String? { lock.withLock { _string } }
|
||||
}
|
||||
|
||||
private final class LockedCounter: @unchecked Sendable {
|
||||
private let lock: NSLock = NSLock()
|
||||
private var _value: Int = 0
|
||||
func increment() { lock.withLock { _value += 1 } }
|
||||
var value: Int { lock.withLock { _value } }
|
||||
}
|
||||
Reference in New Issue
Block a user