Skip to content

Commit

Permalink
Relax duration judgment
Browse files Browse the repository at this point in the history
  • Loading branch information
mirromutth committed Dec 19, 2023
1 parent cb83e52 commit da3c57b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions latches/src/lock/futexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ mod tests {

macro_rules! assert_time {
($time:expr, $m:literal $(,)?) => {
assert!((Duration::from_millis($m)
..Duration::from_millis($m + std::cmp::max($m >> 1, 10)))
assert!((Duration::from_millis($m - 1)
..Duration::from_millis($m + std::cmp::max($m >> 1, 50)))
.contains(&$time))
};
}
Expand Down Expand Up @@ -314,10 +314,10 @@ mod tests {
start.elapsed()
});

thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(20));
unsafe { mutex.unlock() };

assert_time!(t.join().unwrap(), 10);
assert_time!(t.join().unwrap(), 20);
}

#[cfg(coverage)]
Expand Down
8 changes: 4 additions & 4 deletions latches/src/sync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::time::Instant;
#[cfg(feature = "std")]
macro_rules! assert_time {
($time:expr, $mills:literal $(,)?) => {
assert!((Duration::from_millis($mills)
..Duration::from_millis($mills + std::cmp::max($mills >> 1, 10)))
assert!((Duration::from_millis($mills - 1)
..Duration::from_millis($mills + std::cmp::max($mills >> 1, 50)))
.contains(&$time))
};
}
Expand Down Expand Up @@ -215,14 +215,14 @@ fn test_two_wait_timeout() {

let fast = thread::spawn(move || {
let start = Instant::now();
let r = l1.wait_timeout(Duration::from_millis(10));
let r = l1.wait_timeout(Duration::from_millis(20));

assert!(r.is_timed_out());
start.elapsed()
});

assert_time!(slow.join().unwrap(), 100);
assert_time!(fast.join().unwrap(), 10);
assert_time!(fast.join().unwrap(), 20);
}

#[cfg(feature = "std")]
Expand Down
8 changes: 4 additions & 4 deletions latches/src/task/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use super::Latch;
/// Asynchronous timer may not be accurate.
macro_rules! assert_time {
($time:expr, $mills:literal $(,)?) => {
assert!((Duration::from_millis($mills)
..Duration::from_millis($mills + std::cmp::max($mills >> 1, 32)))
assert!((Duration::from_millis($mills - 1)
..Duration::from_millis($mills + std::cmp::max($mills >> 1, 50)))
.contains(&$time))
};
}
Expand Down Expand Up @@ -196,7 +196,7 @@ async fn test_multi_tasks() {
let counter = counter.clone();

tokio::spawn(async move {
sleep(Duration::from_millis(10)).await;
sleep(Duration::from_millis(20)).await;
counter.fetch_add(1, Ordering::Relaxed);
latch.count_down()
});
Expand All @@ -205,7 +205,7 @@ async fn test_multi_tasks() {
let start = Instant::now();

latch.wait().await;
assert_time!(start.elapsed(), 10);
assert_time!(start.elapsed(), 20);
assert_eq!(counter.load(Ordering::Relaxed), SIZE);
assert_eq!(latch.count(), 0);
}
Expand Down

0 comments on commit da3c57b

Please sign in to comment.