Browse Source

Add test to show bug

master
Alex Mikhalev 4 years ago
parent
commit
cdec9b7176
  1. 67
      sprinklers_actors/src/program_runner.rs

67
sprinklers_actors/src/program_runner.rs

@ -139,6 +139,7 @@ impl ProgramRunnerInner { @@ -139,6 +139,7 @@ impl ProgramRunnerInner {
let mut scheduled_run_queue = DelayQueue::with_capacity(self.programs.len());
for (_, prog) in self.programs.clone() {
if !prog.enabled {
// TODO: send NextRun(prog, None) so nextRun will be updated
continue;
}
let ref_time = chrono::Local::now();
@ -881,7 +882,7 @@ mod test { @@ -881,7 +882,7 @@ mod test {
enabled,
schedule,
);
let programs = ordmap![ 1 => program1 ];
let programs = ordmap![ num => program1 ];
programs
};
@ -903,6 +904,7 @@ mod test { @@ -903,6 +904,7 @@ mod test {
// Should run
tokio::time::delay_for(Duration::from_secs(2)).await;
assert_matches!(prog_events.try_recv(), Ok(ProgramEvent::NextRun(_, _)));
assert_matches!(
prog_events.try_recv(),
Ok(ProgramEvent::RunStart(prog))
@ -912,4 +914,67 @@ mod test { @@ -912,4 +914,67 @@ mod test {
runner.quit().await.unwrap();
sec_runner.quit().await.unwrap();
}
#[actix_rt::test]
async fn test_scheduled_run_twice() {
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 now = chrono::Local::now();
let sched_time1 = now.time() + chrono::Duration::seconds(1);
let sched_time2 = now.time() + chrono::Duration::seconds(20);
let schedule = Schedule::new(
vec![sched_time1, sched_time2],
every_day(),
DateTimeBound::None,
DateTimeBound::None,
);
let program1 = make_program_with_schedule(
1,
vec![ProgramItem {
section_id: 1,
duration: Duration::from_secs(10),
}],
true,
schedule,
);
let programs = ordmap![ 1 => program1 ];
runner.update_sections(sections.clone()).await.unwrap();
runner.update_programs(programs).await.unwrap();
tokio::time::pause();
// Should run
tokio::time::delay_for(Duration::from_secs(2)).await;
assert_matches!(prog_events.try_recv(), Ok(ProgramEvent::NextRun(_, _)));
assert_matches!(
prog_events.try_recv(),
Ok(ProgramEvent::RunStart(prog))
if prog.id == 1
);
tokio::time::delay_for(Duration::from_secs(10)).await;
assert_matches!(
prog_events.try_recv(),
Ok(ProgramEvent::RunFinish(prog))
if prog.id == 1
);
// Should run again
tokio::time::delay_for(Duration::from_secs(10)).await;
assert_matches!(
prog_events.try_recv(),
Ok(ProgramEvent::RunStart(prog))
if prog.id == 1
);
tokio::time::delay_for(Duration::from_secs(10)).await;
assert_matches!(
prog_events.try_recv(),
Ok(ProgramEvent::RunFinish(prog))
if prog.id == 1
);
runner.quit().await.unwrap();
sec_runner.quit().await.unwrap();
}
}

Loading…
Cancel
Save