use std::sync::Mutex;
#[derive(Debug, Clone)]
pub struct PendingFault {
pub action_suffix: String,
pub code: String,
pub reason: String,
}
pub struct FaultInjector {
pending: Mutex<Vec<PendingFault>>,
}
impl Default for FaultInjector {
fn default() -> Self {
Self::new()
}
}
impl FaultInjector {
pub fn new() -> Self {
Self {
pending: Mutex::new(Vec::new()),
}
}
pub fn inject(&self, fault: PendingFault) {
self.pending.lock().unwrap().push(fault);
}
pub fn take_for_action(&self, action: &str) -> Option<PendingFault> {
let mut pending = self.pending.lock().unwrap();
let idx = pending
.iter()
.position(|f| action.ends_with(&f.action_suffix))?;
Some(pending.remove(idx))
}
pub fn clear_all(&self) {
self.pending.lock().unwrap().clear();
}
#[allow(dead_code)]
pub fn pending_snapshot(&self) -> Vec<PendingFault> {
self.pending.lock().unwrap().clone()
}
}