Skip to content

Commit b85ef38

Browse files
committed
Add test helpers for waiting for an operation with a timeout.
1 parent 83fff2d commit b85ef38

File tree

1 file changed

+18
-0
lines changed
  • crates/cargo-test-support/src

1 file changed

+18
-0
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
1313
use std::process::{Command, Output};
1414
use std::str;
1515
use std::sync::OnceLock;
16+
use std::thread::JoinHandle;
1617
use std::time::{self, Duration};
1718

1819
use anyhow::{bail, Result};
@@ -1500,3 +1501,20 @@ where
15001501
fn retry_fails() {
15011502
retry(2, || None::<()>);
15021503
}
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+
}

0 commit comments

Comments
 (0)