Skip to main content

max / makenotwork

2.5 KB · 96 lines History Blame Raw
1 //! Incident state machine: decides whether a confirmed health transition should
2 //! open, close, or close-and-open an incident.
3
4 use pom::types::HealthStatus;
5
6 #[derive(Debug, PartialEq, Eq)]
7 pub(crate) enum IncidentAction {
8 None,
9 Open,
10 Close,
11 CloseAndOpen,
12 }
13
14 pub(crate) fn incident_action(prev: HealthStatus, curr: HealthStatus) -> IncidentAction {
15 if prev == curr {
16 return IncidentAction::None;
17 }
18 let prev_op = prev == HealthStatus::Operational;
19 let curr_op = curr == HealthStatus::Operational;
20 match (prev_op, curr_op) {
21 (true, false) => IncidentAction::Open,
22 (false, true) => IncidentAction::Close,
23 (false, false) => IncidentAction::CloseAndOpen,
24 (true, true) => IncidentAction::None,
25 }
26 }
27
28 #[cfg(test)]
29 mod tests {
30 use super::*;
31
32 #[test]
33 fn incident_operational_to_error_opens() {
34 assert_eq!(
35 incident_action(HealthStatus::Operational, HealthStatus::Error),
36 IncidentAction::Open,
37 );
38 }
39
40 #[test]
41 fn incident_operational_to_degraded_opens() {
42 assert_eq!(
43 incident_action(HealthStatus::Operational, HealthStatus::Degraded),
44 IncidentAction::Open,
45 );
46 }
47
48 #[test]
49 fn incident_error_to_operational_closes() {
50 assert_eq!(
51 incident_action(HealthStatus::Error, HealthStatus::Operational),
52 IncidentAction::Close,
53 );
54 }
55
56 #[test]
57 fn incident_degraded_to_operational_closes() {
58 assert_eq!(
59 incident_action(HealthStatus::Degraded, HealthStatus::Operational),
60 IncidentAction::Close,
61 );
62 }
63
64 #[test]
65 fn incident_degraded_to_error_closes_and_opens() {
66 assert_eq!(
67 incident_action(HealthStatus::Degraded, HealthStatus::Error),
68 IncidentAction::CloseAndOpen,
69 );
70 }
71
72 #[test]
73 fn incident_error_to_degraded_closes_and_opens() {
74 assert_eq!(
75 incident_action(HealthStatus::Error, HealthStatus::Degraded),
76 IncidentAction::CloseAndOpen,
77 );
78 }
79
80 #[test]
81 fn incident_operational_to_operational_none() {
82 assert_eq!(
83 incident_action(HealthStatus::Operational, HealthStatus::Operational),
84 IncidentAction::None,
85 );
86 }
87
88 #[test]
89 fn incident_error_to_error_none() {
90 assert_eq!(
91 incident_action(HealthStatus::Error, HealthStatus::Error),
92 IncidentAction::None,
93 );
94 }
95 }
96