Skip to content

Commit

Permalink
adjust thread_sharing test to handle mutex access times
Browse files Browse the repository at this point in the history
  • Loading branch information
museun committed Jun 2, 2024
1 parent 9b012b8 commit dd86591
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ mod tests {

#[test]
fn thread_sharing() {
MockClock::set_time(Duration::ZERO);

let start = Instant::now();

std::thread::spawn(move || {
Expand All @@ -64,14 +66,16 @@ mod tests {
.unwrap();

std::thread::spawn(move || {
let start = Instant::now();
let next = Instant::now();
MockClock::advance(Duration::from_secs(30));
assert_eq!(start.elapsed(), Duration::from_secs(30));
assert_eq!(next.elapsed(), Duration::from_secs(30));
})
.join()
.unwrap();

MockClock::advance(Duration::from_secs(10));
assert_eq!(start.elapsed(), Duration::from_secs(43));
// using seconds because a mutex can be slow on some operating systems
// the creation/locking time may be in microseconds so we'll have something like 43.002 != 43
assert_eq!(start.elapsed().as_secs(), 43);
}
}

0 comments on commit dd86591

Please sign in to comment.