diff --git a/src/program_runner.rs b/src/program_runner.rs index 5bef624..1ea4e77 100644 --- a/src/program_runner.rs +++ b/src/program_runner.rs @@ -531,4 +531,107 @@ mod test { sec_runner.quit().await.unwrap(); yield_now().await; } + + #[tokio::test] + async fn test_close_event_chan() { + let (sections, mut sec_runner, _) = make_sections_and_runner(); + let mut runner = ProgramRunner::new(sec_runner.clone()); + let mut prog_events = runner.subscribe().await.unwrap(); + + let program: ProgramRef = Program { + id: 1, + name: "Program 1".into(), + sequence: vec![], + } + .into(); + + runner.update_sections(sections.clone()).await.unwrap(); + + runner.run_program(program.clone()).await.unwrap(); + prog_events.recv().await.unwrap(); + prog_events.recv().await.unwrap(); + // Make sure it doesn't panic when the events channel is dropped + drop(prog_events); + yield_now().await; + runner.run_program(program).await.unwrap(); + yield_now().await; + + runner.quit().await.unwrap(); + sec_runner.quit().await.unwrap(); + yield_now().await; + } + + #[tokio::test] + async fn test_run_program_id() { + let (sections, mut sec_runner, _) = make_sections_and_runner(); + let mut runner = ProgramRunner::new(sec_runner.clone()); + let mut prog_events = runner.subscribe().await.unwrap(); + + let program1: ProgramRef = Program { + id: 1, + name: "Program 1".into(), + sequence: vec![ + ProgramItem { + section_id: 2, + duration: Duration::from_secs(10), + }, + ], + } + .into(); + let program2: ProgramRef = Program { + id: 2, + name: "Program 2".into(), + sequence: vec![ + ProgramItem { + section_id: 2, + duration: Duration::from_secs(10), + }, + ], + } + .into(); + let programs = ordmap![ 1 => program1, 2 => program2 ]; + + runner.update_sections(sections.clone()).await.unwrap(); + runner.update_programs(programs).await.unwrap(); + + // First try a non-existant program id + runner.run_program_id(3).await.unwrap(); + yield_now().await; + assert!(matches!( + prog_events.try_recv(), + Err(broadcast::TryRecvError::Empty) + )); + + runner.run_program_id(1).await.unwrap(); + yield_now().await; + assert!(matches!( + prog_events.try_recv().unwrap(), + ProgramEvent::RunStart(prog) + if prog.id == 1 + )); + + tokio::time::pause(); + assert!(matches!( + prog_events.recv().await.unwrap(), + ProgramEvent::RunFinish(prog) + if prog.id == 1 + )); + runner.run_program_id(1).await.unwrap(); + yield_now().await; + assert!(matches!( + prog_events.try_recv().unwrap(), + ProgramEvent::RunStart(prog) + if prog.id == 1 + )); + + assert!(matches!( + prog_events.recv().await.unwrap(), + ProgramEvent::RunFinish(prog) + if prog.id == 1 + )); + + runner.quit().await.unwrap(); + sec_runner.quit().await.unwrap(); + yield_now().await; + } }