@@ -2,9 +2,13 @@ use super::Builder;
2
2
use crate :: any:: Any ;
3
3
use crate :: mem;
4
4
use crate :: result;
5
- use crate :: sync:: mpsc:: { channel, Sender } ;
5
+ use crate :: sync:: {
6
+ mpsc:: { channel, Sender } ,
7
+ Arc , Barrier ,
8
+ } ;
6
9
use crate :: thread:: { self , ThreadId } ;
7
10
use crate :: time:: Duration ;
11
+ use crate :: time:: Instant ;
8
12
9
13
// !!! These tests are dangerous. If something is buggy, they will hang, !!!
10
14
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
@@ -46,6 +50,36 @@ fn test_run_basic() {
46
50
rx. recv ( ) . unwrap ( ) ;
47
51
}
48
52
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
+
49
83
#[ test]
50
84
fn test_join_panic ( ) {
51
85
match thread:: spawn ( move || panic ! ( ) ) . join ( ) {
0 commit comments