mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
time: enforce current_thread rt for time::pause (#3289)
Pausing time is a capability added to assist with testing Tokio code dependent on time. Currently, the capability implicitly requires the current_thread runtime. This change enforces the requirement by panicking if called from a multi-threaded runtime.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![cfg(feature = "full")]
|
||||
|
||||
use tokio_test::assert_err;
|
||||
|
||||
#[tokio::test]
|
||||
async fn pause_time_in_main() {
|
||||
tokio::time::pause();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pause_time_in_task() {
|
||||
let t = tokio::spawn(async {
|
||||
tokio::time::pause();
|
||||
});
|
||||
|
||||
t.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
#[should_panic]
|
||||
async fn pause_time_in_main_threads() {
|
||||
tokio::time::pause();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn pause_time_in_spawn_threads() {
|
||||
let t = tokio::spawn(async {
|
||||
tokio::time::pause();
|
||||
});
|
||||
|
||||
assert_err!(t.await);
|
||||
}
|
||||
Reference in New Issue
Block a user