From 53ccfc1fd694ee70c7a4d1e7af09a856bafb49e5 Mon Sep 17 00:00:00 2001 From: Juan Alvarez Date: Thu, 1 Oct 2020 02:24:33 -0500 Subject: time: introduce `sleep` and `sleep_until` functions (#2826) --- tokio/tests/async_send_sync.rs | 4 +- tokio/tests/macros_select.rs | 4 +- tokio/tests/process_issue_2174.rs | 2 +- tokio/tests/process_kill_on_drop.rs | 4 +- tokio/tests/rt_common.rs | 20 +++++----- tokio/tests/stream_timeout.rs | 4 +- tokio/tests/task_local.rs | 2 +- tokio/tests/task_local_set.rs | 16 ++++---- tokio/tests/time_delay.rs | 32 ++++++++-------- tokio/tests/time_delay_queue.rs | 74 ++++++++++++++++++------------------- tokio/tests/time_rt.rs | 6 +-- 11 files changed, 84 insertions(+), 84 deletions(-) (limited to 'tokio/tests') diff --git a/tokio/tests/async_send_sync.rs b/tokio/tests/async_send_sync.rs index b3492b5e..c82d8a5a 100644 --- a/tokio/tests/async_send_sync.rs +++ b/tokio/tests/async_send_sync.rs @@ -243,8 +243,8 @@ async_assert_fn!(tokio::task::LocalSet::run_until(_, BoxFutureSync<()>): !Send & assert_value!(tokio::task::LocalSet: !Send & !Sync); async_assert_fn!(tokio::time::advance(Duration): Send & Sync); -async_assert_fn!(tokio::time::delay_for(Duration): Send & Sync); -async_assert_fn!(tokio::time::delay_until(Instant): Send & Sync); +async_assert_fn!(tokio::time::sleep(Duration): Send & Sync); +async_assert_fn!(tokio::time::sleep_until(Instant): Send & Sync); async_assert_fn!(tokio::time::timeout(Duration, BoxFutureSync<()>): Send & Sync); async_assert_fn!(tokio::time::timeout(Duration, BoxFutureSend<()>): Send & !Sync); async_assert_fn!(tokio::time::timeout(Duration, BoxFuture<()>): !Send & !Sync); diff --git a/tokio/tests/macros_select.rs b/tokio/tests/macros_select.rs index a6c8f8f5..f971409b 100644 --- a/tokio/tests/macros_select.rs +++ b/tokio/tests/macros_select.rs @@ -359,7 +359,7 @@ async fn join_with_select() { async fn use_future_in_if_condition() { use tokio::time::{self, Duration}; - let mut delay = time::delay_for(Duration::from_millis(50)); + let mut delay = time::sleep(Duration::from_millis(50)); tokio::select! { _ = &mut delay, if !delay.is_elapsed() => { @@ -459,7 +459,7 @@ async fn async_noop() {} async fn async_never() -> ! { use tokio::time::Duration; loop { - tokio::time::delay_for(Duration::from_millis(10)).await; + tokio::time::sleep(Duration::from_millis(10)).await; } } diff --git a/tokio/tests/process_issue_2174.rs b/tokio/tests/process_issue_2174.rs index 4493d54a..6ee7d1ab 100644 --- a/tokio/tests/process_issue_2174.rs +++ b/tokio/tests/process_issue_2174.rs @@ -36,7 +36,7 @@ async fn issue_2174() { }); // Sleep enough time so that the child process's stdin's buffer fills. - time::delay_for(Duration::from_secs(1)).await; + time::sleep(Duration::from_secs(1)).await; // Kill the child process. child.kill().await.unwrap(); diff --git a/tokio/tests/process_kill_on_drop.rs b/tokio/tests/process_kill_on_drop.rs index f376c154..f67bb23c 100644 --- a/tokio/tests/process_kill_on_drop.rs +++ b/tokio/tests/process_kill_on_drop.rs @@ -5,7 +5,7 @@ use std::process::Stdio; use std::time::Duration; use tokio::io::AsyncReadExt; use tokio::process::Command; -use tokio::time::delay_for; +use tokio::time::sleep; use tokio_test::assert_ok; #[tokio::test] @@ -30,7 +30,7 @@ async fn kill_on_drop() { .spawn() .unwrap(); - delay_for(Duration::from_secs(2)).await; + sleep(Duration::from_secs(2)).await; let mut out = child.stdout.take().unwrap(); drop(child); diff --git a/tokio/tests/rt_common.rs b/tokio/tests/rt_common.rs index 7f0491c4..3e95c2aa 100644 --- a/tokio/tests/rt_common.rs +++ b/tokio/tests/rt_common.rs @@ -437,7 +437,7 @@ rt_test! { let dur = Duration::from_millis(50); rt.block_on(async move { - time::delay_for(dur).await; + time::sleep(dur).await; }); assert!(now.elapsed() >= dur); @@ -454,7 +454,7 @@ rt_test! { let (tx, rx) = oneshot::channel(); tokio::spawn(async move { - time::delay_for(dur).await; + time::sleep(dur).await; assert_ok!(tx.send(())); }); @@ -526,7 +526,7 @@ rt_test! { // use the futures' block_on fn to make sure we aren't setting // any Tokio context futures::executor::block_on(async { - tokio::time::delay_for(dur).await; + tokio::time::sleep(dur).await; }); assert!(now.elapsed() >= dur); @@ -588,7 +588,7 @@ rt_test! { let jh1 = thread::spawn(move || { rt.block_on(async move { rx2.await.unwrap(); - time::delay_for(Duration::from_millis(5)).await; + time::sleep(Duration::from_millis(5)).await; tx1.send(()).unwrap(); }); }); @@ -596,9 +596,9 @@ rt_test! { let jh2 = thread::spawn(move || { rt2.block_on(async move { tx2.send(()).unwrap(); - time::delay_for(Duration::from_millis(5)).await; + time::sleep(Duration::from_millis(5)).await; rx1.await.unwrap(); - time::delay_for(Duration::from_millis(5)).await; + time::sleep(Duration::from_millis(5)).await; }); }); @@ -850,11 +850,11 @@ rt_test! { let buf = [0]; loop { send_half.send_to(&buf, &addr).await.unwrap(); - tokio::time::delay_for(Duration::from_millis(1)).await; + tokio::time::sleep(Duration::from_millis(1)).await; } }); - tokio::time::delay_for(Duration::from_millis(5)).await; + tokio::time::sleep(Duration::from_millis(5)).await; }); } } @@ -881,7 +881,7 @@ rt_test! { let runtime = rt(); runtime.block_on(async move { - tokio::time::delay_for(std::time::Duration::from_millis(100)).await; + tokio::time::sleep(std::time::Duration::from_millis(100)).await; }); Arc::try_unwrap(runtime).unwrap().shutdown_timeout(Duration::from_secs(10_000)); @@ -1006,7 +1006,7 @@ rt_test! { }).collect::>(); // Hope that all the tasks complete... - time::delay_for(Duration::from_millis(100)).await; + time::sleep(Duration::from_millis(100)).await; poll_fn(|cx| { // At least one task should not be ready diff --git a/tokio/tests/stream_timeout.rs b/tokio/tests/stream_timeout.rs index f65c8351..698c1d39 100644 --- a/tokio/tests/stream_timeout.rs +++ b/tokio/tests/stream_timeout.rs @@ -1,14 +1,14 @@ #![cfg(feature = "full")] use tokio::stream::{self, StreamExt}; -use tokio::time::{self, delay_for, Duration}; +use tokio::time::{self, sleep, Duration}; use tokio_test::*; use futures::StreamExt as _; async fn maybe_delay(idx: i32) -> i32 { if idx % 2 == 0 { - delay_for(ms(200)).await; + sleep(ms(200)).await; } idx } diff --git a/tokio/tests/task_local.rs b/tokio/tests/task_local.rs index 7f508997..58b58183 100644 --- a/tokio/tests/task_local.rs +++ b/tokio/tests/task_local.rs @@ -16,7 +16,7 @@ async fn local() { assert_eq!(*v, 2); }); - tokio::time::delay_for(std::time::Duration::from_millis(10)).await; + tokio::time::sleep(std::time::Duration::from_millis(10)).await; assert_eq!(REQ_ID.get(), 2); })); diff --git a/tokio/tests/task_local_set.rs b/tokio/tests/task_local_set.rs index 23e92586..1dc779ce 100644 --- a/tokio/tests/task_local_set.rs +++ b/tokio/tests/task_local_set.rs @@ -62,11 +62,11 @@ async fn localset_future_timers() { let local = LocalSet::new(); local.spawn_local(async move { - time::delay_for(Duration::from_millis(10)).await; + time::sleep(Duration::from_millis(10)).await; RAN1.store(true, Ordering::SeqCst); }); local.spawn_local(async move { - time::delay_for(Duration::from_millis(20)).await; + time::sleep(Duration::from_millis(20)).await; RAN2.store(true, Ordering::SeqCst); }); local.await; @@ -114,7 +114,7 @@ async fn local_threadpool_timer() { assert!(ON_RT_THREAD.with(|cell| cell.get())); let join = task::spawn_local(async move { assert!(ON_RT_THREAD.with(|cell| cell.get())); - time::delay_for(Duration::from_millis(10)).await; + time::sleep(Duration::from_millis(10)).await; assert!(ON_RT_THREAD.with(|cell| cell.get())); }); join.await.unwrap(); @@ -299,7 +299,7 @@ fn drop_cancels_tasks() { started_tx.send(()).unwrap(); loop { - time::delay_for(Duration::from_secs(3600)).await; + time::sleep(Duration::from_secs(3600)).await; } }); @@ -367,7 +367,7 @@ fn drop_cancels_remote_tasks() { let local = LocalSet::new(); local.spawn_local(async move { while rx.recv().await.is_some() {} }); local.block_on(&rt, async { - time::delay_for(Duration::from_millis(1)).await; + time::sleep(Duration::from_millis(1)).await; }); drop(tx); @@ -415,7 +415,7 @@ async fn local_tasks_are_polled_after_tick() { .run_until(async { let task2 = task::spawn(async move { // Wait a bit - time::delay_for(Duration::from_millis(100)).await; + time::sleep(Duration::from_millis(100)).await; let mut oneshots = Vec::with_capacity(EXPECTED); @@ -426,13 +426,13 @@ async fn local_tasks_are_polled_after_tick() { tx.send(oneshot_rx).unwrap(); } - time::delay_for(Duration::from_millis(100)).await; + time::sleep(Duration::from_millis(100)).await; for tx in oneshots.drain(..) { tx.send(()).unwrap(); } - time::delay_for(Duration::from_millis(300)).await; + time::sleep(Duration::from_millis(300)).await; let rx1 = RX1.load(SeqCst); let rx2 = RX2.load(SeqCst); println!("EXPECT = {}; RX1 = {}; RX2 = {}", EXPECTED, rx1, rx2); diff --git a/tokio/tests/time_delay.rs b/tokio/tests/time_delay.rs index e4804ec6..559c18c8 100644 --- a/tokio/tests/time_delay.rs +++ b/tokio/tests/time_delay.rs @@ -26,7 +26,7 @@ async fn immediate_delay() { let now = Instant::now(); // Ready! - time::delay_until(now).await; + time::sleep_until(now).await; assert_elapsed!(now, 0); } @@ -37,7 +37,7 @@ async fn delayed_delay_level_0() { for &i in &[1, 10, 60] { let now = Instant::now(); - time::delay_until(now + ms(i)).await; + time::sleep_until(now + ms(i)).await; assert_elapsed!(now, i); } @@ -51,7 +51,7 @@ async fn sub_ms_delayed_delay() { let now = Instant::now(); let deadline = now + ms(1) + Duration::new(0, 1); - time::delay_until(deadline).await; + time::sleep_until(deadline).await; assert_elapsed!(now, 1); } @@ -61,10 +61,10 @@ async fn sub_ms_delayed_delay() { async fn delayed_delay_wrapping_level_0() { time::pause(); - time::delay_for(ms(5)).await; + time::sleep(ms(5)).await; let now = Instant::now(); - time::delay_until(now + ms(60)).await; + time::sleep_until(now + ms(60)).await; assert_elapsed!(now, 60); } @@ -75,7 +75,7 @@ async fn reset_future_delay_before_fire() { let now = Instant::now(); - let mut delay = task::spawn(time::delay_until(now + ms(100))); + let mut delay = task::spawn(time::sleep_until(now + ms(100))); assert_pending!(delay.poll()); let mut delay = delay.into_inner(); @@ -92,7 +92,7 @@ async fn reset_past_delay_before_turn() { let now = Instant::now(); - let mut delay = task::spawn(time::delay_until(now + ms(100))); + let mut delay = task::spawn(time::sleep_until(now + ms(100))); assert_pending!(delay.poll()); let mut delay = delay.into_inner(); @@ -109,12 +109,12 @@ async fn reset_past_delay_before_fire() { let now = Instant::now(); - let mut delay = task::spawn(time::delay_until(now + ms(100))); + let mut delay = task::spawn(time::sleep_until(now + ms(100))); assert_pending!(delay.poll()); let mut delay = delay.into_inner(); - time::delay_for(ms(10)).await; + time::sleep(ms(10)).await; delay.reset(now + ms(80)); delay.await; @@ -127,7 +127,7 @@ async fn reset_future_delay_after_fire() { time::pause(); let now = Instant::now(); - let mut delay = time::delay_until(now + ms(100)); + let mut delay = time::sleep_until(now + ms(100)); (&mut delay).await; assert_elapsed!(now, 100); @@ -143,10 +143,10 @@ async fn reset_delay_to_past() { let now = Instant::now(); - let mut delay = task::spawn(time::delay_until(now + ms(100))); + let mut delay = task::spawn(time::sleep_until(now + ms(100))); assert_pending!(delay.poll()); - time::delay_for(ms(50)).await; + time::sleep(ms(50)).await; assert!(!delay.is_woken()); @@ -164,7 +164,7 @@ fn creating_delay_outside_of_context() { // This creates a delay outside of the context of a mock timer. This tests // that it will panic. - let _fut = time::delay_until(now + ms(500)); + let _fut = time::sleep_until(now + ms(500)); } #[should_panic] @@ -172,7 +172,7 @@ fn creating_delay_outside_of_context() { async fn greater_than_max() { const YR_5: u64 = 5 * 365 * 24 * 60 * 60 * 1000; - time::delay_until(Instant::now() + ms(YR_5)).await; + time::sleep_until(Instant::now() + ms(YR_5)).await; } const NUM_LEVELS: usize = 6; @@ -182,13 +182,13 @@ const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1; #[tokio::test] async fn exactly_max() { // TODO: this should not panic but `time::ms()` is acting up - time::delay_for(ms(MAX_DURATION)).await; + time::sleep(ms(MAX_DURATION)).await; } #[tokio::test] async fn no_out_of_bounds_close_to_max() { time::pause(); - time::delay_for(ms(MAX_DURATION - 1)).await; + time::sleep(ms(MAX_DURATION - 1)).await; } fn ms(n: u64) -> Duration { diff --git a/tokio/tests/time_delay_queue.rs b/tokio/tests/time_delay_queue.rs index ba3c9858..d4878b91 100644 --- a/tokio/tests/time_delay_queue.rs +++ b/tokio/tests/time_delay_queue.rs @@ -2,7 +2,7 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "full")] -use tokio::time::{self, delay_for, DelayQueue, Duration, Instant}; +use tokio::time::{self, sleep, DelayQueue, Duration, Instant}; use tokio_test::{assert_ok, assert_pending, assert_ready, task}; macro_rules! poll { @@ -28,7 +28,7 @@ async fn single_immediate_delay() { let _key = queue.insert_at("foo", Instant::now()); // Advance time by 1ms to handle thee rounding - delay_for(ms(1)).await; + sleep(ms(1)).await; assert_ready_ok!(poll!(queue)); @@ -46,7 +46,7 @@ async fn multi_immediate_delays() { let _k = queue.insert_at("2", Instant::now()); let _k = queue.insert_at("3", Instant::now()); - delay_for(ms(1)).await; + sleep(ms(1)).await; let mut res = vec![]; @@ -74,11 +74,11 @@ async fn single_short_delay() { assert_pending!(poll!(queue)); - delay_for(ms(1)).await; + sleep(ms(1)).await; assert!(!queue.is_woken()); - delay_for(ms(5)).await; + sleep(ms(5)).await; assert!(queue.is_woken()); @@ -107,7 +107,7 @@ async fn multi_delay_at_start() { assert!(!queue.is_woken()); for elapsed in 0..1200 { - delay_for(ms(1)).await; + sleep(ms(1)).await; let elapsed = elapsed + 1; if delays.contains(&elapsed) { @@ -130,7 +130,7 @@ async fn insert_in_past_fires_immediately() { let mut queue = task::spawn(DelayQueue::new()); let now = Instant::now(); - delay_for(ms(10)).await; + sleep(ms(10)).await; queue.insert_at("foo", now); @@ -150,7 +150,7 @@ async fn remove_entry() { let entry = queue.remove(&key); assert_eq!(entry.into_inner(), "foo"); - delay_for(ms(10)).await; + sleep(ms(10)).await; let entry = assert_ready!(poll!(queue)); assert!(entry.is_none()); @@ -166,19 +166,19 @@ async fn reset_entry() { let key = queue.insert_at("foo", now + ms(5)); assert_pending!(poll!(queue)); - delay_for(ms(1)).await; + sleep(ms(1)).await; queue.reset_at(&key, now + ms(10)); assert_pending!(poll!(queue)); - delay_for(ms(7)).await; + sleep(ms(7)).await; assert!(!queue.is_woken()); assert_pending!(poll!(queue)); - delay_for(ms(3)).await; + sleep(ms(3)).await; assert!(queue.is_woken()); @@ -197,16 +197,16 @@ async fn reset_much_later() { let mut queue = task::spawn(DelayQueue::new()); let now = Instant::now(); - delay_for(ms(1)).await; + sleep(ms(1)).await; let key = queue.insert_at("foo", now + ms(200)); assert_pending!(poll!(queue)); - delay_for(ms(3)).await; + sleep(ms(3)).await; queue.reset_at(&key, now + ms(5)); - delay_for(ms(20)).await; + sleep(ms(20)).await; assert!(queue.is_woken()); } @@ -219,21 +219,21 @@ async fn reset_twice() { let mut queue = task::spawn(DelayQueue::new()); let now = Instant::now(); - delay_for(ms(1)).await; + sleep(ms(1)).await; let key = queue.insert_at("foo", now + ms(200)); assert_pending!(poll!(queue)); - delay_for(ms(3)).await; + sleep(ms(3)).await; queue.reset_at(&key, now + ms(50)); - delay_for(ms(20)).await; + sleep(ms(20)).await; queue.reset_at(&key, now + ms(40)); - delay_for(ms(20)).await; + sleep(ms(20)).await; assert!(queue.is_woken()); } @@ -246,7 +246,7 @@ async fn remove_expired_item() { let now = Instant::now(); - delay_for(ms(10)).await; + sleep(ms(10)).await; let key = queue.insert_at("foo", now); @@ -272,7 +272,7 @@ async fn expires_before_last_insert() { assert_pending!(poll!(queue)); - delay_for(ms(600)).await; + sleep(ms(600)).await; assert!(queue.is_woken()); @@ -297,18 +297,18 @@ async fn multi_reset() { queue.reset_at(&two, now + ms(350)); queue.reset_at(&one, now + ms(400)); - delay_for(ms(310)).await; + sleep(ms(310)).await; assert_pending!(poll!(queue)); - delay_for(ms(50)).await; + sleep(ms(50)).await; let entry = assert_ready_ok!(poll!(queue)); assert_eq!(*entry.get_ref(), "two"); assert_pending!(poll!(queue)); - delay_for(ms(50)).await; + sleep(ms(50)).await; let entry = assert_ready_ok!(poll!(queue)); assert_eq!(*entry.get_ref(), "one"); @@ -332,7 +332,7 @@ async fn expire_first_key_when_reset_to_expire_earlier() { queue.reset_at(&one, now + ms(100)); - delay_for(ms(100)).await; + sleep(ms(100)).await; assert!(queue.is_woken()); @@ -355,7 +355,7 @@ async fn expire_second_key_when_reset_to_expire_earlier() { queue.reset_at(&two, now + ms(100)); - delay_for(ms(100)).await; + sleep(ms(100)).await; assert!(queue.is_woken()); @@ -377,7 +377,7 @@ async fn reset_first_expiring_item_to_expire_later() { assert_pending!(poll!(queue)); queue.reset_at(&one, now + ms(300)); - delay_for(ms(250)).await; + sleep(ms(250)).await; assert!(queue.is_woken()); @@ -399,11 +399,11 @@ async fn insert_before_first_after_poll() { let _two = queue.insert_at("two", now + ms(100)); - delay_for(ms(99)).await; + sleep(ms(99)).await; assert!(!queue.is_woken()); - delay_for(ms(1)).await; + sleep(ms(1)).await; assert!(queue.is_woken()); @@ -425,7 +425,7 @@ async fn insert_after_ready_poll() { assert_pending!(poll!(queue)); - delay_for(ms(100)).await; + sleep(ms(100)).await; assert!(queue.is_woken()); @@ -456,7 +456,7 @@ async fn reset_later_after_slot_starts() { assert_pending!(poll!(queue)); - delay_for(ms(80)).await; + sleep(ms(80)).await; assert!(!queue.is_woken()); @@ -471,10 +471,10 @@ async fn reset_later_after_slot_starts() { assert_pending!(poll!(queue)); - delay_for(ms(39)).await; + sleep(ms(39)).await; assert!(!queue.is_woken()); - delay_for(ms(1)).await; + sleep(ms(1)).await; assert!(queue.is_woken()); let entry = assert_ready_ok!(poll!(queue)).into_inner(); @@ -494,7 +494,7 @@ async fn reset_inserted_expired() { assert_eq!(1, queue.len()); - delay_for(ms(200)).await; + sleep(ms(200)).await; let entry = assert_ready_ok!(poll!(queue)).into_inner(); assert_eq!(entry, "foo"); @@ -514,7 +514,7 @@ async fn reset_earlier_after_slot_starts() { assert_pending!(poll!(queue)); - delay_for(ms(80)).await; + sleep(ms(80)).await; assert!(!queue.is_woken()); @@ -529,10 +529,10 @@ async fn reset_earlier_after_slot_starts() { assert_pending!(poll!(queue)); - delay_for(ms(39)).await; + sleep(ms(39)).await; assert!(!queue.is_woken()); - delay_for(ms(1)).await; + sleep(ms(1)).await; assert!(queue.is_woken()); let entry = assert_ready_ok!(poll!(queue)).into_inner(); @@ -551,7 +551,7 @@ async fn insert_in_past_after_poll_fires_immediately() { assert_pending!(poll!(queue)); - delay_for(ms(80)).await; + sleep(ms(80)).await; assert!(!queue.is_woken()); queue.insert_at("bar", now + ms(40)); diff --git a/tokio/tests/time_rt.rs b/tokio/tests/time_rt.rs index 19bcd27d..78056f09 100644 --- a/tokio/tests/time_rt.rs +++ b/tokio/tests/time_rt.rs @@ -15,7 +15,7 @@ fn timer_with_threaded_runtime() { rt.spawn(async move { let when = Instant::now() + Duration::from_millis(100); - delay_until(when).await; + sleep_until(when).await; assert!(Instant::now() >= when); tx.send(()).unwrap(); @@ -38,7 +38,7 @@ fn timer_with_basic_scheduler() { rt.block_on(async move { let when = Instant::now() + Duration::from_millis(100); - delay_until(when).await; + sleep_until(when).await; assert!(Instant::now() >= when); tx.send(()).unwrap(); @@ -72,7 +72,7 @@ async fn starving() { } let when = Instant::now() + Duration::from_millis(20); - let starve = Starve(delay_until(when), 0); + let starve = Starve(sleep_until(when), 0); starve.await; assert!(Instant::now() >= when); -- cgit v1.2.3