Skip to content

Commit

Permalink
Add test for JoinHandle::is_running.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Oct 31, 2021
1 parent d718b1a commit 67362b3
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion library/std/src/thread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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. !!!
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 67362b3

Please sign in to comment.