@@ -519,21 +519,29 @@ mod tests {
519
519
. into ( )
520
520
}
521
521
522
- /*
522
+ fn add_node ( graph : & mut Graph , id : u64 , node : Box < dyn AudioProcessor > ) {
523
+ let id = AudioNodeId ( id) ;
524
+ let reclaim_id = llq:: Node :: new ( id) ;
525
+ graph. add_node ( id, reclaim_id, node, 1 , 1 , config ( ) ) ;
526
+ }
527
+
528
+ fn add_edge ( graph : & mut Graph , from : u64 , to : u64 ) {
529
+ graph. add_edge ( ( AudioNodeId ( from) , 0 ) , ( AudioNodeId ( to) , 0 ) ) ;
530
+ }
523
531
524
532
#[ test]
525
533
fn test_add_remove ( ) {
526
- let mut graph = Graph::new();
534
+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
527
535
528
536
let node = Box :: new ( TestNode { } ) ;
529
- graph. add_node(AudioNodeId(0) , node.clone(), 1, 1, config ());
530
- graph. add_node(AudioNodeId(1), node.clone() , 1, 1, config ());
531
- graph. add_node(AudioNodeId(2) , node.clone(), 1, 1, config ());
532
- graph. add_node(AudioNodeId(3), node, 1, 1, config() );
537
+ add_node ( & mut graph , 0 , node. clone ( ) ) ;
538
+ add_node ( & mut graph , 1 , node . clone ( ) ) ;
539
+ add_node ( & mut graph , 2 , node. clone ( ) ) ;
540
+ add_node ( & mut graph , 3 , node ) ;
533
541
534
- graph. add_edge((AudioNodeId(1), 0), (AudioNodeId(0) , 0) );
535
- graph. add_edge((AudioNodeId(2), 0), (AudioNodeId(1), 0) );
536
- graph. add_edge((AudioNodeId(3), 0), (AudioNodeId(0) , 0) );
542
+ add_edge ( & mut graph , 1 , 0 ) ;
543
+ add_edge ( & mut graph , 2 , 1 ) ;
544
+ add_edge ( & mut graph , 3 , 0 ) ;
537
545
538
546
graph. order_nodes ( ) ;
539
547
@@ -574,17 +582,17 @@ mod tests {
574
582
575
583
#[ test]
576
584
fn test_remove_all ( ) {
577
- let mut graph = Graph::new();
585
+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
578
586
579
587
let node = Box :: new ( TestNode { } ) ;
580
- graph. add_node(AudioNodeId(0) , node.clone(), 1, 1, config ());
581
- graph. add_node(AudioNodeId(1), node.clone() , 1, 1, config ());
582
- graph. add_node(AudioNodeId(2), node, 1, 1, config() );
588
+ add_node ( & mut graph , 0 , node. clone ( ) ) ;
589
+ add_node ( & mut graph , 1 , node . clone ( ) ) ;
590
+ add_node ( & mut graph , 2 , node ) ;
583
591
584
592
// link 1->0, 1->2 and 2->0
585
- graph. add_edge((AudioNodeId(1), 0), (AudioNodeId(0) , 0) );
586
- graph. add_edge((AudioNodeId(1), 0), (AudioNodeId(2), 0) );
587
- graph. add_edge((AudioNodeId(2), 0), (AudioNodeId(0) , 0) );
593
+ add_edge ( & mut graph , 1 , 0 ) ;
594
+ add_edge ( & mut graph , 1 , 2 ) ;
595
+ add_edge ( & mut graph , 2 , 0 ) ;
588
596
589
597
graph. order_nodes ( ) ;
590
598
@@ -613,21 +621,21 @@ mod tests {
613
621
614
622
#[ test]
615
623
fn test_cycle ( ) {
616
- let mut graph = Graph::new();
624
+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
617
625
618
626
let node = Box :: new ( TestNode { } ) ;
619
- graph. add_node(AudioNodeId(0) , node.clone(), 1, 1, config ());
620
- graph. add_node(AudioNodeId(1), node.clone() , 1, 1, config ());
621
- graph. add_node(AudioNodeId(2) , node.clone(), 1, 1, config ());
622
- graph. add_node(AudioNodeId(3) , node.clone(), 1, 1, config ());
623
- graph. add_node(AudioNodeId(4), node, 1, 1, config() );
627
+ add_node ( & mut graph , 0 , node. clone ( ) ) ;
628
+ add_node ( & mut graph , 1 , node . clone ( ) ) ;
629
+ add_node ( & mut graph , 2 , node. clone ( ) ) ;
630
+ add_node ( & mut graph , 3 , node. clone ( ) ) ;
631
+ add_node ( & mut graph , 4 , node ) ;
624
632
625
633
// link 4->2, 2->1, 1->0, 1->2, 3->0
626
- graph. add_edge((AudioNodeId(4), 0), (AudioNodeId(2), 0) );
627
- graph. add_edge((AudioNodeId(2), 0), (AudioNodeId(1), 0) );
628
- graph. add_edge((AudioNodeId(1), 0), (AudioNodeId(0) , 0) );
629
- graph. add_edge((AudioNodeId(1), 0), (AudioNodeId(2), 0) );
630
- graph. add_edge((AudioNodeId(3), 0), (AudioNodeId(0) , 0) );
634
+ add_edge ( & mut graph , 4 , 2 ) ;
635
+ add_edge ( & mut graph , 2 , 1 ) ;
636
+ add_edge ( & mut graph , 1 , 0 ) ;
637
+ add_edge ( & mut graph , 1 , 2 ) ;
638
+ add_edge ( & mut graph , 3 , 0 ) ;
631
639
632
640
graph. order_nodes ( ) ;
633
641
@@ -646,5 +654,42 @@ mod tests {
646
654
assert ! ( pos3. unwrap( ) < pos0. unwrap( ) ) ;
647
655
}
648
656
649
- */
657
+ #[ test]
658
+ fn test_lifecycle_and_reclaim ( ) {
659
+ let ( node_id_producer, mut node_id_consumer) = llq:: Queue :: new ( ) . split ( ) ;
660
+ let mut graph = Graph :: new ( node_id_producer) ;
661
+
662
+ let node = Box :: new ( TestNode { } ) ;
663
+
664
+ // Destination Node is always node id 0, and should never drop
665
+ add_node ( & mut graph, 0 , node. clone ( ) ) ;
666
+
667
+ // AudioListener Node is always node id 1, and should never drop
668
+ add_node ( & mut graph, 1 , node. clone ( ) ) ;
669
+
670
+ // Add a regular node at id 3, it has tail time false so after rendering it should be
671
+ // dropped and the AudioNodeId(3) should be reclaimed
672
+ add_node ( & mut graph, 2 , node. clone ( ) ) ;
673
+ // Mark the node as 'detached from the control thread', so it is allowed to drop
674
+ graph. nodes [ 2 ] . get_mut ( ) . free_when_finished = true ;
675
+
676
+ // Connect the regular node to the AudioDestinationNode
677
+ add_edge ( & mut graph, 2 , 0 ) ;
678
+
679
+ // Render a single quantum
680
+ let scope = RenderScope {
681
+ current_frame : 0 ,
682
+ current_time : 0. ,
683
+ sample_rate : 48000. ,
684
+ node_id : std:: cell:: Cell :: new ( AudioNodeId ( 0 ) ) ,
685
+ event_sender : None ,
686
+ } ;
687
+ graph. render ( & scope) ;
688
+
689
+ // The dropped node should be our regular node, not the AudioListener
690
+ let reclaimed = node_id_consumer
691
+ . pop ( )
692
+ . expect ( "should have decommisioned node" ) ;
693
+ assert_eq ! ( reclaimed. 0 , 2 ) ;
694
+ }
650
695
}
0 commit comments