use std::{
panic::panic_any,
thread,
time::Duration,
};
use qubit_atomic::ArcAtomicCount;
use qubit_function::Runnable;
#[derive(Debug, Clone)]
pub enum TestTaskAction {
Succeed,
CountSuccess {
counter: ArcAtomicCount,
},
Fail {
error: &'static str,
},
Panic {
message: &'static str,
},
PanicUsize {
payload: usize,
},
SleepSuccess {
duration: Duration,
},
}
#[derive(Debug, Clone)]
pub struct TestTask {
action: TestTaskAction,
}
impl TestTask {
pub const fn succeed() -> Self {
Self {
action: TestTaskAction::Succeed,
}
}
pub fn count_success(counter: ArcAtomicCount) -> Self {
Self {
action: TestTaskAction::CountSuccess { counter },
}
}
pub const fn fail(error: &'static str) -> Self {
Self {
action: TestTaskAction::Fail { error },
}
}
pub const fn panic(message: &'static str) -> Self {
Self {
action: TestTaskAction::Panic { message },
}
}
pub const fn panic_usize(payload: usize) -> Self {
Self {
action: TestTaskAction::PanicUsize { payload },
}
}
pub const fn sleep_success(duration: Duration) -> Self {
Self {
action: TestTaskAction::SleepSuccess { duration },
}
}
}
impl Runnable<&'static str> for TestTask {
fn run(&mut self) -> Result<(), &'static str> {
match &self.action {
TestTaskAction::Succeed => Ok(()),
TestTaskAction::CountSuccess { counter } => {
counter.inc();
Ok(())
}
TestTaskAction::Fail { error } => Err(*error),
TestTaskAction::Panic { message } => panic_any(*message),
TestTaskAction::PanicUsize { payload } => panic_any(*payload),
TestTaskAction::SleepSuccess { duration } => {
thread::sleep(*duration);
Ok(())
}
}
}
}