Skip to content

Commit baf7d1d

Browse files
committed
adds sleep_until test
1 parent 93e4ce0 commit baf7d1d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

library/std/tests/thread.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#![feature(thread_sleep_until)]
2+
13
use std::sync::{Arc, Mutex};
24
use std::thread;
3-
use std::time::Duration;
5+
use std::time::{Duration, Instant};
46

57
#[test]
68
#[cfg_attr(target_os = "emscripten", ignore)]
@@ -14,3 +16,19 @@ fn sleep() {
1416
thread::sleep(Duration::from_millis(100));
1517
assert_eq!(*finished.lock().unwrap(), false);
1618
}
19+
20+
#[test]
21+
fn sleep_until() {
22+
let now = Instant::now();
23+
let period = Duration::from_millis(100);
24+
let deadline = now + period;
25+
thread::sleep_until(deadline);
26+
27+
let margin = Duration::from_micros(100);
28+
let elapsed = now.elapsed();
29+
assert!(
30+
(period..period + margin).contains(&elapsed),
31+
"elapsed: {} microseconds, expected 100_000 to 100_100",
32+
elapsed.as_micros()
33+
);
34+
}

0 commit comments

Comments
 (0)