diff options
author | hozan23 <hozan23@proton.me> | 2023-11-12 12:18:11 +0300 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2023-11-12 12:18:11 +0300 |
commit | 6acf69b6527f5b156c884ece8f3883a89cd67010 (patch) | |
tree | 77ed9977b53a2027c036295cd735eba8acd2e24d | |
parent | 5d91ead06c62fd7c3cd846659b935012616ce5ae (diff) |
core: enhance the CondWait test coverage
-rw-r--r-- | core/src/async_utils/condwait.rs | 43 |
1 files 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); }); } } |