v2.4.0 release: event patterns + live event conductor
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
// RoutineContextScheduledSetTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Synchronization
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
/// `LyrebirdRoutineContext.set(_:to:after:)` — non-blocking scheduled parameter writes, serviced
|
||||
/// on the body's own timeline during waits. These back the event-pattern gate note-off.
|
||||
///
|
||||
/// Not `@MainActor`: `LyrebirdTask.run` blocks the calling thread and the body runs off-main, so
|
||||
/// running the test off-main leaves the main queue free (matching the other `LyrebirdTask` suites).
|
||||
/// A bare `ParameterNode` is the write target — its value get/set is atomic and thread-safe, so no
|
||||
/// synthesizer or engine graph is needed to observe the write.
|
||||
@Suite("RoutineContext scheduled set") struct RoutineContextScheduledSetTests {
|
||||
|
||||
@Test func scheduledSetFiresAfterDelayNotBefore() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let node: ParameterNode = ParameterNode(1.0)
|
||||
let before: Ref<Double> = Ref(-1.0)
|
||||
let after: Ref<Double> = Ref(-1.0)
|
||||
|
||||
LyrebirdTask.run(engine: engine) { ctx in
|
||||
ctx.set(node, to: 0.0, after: 0.05)
|
||||
before.value = node.value // not yet due — still 1.0
|
||||
try ctx.wait(seconds: 0.15) // services the pending set en route
|
||||
after.value = node.value // fired — 0.0
|
||||
}
|
||||
|
||||
#expect(before.value == 1.0)
|
||||
#expect(after.value == 0.0)
|
||||
}
|
||||
|
||||
@Test func multipleScheduledSetsFireIndependently() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let a: ParameterNode = ParameterNode(0.0)
|
||||
let b: ParameterNode = ParameterNode(0.0)
|
||||
let midA: Ref<Double> = Ref(-1.0)
|
||||
let midB: Ref<Double> = Ref(-1.0)
|
||||
let endA: Ref<Double> = Ref(-1.0)
|
||||
let endB: Ref<Double> = Ref(-1.0)
|
||||
|
||||
LyrebirdTask.run(engine: engine) { ctx in
|
||||
ctx.set(a, to: 1.0, after: 0.04) // fires first
|
||||
ctx.set(b, to: 1.0, after: 0.12) // fires later
|
||||
try ctx.wait(seconds: 0.08) // past a, before b
|
||||
midA.value = a.value // 1.0
|
||||
midB.value = b.value // still 0.0
|
||||
try ctx.wait(seconds: 0.10) // past b
|
||||
endA.value = a.value
|
||||
endB.value = b.value // 1.0
|
||||
}
|
||||
|
||||
#expect(midA.value == 1.0)
|
||||
#expect(midB.value == 0.0)
|
||||
#expect(endA.value == 1.0)
|
||||
#expect(endB.value == 1.0)
|
||||
}
|
||||
|
||||
@Test func waitForScheduledSetsDrainsPending() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let node: ParameterNode = ParameterNode(1.0)
|
||||
let result: Ref<Double> = Ref(-1.0)
|
||||
|
||||
LyrebirdTask.run(engine: engine) { ctx in
|
||||
ctx.set(node, to: 0.0, after: 0.05)
|
||||
// No explicit wait — `waitForScheduledSets` must itself block until the write fires.
|
||||
try ctx.waitForScheduledSets()
|
||||
result.value = node.value
|
||||
}
|
||||
|
||||
#expect(result.value == 0.0)
|
||||
}
|
||||
|
||||
@Test func flushFiresPendingSetsImmediately() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let node: ParameterNode = ParameterNode(1.0)
|
||||
let afterFlush: Ref<Double> = Ref(-1.0)
|
||||
|
||||
LyrebirdTask.run(engine: engine) { ctx in
|
||||
ctx.set(node, to: 0.0, after: 100.0) // due far in the future
|
||||
ctx._flushScheduledSets() // fire now regardless of due time
|
||||
afterFlush.value = node.value
|
||||
}
|
||||
|
||||
#expect(afterFlush.value == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Minimal reference box so a `@Sendable` task body can report a result back to the test.
|
||||
/// `@unchecked Sendable`: the body writes `value`, the test reads it only after `LyrebirdTask.run`
|
||||
/// has joined the body thread — no concurrent access.
|
||||
private final class Ref<T>: @unchecked Sendable {
|
||||
var value: T
|
||||
init(_ value: T) { self.value = value }
|
||||
}
|
||||
Reference in New Issue
Block a user