use std::sync::mpsc;
use std::sync::mpsc::{RecvTimeoutError, TrySendError};
use std::time::Duration;
#[derive(Clone, Debug, PartialEq)]
pub enum TestKind {
ExpectSuccess,
ExpectNoFailure,
}
#[derive(Clone, Debug)] pub struct StatusNotifier {
kind: TestKind,
tx: mpsc::SyncSender<()>,
}
impl StatusNotifier {
pub fn notify_success(&self) {
if self.kind == TestKind::ExpectSuccess {
match self.tx.try_send(()) {
Ok(()) => (),
Err(TrySendError::Full(_)) => (), Err(TrySendError::Disconnected(_)) => (), }
}
}
#[allow(dead_code)]
pub fn notify_failure(&self) {
if self.kind == TestKind::ExpectNoFailure {
match self.tx.try_send(()) {
Ok(()) => (),
Err(TrySendError::Full(_)) => (), Err(TrySendError::Disconnected(_)) => (), }
}
}
}
#[derive(Debug)]
pub struct Status {
notifier: StatusNotifier,
rx: mpsc::Receiver<()>,
}
impl Status {
pub fn new(kind: TestKind) -> Self {
let (tx, rx) = mpsc::sync_channel(1);
Self {
notifier: StatusNotifier { kind, tx },
rx,
}
}
pub fn notifier(&self) -> StatusNotifier {
self.notifier.clone()
}
pub fn assert_passed(&self) {
let timeout = Duration::from_secs(10);
match self.notifier.kind {
TestKind::ExpectSuccess => match self.rx.recv_timeout(timeout) {
Ok(()) => {}
Err(RecvTimeoutError::Timeout) => {
panic!("Test did not pass within the allowed timeout");
}
_ => panic!("Should not happen, the sending end has not hung up."),
},
TestKind::ExpectNoFailure => match self.rx.recv_timeout(timeout) {
Ok(()) => {
panic!("Test failed within the allowed timeout");
}
Err(RecvTimeoutError::Timeout) => {}
_ => panic!("Should not happen, the sending end has not hung up."),
},
}
}
}