From a0bb28126381abc956f7b3d5b94dbb7285842400 Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Sun, 13 Oct 2024 18:20:27 +0800 Subject: [PATCH 1/2] [#213] test more cases for unique_system_id --- iceoryx2-bb/posix/src/unique_system_id.rs | 53 +++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/iceoryx2-bb/posix/src/unique_system_id.rs b/iceoryx2-bb/posix/src/unique_system_id.rs index 22f8eb43c..060eb122b 100644 --- a/iceoryx2-bb/posix/src/unique_system_id.rs +++ b/iceoryx2-bb/posix/src/unique_system_id.rs @@ -98,7 +98,6 @@ impl From for UniqueSystemId { impl UniqueSystemId { /// Creates a new system wide unique id pub fn new() -> Result { - static COUNTER: IoxAtomicU32 = IoxAtomicU32::new(0); let msg = "Failed to create UniqueSystemId"; let pid = Process::from_self().id().value() as _; let now = fail!(from "UniqueSystemId::new()", @@ -106,12 +105,17 @@ impl UniqueSystemId { with UniqueSystemIdCreationError::FailedToAcquireTime, "{} since the current time could not be acquired.", msg); - Ok(UniqueSystemId { + Ok(Self::create(pid, now)) + } + + fn create(pid: u32, now: Time) -> UniqueSystemId { + static COUNTER: IoxAtomicU32 = IoxAtomicU32::new(0); + UniqueSystemId { pid, seconds: now.seconds() as u32, nanoseconds: now.nanoseconds(), counter: COUNTER.fetch_add(1, Ordering::Relaxed), - }) + } } /// Returns the underlying value of the new system wide unique id @@ -133,3 +137,46 @@ impl UniqueSystemId { } } } + +#[cfg(test)] +use iceoryx2_bb_testing::assert_that; + +#[test] +// ensures the unique_system_id is unique when a process creates the id simultaneously. +fn test_unique_system_id_when_creating_simultaneously() { + let pid = Process::from_self().id().value() as _; + let now = Time::now_with_clock(ClockType::default()).unwrap(); + + let handle1 = + std::thread::spawn(move || -> UniqueSystemId { UniqueSystemId::create(pid, now) }); + let handle2 = + std::thread::spawn(move || -> UniqueSystemId { UniqueSystemId::create(pid, now) }); + + let id1 = handle1.join().unwrap(); + let id2 = handle2.join().unwrap(); + assert_that!(id1.pid(), eq id2.pid()); + assert_that!(id1.creation_time(), eq id2.creation_time()); + assert_that!(id1.value(), ne id2.value()); +} + +#[test] +// ensures the unique_system_id is unique when 2 processes create their id simultaneously. +fn test_unique_system_id_across_processes() { + let pid = Process::from_self().id().value() as _; + let now = Time::now_with_clock(ClockType::default()).unwrap(); + + let id1 = UniqueSystemId::create(pid, now); + // ideally, fork and exec the current process to reset the static counter inside UniqueSystemId::create + // is better, but to ease the test now, we can duplicate the logic inside the lambda instead. + static COUNTER: IoxAtomicU32 = IoxAtomicU32::new(0); + let id2 = UniqueSystemId { + pid: pid + 1, + seconds: now.seconds() as u32, + nanoseconds: now.nanoseconds(), + counter: COUNTER.fetch_add(1, Ordering::Relaxed), + }; + + assert_that!(id1.pid(), ne id2.pid()); + assert_that!(id1.creation_time(), eq id2.creation_time()); + assert_that!(id1.value(), ne id2.value()); +} From 32dd7734cb1e10900d9573bae9a4c795780f561f Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Sun, 13 Oct 2024 22:54:14 +0800 Subject: [PATCH 2/2] [#213] test more stages inside waiter --- .../posix/tests/adaptive_wait_tests.rs | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/iceoryx2-bb/posix/tests/adaptive_wait_tests.rs b/iceoryx2-bb/posix/tests/adaptive_wait_tests.rs index 420996b02..56becd365 100644 --- a/iceoryx2-bb/posix/tests/adaptive_wait_tests.rs +++ b/iceoryx2-bb/posix/tests/adaptive_wait_tests.rs @@ -20,20 +20,50 @@ use std::time::Instant; const TIMEOUT: Duration = Duration::from_millis(50); #[test] -fn adaptive_wait_wait_loop_initial_repetitions_plus_one_is_at_least_final_waiting_time() { - let start = Instant::now(); +fn adaptive_wait_wait_at_different_time_depends_on_repetition_times() { let mut counter: u64 = 0; - AdaptiveWaitBuilder::new() - .create() - .unwrap() + let mut waiter = AdaptiveWaitBuilder::new().create().unwrap(); + + waiter + .wait_while(move || -> bool { + counter += 1; + counter <= ADAPTIVE_WAIT_YIELD_REPETITIONS + }) + .expect("failed to test wait_loop"); + // the waiter starts to sleep ADAPTIVE_WAIT_INITIAL_WAITING_TIME instead of yield later. + assert_that!(waiter.yield_count(), eq ADAPTIVE_WAIT_YIELD_REPETITIONS); + + // test sleep time ADAPTIVE_WAIT_INITIAL_WAITING_TIME + let start = Instant::now(); + waiter .wait_while(move || -> bool { counter += 1; - counter < ADAPTIVE_WAIT_INITIAL_REPETITIONS + counter <= 1 // stop at the second turn }) .expect("failed to test wait_loop"); + assert_that!(start.elapsed(), time_at_least ADAPTIVE_WAIT_INITIAL_WAITING_TIME); - assert_that!(start.elapsed(), ge ADAPTIVE_WAIT_FINAL_WAITING_TIME); + waiter + .wait_while(move || -> bool { + counter += 1; + // continue to reach the edge of sleeping time. + counter < ADAPTIVE_WAIT_INITIAL_REPETITIONS - ADAPTIVE_WAIT_YIELD_REPETITIONS + }) + .expect("failed to test wait_loop"); + + // verify the waiter will enter the next stage at the next repetition. + // the waiter starts to sleep longer as ADAPTIVE_WAIT_FINAL_WAITING_TIME + // instead of ADAPTIVE_WAIT_INITIAL_WAITING_TIME later. + assert_that!(waiter.yield_count(), eq ADAPTIVE_WAIT_INITIAL_REPETITIONS); + let start = Instant::now(); + waiter + .wait_while(move || -> bool { + counter += 1; + counter <= 1 // stop at the second turn + }) + .expect("failed to test wait_loop"); + assert_that!(start.elapsed(), time_at_least ADAPTIVE_WAIT_FINAL_WAITING_TIME); } #[test]