use color_eyre::eyre::Context;
use tokio::runtime::{Builder, Runtime};
use super::panic_utils::nested_runtime_thread_panic;
use crate::error::{BootstrapError, BootstrapResult};
pub(crate) fn build_runtime() -> BootstrapResult<Runtime> {
Builder::new_current_thread()
.enable_all()
.build()
.context("failed to create Tokio runtime for TestCluster")
.map_err(BootstrapError::from)
}
pub(crate) fn run_with_runtime<T, F>(context: &'static str, operation: F) -> BootstrapResult<T>
where
F: FnOnce(&Runtime) -> BootstrapResult<T> + Send,
T: Send,
{
if tokio::runtime::Handle::try_current().is_ok() {
return std::thread::scope(|scope| {
scope
.spawn(move || {
let runtime = build_runtime()?;
operation(&runtime)
})
.join()
})
.map_err(|panic_payload| {
nested_runtime_thread_panic(context, "lifecycle", panic_payload)
})?;
}
let runtime = build_runtime()?;
operation(&runtime)
}