From 88edd2defcfba298f9d3bc6d651f7ba6da50400b Mon Sep 17 00:00:00 2001 From: Ivan Arkhipov Date: Sun, 24 Mar 2024 13:04:11 +0300 Subject: [PATCH 1/4] fix some tests --- .github/workflows/rust.yml | 11 +++-- Cargo.toml | 3 ++ src/task_manager.rs | 48 +++++++++++-------- tests/unit_tests.rs | 94 ++++++++++++++++---------------------- 4 files changed, 79 insertions(+), 77 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b8b7734..e4625ce 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,10 +1,13 @@ name: Rust +#on: +# push: +# branches: [ "main" ] +# pull_request: +# branches: [ "main" ] + on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] + - push env: CARGO_TERM_COLOR: always diff --git a/Cargo.toml b/Cargo.toml index eeae42f..41a0fea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ edition = "2021" [features] default = [] c-library = [] + +[dev-dependencies] +sequential-test = "0.2.4" diff --git a/src/task_manager.rs b/src/task_manager.rs index 792069a..650a305 100644 --- a/src/task_manager.rs +++ b/src/task_manager.rs @@ -227,28 +227,40 @@ impl TaskManager { } } - /// Starts task manager work. + /// One step of task manager's work. // TODO: Support priorities. // TODO: Delete tasks from task vector if they are pending - pub fn start_task_manager() -> ! { - loop { - if unsafe { !TASK_MANAGER.tasks.is_empty() } { - let waker = task_waker(); - - let task = unsafe { &mut TASK_MANAGER.tasks[TASK_MANAGER.task_to_execute_index] }; - let mut task_future_pin = Pin::new(task); - let _ = task_future_pin - .as_mut() - .poll(&mut Context::from_waker(&waker)); - - unsafe { - if TASK_MANAGER.task_to_execute_index + 1 < TASK_MANAGER.tasks.len() { - TASK_MANAGER.task_to_execute_index += 1; - } else { - TASK_MANAGER.task_to_execute_index = 0; - } + fn task_manager_step() { + if unsafe { !TASK_MANAGER.tasks.is_empty() } { + let waker = task_waker(); + + let task = unsafe { &mut TASK_MANAGER.tasks[TASK_MANAGER.task_to_execute_index] }; + let mut task_future_pin = Pin::new(task); + let _ = task_future_pin + .as_mut() + .poll(&mut Context::from_waker(&waker)); + + unsafe { + if TASK_MANAGER.task_to_execute_index + 1 < TASK_MANAGER.tasks.len() { + TASK_MANAGER.task_to_execute_index += 1; + } else { + TASK_MANAGER.task_to_execute_index = 0; } } } } + + /// Starts task manager work. + pub fn start_task_manager() -> ! { + loop { + TaskManager::task_manager_step(); + } + } + + /// Starts task manager work. Returns after 1000 steps only for testing task_manager_step. + pub fn test_start_task_manager() { + for _n in 1..=1000 { + TaskManager::task_manager_step(); + } + } } diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 892b53b..328f2d7 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -2,26 +2,19 @@ mod unit_tests { use ma_rtos::task_manager::TaskManager; use ma_rtos::timer::Timer; + use sequential_test::sequential; use std::sync::atomic::{AtomicU32, Ordering}; - use std::thread::{sleep, spawn}; - use std::time::Duration; // TODO: fix tests for task manager. They can't be run in parallel because of static task manager // TODO: refactor unit tests. They should check less. Separate tests for setup, loop and stop functions. // TODO: refactor unit tests. Task manager and timer tests should be in different files in one directory. - // #[test] - // /// Tests if task manager without tasks works during 1 second without panic. - // fn test_empty_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::start_task_manager(); - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // } + #[test] + #[sequential] + /// Tests if task manager without tasks works during some time. + fn test_empty_task_manager() { + TaskManager::test_start_task_manager(); + } /// Counter for task for test_one_finite_task_task_manager. static TEST_ONE_FINITE_TASK_TASK_MANAGER_COUNTER: AtomicU32 = AtomicU32::new(1); @@ -40,54 +33,45 @@ mod unit_tests { return false; } #[test] - /// Tests if task manager with one finite task works correctly during 1 second without panic. + #[sequential] + /// Tests if task manager with one finite task works correctly during some time without panic. fn test_one_finite_task_task_manager() { - let fun_thread = spawn(|| { - TaskManager::add_task( - test_one_finite_task_task_manager_setup_fn, - test_one_finite_task_task_manager_loop_fn, - test_one_finite_task_task_manager_stop_condition_fn, - ); - TaskManager::start_task_manager() - }); - sleep(Duration::from_secs(1)); + TaskManager::add_task( + test_one_finite_task_task_manager_setup_fn, + test_one_finite_task_task_manager_loop_fn, + test_one_finite_task_task_manager_stop_condition_fn, + ); + TaskManager::test_start_task_manager(); - assert_eq!(fun_thread.is_finished(), false); assert_eq!( unsafe { TEST_ONE_FINITE_TASK_TASK_MANAGER_COUNTER.as_ptr().read() }, 50 ); } - // - // /// Counter for task for test_one_infinite_task_task_manager. - // static TEST_ONE_INFINITE_TASK_TASK_MANAGER_COUNTER: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_one_infinite_task_task_manager. - // fn test_one_infinite_task_task_manager_setup_fn() {} - // /// Loop function for task for test_one_infinite_task_task_manager. - // fn test_one_infinite_task_task_manager_loop_fn() { - // TEST_ONE_INFINITE_TASK_TASK_MANAGER_COUNTER.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_one_infinite_task_task_manager. - // fn test_one_infinite_task_task_manager_stop_condition_fn() -> bool { - // return false; - // } - // #[test] - // /// Tests if task manager with one infinite task works correctly during 1 second without panic. - // fn test_one_infinite_task_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::add_task( - // test_one_infinite_task_task_manager_setup_fn, - // test_one_infinite_task_task_manager_loop_fn, - // test_one_infinite_task_task_manager_stop_condition_fn, - // ); - // TaskExecutor::start_task_manager() - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // } + + /// Counter for task for test_one_infinite_task_task_manager. + static TEST_ONE_INFINITE_TASK_TASK_MANAGER_COUNTER: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_one_infinite_task_task_manager. + fn test_one_infinite_task_task_manager_setup_fn() {} + /// Loop function for task for test_one_infinite_task_task_manager. + fn test_one_infinite_task_task_manager_loop_fn() { + TEST_ONE_INFINITE_TASK_TASK_MANAGER_COUNTER.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_one_infinite_task_task_manager. + fn test_one_infinite_task_task_manager_stop_condition_fn() -> bool { + return false; + } + #[test] + #[sequential] + /// Tests if task manager with one infinite task works correctly during some time without panic. + fn test_one_infinite_task_task_manager() { + TaskManager::add_task( + test_one_infinite_task_task_manager_setup_fn, + test_one_infinite_task_task_manager_loop_fn, + test_one_infinite_task_task_manager_stop_condition_fn, + ); + TaskManager::test_start_task_manager(); + } // // /// Counter for task for test_two_finite_tasks_task_manager. // static TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); From 506bead4a8969c4625d8e68463627668aa847eab Mon Sep 17 00:00:00 2001 From: Ivan Arkhipov Date: Sun, 24 Mar 2024 13:17:21 +0300 Subject: [PATCH 2/4] fix some tests --- tests/unit_tests.rs | 242 +++++++++++++++++++++----------------------- 1 file changed, 116 insertions(+), 126 deletions(-) diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 328f2d7..96faa2b 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -72,132 +72,122 @@ mod unit_tests { ); TaskManager::test_start_task_manager(); } - // - // /// Counter for task for test_two_finite_tasks_task_manager. - // static TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_setup_fn1() {} - // /// Loop function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_loop_fn1() { - // TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_stop_condition_fn1() -> bool { - // let value = unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.as_ptr().read() }; - // if value % 50 == 0 { - // return true; - // } - // return false; - // } - // /// Counter for task for test_two_finite_tasks_task_manager. - // static TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_setup_fn2() {} - // /// Loop function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_loop_fn2() { - // TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_finite_tasks_task_manager. - // fn test_two_finite_tasks_task_manager_stop_condition_fn2() -> bool { - // let value = unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.as_ptr().read() }; - // if value % 25 == 0 { - // return true; - // } - // return false; - // } - // #[test] - // /// Tests if task manager with two finite tasks works correctly during 1 second without panic. - // fn test_two_finite_tasks_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::add_task( - // test_two_finite_tasks_task_manager_setup_fn1, - // test_two_finite_tasks_task_manager_loop_fn1, - // test_two_finite_tasks_task_manager_stop_condition_fn1, - // ); - // TaskExecutor::add_task( - // test_two_finite_tasks_task_manager_setup_fn2, - // test_two_finite_tasks_task_manager_loop_fn2, - // test_two_finite_tasks_task_manager_stop_condition_fn2, - // ); - // TaskExecutor::start_task_manager() - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // assert_eq!( - // unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.as_ptr().read() }, - // 50 - // ); - // assert_eq!( - // unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.as_ptr().read() }, - // 25 - // ); - // } - // - // /// Counter for task for test_two_different_tasks_task_manager. - // static TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_setup_fn1() {} - // /// Loop function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_loop_fn1() { - // TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_stop_condition_fn1() -> bool { - // let value = unsafe { - // TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1 - // .as_ptr() - // .read() - // }; - // if value % 50 == 0 { - // return true; - // } - // return false; - // } - // /// Counter for task for test_two_different_tasks_task_manager. - // static TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_setup_fn2() {} - // /// Loop function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_loop_fn2() { - // TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_different_tasks_task_manager. - // fn test_two_different_tasks_task_manager_stop_condition_fn2() -> bool { - // return false; - // } - // #[test] - // /// Tests if task manager with two different (finite and infinite) tasks works correctly during 1 second without panic. - // fn test_two_different_tasks_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::add_task( - // test_two_different_tasks_task_manager_setup_fn1, - // test_two_different_tasks_task_manager_loop_fn1, - // test_two_different_tasks_task_manager_stop_condition_fn1, - // ); - // TaskExecutor::add_task( - // test_two_different_tasks_task_manager_setup_fn2, - // test_two_different_tasks_task_manager_loop_fn2, - // test_two_different_tasks_task_manager_stop_condition_fn2, - // ); - // TaskExecutor::start_task_manager() - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // assert_eq!( - // unsafe { - // TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1 - // .as_ptr() - // .read() - // }, - // 50 - // ); - // } + + /// Counter for task for test_two_finite_tasks_task_manager. + static TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_setup_fn1() {} + /// Loop function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_loop_fn1() { + TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_stop_condition_fn1() -> bool { + let value = unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.as_ptr().read() }; + if value % 50 == 0 { + return true; + } + return false; + } + /// Counter for task for test_two_finite_tasks_task_manager. + static TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_setup_fn2() {} + /// Loop function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_loop_fn2() { + TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_finite_tasks_task_manager. + fn test_two_finite_tasks_task_manager_stop_condition_fn2() -> bool { + let value = unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.as_ptr().read() }; + if value % 25 == 0 { + return true; + } + return false; + } + #[test] + #[sequential] + /// Tests if task manager with two finite tasks works correctly during some time without panic. + fn test_two_finite_tasks_task_manager() { + TaskManager::add_task( + test_two_finite_tasks_task_manager_setup_fn1, + test_two_finite_tasks_task_manager_loop_fn1, + test_two_finite_tasks_task_manager_stop_condition_fn1, + ); + TaskManager::add_task( + test_two_finite_tasks_task_manager_setup_fn2, + test_two_finite_tasks_task_manager_loop_fn2, + test_two_finite_tasks_task_manager_stop_condition_fn2, + ); + TaskManager::test_start_task_manager(); + + assert_eq!( + unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER1.as_ptr().read() }, + 50 + ); + assert_eq!( + unsafe { TEST_TWO_FINITE_TASK_TASK_MANAGER_COUNTER2.as_ptr().read() }, + 25 + ); + } + + /// Counter for task for test_two_different_tasks_task_manager. + static TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_setup_fn1() {} + /// Loop function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_loop_fn1() { + TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_stop_condition_fn1() -> bool { + let value = unsafe { + TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1 + .as_ptr() + .read() + }; + if value % 50 == 0 { + return true; + } + return false; + } + /// Counter for task for test_two_different_tasks_task_manager. + static TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_setup_fn2() {} + /// Loop function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_loop_fn2() { + TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_different_tasks_task_manager. + fn test_two_different_tasks_task_manager_stop_condition_fn2() -> bool { + return false; + } + #[test] + #[sequential] + /// Tests if task manager with two different (finite and infinite) tasks works correctly during 1 second without panic. + fn test_two_different_tasks_task_manager() { + TaskManager::add_task( + test_two_different_tasks_task_manager_setup_fn1, + test_two_different_tasks_task_manager_loop_fn1, + test_two_different_tasks_task_manager_stop_condition_fn1, + ); + TaskManager::add_task( + test_two_different_tasks_task_manager_setup_fn2, + test_two_different_tasks_task_manager_loop_fn2, + test_two_different_tasks_task_manager_stop_condition_fn2, + ); + TaskManager::test_start_task_manager(); + + assert_eq!( + unsafe { + TEST_TWO_DIFFERENT_TASK_TASK_MANAGER_COUNTER1 + .as_ptr() + .read() + }, + 50 + ); + } // // /// Counter for task for test_two_infinite_tasks_task_manager. // static TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); From e533ce8b6312ca9310b6325853936870090ade23 Mon Sep 17 00:00:00 2001 From: Ivan Arkhipov Date: Mon, 25 Mar 2024 09:43:22 +0300 Subject: [PATCH 3/4] fix all tests --- .github/workflows/rust.yml | 11 +-- src/task_manager.rs | 4 + tests/unit_tests.rs | 153 +++++++++++++++++-------------------- 3 files changed, 79 insertions(+), 89 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e4625ce..b8b7734 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,13 +1,10 @@ name: Rust -#on: -# push: -# branches: [ "main" ] -# pull_request: -# branches: [ "main" ] - on: - - push + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] env: CARGO_TERM_COLOR: always diff --git a/src/task_manager.rs b/src/task_manager.rs index 650a305..dec6c15 100644 --- a/src/task_manager.rs +++ b/src/task_manager.rs @@ -262,5 +262,9 @@ impl TaskManager { for _n in 1..=1000 { TaskManager::task_manager_step(); } + // This is bad idea + unsafe { + TASK_MANAGER.tasks_number = 0; + } } } diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 96faa2b..ec4fef2 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -165,7 +165,7 @@ mod unit_tests { } #[test] #[sequential] - /// Tests if task manager with two different (finite and infinite) tasks works correctly during 1 second without panic. + /// Tests if task manager with two different (finite and infinite) tasks works correctly during some time without panic. fn test_two_different_tasks_task_manager() { TaskManager::add_task( test_two_different_tasks_task_manager_setup_fn1, @@ -188,87 +188,76 @@ mod unit_tests { 50 ); } - // - // /// Counter for task for test_two_infinite_tasks_task_manager. - // static TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_setup_fn1() {} - // /// Loop function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_loop_fn1() { - // TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_stop_condition_fn1() -> bool { - // return false; - // } - // /// Counter for task for test_two_infinite_tasks_task_manager. - // static TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_setup_fn2() {} - // /// Loop function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_loop_fn2() { - // TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); - // } - // /// Stop function for task for test_two_infinite_tasks_task_manager. - // fn test_two_infinite_tasks_task_manager_stop_condition_fn2() -> bool { - // return false; - // } - // #[test] - // /// Tests if task manager with two infinite tasks works correctly during 1 second without panic. - // fn test_two_infinite_tasks_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::add_task( - // test_two_infinite_tasks_task_manager_setup_fn1, - // test_two_infinite_tasks_task_manager_loop_fn1, - // test_two_infinite_tasks_task_manager_stop_condition_fn1, - // ); - // TaskExecutor::add_task( - // test_two_infinite_tasks_task_manager_setup_fn2, - // test_two_infinite_tasks_task_manager_loop_fn2, - // test_two_infinite_tasks_task_manager_stop_condition_fn2, - // ); - // TaskExecutor::start_task_manager() - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // } - // - // /// Counter for task for test_setup_task_manager. - // static TEST_SETUP_TASK_MANAGER_COUNTER: AtomicU32 = AtomicU32::new(1); - // /// Setup function for task for test_setup_task_manager. - // fn test_setup_task_manager_setup_fn() { - // TEST_SETUP_TASK_MANAGER_COUNTER.store(42, Ordering::Relaxed); - // } - // /// Loop function for task for test_setup_task_manager. - // fn test_setup_task_manager_loop_fn() {} - // /// Stop function for task for test_setup_task_manager. - // fn test_setup_task_manager_stop_condition_fn() -> bool { - // return false; - // } - // #[test] - // /// Tests if task manager works correctly with setup function during 1 second without panic. - // fn test_setup_task_manager() { - // TaskExecutor::drop_task_executor(); - // - // let fun_thread = spawn(|| { - // TaskExecutor::add_task( - // test_setup_task_manager_setup_fn, - // test_setup_task_manager_loop_fn, - // test_setup_task_manager_stop_condition_fn, - // ); - // TaskExecutor::start_task_manager() - // }); - // sleep(Duration::from_secs(1)); - // - // assert_eq!(fun_thread.is_finished(), false); - // assert_eq!( - // unsafe { TEST_SETUP_TASK_MANAGER_COUNTER.as_ptr().read() }, - // 42 - // ); - // } + + /// Counter for task for test_two_infinite_tasks_task_manager. + static TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER1: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_setup_fn1() {} + /// Loop function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_loop_fn1() { + TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER1.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_stop_condition_fn1() -> bool { + return false; + } + /// Counter for task for test_two_infinite_tasks_task_manager. + static TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER2: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_setup_fn2() {} + /// Loop function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_loop_fn2() { + TEST_TWO_INFINITE_TASK_TASK_MANAGER_COUNTER2.fetch_add(1, Ordering::Relaxed); + } + /// Stop function for task for test_two_infinite_tasks_task_manager. + fn test_two_infinite_tasks_task_manager_stop_condition_fn2() -> bool { + return false; + } + #[test] + #[sequential] + /// Tests if task manager with two infinite tasks works correctly during some without panic. + fn test_two_infinite_tasks_task_manager() { + TaskManager::add_task( + test_two_infinite_tasks_task_manager_setup_fn1, + test_two_infinite_tasks_task_manager_loop_fn1, + test_two_infinite_tasks_task_manager_stop_condition_fn1, + ); + TaskManager::add_task( + test_two_infinite_tasks_task_manager_setup_fn2, + test_two_infinite_tasks_task_manager_loop_fn2, + test_two_infinite_tasks_task_manager_stop_condition_fn2, + ); + TaskManager::test_start_task_manager(); + } + + /// Counter for task for test_setup_task_manager. + static TEST_SETUP_TASK_MANAGER_COUNTER: AtomicU32 = AtomicU32::new(1); + /// Setup function for task for test_setup_task_manager. + fn test_setup_task_manager_setup_fn() { + TEST_SETUP_TASK_MANAGER_COUNTER.store(42, Ordering::Relaxed); + } + /// Loop function for task for test_setup_task_manager. + fn test_setup_task_manager_loop_fn() {} + /// Stop function for task for test_setup_task_manager. + fn test_setup_task_manager_stop_condition_fn() -> bool { + return false; + } + #[test] + #[sequential] + /// Tests if task manager works correctly with setup function during some time without panic. + fn test_setup_task_manager() { + TaskManager::add_task( + test_setup_task_manager_setup_fn, + test_setup_task_manager_loop_fn, + test_setup_task_manager_stop_condition_fn, + ); + TaskManager::test_start_task_manager(); + + assert_eq!( + unsafe { TEST_SETUP_TASK_MANAGER_COUNTER.as_ptr().read() }, + 42 + ); + } #[test] /// Tests setup timer function and getting tick counter (bad unit test). From 19175bf2b526a5e877faa28a0572dcfa7d2767dd Mon Sep 17 00:00:00 2001 From: Ivan Arkhipov Date: Mon, 25 Mar 2024 09:45:08 +0300 Subject: [PATCH 4/4] delete todo --- tests/unit_tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index ec4fef2..298f230 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -5,7 +5,6 @@ mod unit_tests { use sequential_test::sequential; use std::sync::atomic::{AtomicU32, Ordering}; - // TODO: fix tests for task manager. They can't be run in parallel because of static task manager // TODO: refactor unit tests. They should check less. Separate tests for setup, loop and stop functions. // TODO: refactor unit tests. Task manager and timer tests should be in different files in one directory.