use std::task::Waker;
use std::time::Duration;
use std::{env, thread};
use scopeguard::{Always, ScopeGuard};
use crate::{CycleOutcome, Executor};
const MUTATION_TESTING_SHUTDOWN_TIMEOUT: Duration = Duration::from_mins(15);
const TEST_TIMEOUT: Duration = Duration::from_secs(10);
#[cfg_attr(test, mutants::skip)] fn is_mutation_testing() -> bool {
env::var("MUTATION_TESTING").as_deref() == Ok("1")
}
#[must_use]
#[cfg_attr(test, mutants::skip)] pub fn new_guarded_executor(owner_waker: Waker) -> ScopeGuard<Executor, fn(Executor), Always> {
scopeguard::guard(
{
let mut builder = Executor::builder().owner_waker(owner_waker);
if is_mutation_testing() {
builder = builder.shutdown_timeout(MUTATION_TESTING_SHUTDOWN_TIMEOUT);
} else {
builder = builder.shutdown_timeout(TEST_TIMEOUT);
}
unsafe { builder.build() }
},
|executor: Executor| {
executor.begin_shutdown();
while executor.execute_cycle() != CycleOutcome::Shutdown {
thread::yield_now();
}
},
)
}