1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
#![cfg(not(miri))] // Too slow on Miri.
use std::future::Future;
use std::task::Context;
use futures::task::noop_waker_ref;
use tokio::time::{self, Duration, Instant};
use tokio_test::{assert_elapsed, assert_pending, assert_ready, task};
#[tokio::test]
async fn immediate_sleep() {
time::pause();
let now = Instant::now();
// Ready!
time::sleep_until(now).await;
assert_elapsed!(now, ms(1));
}
#[tokio::test]
async fn is_elapsed() {
time::pause();
let sleep = time::sleep(Duration::from_millis(10));
tokio::pin!(sleep);
assert!(!sleep.is_elapsed());
assert!(futures::poll!(sleep.as_mut()).is_pending());
assert!(!sleep.is_elapsed());
sleep.as_mut().await;
assert!(sleep.is_elapsed());
}
#[tokio::test]
async fn delayed_sleep_level_0() {
time::pause();
for &i in &[1, 10, 60] {
let now = Instant::now();
let dur = ms(i);
time::sleep_until(now + dur).await;
assert_elapsed!(now, dur);
}
}
#[tokio::test]
async fn sub_ms_delayed_sleep() {
time::pause();
for _ in 0..5 {
let now = Instant::now();
let deadline = now + ms(1) + Duration::new(0, 1);
time::sleep_until(deadline).await;
assert_elapsed!(now, ms(1));
}
}
#[tokio::test]
async fn delayed_sleep_wrapping_level_0() {
time::pause();
time::sleep(ms(5)).await;
let now = Instant::now();
time::sleep_until(now + ms(60)).await;
assert_elapsed!(now, ms(60));
}
#[tokio::test]
async fn reset_future_sleep_before_fire() {
time::pause();
let now = Instant::now();
let mut sleep = task::spawn(Box::pin(time::sleep_until(now + ms(100))));
assert_pending!(sleep.poll());
let mut sleep = sleep.into_inner();
sleep.as_mut().reset(Instant::now() + ms(200));
sleep.await;
assert_elapsed!(now, ms(200));
}
#[tokio::test]
async fn reset_past_sleep_before_turn() {
time::pause();
let now = Instant::now();
let mut sleep = task::spawn(Box::pin(time::sleep_until(now + ms(100))));
assert_pending!(sleep.poll());
let mut sleep = sleep.into_inner();
sleep.as_mut().reset(now + ms(80));
sleep.await;
assert_elapsed!(now, ms(80));
}
#[tokio::test]
async fn reset_past_sleep_before_fire() {
time::pause();
let now = Instant::now();
let mut sleep = task::spawn(Box::pin(time::sleep_until(now + ms(100))));
assert_pending!(sleep.poll());
let mut sleep = sleep.into_inner();
time::sleep(ms(10)).await;
sleep.as_mut().reset(now + ms(80));
sleep.await;
assert_elapsed!(now, ms(80));
}
#[tokio::test]
async fn reset_future_sleep_after_fire() {
time::pause();
let now = Instant::now();
let mut sleep = Box::pin(time::sleep_until(now + ms(100)));
sleep.as_mut().await;
assert_elapsed!(now, ms(100));
sleep.as_mut().reset(now + ms(110));
sleep.await;
assert_elapsed!(now, ms(110));
}
#[tokio::test]
async fn reset_sleep_to_past() {
time::pause();
let now = Instant::now();
let mut sleep = task::spawn(Box::pin(time::sleep_until(now + ms(100))));
assert_pending!(sleep.poll());
time::sleep(ms(50)).await;
assert!(!sleep.is_woken());
sleep.as_mut().reset(now + ms(40));
// TODO: is this required?
//assert!(sleep.is_woken());
assert_ready!(sleep.poll());
}
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support panic recovery
#[test]
#[should_panic]
fn creating_sleep_outside_of_context() {
let now = Instant::now();
// This creates a delay outside of the context of a mock timer. This tests
// that it will panic.
let _fut = time::sleep_until(now + ms(500));
}
#[tokio::test]
async fn greater_than_max() {
const YR_5: u64 = 5 * 365 * 24 * 60 * 60 * 1000;
time::pause();
time::sleep_until(Instant::now() + ms(YR_5)).await;
}
#[tokio::test]
async fn short_sleeps() {
for _ in 0..1000 {
tokio::time::sleep(std::time::Duration::from_millis(0)).await;
}
}
#[tokio::test]
async fn multi_long_sleeps() {
tokio::time::pause();
for _ in 0..5u32 {
tokio::time::sleep(Duration::from_secs(
// about a year
365 * 24 * 3600,
))
.await;
}
let deadline = tokio::time::Instant::now()
+ Duration::from_secs(
// about 10 years
10 * 365 * 24 * 3600,
);
tokio::time::sleep_until(deadline).await;
assert!(tokio::time::Instant::now() >= deadline);
}
#[tokio::test]
async fn long_sleeps() {
tokio::time::pause();
let deadline = tokio::time::Instant::now()
+ Duration::from_secs(
// about 10 years
10 * 365 * 24 * 3600,
);
tokio::time::sleep_until(deadline).await;
assert!(tokio::time::Instant::now() >= deadline);
assert!(tokio::time::Instant::now() <= deadline + Duration::from_millis(1));
}
#[tokio::test]
async fn reset_after_firing() {
let timer = tokio::time::sleep(std::time::Duration::from_millis(1));
tokio::pin!(timer);
let deadline = timer.deadline();
timer.as_mut().await;
assert_ready!(timer
.as_mut()
.poll(&mut Context::from_waker(noop_waker_ref())));
timer
.as_mut()
.reset(tokio::time::Instant::now() + std::time::Duration::from_secs(600));
assert_ne!(deadline, timer.deadline());
assert_pending!(timer
.as_mut()
.poll(&mut Context::from_waker(noop_waker_ref())));
assert_pending!(timer
.as_mut()
.poll(&mut Context::from_waker(noop_waker_ref())));
}
const NUM_LEVELS: usize = 6;
const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
#[tokio::test]
async fn exactly_max() {
time::pause();
time::sleep(ms(MAX_DURATION)).await;
}
#[tokio::test]
async fn issue_5183() {
time::pause();
let big = std::time::Duration::from_secs(u64::MAX / 10);
// This is a workaround since awaiting sleep(big) will never finish.
#[rustfmt::skip]
tokio::select! {
biased;
_ = tokio::time::sleep(big) => {}
_ = tokio::time::sleep(std::time::Duration::from_nanos(1)) => {}
}
}
#[tokio::test]
async fn no_out_of_bounds_close_to_max() {
time::pause();
time::sleep(ms(MAX_DURATION - 1)).await;
}
fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}
#[tokio::test]
async fn drop_after_reschedule_at_new_scheduled_time() {
use futures::poll;
tokio::time::pause();
let start = tokio::time::Instant::now();
let mut a = Box::pin(tokio::time::sleep(Duration::from_millis(5)));
let mut b = Box::pin(tokio::time::sleep(Duration::from_millis(5)));
let mut c = Box::pin(tokio::time::sleep(Duration::from_millis(10)));
let _ = poll!(&mut a);
let _ = poll!(&mut b);
let _ = poll!(&mut c);
b.as_mut().reset(start + Duration::from_millis(10));
a.await;
drop(b);
}
#[tokio::test]
async fn drop_from_wake() {
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::task::Context;
let panicked = Arc::new(AtomicBool::new(false));
let list: Arc<Mutex<Vec<Pin<Box<tokio::time::Sleep>>>>> = Arc::new(Mutex::new(Vec::new()));
let arc_wake = Arc::new(DropWaker(panicked.clone(), list.clone()));
let arc_wake = futures::task::waker(arc_wake);
tokio::time::pause();
{
let mut lock = list.lock().unwrap();
for _ in 0..100 {
let mut timer = Box::pin(tokio::time::sleep(Duration::from_millis(10)));
let _ = timer.as_mut().poll(&mut Context::from_waker(&arc_wake));
lock.push(timer);
}
}
tokio::time::sleep(Duration::from_millis(11)).await;
assert!(
!panicked.load(Ordering::SeqCst),
"panicked when dropping timers"
);
#[derive(Clone)]
struct DropWaker(
Arc<AtomicBool>,
Arc<Mutex<Vec<Pin<Box<tokio::time::Sleep>>>>>,
);
impl futures::task::ArcWake for DropWaker {
fn wake_by_ref(arc_self: &Arc<Self>) {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
*arc_self.1.lock().expect("panic in lock") = Vec::new()
}));
if result.is_err() {
arc_self.0.store(true, Ordering::SeqCst);
}
}
}
}
|