// MultiEngineRegressionTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Multi-engine regression coverage. /// /// Validates the ownership-binding + lifecycle-coordinator /// machinery against the two scenarios that motivated it: /// /// 1. **Two engines, two threads, disjoint Synth graphs** — drives both /// engines for ≥ 1000 segments each in parallel. Acts as a /// deterministic repro target for the deferred parallel-`swift test` /// crash (`_ContiguousArrayStorage retain count 2` at xctest teardown); /// if RetainQueue / coordinator wiring is correct, this passes /// cleanly under `--no-parallel` (single test process, two threads /// inside). /// /// 2. **Binding precondition coverage** — `engine.add` throws /// `.boundToOtherEngine` for cross-engine sharing, `.doubleAdd` for /// same-engine re-add without intervening `remove`, and accepts /// re-add after `remove` clears the binding. @MainActor @Suite("Multi-engine regression") struct MultiEngineRegressionTests { // MARK: - Two-engine, two-thread driver /// Drives two engines concurrently on dedicated threads, each with /// its own disjoint Synth graph, for `segmentCount` processBlock /// iterations. Joins both threads, stops both engines, and asserts /// neither crashed nor produced obvious miscount. /// /// `segmentCount = 1000` matches the plan exit criterion. /// Adjust upward for stress runs; downward to keep CI fast. @Test func twoEnginesTwoThreadsDisjointGraphs() throws { let segmentCount: Int = 1000 let engineA: SynthesisEngine = makeTestEngine( blockSize: 64, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, eventQueueCapacity: 64 ) let engineB: SynthesisEngine = makeTestEngine( blockSize: 64, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, eventQueueCapacity: 64 ) // Distinct UUIDs — confirms identifier-per-instance contract. #expect(engineA.identifier != engineB.identifier) // Both engines must be visible in the lifecycle registry. // Note: other tests in the same process may have left engines // registered; only assert both A and B appear. let registered: [SynthesisEngine] = EngineLifecycleCoordinator.shared.engines #expect(registered.contains { $0 === engineA }) #expect(registered.contains { $0 === engineB }) try engineA.start() try engineB.start() // Disjoint Synth graphs — each engine gets its own synth instance. // Distinct bus indices keep the language-side bus assignment unambiguous. let synthA: ConstantSynth = ConstantSynth(value: 0.5, outputBus: 0) let synthB: ConstantSynth = ConstantSynth(value: 0.25, outputBus: 0) try engineA.add(synthA) try engineB.add(synthB) let group: DispatchGroup = DispatchGroup() let queueA: DispatchQueue = DispatchQueue(label: "lyrebird.test.engineA") let queueB: DispatchQueue = DispatchQueue(label: "lyrebird.test.engineB") // Run both processBlock loops on dedicated background queues so // the segments interleave on two distinct OS threads. Capture // engines by reference inside the closures. let engineARef: SynthesisEngine = engineA let engineBRef: SynthesisEngine = engineB queueA.async(group: group) { for _ in 0..