From 834b023e1a6843cb71dc68084006099b0369fbba Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Mon, 25 Mar 2024 16:33:02 +0800 Subject: [PATCH] Move test_helpers.rs into tests to follow the guideline of integration tests. --- commons/zenoh-shm/src/lib.rs | 10 ---------- .../test_helpers.rs => tests/common/mod.rs} | 17 +++++++++-------- commons/zenoh-shm/tests/header.rs | 12 ++++++------ commons/zenoh-shm/tests/periodic_task.rs | 5 ++++- commons/zenoh-shm/tests/posix_array.rs | 5 ++++- commons/zenoh-shm/tests/posix_segment.rs | 8 ++++---- commons/zenoh-shm/tests/watchdog.rs | 10 +++++----- 7 files changed, 32 insertions(+), 35 deletions(-) rename commons/zenoh-shm/{src/test_helpers.rs => tests/common/mod.rs} (93%) diff --git a/commons/zenoh-shm/src/lib.rs b/commons/zenoh-shm/src/lib.rs index 13bc69ee9a..c0e10e4142 100644 --- a/commons/zenoh-shm/src/lib.rs +++ b/commons/zenoh-shm/src/lib.rs @@ -39,14 +39,6 @@ macro_rules! tested_crate_module { }; } -#[macro_export] -macro_rules! test_helpers_module { - () => { - #[cfg(feature = "test")] - pub mod test_helpers; - }; -} - pub mod api; pub mod header; pub mod posix_shm; @@ -54,8 +46,6 @@ pub mod reader; pub mod watchdog; pub mod zsliceshm_access; -test_helpers_module!(); - /// Informations about a [`SharedMemoryBuf`]. /// /// This that can be serialized and can be used to retrieve the [`SharedMemoryBuf`] in a remote process. diff --git a/commons/zenoh-shm/src/test_helpers.rs b/commons/zenoh-shm/tests/common/mod.rs similarity index 93% rename from commons/zenoh-shm/src/test_helpers.rs rename to commons/zenoh-shm/tests/common/mod.rs index d9fc68cab9..a97773f686 100644 --- a/commons/zenoh-shm/src/test_helpers.rs +++ b/commons/zenoh-shm/tests/common/mod.rs @@ -12,7 +12,10 @@ // ZettaScale Zenoh Team, // -use std::{sync::{Arc, atomic::AtomicBool}, thread::JoinHandle}; +use std::{ + sync::{atomic::AtomicBool, Arc}, + thread::JoinHandle, +}; use zenoh_result::ZResult; @@ -66,7 +69,7 @@ pub fn load_fn( pub struct CpuLoad { handle: Option>, - flag: Arc + flag: Arc, } impl Drop for CpuLoad { @@ -89,16 +92,14 @@ impl CpuLoad { Self::new(1) } - fn new(thread_count: usize) -> Self { + fn new(thread_count: usize) -> Self { let flag = Arc::new(AtomicBool::new(true)); - + let c_flag = flag.clone(); let handle = Some(std::thread::spawn(move || { execute_concurrent(thread_count, 1, load_fn(c_flag)); })); - - Self{handle, flag} - + + Self { handle, flag } } } - diff --git a/commons/zenoh-shm/tests/header.rs b/commons/zenoh-shm/tests/header.rs index ab02a45b37..a734abf108 100644 --- a/commons/zenoh-shm/tests/header.rs +++ b/commons/zenoh-shm/tests/header.rs @@ -16,14 +16,14 @@ use std::sync::atomic::Ordering::Relaxed; use rand::Rng; use zenoh_result::ZResult; -use zenoh_shm::{ - header::{ - descriptor::HeaderDescriptor, storage::GLOBAL_HEADER_STORAGE, - subscription::GLOBAL_HEADER_SUBSCRIPTION, - }, - test_helpers::execute_concurrent, +use zenoh_shm::header::{ + descriptor::HeaderDescriptor, storage::GLOBAL_HEADER_STORAGE, + subscription::GLOBAL_HEADER_SUBSCRIPTION, }; +pub mod common; +use common::execute_concurrent; + fn header_alloc_fn() -> impl Fn(usize, usize) -> ZResult<()> + Clone + Send + Sync + 'static { |_task_index: usize, _iteration: usize| -> ZResult<()> { let _allocated_header = GLOBAL_HEADER_STORAGE.allocate_header()?; diff --git a/commons/zenoh-shm/tests/periodic_task.rs b/commons/zenoh-shm/tests/periodic_task.rs index 0ab27e3768..dcfd560d7d 100644 --- a/commons/zenoh-shm/tests/periodic_task.rs +++ b/commons/zenoh-shm/tests/periodic_task.rs @@ -17,7 +17,10 @@ use std::{ time::{Duration, Instant}, }; -use zenoh_shm::{test_helpers::CpuLoad, watchdog::periodic_task::PeriodicTask}; +use zenoh_shm::watchdog::periodic_task::PeriodicTask; + +pub mod common; +use common::CpuLoad; const TASK_PERIOD: Duration = Duration::from_millis(50); const TASK_DELTA: Duration = Duration::from_millis(5); diff --git a/commons/zenoh-shm/tests/posix_array.rs b/commons/zenoh-shm/tests/posix_array.rs index 05526438f1..562102ea17 100644 --- a/commons/zenoh-shm/tests/posix_array.rs +++ b/commons/zenoh-shm/tests/posix_array.rs @@ -15,7 +15,10 @@ use std::{fmt::Debug, mem::size_of}; use num_traits::{AsPrimitive, PrimInt, Unsigned}; -use zenoh_shm::{posix_shm::array::ArrayInSHM, test_helpers::TEST_SEGMENT_PREFIX}; +use zenoh_shm::posix_shm::array::ArrayInSHM; + +pub mod common; +use common::TEST_SEGMENT_PREFIX; type TestSegmentID = u32; diff --git a/commons/zenoh-shm/tests/posix_segment.rs b/commons/zenoh-shm/tests/posix_segment.rs index 2f7c3c4dd4..907f70cc4e 100644 --- a/commons/zenoh-shm/tests/posix_segment.rs +++ b/commons/zenoh-shm/tests/posix_segment.rs @@ -14,10 +14,10 @@ use std::{fmt::Display, slice}; -use zenoh_shm::{ - posix_shm::segment::Segment, - test_helpers::{validate_memory, TEST_SEGMENT_PREFIX}, -}; +use zenoh_shm::posix_shm::segment::Segment; + +pub mod common; +use common::{validate_memory, TEST_SEGMENT_PREFIX}; fn validate_segment(segment1: &Segment, segment2: &Segment) where diff --git a/commons/zenoh-shm/tests/watchdog.rs b/commons/zenoh-shm/tests/watchdog.rs index 3c78e01f1b..4faa0ea529 100644 --- a/commons/zenoh-shm/tests/watchdog.rs +++ b/commons/zenoh-shm/tests/watchdog.rs @@ -18,13 +18,13 @@ use std::{ }; use zenoh_result::{bail, ZResult}; -use zenoh_shm::{ - test_helpers::{execute_concurrent, CpuLoad}, - watchdog::{ - confirmator::GLOBAL_CONFIRMATOR, storage::GLOBAL_STORAGE, validator::GLOBAL_VALIDATOR, - }, +use zenoh_shm::watchdog::{ + confirmator::GLOBAL_CONFIRMATOR, storage::GLOBAL_STORAGE, validator::GLOBAL_VALIDATOR, }; +pub mod common; +use common::{execute_concurrent, CpuLoad}; + const VALIDATION_PERIOD: Duration = Duration::from_millis(100); const CONFIRMATION_PERIOD: Duration = Duration::from_millis(50);