Add test for JoinHandle::is_running.

This commit is contained in:
Mara Bos 2021-10-31 15:02:49 +01:00
parent d718b1a795
commit 67362b301b

View file

@ -2,9 +2,13 @@ use super::Builder;
use crate::any::Any;
use crate::mem;
use crate::result;
use crate::sync::mpsc::{channel, Sender};
use crate::sync::{
mpsc::{channel, Sender},
Arc, Barrier,
};
use crate::thread::{self, ThreadId};
use crate::time::Duration;
use crate::time::Instant;
// !!! These tests are dangerous. If something is buggy, they will hang, !!!
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
@ -46,6 +50,36 @@ fn test_run_basic() {
rx.recv().unwrap();
}
#[test]
fn test_is_running() {
let b = Arc::new(Barrier::new(2));
let t = thread::spawn({
let b = b.clone();
move || {
b.wait();
1234
}
});
// Thread is definitely running here, since it's still waiting for the barrier.
assert_eq!(t.is_running(), true);
// Unblock the barrier.
b.wait();
// Now check that t.is_running() becomes false within a reasonable time.
let start = Instant::now();
while t.is_running() {
assert!(start.elapsed() < Duration::from_secs(2));
thread::sleep(Duration::from_millis(15));
}
// Joining the thread should not block for a significant time now.
let join_time = Instant::now();
assert_eq!(t.join().unwrap(), 1234);
assert!(join_time.elapsed() < Duration::from_secs(2));
}
#[test]
fn test_join_panic() {
match thread::spawn(move || panic!()).join() {