guard/tests/manager_heartbeat.rs
loki5512344 e2adf8601c
refactor: replace unmaintained chrono with time (RUSTSEC-2026-0543)
Adds a timeutil module (unix_now, is_valid_unix, RFC 3339 format/parse) and moves
ClickHouse event timestamps to time::serde::rfc3339. cargo deny is green again.
2026-11-21 18:25:22 +01:00

48 lines
1.5 KiB
Rust

//! Node heartbeat expiry: a stale, missing or unparsable `last_heartbeat`
//! marks the node offline, while a fresh heartbeat leaves the record untouched.
#![cfg(feature = "store-redis")]
use rampart::manager::sync::heartbeat::{NODES_KEY_PATTERN, offline_update};
fn rfc3339(ts: i64) -> String {
rampart::timeutil::format_rfc3339(ts).expect("in-range timestamp")
}
#[test]
fn expired_heartbeat_is_marked_offline() {
let now = 2_000_000_i64;
let raw = format!(
r#"{{"name":"edge1","last_heartbeat":"{}","status":"online"}}"#,
rfc3339(now - 120)
);
let updated = offline_update(&raw, now).expect("expired node should be marked offline");
assert!(updated.contains("offline"));
assert!(updated.contains("edge1"));
}
#[test]
fn fresh_heartbeat_is_left_untouched() {
let now = 2_000_000_i64;
let raw = format!(r#"{{"last_heartbeat":"{}","status":"online"}}"#, rfc3339(now - 5));
assert!(offline_update(&raw, now).is_none());
}
#[test]
fn missing_or_broken_timestamp_counts_as_expired() {
let now = 2_000_000_i64;
let updated = offline_update(r#"{"status":"online"}"#, now).expect("missing heartbeat counts as expired");
assert!(updated.contains("offline"));
let updated = offline_update(r#"{"last_heartbeat":"garbage","status":"online"}"#, now)
.expect("unparseable heartbeat counts as expired");
assert!(updated.contains("offline"));
}
#[test]
fn invalid_json_is_ignored() {
assert!(offline_update("not json", 2_000_000).is_none());
}
#[test]
fn node_key_pattern_targets_rampart_nodes() {
assert_eq!(NODES_KEY_PATTERN, "rampart:nodes:*");
}