From 5702918b797f82958a189976fc2d84521589d142 Mon Sep 17 00:00:00 2001 From: Wayne Robinson Date: Thu, 15 Feb 2024 14:52:31 +1000 Subject: [PATCH 1/5] Fix Reversed Logic for QuantaUpkeepInstant.now() --- governor/src/clock/quanta.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/governor/src/clock/quanta.rs b/governor/src/clock/quanta.rs index 4a0b571f..e20c077b 100644 --- a/governor/src/clock/quanta.rs +++ b/governor/src/clock/quanta.rs @@ -100,8 +100,7 @@ impl Clock for QuantaUpkeepClock { fn now(&self) -> Self::Instant { QuantaInstant(Nanos::from( - self.reference - .saturating_duration_since(self.clock.recent()), + self.clock.recent().saturating_duration_since(self.reference), )) } } @@ -116,6 +115,7 @@ mod test { use super::*; use crate::clock::{Clock, QuantaClock, QuantaUpkeepClock, Reference}; use crate::nanos::Nanos; + use std::thread; use std::time::Duration; #[test] @@ -147,4 +147,15 @@ mod test { now ); } + + #[test] + fn quanta_upkeep_advances() { + let clock = QuantaUpkeepClock::from_interval(Duration::from_micros(10)).unwrap(); + let start = clock.now(); + for _ in 0..10 { + thread::sleep(Duration::from_micros(100)); + let now = clock.now(); + assert!(now > start, "now={:?} not after start={:?}", now, start); + } + } } From b2160a7b37728c8de9db11733a80bcce8183c097 Mon Sep 17 00:00:00 2001 From: Wayne Robinson Date: Fri, 16 Feb 2024 02:50:23 +1000 Subject: [PATCH 2/5] Reformat --- governor/src/clock/quanta.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/governor/src/clock/quanta.rs b/governor/src/clock/quanta.rs index e20c077b..95451570 100644 --- a/governor/src/clock/quanta.rs +++ b/governor/src/clock/quanta.rs @@ -100,7 +100,9 @@ impl Clock for QuantaUpkeepClock { fn now(&self) -> Self::Instant { QuantaInstant(Nanos::from( - self.clock.recent().saturating_duration_since(self.reference), + self.clock + .recent() + .saturating_duration_since(self.reference), )) } } From 8e34dca0c1c1f4a78349010de9eec3920a3096a1 Mon Sep 17 00:00:00 2001 From: Wayne Robinson Date: Fri, 16 Feb 2024 02:55:34 +1000 Subject: [PATCH 3/5] Move QuantaUpkeepClock Advances Test with Other as Upkeep Thread is Global --- governor/src/clock/quanta.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/governor/src/clock/quanta.rs b/governor/src/clock/quanta.rs index 95451570..78ba2269 100644 --- a/governor/src/clock/quanta.rs +++ b/governor/src/clock/quanta.rs @@ -135,11 +135,11 @@ mod test { } #[test] - fn quanta_upkeep_impls_coverage() { + fn quanta_upkeep_impls_coverage_and_advances() { let one_ns = Nanos::new(1); // let _c1 = // QuantaUpkeepClock::from_builder(quanta::Upkeep::new(Duration::from_secs(1))).unwrap(); - let c = QuantaUpkeepClock::from_interval(Duration::from_secs(1)).unwrap(); + let c = QuantaUpkeepClock::from_interval(Duration::from_micros(10)).unwrap(); let now = c.now(); assert_ne!(now + one_ns, now); assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now)); @@ -148,15 +148,13 @@ mod test { Reference::saturating_sub(&(now + Duration::from_nanos(1).into()), one_ns), now ); - } - #[test] - fn quanta_upkeep_advances() { - let clock = QuantaUpkeepClock::from_interval(Duration::from_micros(10)).unwrap(); - let start = clock.now(); + // Test clock advances over time. + // (included in one test as only one QuantaUpkeepClock thread can be run at a time) + let start = c.now(); for _ in 0..10 { thread::sleep(Duration::from_micros(100)); - let now = clock.now(); + let now = c.now(); assert!(now > start, "now={:?} not after start={:?}", now, start); } } From a35518281bd309ef63c6b27b19cb4c3d00a000d1 Mon Sep 17 00:00:00 2001 From: Wayne Robinson Date: Fri, 16 Feb 2024 06:05:11 +1000 Subject: [PATCH 4/5] Decrease Update Precision to Stabilise Tests --- governor/src/clock/quanta.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/governor/src/clock/quanta.rs b/governor/src/clock/quanta.rs index 78ba2269..f8823933 100644 --- a/governor/src/clock/quanta.rs +++ b/governor/src/clock/quanta.rs @@ -139,7 +139,7 @@ mod test { let one_ns = Nanos::new(1); // let _c1 = // QuantaUpkeepClock::from_builder(quanta::Upkeep::new(Duration::from_secs(1))).unwrap(); - let c = QuantaUpkeepClock::from_interval(Duration::from_micros(10)).unwrap(); + let c = QuantaUpkeepClock::from_interval(Duration::from_millis(50)).unwrap(); let now = c.now(); assert_ne!(now + one_ns, now); assert_eq!(one_ns, Reference::duration_since(&(now + one_ns), now)); @@ -152,10 +152,10 @@ mod test { // Test clock advances over time. // (included in one test as only one QuantaUpkeepClock thread can be run at a time) let start = c.now(); - for _ in 0..10 { - thread::sleep(Duration::from_micros(100)); + for i in 0..5 { + thread::sleep(Duration::from_millis(250)); let now = c.now(); - assert!(now > start, "now={:?} not after start={:?}", now, start); + assert!(now > start, "now={:?} not after start={:?} on iteration={}", now, start, i); } } } From b60cc4aada79070960457e378ecd89239f4406f1 Mon Sep 17 00:00:00 2001 From: Wayne Robinson Date: Fri, 16 Feb 2024 06:21:44 +1000 Subject: [PATCH 5/5] Fix Formatting --- governor/src/clock/quanta.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/governor/src/clock/quanta.rs b/governor/src/clock/quanta.rs index f8823933..12383d31 100644 --- a/governor/src/clock/quanta.rs +++ b/governor/src/clock/quanta.rs @@ -155,7 +155,13 @@ mod test { for i in 0..5 { thread::sleep(Duration::from_millis(250)); let now = c.now(); - assert!(now > start, "now={:?} not after start={:?} on iteration={}", now, start, i); + assert!( + now > start, + "now={:?} not after start={:?} on iteration={}", + now, + start, + i + ); } } }