@@ -2,9 +2,13 @@ use super::Builder;
22use crate :: any:: Any ;
33use crate :: mem;
44use crate :: result;
5- use crate :: sync:: mpsc:: { channel, Sender } ;
5+ use crate :: sync:: {
6+ mpsc:: { channel, Sender } ,
7+ Arc , Barrier ,
8+ } ;
69use crate :: thread:: { self , ThreadId } ;
710use crate :: time:: Duration ;
11+ use crate :: time:: Instant ;
812
913// !!! These tests are dangerous. If something is buggy, they will hang, !!!
1014// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
@@ -46,6 +50,36 @@ fn test_run_basic() {
4650 rx. recv ( ) . unwrap ( ) ;
4751}
4852
53+ #[ test]
54+ fn test_is_running ( ) {
55+ let b = Arc :: new ( Barrier :: new ( 2 ) ) ;
56+ let t = thread:: spawn ( {
57+ let b = b. clone ( ) ;
58+ move || {
59+ b. wait ( ) ;
60+ 1234
61+ }
62+ } ) ;
63+
64+ // Thread is definitely running here, since it's still waiting for the barrier.
65+ assert_eq ! ( t. is_running( ) , true ) ;
66+
67+ // Unblock the barrier.
68+ b. wait ( ) ;
69+
70+ // Now check that t.is_running() becomes false within a reasonable time.
71+ let start = Instant :: now ( ) ;
72+ while t. is_running ( ) {
73+ assert ! ( start. elapsed( ) < Duration :: from_secs( 2 ) ) ;
74+ thread:: sleep ( Duration :: from_millis ( 15 ) ) ;
75+ }
76+
77+ // Joining the thread should not block for a significant time now.
78+ let join_time = Instant :: now ( ) ;
79+ assert_eq ! ( t. join( ) . unwrap( ) , 1234 ) ;
80+ assert ! ( join_time. elapsed( ) < Duration :: from_secs( 2 ) ) ;
81+ }
82+
4983#[ test]
5084fn test_join_panic ( ) {
5185 match thread:: spawn ( move || panic ! ( ) ) . join ( ) {
0 commit comments