Add test helpers for waiting for an operation with a timeout.

This commit is contained in:
Eric Huss 2023-09-06 19:30:45 -07:00
parent 83fff2d1b1
commit b85ef38187

View File

@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::str;
use std::sync::OnceLock;
use std::thread::JoinHandle;
use std::time::{self, Duration};
use anyhow::{bail, Result};
@ -1500,3 +1501,20 @@ where
fn retry_fails() {
retry(2, || None::<()>);
}
/// Helper that waits for a thread to finish, up to `n` tenths of a second.
pub fn thread_wait_timeout<T>(n: u32, thread: JoinHandle<T>) -> T {
retry(n, || thread.is_finished().then_some(()));
thread.join().unwrap()
}
/// Helper that runs some function, and waits up to `n` tenths of a second for
/// it to finish.
pub fn threaded_timeout<F, R>(n: u32, f: F) -> R
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let thread = std::thread::spawn(|| f());
thread_wait_timeout(n, thread)
}