use std::{future::Future, sync::Arc};
use jsonrpsee::Extensions;
use pezsc_rpc_api::DenyUnsafe;
#[derive(Clone)]
pub struct TokioTestExecutor(tokio::runtime::Handle);
impl TokioTestExecutor {
pub fn new() -> Self {
Self(tokio::runtime::Handle::current())
}
}
impl Default for TokioTestExecutor {
fn default() -> Self {
Self::new()
}
}
impl pezsp_core::traits::SpawnNamed for TokioTestExecutor {
fn spawn_blocking(
&self,
_name: &'static str,
_group: Option<&'static str>,
future: futures::future::BoxFuture<'static, ()>,
) {
let handle = self.0.clone();
self.0.spawn_blocking(move || {
handle.block_on(future);
});
}
fn spawn(
&self,
_name: &'static str,
_group: Option<&'static str>,
future: futures::future::BoxFuture<'static, ()>,
) {
self.0.spawn(future);
}
}
pub fn test_executor() -> Arc<TokioTestExecutor> {
Arc::new(TokioTestExecutor::default())
}
pub fn timeout_secs<I, F: Future<Output = I>>(s: u64, f: F) -> tokio::time::Timeout<F> {
tokio::time::timeout(std::time::Duration::from_secs(s), f)
}
pub fn deny_unsafe() -> Extensions {
let mut ext = Extensions::new();
ext.insert(DenyUnsafe::Yes);
ext
}
pub fn allow_unsafe() -> Extensions {
let mut ext = Extensions::new();
ext.insert(DenyUnsafe::No);
ext
}