diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 20181848bf..416fc9984e 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -166,7 +166,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 /// } /// diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 42cb03f8f9..ed8996e6f1 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -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"); + } }