Skip to content

Commit 0e82dc9

Browse files
bjorn3gitbot
authored and
gitbot
committed
Move std::time unit tests to integration tests
1 parent 5fd13b1 commit 0e82dc9

File tree

4 files changed

+53
-53
lines changed

4 files changed

+53
-53
lines changed

std/benches/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ extern crate test;
66

77
mod hash;
88
mod path;
9+
mod time;

std/benches/time.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::time::Instant;
2+
3+
#[cfg(not(target_arch = "wasm32"))]
4+
use test::{Bencher, black_box};
5+
6+
macro_rules! bench_instant_threaded {
7+
($bench_name:ident, $thread_count:expr) => {
8+
#[bench]
9+
#[cfg(not(target_arch = "wasm32"))]
10+
fn $bench_name(b: &mut Bencher) -> std::thread::Result<()> {
11+
use std::sync::Arc;
12+
use std::sync::atomic::{AtomicBool, Ordering};
13+
14+
let running = Arc::new(AtomicBool::new(true));
15+
16+
let threads: Vec<_> = (0..$thread_count)
17+
.map(|_| {
18+
let flag = Arc::clone(&running);
19+
std::thread::spawn(move || {
20+
while flag.load(Ordering::Relaxed) {
21+
black_box(Instant::now());
22+
}
23+
})
24+
})
25+
.collect();
26+
27+
b.iter(|| {
28+
let a = Instant::now();
29+
let b = Instant::now();
30+
assert!(b >= a);
31+
});
32+
33+
running.store(false, Ordering::Relaxed);
34+
35+
for t in threads {
36+
t.join()?;
37+
}
38+
Ok(())
39+
}
40+
};
41+
}
42+
43+
bench_instant_threaded!(instant_contention_01_threads, 0);
44+
bench_instant_threaded!(instant_contention_02_threads, 1);
45+
bench_instant_threaded!(instant_contention_04_threads, 3);
46+
bench_instant_threaded!(instant_contention_08_threads, 7);
47+
bench_instant_threaded!(instant_contention_16_threads, 15);

std/src/time.rs

-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
3232
#![stable(feature = "time", since = "1.3.0")]
3333

34-
#[cfg(test)]
35-
mod tests;
36-
3734
#[stable(feature = "time", since = "1.3.0")]
3835
pub use core::time::Duration;
3936
#[stable(feature = "duration_checked_float", since = "1.66.0")]

std/src/time/tests.rs std/tests/time.rs

+5-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use core::fmt::Debug;
1+
#![feature(duration_constants)]
22

3-
#[cfg(not(target_arch = "wasm32"))]
4-
use test::{Bencher, black_box};
5-
6-
use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
3+
use std::fmt::Debug;
4+
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
75

86
macro_rules! assert_almost_eq {
97
($a:expr, $b:expr) => {{
@@ -29,10 +27,10 @@ fn instant_monotonic() {
2927

3028
#[test]
3129
#[cfg(not(target_arch = "wasm32"))]
32-
fn instant_monotonic_concurrent() -> crate::thread::Result<()> {
30+
fn instant_monotonic_concurrent() -> std::thread::Result<()> {
3331
let threads: Vec<_> = (0..8)
3432
.map(|_| {
35-
crate::thread::spawn(|| {
33+
std::thread::spawn(|| {
3634
let mut old = Instant::now();
3735
let count = if cfg!(miri) { 1_000 } else { 5_000_000 };
3836
for _ in 0..count {
@@ -229,46 +227,3 @@ fn big_math() {
229227
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
230228
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
231229
}
232-
233-
macro_rules! bench_instant_threaded {
234-
($bench_name:ident, $thread_count:expr) => {
235-
#[bench]
236-
#[cfg(not(target_arch = "wasm32"))]
237-
fn $bench_name(b: &mut Bencher) -> crate::thread::Result<()> {
238-
use crate::sync::Arc;
239-
use crate::sync::atomic::{AtomicBool, Ordering};
240-
241-
let running = Arc::new(AtomicBool::new(true));
242-
243-
let threads: Vec<_> = (0..$thread_count)
244-
.map(|_| {
245-
let flag = Arc::clone(&running);
246-
crate::thread::spawn(move || {
247-
while flag.load(Ordering::Relaxed) {
248-
black_box(Instant::now());
249-
}
250-
})
251-
})
252-
.collect();
253-
254-
b.iter(|| {
255-
let a = Instant::now();
256-
let b = Instant::now();
257-
assert!(b >= a);
258-
});
259-
260-
running.store(false, Ordering::Relaxed);
261-
262-
for t in threads {
263-
t.join()?;
264-
}
265-
Ok(())
266-
}
267-
};
268-
}
269-
270-
bench_instant_threaded!(instant_contention_01_threads, 0);
271-
bench_instant_threaded!(instant_contention_02_threads, 1);
272-
bench_instant_threaded!(instant_contention_04_threads, 3);
273-
bench_instant_threaded!(instant_contention_08_threads, 7);
274-
bench_instant_threaded!(instant_contention_16_threads, 15);

0 commit comments

Comments
 (0)