// RTBufferSlotTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("RTBufferSlot") struct RTBufferSlotTests { private func makeEngine() -> SynthesisEngine { SynthesisEngine( blockSize: 512, sampleRate: 48000, numOutputChannels: 2, numInputChannels: 0, driver: OfflineDriver() ) } /// Synchronously drain `engine.bufferSwapKillQueue` to release displaced buffers /// without waiting on the background timer. Mirrors `drainBufferSwapKillQueue`. private func drain(_ engine: SynthesisEngine) { engine.drainBufferSwapKillQueue() } // MARK: - Construction @Test func initWithNilLoadsToNil() { let engine: SynthesisEngine = makeEngine() let slot: RTBufferSlot = RTBufferSlot(initial: nil) #expect(slot.load(engine: engine) == nil) } @Test func initWithBufferLoadsToSameInstance() { let engine: SynthesisEngine = makeEngine() let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: buffer) #expect(slot.load(engine: engine) === buffer) } // MARK: - Swap @Test func swapReplacesBufferAfterLoad() { let engine: SynthesisEngine = makeEngine() let first: LyrebirdBuffer = LyrebirdBuffer(count: 16) let second: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: first) slot.swap(second, engine: engine) // load() drains the pending swap; subsequent reads see the new buffer. #expect(slot.load(engine: engine) === second) drain(engine) } @Test func swapToNilEmptiesSlotAfterLoad() { let engine: SynthesisEngine = makeEngine() let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: buffer) slot.swap(nil, engine: engine) #expect(slot.load(engine: engine) == nil) drain(engine) } @Test func swapFromEmptyToBufferInstalls() { let engine: SynthesisEngine = makeEngine() let slot: RTBufferSlot = RTBufferSlot(initial: nil) let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) slot.swap(buffer, engine: engine) #expect(slot.load(engine: engine) === buffer) drain(engine) } @Test func swapPreservesContentVisibility() { let engine: SynthesisEngine = makeEngine() let next: LyrebirdBuffer = LyrebirdBuffer(count: 8) next.fill(with: 0.42) let slot: RTBufferSlot = RTBufferSlot(initial: nil) slot.swap(next, engine: engine) guard let loaded: LyrebirdBuffer = slot.load(engine: engine) else { Issue.record("Loaded buffer should be non-nil") return } for i: Int in 0..<8 { #expect(loaded[i] == 0.42, "Sample \(i) should be 0.42 after swap, got \(loaded[i])") } drain(engine) } // MARK: - Memory handoff @Test func loadAfterSwapEnqueuesPreviousBufferToKillQueue() { let engine: SynthesisEngine = makeEngine() let first: LyrebirdBuffer = LyrebirdBuffer(count: 16) let second: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: first) slot.swap(second, engine: engine) _ = slot.load(engine: engine) // drain pending; pushes `first` to bufferSwapKillQueue let count: Int = engine.drainBufferSwapKillQueue() #expect(count == 1, "Exactly one reference should be enqueued, got \(count)") } @Test func loadAfterSwapToNilEnqueuesPreviousBuffer() { let engine: SynthesisEngine = makeEngine() let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: buffer) slot.swap(nil, engine: engine) _ = slot.load(engine: engine) let count: Int = engine.drainBufferSwapKillQueue() #expect(count == 1) } @Test func loadAfterSwapFromEmptyDoesNotEnqueue() { let engine: SynthesisEngine = makeEngine() let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: nil) slot.swap(buffer, engine: engine) _ = slot.load(engine: engine) #expect(engine.drainBufferSwapKillQueue() == 0) } @Test func previousBufferDeallocatesAfterDrain() { let engine: SynthesisEngine = makeEngine() weak var weakRef: LyrebirdBuffer? let slot: RTBufferSlot = RTBufferSlot(initial: nil) do { let first: LyrebirdBuffer = LyrebirdBuffer(count: 16) weakRef = first slot.swap(first, engine: engine) _ = slot.load(engine: engine) // `first` leaves scope. The slot holds the only remaining strong ref. } #expect(weakRef != nil, "Slot must keep buffer alive while installed") slot.swap(nil, engine: engine) _ = slot.load(engine: engine) // drain pending; pushes `first` to kill queue #expect(weakRef != nil, "Buffer ref enqueued but not yet released") drain(engine) #expect(weakRef == nil, "Buffer should be deallocated after kill-queue drain") } @Test func slotDeinitReleasesHeldBuffer() { weak var weakRef: LyrebirdBuffer? do { let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16) weakRef = buffer _ = RTBufferSlot(initial: buffer) // Slot leaves scope. Slot's deinit must release the +1 retain. } #expect(weakRef == nil, "Slot deinit should release the retained buffer") } @Test func slotDeinitDrainsUnconsumedPendingEntries() { let engine: SynthesisEngine = makeEngine() weak var weakRef: LyrebirdBuffer? do { let slot: RTBufferSlot = RTBufferSlot(initial: nil) let pending: LyrebirdBuffer = LyrebirdBuffer(count: 16) weakRef = pending slot.swap(pending, engine: engine) // slot.load() never called — pending entry must be released by deinit. } #expect(weakRef == nil, "Pending swap entry should be released when slot deinits without load") } @Test func multipleSwapsAllDrainThroughKillQueueAfterLoad() { let engine: SynthesisEngine = makeEngine() let bufA: LyrebirdBuffer = LyrebirdBuffer(count: 16) let bufB: LyrebirdBuffer = LyrebirdBuffer(count: 16) let bufC: LyrebirdBuffer = LyrebirdBuffer(count: 16) let slot: RTBufferSlot = RTBufferSlot(initial: bufA) slot.swap(bufB, engine: engine) // pending: B slot.swap(bufC, engine: engine) // pending: B, C slot.swap(nil, engine: engine) // pending: B, C, nil _ = slot.load(engine: engine) // drains pending — A, B, C all displaced let count: Int = engine.drainBufferSwapKillQueue() #expect(count == 3, "Three previous-buffer refs expected, got \(count)") #expect(slot.load(engine: engine) == nil) } }