Skip to content

Commit

Permalink
Add test for wait_timeout with no lock held
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Nov 24, 2019
1 parent 550e24f commit 811280e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl<'a, 'b, T> Future for AwaitNotify<'a, 'b, T> {
}
None => {
if let Some(key) = self.key {
if self.cond.wakers.complete_if_notified(key, cx) {
if self.cond.wakers.remove_if_notified(key, cx) {
self.key = None;
Poll::Ready(())
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/sync/waker_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ impl WakerSet {
/// the waker for the entry, and return false. If the waker has been notified,
/// treat the entry as completed and return true.
#[cfg(feature = "unstable")]
pub fn complete_if_notified(&self, key: usize, cx: &Context<'_>) -> bool {
pub fn remove_if_notified(&self, key: usize, cx: &Context<'_>) -> bool {
let mut inner = self.lock();

match &mut inner.entries[key] {
None => {
inner.entries.remove(key);
inner.none_count -= 1;
true
}
Some(w) => {
Expand Down
15 changes: 14 additions & 1 deletion tests/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_std::sync::{Condvar, Mutex};
use async_std::task::{self, JoinHandle};

#[test]
fn wait_timeout() {
fn wait_timeout_with_lock() {
task::block_on(async {
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
Expand All @@ -26,6 +26,19 @@ fn wait_timeout() {
})
}

#[test]
fn wait_timeout_without_lock() {
task::block_on(async {
let m = Mutex::new(false);
let c = Condvar::new();

let (_, wait_result) = c
.wait_timeout(m.lock().await, Duration::from_millis(10))
.await;
assert!(wait_result.timed_out());
})
}

#[test]
fn wait_timeout_until_timed_out() {
task::block_on(async {
Expand Down

0 comments on commit 811280e

Please sign in to comment.