use std::{process, sync::mpsc, thread, time::Duration};
#[inline]
pub(crate) fn make_pattern_data(len: usize, factor: usize, add: usize) -> Vec<u8> {
(0..len).map(|i| (i * factor + add) as u8).collect()
}
pub(crate) struct Watchdog {
tx: Option<mpsc::Sender<()>>,
handle: Option<thread::JoinHandle<()>>,
}
impl Watchdog {
pub(crate) fn start(secs: u64) -> Self {
let (tx, rx) = mpsc::channel::<()>();
let handle = thread::spawn(move || {
if rx.recv_timeout(Duration::from_secs(secs)).is_err() {
eprintln!("看门狗超时({secs}s):sync 契约测试挂死,强制退出进程");
process::exit(101);
}
});
Self {
tx: Some(tx),
handle: Some(handle),
}
}
}
impl Drop for Watchdog {
fn drop(&mut self) {
if let Some(tx) = self.tx.take() {
let _ = tx.send(());
}
if let Some(handle) = self.handle.take() {
let _ = handle.join();
}
}
}