use std::sync::Arc;
use std::time::Duration;
use tokio::task::JoinHandle;
use crate::condvar::Condvar;
use crate::mutex::Mutex;
use crate::test_runtime;
#[test]
fn notify_all() {
test_runtime().block_on(async {
let mut tasks: Vec<JoinHandle<()>> = Vec::new();
let pair = Arc::new((Mutex::new(0u32), Condvar::new()));
for _ in 0..10 {
let pair = pair.clone();
tasks.push(tokio::spawn(async move {
let (m, c) = &*pair;
let mut count = m.lock().await;
while *count == 0 {
count = c.wait(count).await;
}
*count += 1;
}));
}
tokio::time::sleep(Duration::from_millis(50)).await;
let (m, c) = &*pair;
{
let mut count = m.lock().await;
*count += 1;
c.notify_all();
}
for t in tasks {
t.await.unwrap();
}
let count = m.lock().await;
assert_eq!(11, *count);
});
}