1use std::{future::Future, sync::Arc};
22
23use jsonrpsee::Extensions;
24use sc_rpc_api::DenyUnsafe;
25
26#[derive(Clone)]
30pub struct TokioTestExecutor(tokio::runtime::Handle);
31
32impl TokioTestExecutor {
33 pub fn new() -> Self {
35 Self(tokio::runtime::Handle::current())
36 }
37}
38
39impl Default for TokioTestExecutor {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl sp_core::traits::SpawnNamed for TokioTestExecutor {
46 fn spawn_blocking(
47 &self,
48 _name: &'static str,
49 _group: Option<&'static str>,
50 future: futures::future::BoxFuture<'static, ()>,
51 ) {
52 let handle = self.0.clone();
53 self.0.spawn_blocking(move || {
54 handle.block_on(future);
55 });
56 }
57 fn spawn(
58 &self,
59 _name: &'static str,
60 _group: Option<&'static str>,
61 future: futures::future::BoxFuture<'static, ()>,
62 ) {
63 self.0.spawn(future);
64 }
65}
66
67pub fn test_executor() -> Arc<TokioTestExecutor> {
69 Arc::new(TokioTestExecutor::default())
70}
71
72pub fn timeout_secs<I, F: Future<Output = I>>(s: u64, f: F) -> tokio::time::Timeout<F> {
74 tokio::time::timeout(std::time::Duration::from_secs(s), f)
75}
76
77pub fn deny_unsafe() -> Extensions {
79 let mut ext = Extensions::new();
80 ext.insert(DenyUnsafe::Yes);
81 ext
82}
83
84pub fn allow_unsafe() -> Extensions {
86 let mut ext = Extensions::new();
87 ext.insert(DenyUnsafe::No);
88 ext
89}