Add a timeout test for conditional variables.

This commit is contained in:
Vytautas Astrauskas 2020-04-30 14:59:35 -07:00 committed by Vytautas Astrauskas
parent 6e774dec86
commit 4a303b1309

View File

@ -1,8 +1,10 @@
// ignore-windows: Concurrency on Windows is not supported yet.
// compile-flags: -Zmiri-disable-isolation
use std::sync::mpsc::{channel, sync_channel};
use std::sync::{Arc, Barrier, Condvar, Mutex, Once, RwLock};
use std::thread;
use std::time::{Duration, Instant};
// Check if Rust barriers are working.
@ -50,6 +52,17 @@ fn check_conditional_variables() {
}
}
/// Test that waiting on a conditional variable with a timeout does not
/// deadlock.
fn check_conditional_variables_timeout() {
let lock = Mutex::new(());
let cvar = Condvar::new();
let guard = lock.lock().unwrap();
let now = Instant::now();
let _guard = cvar.wait_timeout(guard, Duration::from_millis(100)).unwrap().0;
assert!(now.elapsed().as_millis() >= 100);
}
// Check if locks are working.
fn check_mutex() {
@ -206,6 +219,7 @@ fn check_once() {
fn main() {
check_barriers();
check_conditional_variables();
check_conditional_variables_timeout();
check_mutex();
check_rwlock_write();
check_rwlock_read_no_deadlock();