File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed
crates/cargo-test-support/src Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
13
13
use std:: process:: { Command , Output } ;
14
14
use std:: str;
15
15
use std:: sync:: OnceLock ;
16
+ use std:: thread:: JoinHandle ;
16
17
use std:: time:: { self , Duration } ;
17
18
18
19
use anyhow:: { bail, Result } ;
@@ -1500,3 +1501,20 @@ where
1500
1501
fn retry_fails ( ) {
1501
1502
retry ( 2 , || None :: < ( ) > ) ;
1502
1503
}
1504
+
1505
+ /// Helper that waits for a thread to finish, up to `n` tenths of a second.
1506
+ pub fn thread_wait_timeout < T > ( n : u32 , thread : JoinHandle < T > ) -> T {
1507
+ retry ( n, || thread. is_finished ( ) . then_some ( ( ) ) ) ;
1508
+ thread. join ( ) . unwrap ( )
1509
+ }
1510
+
1511
+ /// Helper that runs some function, and waits up to `n` tenths of a second for
1512
+ /// it to finish.
1513
+ pub fn threaded_timeout < F , R > ( n : u32 , f : F ) -> R
1514
+ where
1515
+ F : FnOnce ( ) -> R + Send + ' static ,
1516
+ R : Send + ' static ,
1517
+ {
1518
+ let thread = std:: thread:: spawn ( || f ( ) ) ;
1519
+ thread_wait_timeout ( n, thread)
1520
+ }
You can’t perform that action at this time.
0 commit comments