diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-10-31 15:02:49 +0100 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2021-10-31 15:23:36 +0100 |
| commit | 67362b301bd00802c10c9fa9bc7396dc7ada83dd (patch) | |
| tree | 6ee232ffa6428b77fe8cc3f6b3bb349679985a74 /library/std/src/thread | |
| parent | d718b1a79508169f10cd4b691071d4308ac15fc3 (diff) | |
| download | rust-67362b301bd00802c10c9fa9bc7396dc7ada83dd.tar.gz rust-67362b301bd00802c10c9fa9bc7396dc7ada83dd.zip | |
Add test for JoinHandle::is_running.
Diffstat (limited to 'library/std/src/thread')
| -rw-r--r-- | library/std/src/thread/tests.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/library/std/src/thread/tests.rs b/library/std/src/thread/tests.rs index 16ad366fc12..ca0d88135a5 100644 --- a/library/std/src/thread/tests.rs +++ b/library/std/src/thread/tests.rs @@ -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. !!! @@ -47,6 +51,36 @@ fn test_run_basic() { } #[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() { result::Result::Err(_) => (), |
