Skip to content

Commit 80aece0

Browse files
committed
Add test for notify_all
1 parent 8c1f275 commit 80aece0

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/sync/condvar.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ impl<'a, 'b, T> Drop for AwaitNotify<'a, 'b, T> {
352352
// we are dropping it before it can finished.
353353
// This may result in a spurious wake-up, but that's ok.
354354
notify(blocked, false);
355-
356355
}
357356
}
358357
}

tests/condvar.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22
use std::time::Duration;
33

44
use async_std::sync::{Condvar, Mutex};
5-
use async_std::task;
5+
use async_std::task::{self, JoinHandle};
66

77
#[test]
88
fn wait_timeout() {
@@ -25,3 +25,38 @@ fn wait_timeout() {
2525
})
2626
}
2727

28+
#[test]
29+
fn notify_all() {
30+
task::block_on(async {
31+
let mut tasks: Vec<JoinHandle<()>> = Vec::new();
32+
let pair = Arc::new((Mutex::new(0u32), Condvar::new()));
33+
34+
for i in 0..10 {
35+
let pair = pair.clone();
36+
tasks.push(task::spawn(async move {
37+
let (m, c) = &*pair;
38+
let mut count = m.lock().await;
39+
while *count == 0 {
40+
count = c.wait(count).await;
41+
}
42+
*count += 1;
43+
}));
44+
}
45+
46+
// Give some time for tasks to start up
47+
task::sleep(Duration::from_millis(5)).await;
48+
49+
let (m, c) = &*pair;
50+
{
51+
let mut count = m.lock().await;
52+
*count += 1;
53+
c.notify_all();
54+
}
55+
56+
for t in tasks {
57+
t.await;
58+
}
59+
let count = m.lock().await;
60+
assert!(*count == 11);
61+
})
62+
}

0 commit comments

Comments
 (0)