From a5e341c16e739e22f6e4b6f3dddafe2397a1eb29 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 20 Nov 2024 17:20:19 +0100 Subject: [PATCH] added some more tests --- satrs/src/scheduling.rs | 78 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/satrs/src/scheduling.rs b/satrs/src/scheduling.rs index dd8f404..5df7033 100644 --- a/satrs/src/scheduling.rs +++ b/satrs/src/scheduling.rs @@ -103,6 +103,7 @@ pub mod std_mod { pub struct TaskWithSchedulingTable { start_of_slot: Instant, end_of_slot: Instant, + deadline_missed_ms_count: u32, table: SchedulingTable, tasks: Vec>, } @@ -146,6 +147,7 @@ pub mod std_mod { Self { start_of_slot: Instant::now(), end_of_slot: Instant::now(), + deadline_missed_ms_count: 10, table: SchedulingTable::new(execution_frequency), tasks: Default::default(), } @@ -172,7 +174,9 @@ pub mod std_mod { if now < scheduled_execution_time { std::thread::sleep(scheduled_execution_time - now); - } else { + } else if (now - scheduled_execution_time).as_millis() + > self.deadline_missed_ms_count.into() + { task_with_op_code.task.with_task(|task| { deadline_missed_cb .deadline_missed_callback(task.task_name(), task_with_op_code.op_code); @@ -186,12 +190,13 @@ pub mod std_mod { } let now = Instant::now(); - if now < self.end_of_slot { + if now <= self.end_of_slot { let diff = self.end_of_slot - now; std::thread::sleep(diff); self.start_of_slot = self.end_of_slot; - } else { - self.start_of_slot = Instant::now(); + } else if now > self.end_of_slot + self.table.execution_frequency { + // We're getting strongly out of sync. Set the new start timt to now. + self.start_of_slot = now; } Ok(()) @@ -203,8 +208,12 @@ pub mod std_mod { mod tests { use core::{cell::RefCell, convert::Infallible, time::Duration}; use std::{ + println, rc::Rc, - sync::{mpsc::{self, TryRecvError}, Arc, Mutex}, + sync::{ + mpsc::{self, TryRecvError}, + Arc, Mutex, + }, time::Instant, }; @@ -264,7 +273,8 @@ mod tests { } impl DeadlineMissedHandler for DeadlineMissed { - fn deadline_missed_callback(&mut self, _task_name: &'static str, _op_code: i32) { + fn deadline_missed_callback(&mut self, task_name: &'static str, _op_code: i32) { + println!("task name {task_name} missed the deadline"); self.call_count += 1; } } @@ -317,8 +327,58 @@ mod tests { assert!(diff_call_to_start.as_millis() < 180); assert!(diff_call_to_start.as_millis() >= 150); matches!(rx_t2.try_recv().unwrap_err(), TryRecvError::Empty); + assert_eq!(deadline_missed_cb.call_count, 0); + } + + #[test] + pub fn basic_test_with_arc_mutex() { + let (tx_t1, rx_t1) = mpsc::channel(); + let (tx_t2, rx_t2) = mpsc::channel(); + let t1 = Task1 { + called_queue: tx_t1, + }; + let t2 = Task2 { + called_queue: tx_t2, + }; + let mut deadline_missed_cb = DeadlineMissed::default(); + let mut exec_task = TaskWithSchedulingTable::new(Duration::from_millis(200)); + let t1_first_slot = Arc::new(Mutex::new(t1)); + let t1_second_slot = t1_first_slot.clone(); + let t2_first_slot = Arc::new(Mutex::new(t2)); + let t2_second_slot = t2_first_slot.clone(); - //assert_eq!(deadline_missed_cb.call_count, 0); + exec_task.add_task_sendable(0, t1_first_slot, 0).unwrap(); + exec_task.add_task_sendable(50, t1_second_slot, -1).unwrap(); + exec_task.add_task_sendable(100, t2_first_slot, 1).unwrap(); + exec_task.add_task_sendable(150, t2_second_slot, 2).unwrap(); + let now = Instant::now(); + exec_task.init_start_of_slot(); + exec_task + .run_one_task_cycle(&mut deadline_missed_cb) + .unwrap(); + let mut call_info = rx_t1.try_recv().unwrap(); + assert_eq!(call_info.op_code, 0); + let diff_call_to_start = call_info.time - now; + assert!(diff_call_to_start.as_millis() < 30); + call_info = rx_t1.try_recv().unwrap(); + assert_eq!(call_info.op_code, -1); + let diff_call_to_start = call_info.time - now; + assert!(diff_call_to_start.as_millis() < 80); + assert!(diff_call_to_start.as_millis() >= 50); + matches!(rx_t1.try_recv().unwrap_err(), TryRecvError::Empty); + + call_info = rx_t2.try_recv().unwrap(); + assert_eq!(call_info.op_code, 1); + let diff_call_to_start = call_info.time - now; + assert!(diff_call_to_start.as_millis() < 120); + assert!(diff_call_to_start.as_millis() >= 100); + call_info = rx_t2.try_recv().unwrap(); + assert_eq!(call_info.op_code, 2); + let diff_call_to_start = call_info.time - now; + assert!(diff_call_to_start.as_millis() < 180); + assert!(diff_call_to_start.as_millis() >= 150); + matches!(rx_t2.try_recv().unwrap_err(), TryRecvError::Empty); + assert_eq!(deadline_missed_cb.call_count, 0); } #[test] @@ -344,7 +404,9 @@ mod tests { called_queue: tx_t1, }; let mut exec_task_sendable = TaskWithSchedulingTable::new(Duration::from_millis(200)); - exec_task_sendable.add_task_sendable(0, Arc::new(Mutex::new(t1)), 1).unwrap(); + exec_task_sendable + .add_task_sendable(0, Arc::new(Mutex::new(t1)), 1) + .unwrap(); std::thread::spawn(move || { exec_task_sendable .run_one_task_cycle(&mut deadline_missed_cb)