use crate::id::types::ChildId;
#[cfg(debug_assertions)]
use std::sync::Mutex;
#[cfg(debug_assertions)]
struct SpawnFailureHookState {
remaining: usize,
child_id: ChildId,
}
#[cfg(debug_assertions)]
static SPAWN_FAILURE_HOOK: Mutex<Option<SpawnFailureHookState>> = Mutex::new(None);
pub fn fail_next_child_spawns_for(child_id: ChildId, count: usize) {
#[cfg(debug_assertions)]
{
let mut guard = SPAWN_FAILURE_HOOK
.lock()
.expect("spawn hook mutex should remain valid for debug integration tests");
*guard = Some(SpawnFailureHookState {
remaining: count,
child_id,
});
}
#[cfg(not(debug_assertions))]
{
let _ = child_id;
let _ = count;
}
}
#[cfg(debug_assertions)]
pub(crate) fn take_child_spawn_failure_attempt(child_id: &ChildId) -> bool {
let Ok(mut guard) = SPAWN_FAILURE_HOOK.lock() else {
return false;
};
let Some(mut state) = guard.take() else {
return false;
};
if state.child_id != *child_id {
*guard = Some(state);
return false;
}
if state.remaining == 0 {
return false;
}
state.remaining = state.remaining.saturating_sub(1);
if state.remaining > 0 {
*guard = Some(state);
}
true
}