feat(M3.3): bound and order adapter dispatch

Add a fixed-capacity dispatch lane that coalesces only unsent volume writes, preserves mute edges in arrival order, and exposes overflow as an explicit resynchronization fault.

Tests: cargo fmt --all -- --check; cargo test -p volume_lib --all-targets --locked; cargo clippy -p volume_lib --all-targets --locked -- -D warnings (all pass)
This commit is contained in:
Dandaedre 2026-10-14 15:39:40 +02:00
parent 4e115b8562
commit 58e08f3741
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
2 changed files with 153 additions and 1 deletions

View File

@ -2,7 +2,7 @@
#![forbid(unsafe_code)]
use std::collections::BTreeMap;
use std::collections::{BTreeMap, VecDeque};
use fader_core::{AdapterCommand, AdapterEvent, BackendEpoch, ChannelGains, Mute, StreamHandle};
@ -37,6 +37,86 @@ pub struct FakeAdapter {
during_snapshot: Vec<AdapterEvent>,
}
/// Result of enqueueing work into the bounded adapter lane.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Enqueue {
Queued,
/// An unsent volume operation was replaced by a newer one.
Coalesced {
superseded: fader_core::OperationId,
},
/// An event could not be retained; callers must expose a fault and resync.
Overflow,
}
/// Bounded ordered dispatch with volume-only coalescing.
#[derive(Debug)]
pub struct AdapterQueue {
capacity: usize,
commands: VecDeque<AdapterCommand>,
faulted: bool,
}
impl AdapterQueue {
#[must_use]
pub fn new(capacity: usize) -> Self {
Self {
capacity,
commands: VecDeque::with_capacity(capacity),
faulted: false,
}
}
pub fn push(&mut self, command: AdapterCommand) -> Enqueue {
if let AdapterCommand::SetVolume {
operation: _,
stream,
..
} = &command
&& let Some(index) = self.commands.iter().position(|queued| {
matches!(queued, AdapterCommand::SetVolume { stream: queued_stream, .. } if queued_stream == stream)
})
&& let Some(AdapterCommand::SetVolume {
operation: superseded,
..
}) = self.commands.remove(index)
{
self.commands.insert(index, command);
return Enqueue::Coalesced { superseded };
}
if self.commands.len() >= self.capacity {
self.faulted = true;
return Enqueue::Overflow;
}
self.commands.push_back(command);
Enqueue::Queued
}
pub fn pop(&mut self) -> Option<AdapterCommand> {
self.commands.pop_front()
}
#[must_use]
pub fn len(&self) -> usize {
self.commands.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
#[must_use]
pub const fn faulted(&self) -> bool {
self.faulted
}
pub fn clear_fault(&mut self) {
self.faulted = false;
}
}
impl FakeAdapter {
#[must_use]
pub fn new(epoch: BackendEpoch) -> Self {

72
sound/tests/ordering.rs Normal file
View File

@ -0,0 +1,72 @@
use fader_core::{
AdapterCommand, BackendEpoch, ChannelGains, Gain, Mute, OperationId, StreamHandle,
};
use volume_lib::{AdapterQueue, Enqueue};
fn stream(id: u64) -> StreamHandle {
StreamHandle::new(BackendEpoch::new(1), id, 1)
}
fn volume(operation: u64, stream: StreamHandle, value: f64) -> AdapterCommand {
AdapterCommand::SetVolume {
operation: OperationId::new(operation),
stream,
channels: ChannelGains::new(vec![Gain::new(value).unwrap()]).unwrap(),
}
}
fn mute(operation: u64, muted: Mute) -> AdapterCommand {
AdapterCommand::SetMute {
operation: OperationId::new(operation),
stream: stream(1),
muted,
}
}
#[test]
fn volume_burst_stays_bounded_and_ends_at_latest_value() {
let mut queue = AdapterQueue::new(2);
assert_eq!(queue.push(volume(1, stream(1), 0.1)), Enqueue::Queued);
for operation in 2_u32..=100 {
assert!(matches!(
queue.push(volume(
u64::from(operation),
stream(1),
f64::from(operation) / 100.0
)),
Enqueue::Coalesced { .. }
));
}
assert_eq!(queue.len(), 1);
let AdapterCommand::SetVolume {
operation,
channels,
..
} = queue.pop().unwrap()
else {
panic!("volume command");
};
assert_eq!(operation, OperationId::new(100));
assert_eq!(channels.maximum(), Gain::UNITY);
}
#[test]
fn mute_edges_are_ordered_and_never_silently_coalesced() {
let mut queue = AdapterQueue::new(2);
assert_eq!(queue.push(mute(1, Mute::Muted)), Enqueue::Queued);
assert_eq!(queue.push(mute(2, Mute::Unmuted)), Enqueue::Queued);
assert_eq!(queue.push(mute(3, Mute::Muted)), Enqueue::Overflow);
assert!(queue.faulted());
assert!(matches!(
queue.pop(),
Some(AdapterCommand::SetMute {
muted: Mute::Muted,
..
})
));
assert!(matches!(
queue.pop(),
Some(AdapterCommand::SetMute {
muted: Mute::Unmuted,
..
})
));
}