@@ -519,21 +519,29 @@ mod tests {
519519 . into ( )
520520 }
521521
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+ }
523531
524532 #[ test]
525533 fn test_add_remove ( ) {
526- let mut graph = Graph::new();
534+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
527535
528536 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 ) ;
533541
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 ) ;
537545
538546 graph. order_nodes ( ) ;
539547
@@ -574,17 +582,17 @@ mod tests {
574582
575583 #[ test]
576584 fn test_remove_all ( ) {
577- let mut graph = Graph::new();
585+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
578586
579587 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 ) ;
583591
584592 // 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 ) ;
588596
589597 graph. order_nodes ( ) ;
590598
@@ -613,21 +621,21 @@ mod tests {
613621
614622 #[ test]
615623 fn test_cycle ( ) {
616- let mut graph = Graph::new();
624+ let mut graph = Graph :: new ( llq :: Queue :: new ( ) . split ( ) . 0 ) ;
617625
618626 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 ) ;
624632
625633 // 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 ) ;
631639
632640 graph. order_nodes ( ) ;
633641
@@ -646,5 +654,42 @@ mod tests {
646654 assert ! ( pos3. unwrap( ) < pos0. unwrap( ) ) ;
647655 }
648656
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+ }
650695}
0 commit comments