Skip to content

Commit 7cefb25

Browse files
committed
Add test for notify_all
1 parent b43809a commit 7cefb25

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/sync/condvar.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct WaitTimeoutResult(bool);
1515
/// not
1616
impl WaitTimeoutResult {
1717
/// Returns `true` if the wait was known to have timed out.
18-
pub fn timed_out(&self) -> bool {
18+
pub fn timed_out(self) -> bool {
1919
self.0
2020
}
2121
}
@@ -62,6 +62,12 @@ pub struct Condvar {
6262
blocked: std::sync::Mutex<Slab<Option<Waker>>>,
6363
}
6464

65+
impl Default for Condvar {
66+
fn default() -> Self {
67+
Condvar::new()
68+
}
69+
}
70+
6571
impl Condvar {
6672
/// Creates a new condition variable
6773
///
@@ -111,6 +117,7 @@ impl Condvar {
111117
/// }
112118
/// # }) }
113119
/// ```
120+
#[allow(clippy::needless_lifetimes)]
114121
pub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
115122
let mutex = guard_lock(&guard);
116123

@@ -161,6 +168,7 @@ impl Condvar {
161168
/// # }) }
162169
/// ```
163170
#[cfg(feature = "unstable")]
171+
#[allow(clippy::needless_lifetimes)]
164172
pub async fn wait_until<'a, T, F>(
165173
&self,
166174
mut guard: MutexGuard<'a, T>,
@@ -213,6 +221,7 @@ impl Condvar {
213221
/// #
214222
/// # }) }
215223
/// ```
224+
#[allow(clippy::needless_lifetimes)]
216225
pub async fn wait_timeout<'a, T>(
217226
&self,
218227
guard: MutexGuard<'a, T>,
@@ -352,7 +361,6 @@ impl<'a, 'b, T> Drop for AwaitNotify<'a, 'b, T> {
352361
// we are dropping it before it can finished.
353362
// This may result in a spurious wake-up, but that's ok.
354363
notify(blocked, false);
355-
356364
}
357365
}
358366
}

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 _ 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_eq!(11, *count);
61+
})
62+
}

0 commit comments

Comments
 (0)