#[cfg(windows)]
use crate::test_child::Parked;
pub(crate) fn paused() -> ::tokio::runtime::Runtime {
::tokio::runtime::Builder::new_current_thread()
.enable_all()
.start_paused(true)
.build()
.expect("build the paused test runtime")
}
pub(crate) fn single_thread_pool_runtime() -> ::tokio::runtime::Runtime {
::tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(1)
.build()
.expect("build a runtime with a one-thread blocking pool")
}
#[cfg(windows)]
#[derive(Clone, Default)]
pub(crate) struct PoolBlocks(std::sync::Arc<std::sync::atomic::AtomicU32>);
#[cfg(windows)]
impl PoolBlocks {
#[track_caller]
pub(crate) fn assert_none(&self) {
let blocks = self.0.load(std::sync::atomic::Ordering::SeqCst);
assert_eq!(blocks, 0, "an unbounded exit wait blocked on the blocking pool");
}
}
#[cfg(windows)]
thread_local! {
static POOL_HOOK: std::cell::RefCell<Option<crate::wait::block_probe::Armed>> =
const { std::cell::RefCell::new(None) };
}
#[cfg(windows)]
pub(crate) fn watched_pool_runtime(held: Vec<Parked>) -> (::tokio::runtime::Runtime, PoolBlocks) {
use crate::wait::block_probe::{on_each_block, WaitObject};
let blocks = PoolBlocks::default();
let seen = blocks.clone();
let rt = ::tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(1)
.on_thread_start(move || {
let seen = seen.clone();
let held = held.clone();
let hook = on_each_block(move |block| {
if matches!(block.on, WaitObject::Handles) && block.armed.is_none() {
seen.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
held.iter().for_each(Parked::release);
});
POOL_HOOK.with(|slot| *slot.borrow_mut() = Some(hook));
})
.on_thread_stop(|| POOL_HOOK.with(|slot| drop(slot.borrow_mut().take())))
.build()
.expect("build a runtime whose one-thread blocking pool is watched");
(rt, blocks)
}
pub(crate) fn assert_current_thread() {
assert_eq!(
::tokio::runtime::Handle::current().runtime_flavor(),
::tokio::runtime::RuntimeFlavor::CurrentThread,
"this test's seam is thread-local: its futures must be polled on this thread"
);
}
struct Woken(std::sync::Mutex<std::sync::mpsc::Sender<()>>);
impl std::task::Wake for Woken {
fn wake(self: std::sync::Arc<Self>) {
_ = self.0.lock().expect("the sender").send(());
}
}
pub(crate) fn counting_waker() -> (std::task::Waker, std::sync::mpsc::Receiver<()>) {
let (woken, wakes) = std::sync::mpsc::channel();
let waker = std::task::Waker::from(std::sync::Arc::new(Woken(std::sync::Mutex::new(woken))));
(waker, wakes)
}
#[cfg(test)]
#[path = "test_runtime_tests.rs"]
mod test_runtime_tests;