Skip to content

Commit 404c48b

Browse files
committed
Add tests for dynamic lifetime of AudioParams
1 parent c145570 commit 404c48b

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

src/render/graph.rs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ mod tests {
496496
use super::*;
497497

498498
#[derive(Debug, Clone)]
499-
struct TestNode {}
499+
struct TestNode {
500+
tail_time: bool,
501+
}
500502

501503
impl AudioProcessor for TestNode {
502504
fn process(
@@ -506,7 +508,7 @@ mod tests {
506508
_params: AudioParamValues<'_>,
507509
_scope: &RenderScope,
508510
) -> bool {
509-
false
511+
self.tail_time
510512
}
511513
}
512514

@@ -529,11 +531,15 @@ mod tests {
529531
graph.add_edge((AudioNodeId(from), 0), (AudioNodeId(to), 0));
530532
}
531533

534+
fn add_audioparam(graph: &mut Graph, from: u64, to: u64) {
535+
graph.add_edge((AudioNodeId(from), 0), (AudioNodeId(to), usize::MAX));
536+
}
537+
532538
#[test]
533539
fn test_add_remove() {
534540
let mut graph = Graph::new(llq::Queue::new().split().0);
535541

536-
let node = Box::new(TestNode {});
542+
let node = Box::new(TestNode { tail_time: false });
537543
add_node(&mut graph, 0, node.clone());
538544
add_node(&mut graph, 1, node.clone());
539545
add_node(&mut graph, 2, node.clone());
@@ -584,7 +590,7 @@ mod tests {
584590
fn test_remove_all() {
585591
let mut graph = Graph::new(llq::Queue::new().split().0);
586592

587-
let node = Box::new(TestNode {});
593+
let node = Box::new(TestNode { tail_time: false });
588594
add_node(&mut graph, 0, node.clone());
589595
add_node(&mut graph, 1, node.clone());
590596
add_node(&mut graph, 2, node);
@@ -623,7 +629,7 @@ mod tests {
623629
fn test_cycle() {
624630
let mut graph = Graph::new(llq::Queue::new().split().0);
625631

626-
let node = Box::new(TestNode {});
632+
let node = Box::new(TestNode { tail_time: false });
627633
add_node(&mut graph, 0, node.clone());
628634
add_node(&mut graph, 1, node.clone());
629635
add_node(&mut graph, 2, node.clone());
@@ -659,7 +665,7 @@ mod tests {
659665
let (node_id_producer, mut node_id_consumer) = llq::Queue::new().split();
660666
let mut graph = Graph::new(node_id_producer);
661667

662-
let node = Box::new(TestNode {});
668+
let node = Box::new(TestNode { tail_time: false });
663669

664670
// Destination Node is always node id 0, and should never drop
665671
add_node(&mut graph, 0, node.clone());
@@ -691,5 +697,55 @@ mod tests {
691697
.pop()
692698
.expect("should have decommisioned node");
693699
assert_eq!(reclaimed.0, 2);
700+
701+
// No other dropped nodes
702+
assert!(node_id_consumer.pop().is_none());
703+
}
704+
705+
#[test]
706+
fn test_audio_param_lifecycle() {
707+
let (node_id_producer, mut node_id_consumer) = llq::Queue::new().split();
708+
let mut graph = Graph::new(node_id_producer);
709+
710+
let node = Box::new(TestNode { tail_time: false });
711+
712+
// Destination Node is always node id 0, and should never drop
713+
add_node(&mut graph, 0, node.clone());
714+
715+
// AudioListener Node is always node id 1, and should never drop
716+
add_node(&mut graph, 1, node.clone());
717+
718+
// Add a regular node at id 3, it has tail time false so after rendering it should be
719+
// dropped and the AudioNodeId(3) should be reclaimed
720+
add_node(&mut graph, 2, node.clone());
721+
// Mark the node as 'detached from the control thread', so it is allowed to drop
722+
graph.nodes[2].get_mut().free_when_finished = true;
723+
724+
// Connect the regular node to the AudioDestinationNode
725+
add_edge(&mut graph, 2, 0);
726+
727+
// Add an AudioParam at id 4, it should be dropped alongside the regular node
728+
let param = Box::new(TestNode { tail_time: true }); // audio params have tail time true
729+
add_node(&mut graph, 3, param);
730+
731+
// Connect the audioparam to the regular node
732+
add_audioparam(&mut graph, 3, 2);
733+
734+
// Render a single quantum
735+
let scope = RenderScope {
736+
current_frame: 0,
737+
current_time: 0.,
738+
sample_rate: 48000.,
739+
node_id: std::cell::Cell::new(AudioNodeId(0)),
740+
event_sender: None,
741+
};
742+
graph.render(&scope);
743+
744+
// First the regular node should be dropped, then the audioparam
745+
assert_eq!(node_id_consumer.pop().unwrap().0, 2);
746+
assert_eq!(node_id_consumer.pop().unwrap().0, 3);
747+
748+
// No other dropped nodes
749+
assert!(node_id_consumer.pop().is_none());
694750
}
695751
}

0 commit comments

Comments
 (0)