From d09bab3d0caf83fdbd6faac673dfbea980b80c58 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 25 Nov 2025 11:48:24 +1100 Subject: [PATCH] test(relay): go back to the future before healthcheck (#10961) The health-check tests for the relay use `Instant::elapsed` which implicitly uses `Instant::now`. On a freshly booted Windows machine, these tests might easily fail because we are subtracting 15 minutes from `Instant::now` which might result in an underflow as Windows cannot represent `Instant`s prior to the boot time. Related: #10927 --- rust/relay/server/src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/rust/relay/server/src/main.rs b/rust/relay/server/src/main.rs index bc66b94b9..32cf1a2c4 100644 --- a/rust/relay/server/src/main.rs +++ b/rust/relay/server/src/main.rs @@ -769,10 +769,10 @@ fn fmt_human_throughput(mut throughput: f64) -> String { fn make_is_healthy( last_heartbeat_sent: Arc>>, ) -> impl Fn() -> bool + Clone + Send + Sync + 'static { - move || is_healthy(last_heartbeat_sent.clone()) + move || is_healthy(Instant::now(), last_heartbeat_sent.clone()) } -fn is_healthy(last_heartbeat_sent: Arc>>) -> bool { +fn is_healthy(now: Instant, last_heartbeat_sent: Arc>>) -> bool { let guard = last_heartbeat_sent .lock() .unwrap_or_else(|e| e.into_inner()); @@ -781,7 +781,7 @@ fn is_healthy(last_heartbeat_sent: Arc>>) -> bool { return true; // If we are not connected to the portal, we are always healthy. }; - last_hearbeat_sent.elapsed() < MAX_PARTITION_TIME + now.duration_since(last_hearbeat_sent) < MAX_PARTITION_TIME } fn make_otel_metadata() -> opentelemetry_sdk::Resource { @@ -814,25 +814,31 @@ mod tests { // If we are running in standalone mode, we are always healthy. #[test] fn given_no_heartbeat_is_healthy() { - let is_healthy = is_healthy(Arc::new(Mutex::new(None))); + let is_healthy = is_healthy(Instant::now(), Arc::new(Mutex::new(None))); assert!(is_healthy) } #[test] fn given_heartbeat_in_last_15_min_is_healthy() { - let is_healthy = is_healthy(Arc::new(Mutex::new(Some( - Instant::now() - Duration::from_secs(10), - )))); + let now = Instant::now() + Duration::from_hours(1); + + let is_healthy = is_healthy( + now, + Arc::new(Mutex::new(Some(now - Duration::from_secs(10)))), + ); assert!(is_healthy) } #[test] fn given_last_heartbeat_older_than_15_min_is_not_healthy() { - let is_healthy = is_healthy(Arc::new(Mutex::new(Some( - Instant::now() - Duration::from_secs(60 * 15), - )))); + let now = Instant::now() + Duration::from_hours(1); // Advance to the future to avoid underflow. + + let is_healthy = is_healthy( + now, + Arc::new(Mutex::new(Some(now - Duration::from_secs(60 * 15)))), + ); assert!(!is_healthy) }