Skip to content

Commit

Permalink
Test that test-override is unique per thread
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanburgers authored and pitdicker committed Sep 3, 2023
1 parent fc0c9fc commit c4c4aec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ impl Local {
/// use chrono::{Local, FixedOffset, TimeZone, Datelike};
/// fn is_today_leap_day() -> bool {
/// let today = Local::now().date_naive();
/// dbg!(today);
/// today.month() == 2 && today.day() == 29
/// }
///
Expand Down
36 changes: 36 additions & 0 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,40 @@ mod tests {
Local::override_now(None);
assert_ne!(Utc::now(), now);
}

#[cfg(all(feature = "clock", feature = "test-override"))]
#[test]
fn test_override_multiple_threads() {
use std::sync::{Arc, Barrier};
use std::thread::spawn;

let barrier = Arc::new(Barrier::new(3));

let barrier_1 = barrier.clone();
let thread_1 = spawn(move || {
Local::override_now(Some(Utc.with_ymd_and_hms(2020, 2, 29, 12, 0, 0).unwrap().into()));
barrier_1.wait();

assert_eq!(Utc::now(), Utc.with_ymd_and_hms(2020, 2, 29, 12, 0, 0).unwrap());
});

let barrier_2 = barrier.clone();
let thread_2 = spawn(move || {
Local::override_now(Some(Utc.with_ymd_and_hms(2016, 2, 29, 12, 0, 0).unwrap().into()));
barrier_2.wait();

assert_eq!(Utc::now(), Utc.with_ymd_and_hms(2016, 2, 29, 12, 0, 0).unwrap());
});

let barrier_3 = barrier;
let thread_3 = spawn(move || {
barrier_3.wait();

assert!(Utc::now() > Utc.with_ymd_and_hms(2021, 8, 7, 13, 0, 0).unwrap());
});

thread_1.join().expect("Thread 1 should succeed");
thread_2.join().expect("Thread 2 should succeed");
thread_3.join().expect("Thread 3 should succeed");
}
}

0 comments on commit c4c4aec

Please sign in to comment.