| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use pom::types::HealthStatus; |
| 15 |
|
| 16 |
|
| 17 |
#[derive(Debug, Clone)] |
| 18 |
pub(crate) struct TransitionGate { |
| 19 |
|
| 20 |
confirmed: Option<HealthStatus>, |
| 21 |
|
| 22 |
pending: Option<(HealthStatus, u32)>, |
| 23 |
|
| 24 |
confirmations: u32, |
| 25 |
} |
| 26 |
|
| 27 |
impl TransitionGate { |
| 28 |
|
| 29 |
|
| 30 |
pub(crate) fn seeded(confirmed: Option<HealthStatus>, confirmations: u32) -> Self { |
| 31 |
Self { |
| 32 |
confirmed, |
| 33 |
pending: None, |
| 34 |
confirmations: confirmations.max(1), |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub(crate) fn observe( |
| 43 |
&mut self, |
| 44 |
observed: HealthStatus, |
| 45 |
) -> Option<(HealthStatus, HealthStatus)> { |
| 46 |
match self.confirmed { |
| 47 |
|
| 48 |
|
| 49 |
None => { |
| 50 |
self.confirmed = Some(observed); |
| 51 |
self.pending = None; |
| 52 |
None |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
Some(cur) if observed == cur => { |
| 57 |
self.pending = None; |
| 58 |
None |
| 59 |
} |
| 60 |
|
| 61 |
Some(cur) => { |
| 62 |
let count = match self.pending { |
| 63 |
Some((p, c)) if p == observed => c + 1, |
| 64 |
_ => 1, |
| 65 |
}; |
| 66 |
if count >= self.confirmations { |
| 67 |
self.pending = None; |
| 68 |
self.confirmed = Some(observed); |
| 69 |
Some((cur, observed)) |
| 70 |
} else { |
| 71 |
self.pending = Some((observed, count)); |
| 72 |
None |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg(test)] |
| 80 |
mod tests { |
| 81 |
use super::*; |
| 82 |
use pom::types::HealthStatus::*; |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn confirmations_1_fires_on_first_difference() { |
| 86 |
let mut g = TransitionGate::seeded(Some(Operational), 1); |
| 87 |
assert_eq!(g.observe(Error), Some((Operational, Error))); |
| 88 |
assert_eq!(g.observe(Error), None); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn single_blip_is_absorbed_at_confirmations_2() { |
| 93 |
let mut g = TransitionGate::seeded(Some(Operational), 2); |
| 94 |
|
| 95 |
assert_eq!(g.observe(Error), None); |
| 96 |
|
| 97 |
assert_eq!(g.observe(Operational), None); |
| 98 |
|
| 99 |
assert_eq!(g.observe(Error), None); |
| 100 |
assert_eq!(g.observe(Error), Some((Operational, Error))); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn flapping_candidate_resets_the_run() { |
| 105 |
let mut g = TransitionGate::seeded(Some(Operational), 3); |
| 106 |
assert_eq!(g.observe(Error), None); |
| 107 |
assert_eq!(g.observe(Unreachable), None); |
| 108 |
assert_eq!(g.observe(Unreachable), None); |
| 109 |
assert_eq!(g.observe(Unreachable), Some((Operational, Unreachable))); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn seeded_status_does_not_refire_on_restart() { |
| 114 |
|
| 115 |
|
| 116 |
let mut g = TransitionGate::seeded(Some(Error), 2); |
| 117 |
assert_eq!(g.observe(Error), None); |
| 118 |
assert_eq!(g.observe(Error), None); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn first_ever_observation_is_silent() { |
| 123 |
let mut g = TransitionGate::seeded(None, 2); |
| 124 |
assert_eq!(g.observe(Operational), None); |
| 125 |
|
| 126 |
assert_eq!(g.observe(Error), None); |
| 127 |
assert_eq!(g.observe(Error), Some((Operational, Error))); |
| 128 |
} |
| 129 |
} |
| 130 |
|