use super::{Admission, Arc, Failpoint, HostDirJournalBackend, JournalError};
pub(super) type FailHook = Arc<dyn Fn(Failpoint) -> bool + Send + Sync>;
pub(super) type AdmissionFailHook = Arc<dyn Fn(Failpoint, &Admission) -> bool + Send + Sync>;
impl HostDirJournalBackend {
#[cfg(test)]
pub fn with_failpoint_hook(
mut self,
hook: impl Fn(Failpoint) -> bool + Send + Sync + 'static,
) -> Self {
self.fail = Some(Arc::new(hook));
self
}
#[cfg(test)]
pub fn with_admission_failpoint_hook(
mut self,
hook: impl Fn(Failpoint, &Admission) -> bool + Send + Sync + 'static,
) -> Self {
self.admission_fail = Some(Arc::new(hook));
self
}
pub(super) fn trip(&self, point: Failpoint) -> Result<(), JournalError> {
if self.fail.as_ref().is_some_and(|hook| hook(point)) {
Err(JournalError::InjectedCrash(point.label()))
} else {
Ok(())
}
}
pub(super) fn trip_admission(
&self,
point: Failpoint,
admission: &Admission,
) -> Result<(), JournalError> {
self.trip(point)?;
if self
.admission_fail
.as_ref()
.is_some_and(|hook| hook(point, admission))
{
Err(JournalError::InjectedCrash(point.label()))
} else {
Ok(())
}
}
}