From 903597daa444d7745b0229d3826664b3b073c778 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Fri, 22 Dec 2023 11:23:36 +0100 Subject: [PATCH] [#51] More robust concurrecny tests, ignore platform specific thread documentation example --- iceoryx2-bb/posix/src/thread.rs | 2 +- .../posix/tests/condition_variable_tests.rs | 6 +++++- iceoryx2-bb/posix/tests/mutex_tests.rs | 2 ++ .../threadsafe/tests/trigger_queue_tests.rs | 15 ++++++++++----- .../tests/condition_variable_tests.rs | 4 ++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/iceoryx2-bb/posix/src/thread.rs b/iceoryx2-bb/posix/src/thread.rs index 3d42d645c..309dd96d2 100644 --- a/iceoryx2-bb/posix/src/thread.rs +++ b/iceoryx2-bb/posix/src/thread.rs @@ -48,7 +48,7 @@ //! //! ## Create a highly customized thread with guarded stack //! -//! ``` +//! ```ignore //! use iceoryx2_bb_posix::thread::*; //! use iceoryx2_bb_posix::scheduler::*; //! use iceoryx2_bb_posix::system_configuration::*; diff --git a/iceoryx2-bb/posix/tests/condition_variable_tests.rs b/iceoryx2-bb/posix/tests/condition_variable_tests.rs index b48099683..3c8ffa0ef 100644 --- a/iceoryx2-bb/posix/tests/condition_variable_tests.rs +++ b/iceoryx2-bb/posix/tests/condition_variable_tests.rs @@ -12,13 +12,14 @@ use iceoryx2_bb_posix::condition_variable::*; use iceoryx2_bb_testing::assert_that; +use iceoryx2_bb_testing::watchdog::Watchdog; use std::sync::atomic::{AtomicI32, Ordering}; use std::sync::Arc; use std::thread; use std::time::Duration; use std::time::Instant; -static TIMEOUT: Duration = Duration::from_millis(10); +static TIMEOUT: Duration = Duration::from_millis(100); #[test] fn multi_condition_variable_construction_works() { @@ -369,6 +370,7 @@ fn condition_variable_modify_notify_all_signals_all_waiters() { #[test] fn condition_variable_modify_notify_one_signals_one_waiter() { + let _watchdog = Watchdog::new(Duration::from_secs(10)); let handle = MutexHandle::>::new(); thread::scope(|s| { let counter = Arc::new(AtomicI32::new(0)); @@ -396,6 +398,8 @@ fn condition_variable_modify_notify_one_signals_one_waiter() { let counter_old_1 = counter.load(Ordering::Relaxed); sut.modify_notify_one(|value| *value = 2213).unwrap(); + while counter.load(Ordering::Relaxed) == 0 {} + thread::sleep(TIMEOUT); let counter_old_2 = counter.load(Ordering::Relaxed); sut.modify_notify_one(|value| *value = 2213).unwrap(); diff --git a/iceoryx2-bb/posix/tests/mutex_tests.rs b/iceoryx2-bb/posix/tests/mutex_tests.rs index b72a5ae32..4e2ffec95 100644 --- a/iceoryx2-bb/posix/tests/mutex_tests.rs +++ b/iceoryx2-bb/posix/tests/mutex_tests.rs @@ -16,6 +16,7 @@ use iceoryx2_bb_posix::system_configuration::Feature; use iceoryx2_bb_posix::unmovable_ipc_handle::AcquireIpcHandleError; use iceoryx2_bb_testing::assert_that; use iceoryx2_bb_testing::test_requires; +use iceoryx2_bb_testing::watchdog::Watchdog; use std::sync::Arc; use std::sync::Barrier; use std::thread; @@ -429,6 +430,7 @@ fn mutex_can_be_recovered_when_thread_died() { #[test] fn mutex_in_unrecoverable_state_if_state_of_leaked_mutex_is_not_repaired() { + let _watchdog = Watchdog::new(Duration::from_secs(10)); let handle = MutexHandle::::new(); let sut = MutexBuilder::new() .thread_termination_behavior(MutexThreadTerminationBehavior::ReleaseWhenLocked) diff --git a/iceoryx2-bb/threadsafe/tests/trigger_queue_tests.rs b/iceoryx2-bb/threadsafe/tests/trigger_queue_tests.rs index 7513b3488..6ef9f9aa1 100644 --- a/iceoryx2-bb/threadsafe/tests/trigger_queue_tests.rs +++ b/iceoryx2-bb/threadsafe/tests/trigger_queue_tests.rs @@ -17,9 +17,10 @@ use std::time::Duration; use iceoryx2_bb_posix::clock::{nanosleep, Time}; use iceoryx2_bb_posix::mutex::MutexHandle; use iceoryx2_bb_testing::assert_that; +use iceoryx2_bb_testing::watchdog::Watchdog; use iceoryx2_bb_threadsafe::trigger_queue::*; -const TIMEOUT: Duration = Duration::from_millis(25); +const TIMEOUT: Duration = Duration::from_millis(100); const SUT_CAPACITY: usize = 128; type Sut<'a> = TriggerQueue<'a, usize, SUT_CAPACITY>; @@ -153,6 +154,7 @@ fn trigger_queue_blocking_push_blocks_until_there_is_space_again() { let mtx_handle = MutexHandle::new(); let free_handle = UnnamedSemaphoreHandle::new(); let used_handle = UnnamedSemaphoreHandle::new(); + let _watchdog = Watchdog::new(Duration::from_secs(10)); let sut = Sut::new(&mtx_handle, &free_handle, &used_handle); @@ -170,10 +172,11 @@ fn trigger_queue_blocking_push_blocks_until_there_is_space_again() { nanosleep(TIMEOUT).unwrap(); let counter_old = counter.load(Ordering::Relaxed); sut.blocking_pop(); - nanosleep(TIMEOUT).unwrap(); assert_that!(counter_old, eq 0); - assert_that!(counter.load(Ordering::Relaxed), eq 1); + + // if the thread is not unblocked the counter stays zero until the watchdog intervenes + while counter.load(Ordering::Relaxed) == 0 {} }); } @@ -182,6 +185,7 @@ fn trigger_queue_blocking_pop_blocks_until_there_is_something_pushed() { let mtx_handle = MutexHandle::new(); let free_handle = UnnamedSemaphoreHandle::new(); let used_handle = UnnamedSemaphoreHandle::new(); + let _watchdog = Watchdog::new(Duration::from_secs(10)); let sut = Sut::new(&mtx_handle, &free_handle, &used_handle); @@ -196,10 +200,11 @@ fn trigger_queue_blocking_pop_blocks_until_there_is_something_pushed() { nanosleep(TIMEOUT).unwrap(); let counter_old = counter.load(Ordering::Relaxed); sut.blocking_push(0); - nanosleep(TIMEOUT).unwrap(); assert_that!(counter_old, eq 0); - assert_that!(counter.load(Ordering::Relaxed), eq 1); + + // if the thread is not unblocked the counter stays zero until the watchdog intervenes + while counter.load(Ordering::Relaxed) == 0 {} }); } diff --git a/iceoryx2-pal/concurrency-sync/tests/condition_variable_tests.rs b/iceoryx2-pal/concurrency-sync/tests/condition_variable_tests.rs index 40694a165..0b7f48f1c 100644 --- a/iceoryx2-pal/concurrency-sync/tests/condition_variable_tests.rs +++ b/iceoryx2-pal/concurrency-sync/tests/condition_variable_tests.rs @@ -49,13 +49,13 @@ impl ThreadInWait { loop { let mut wait_for_thread = false; for v in &self.thread_in_wait { - if v.load(Ordering::Relaxed) == false { + if !v.load(Ordering::Relaxed) { wait_for_thread = true; break; } } - if wait_for_thread == false { + if !wait_for_thread { break; } }