1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use super::jittered_health_delay;
use std::{collections::HashSet, time::Duration};
/// Module ids drawn from a real fleet, so the dispersal claim is about names
/// that actually occur rather than invented ones.
///
/// This is a SAMPLE, not a registry: the property under test is that distinct
/// ids disperse, which holds for any set of distinct strings. Several entries
/// are already historical (modules get renamed), and that costs nothing here --
/// but it means a reader must not mistake this for the live module set, and a
/// rename sweep will match it without there being anything to change.
const FLEET: [&str; 14] = [
"aft",
"alfonso-core",
"magic-context",
"broca",
"thalamus",
"quota",
"engram",
"plexus",
"cerebellum",
"astrocyte",
"synapse",
"subc-mcp",
"cortexkit-credentials",
"subc-federation",
];
/// Probes must not converge after a fleet-wide restart.
///
/// This is the property the jitter exists for: every module reconnects at
/// once, and without dispersal all fourteen would then probe on the same
/// tick forever. Nothing failed visibly when this went untested -- a
/// convergent fleet still probes correctly, just in a burst, so the symptom
/// is a periodic load spike that looks like whatever else is running.
#[test]
fn probe_delays_disperse_across_the_fleet() {
let cadence = Duration::from_secs(30);
let delays: HashSet<Duration> = FLEET
.iter()
.map(|id| jittered_health_delay(id, 0, cadence))
.collect();
assert_eq!(
delays.len(),
FLEET.len(),
"every supervised module must land on its own probe offset"
);
}
/// The offset may only ever DELAY a probe, never bring it forward.
///
/// A delay below the cadence would probe a module more often than
/// configured, which is the opposite of what an operator asked for and
/// would tighten the failure budget without anyone changing it.
#[test]
fn jitter_only_delays_and_stays_within_one_tenth_of_cadence() {
let cadence = Duration::from_secs(30);
let span = cadence / 10;
for id in FLEET {
for probe_index in 0..8 {
let delay = jittered_health_delay(id, probe_index, cadence);
assert!(
delay >= cadence,
"{id}#{probe_index}: jitter must not shorten the cadence"
);
assert!(
delay < cadence + span,
"{id}#{probe_index}: jitter must stay inside one tenth of the cadence"
);
}
}
}
/// A module keeps its offset across daemon restarts.
///
/// The delay is derived rather than randomised precisely so a restart does
/// not re-roll every module into a fresh chance of collision. A random
/// source would satisfy the dispersal test above and quietly lose this.
#[test]
fn a_module_offset_is_stable_across_restarts() {
let cadence = Duration::from_secs(30);
for id in FLEET {
assert_eq!(
jittered_health_delay(id, 0, cadence),
jittered_health_delay(id, 0, cadence),
"{id}: the same module and probe index must produce the same offset"
);
}
}
/// A zero cadence disables probing rather than producing a busy loop.
#[test]
fn zero_cadence_yields_zero_delay() {
assert_eq!(
jittered_health_delay("aft", 0, Duration::ZERO),
Duration::ZERO
);
}