From 6acf69b6527f5b156c884ece8f3883a89cd67010 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 12 Nov 2023 12:18:11 +0300 Subject: core: enhance the CondWait test coverage --- core/src/async_utils/condwait.rs | 43 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/core/src/async_utils/condwait.rs b/core/src/async_utils/condwait.rs index f16a99e..e31fac3 100644 --- a/core/src/async_utils/condwait.rs +++ b/core/src/async_utils/condwait.rs @@ -77,20 +77,57 @@ impl Default for CondWait { #[cfg(test)] mod tests { use super::*; - use std::sync::Arc; + use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, + }; #[test] fn test_cond_wait() { smol::block_on(async { let cond_wait = Arc::new(CondWait::new()); + let count = Arc::new(AtomicUsize::new(0)); + let cond_wait_cloned = cond_wait.clone(); + let count_cloned = count.clone(); let task = smol::spawn(async move { cond_wait_cloned.wait().await; - true + count_cloned.fetch_add(1, Ordering::Relaxed); + // do something }); + // Send a signal to the waiting task cond_wait.signal().await; - assert!(task.await); + + task.await; + + // Reset the boolean flag + cond_wait.reset().await; + + assert_eq!(count.load(Ordering::Relaxed), 1); + + let cond_wait_cloned = cond_wait.clone(); + let count_cloned = count.clone(); + let task1 = smol::spawn(async move { + cond_wait_cloned.wait().await; + count_cloned.fetch_add(1, Ordering::Relaxed); + // do something + }); + + let cond_wait_cloned = cond_wait.clone(); + let count_cloned = count.clone(); + let task2 = smol::spawn(async move { + cond_wait_cloned.wait().await; + count_cloned.fetch_add(1, Ordering::Relaxed); + // do something + }); + + // Broadcast a signal to all waiting tasks + cond_wait.broadcast().await; + + task1.await; + task2.await; + assert_eq!(count.load(Ordering::Relaxed), 3); }); } } -- cgit v1.2.3