use std::time::Duration;
#[test]
fn block_on_outside_a_tokio_runtime_uses_the_reusable_runtime() {
std::thread::spawn(|| {
assert_eq!(super::block_on(async { 1 + 1 }), 2);
})
.join()
.unwrap();
}
#[test]
fn runtime_handle_outside_a_tokio_runtime_returns_a_usable_handle() {
std::thread::spawn(|| {
let handle = super::runtime_handle();
assert_eq!(handle.block_on(async { 40 + 2 }), 42);
})
.join()
.unwrap();
}
#[test]
fn runtime_handle_inside_a_tokio_runtime_returns_the_ambient_handle() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
let (tx, rx) = tokio::sync::oneshot::channel();
super::runtime_handle().spawn(async move {
let _ = tx.send(());
});
tokio::time::timeout(Duration::from_secs(2), rx)
.await
.expect("task spawned via runtime_handle() should run on the ambient runtime")
.unwrap();
});
}
#[test]
fn block_on_inside_a_spawn_blocking_thread_is_allowed() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
tokio::task::spawn_blocking(|| {
assert_eq!(super::block_on(async { 1 + 1 }), 2);
})
.await
.unwrap();
});
}
#[test]
#[should_panic(expected = "gone too deep")]
fn block_on_inside_a_plain_async_task_panics() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
super::block_on(async { 1 + 1 });
});
}
#[test]
fn block_on_mt_outside_a_tokio_runtime_uses_the_reusable_runtime() {
std::thread::spawn(|| {
assert_eq!(super::block_on_mt(async { 1 + 1 }), 2);
})
.join()
.unwrap();
}
#[test]
fn block_on_mt_inside_a_spawn_blocking_thread_uses_the_ambient_runtime() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
tokio::task::spawn_blocking(|| {
assert_eq!(super::block_on_mt(async { 1 + 1 }), 2);
})
.await
.unwrap();
});
}
#[test]
fn block_on_mt_inside_a_plain_async_task_falls_back_to_a_dedicated_thread() {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
assert_eq!(super::block_on_mt(async { 1 + 1 }), 2);
});
}