Files
Lyrebird/Tests/LyrebirdTests/MaxEventsPerBlockTests.swift
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

107 lines
4.1 KiB
Swift

// MaxEventsPerBlockTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
/// Verifies the configurable per-block scheduling cap governs the future-event
/// look-ahead depth carried across block boundaries.
@MainActor
@Suite("maxEventsPerBlock look-ahead cap") struct MaxEventsPerBlockTests {
private static let blockSize: Int = 64
// Fire target well past block 0 so every scheduled event lands in carry-over first.
private static let targetBlock: Int = 8
private static let scheduledCount: Int = 300
/// Schedule `scheduledCount` synths at a single future time, then run blocks until it
/// passes. Returns how many actually activated.
private func activatedCount(maxEventsPerBlock: Int) throws -> Int {
let engine: SynthesisEngine = makeTestEngine(
blockSize: Self.blockSize,
eventQueueCapacity: 1024,
maxEventsPerBlock: maxEventsPerBlock
)
try engine.start()
defer { engine.stop() }
let targetTime: SampleTime = SampleTime(Self.targetBlock * Self.blockSize)
for _ in 0..<Self.scheduledCount {
try engine.schedule(ConstantSynth(value: 0.0), at: targetTime)
}
// Run past the target block: block 0 drains the pending queue into carry-over
// (capped), later blocks carry it forward, the target block fires it.
for _ in 0...(Self.targetBlock + 1) {
engine.processBlock(inputSamples: [])
}
return engine.synthesizerCount
}
@Test func defaultCapDropsSurplusLookAhead() throws {
// 300 future events > default cap 256 → only the cap survives carry-over.
let activated: Int = try activatedCount(maxEventsPerBlock: lyrebirdDefaultMaxEventsPerBlock)
#expect(activated == lyrebirdDefaultMaxEventsPerBlock)
}
@Test func raisedCapKeepsAllLookAhead() throws {
// Cap 512 > 300 scheduled → every event survives carry-over and fires.
let activated: Int = try activatedCount(maxEventsPerBlock: 512)
#expect(activated == Self.scheduledCount)
}
@Test func exceedingCapIncrementsDropCounter() throws {
let engine: SynthesisEngine = makeTestEngine(
blockSize: Self.blockSize,
eventQueueCapacity: 1024,
maxEventsPerBlock: lyrebirdDefaultMaxEventsPerBlock
)
try engine.start()
defer { engine.stop() }
#expect(engine.perBlockEventDropCount == 0)
let targetTime: SampleTime = SampleTime(Self.targetBlock * Self.blockSize)
for _ in 0..<Self.scheduledCount {
try engine.schedule(ConstantSynth(value: 0.0), at: targetTime)
}
for _ in 0...(Self.targetBlock + 1) {
engine.processBlock(inputSamples: [])
}
// 300 scheduled, 256 cap → 44 dropped from the look-ahead carry-over.
#expect(engine.perBlockEventDropCount == UInt64(Self.scheduledCount - lyrebirdDefaultMaxEventsPerBlock))
}
@Test func withinCapLeavesDropCounterZero() throws {
let engine: SynthesisEngine = makeTestEngine(
blockSize: Self.blockSize,
eventQueueCapacity: 1024,
maxEventsPerBlock: 512
)
try engine.start()
defer { engine.stop() }
let targetTime: SampleTime = SampleTime(Self.targetBlock * Self.blockSize)
for _ in 0..<Self.scheduledCount {
try engine.schedule(ConstantSynth(value: 0.0), at: targetTime)
}
for _ in 0...(Self.targetBlock + 1) {
engine.processBlock(inputSamples: [])
}
#expect(engine.perBlockEventDropCount == 0)
}
@Test func configuredValueIsExposed() {
let engine: SynthesisEngine = makeTestEngine(eventQueueCapacity: 2048, maxEventsPerBlock: 1024)
#expect(engine.maxEventsPerBlock == 1024)
}
@Test func defaultValueMatchesConstant() {
let engine: SynthesisEngine = makeTestEngine()
#expect(engine.maxEventsPerBlock == lyrebirdDefaultMaxEventsPerBlock)
}
}