blob: d86c790995c8f5712f95207ba1a19050ca9e6475 [file] [log] [blame]
Tomáš Pecka50584c52023-04-19 10:17:01 +02001/*
2 * Copyright (C) 2016-2021 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
5 *
6 */
7
8#include "trompeloeil_doctest.h"
9#include "ietf-hardware/thresholds.h"
10
11using namespace velia::ietf_hardware;
12using Thrs = Thresholds<int16_t>;
13using OneThr = OneThreshold<int16_t>;
14
Tomáš Pecka50584c52023-04-19 10:17:01 +020015class Transitions {
16public:
17 MAKE_MOCK1(record, void(State));
18};
19
20#define EVENTS_INIT \
21 trompeloeil::sequence seq; \
22 Transitions eventLog; \
Tomáš Peckabacb5da2023-05-04 20:14:20 +020023 Watcher w(thr);
Tomáš Pecka50584c52023-04-19 10:17:01 +020024
Tomáš Peckabacb5da2023-05-04 20:14:20 +020025
26#define EXPECT_EVENT(E, STATE) \
27 { \
28 auto statusChange = E; \
29 REQUIRE(statusChange); \
30 REQUIRE(statusChange == STATE); \
31 }
32
33#define EXPECT_NONE(E) \
34 { \
35 auto statusChange = E; \
36 REQUIRE(!statusChange); \
37 }
Tomáš Pecka50584c52023-04-19 10:17:01 +020038
39TEST_CASE("just one threshold")
40{
41 Thrs thr;
42
43 SECTION("critical-low ok -> failed -> ok")
44 {
45 thr.criticalLow = OneThr{0, 1};
46 EVENTS_INIT
Tomáš Peckabacb5da2023-05-04 20:14:20 +020047 EXPECT_EVENT(w.update(10), State::Normal);
48 EXPECT_EVENT(w.update(-10), State::CriticalLow);
49 EXPECT_EVENT(w.update(10), State::Normal);
Tomáš Pecka50584c52023-04-19 10:17:01 +020050 }
51
52 SECTION("critical-low failed")
53 {
54 thr.criticalLow = OneThr{0, 1};
55 EVENTS_INIT
Tomáš Peckabacb5da2023-05-04 20:14:20 +020056 EXPECT_EVENT(w.update(-10), State::CriticalLow);
Tomáš Pecka50584c52023-04-19 10:17:01 +020057 }
58
59 SECTION("warning-low ok")
60 {
61 thr.warningLow = OneThr{0, 1};
62 EVENTS_INIT
Tomáš Peckabacb5da2023-05-04 20:14:20 +020063 EXPECT_EVENT(w.update(10), State::Normal);
Tomáš Pecka50584c52023-04-19 10:17:01 +020064 }
65
66 SECTION("warning-low failed -> ignoring")
67 {
68 thr.warningLow = OneThr{0, 1};
69 EVENTS_INIT
Tomáš Peckabacb5da2023-05-04 20:14:20 +020070 EXPECT_EVENT(w.update(-10), State::WarningLow);
Tomáš Pecka50584c52023-04-19 10:17:01 +020071 thr.warningLow.reset();
Tomáš Peckabacb5da2023-05-04 20:14:20 +020072 EXPECT_EVENT(w.setThresholds(thr), State::Disabled);
73
74 EXPECT_NONE(w.update(-20));
75 EXPECT_NONE(w.update(-10));
76 EXPECT_NONE(w.update(0));
77 EXPECT_NONE(w.update(10));
78 EXPECT_NONE(w.update(-10));
Tomáš Pecka50584c52023-04-19 10:17:01 +020079 }
Tomáš Pecka68f08ae2023-05-22 14:48:47 +020080
81 SECTION("setting thresholds before updating first value does not trigger any events")
82 {
83 EVENTS_INIT;
84 thr.criticalLow = OneThr{0, 1};
85 EXPECT_NONE(w.setThresholds(thr));
86 }
Tomáš Pecka50584c52023-04-19 10:17:01 +020087}
88
89TEST_CASE("state transitions")
90{
91 Thrs thr;
92 thr.criticalLow = OneThr{10, 1};
93
94 EVENTS_INIT
95
Tomáš Peckabacb5da2023-05-04 20:14:20 +020096 EXPECT_EVENT(w.update(10), State::Normal);
97 EXPECT_NONE(w.update(12));
98 EXPECT_EVENT(w.update(8), State::CriticalLow);
Tomáš Pecka50584c52023-04-19 10:17:01 +020099
Tomáš Pecka68f08ae2023-05-22 14:48:47 +0200100 EXPECT_EVENT(w.update(std::nullopt), State::NoValue);
101 EXPECT_NONE(w.update(std::nullopt));
102 EXPECT_EVENT(w.update(10), State::Normal);
103 EXPECT_EVENT(w.update(std::nullopt), State::NoValue);
104 EXPECT_EVENT(w.update(6), State::CriticalLow);
105
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200106 thr.warningHigh = OneThr{20, 1};
107 EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow);
Tomáš Pecka50584c52023-04-19 10:17:01 +0200108
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200109 EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow);
110 EXPECT_EVENT(w.update(10), State::Normal);
Tomáš Pecka50584c52023-04-19 10:17:01 +0200111
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200112 thr.warningLow = OneThr{13, 1};
113 thr.criticalHigh = OneThr{30, 1};
114 EXPECT_EVENT( w.setThresholds(thr), State::WarningLow);
Tomáš Pecka50584c52023-04-19 10:17:01 +0200115
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200116 EXPECT_NONE(w.update(12));
Tomáš Pecka50584c52023-04-19 10:17:01 +0200117}
118
119TEST_CASE("hysteresis")
120{
121 Thrs thr;
122 thr.criticalHigh = OneThr{40, 2};
123 thr.warningHigh = OneThr{30, 2};
124 thr.warningLow = OneThr{20, 2};
125 thr.criticalLow = OneThr{10, 2};
126 EVENTS_INIT
127
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200128 EXPECT_EVENT(w.update(25), State::Normal);
129 EXPECT_EVENT(w.update(31), State::WarningHigh);
Tomáš Pecka50584c52023-04-19 10:17:01 +0200130
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200131 EXPECT_NONE(w.update(31));
132 EXPECT_NONE(w.update(31));
133 EXPECT_NONE(w.update(29));
134 EXPECT_NONE(w.update(29));
135 EXPECT_NONE(w.update(29));
136 EXPECT_NONE(w.update(31));
137 EXPECT_NONE(w.update(29));
138 EXPECT_NONE(w.update(31));
139 EXPECT_NONE(w.update(29));
Tomáš Pecka50584c52023-04-19 10:17:01 +0200140
Tomáš Peckabacb5da2023-05-04 20:14:20 +0200141 EXPECT_EVENT(w.update(41), State::CriticalHigh);
142 EXPECT_EVENT(w.update(37), State::WarningHigh);
143
144 EXPECT_NONE(w.update(38));
145 EXPECT_NONE(w.update(39));
146 EXPECT_NONE(w.update(40));
Tomáš Pecka68f08ae2023-05-22 14:48:47 +0200147
148 EXPECT_EVENT(w.update(41), State::CriticalHigh);
149 EXPECT_NONE(w.update(39));
150 EXPECT_EVENT(w.update(std::nullopt), State::NoValue);
151 EXPECT_EVENT(w.update(41), State::CriticalHigh);
152 EXPECT_EVENT(w.update(std::nullopt), State::NoValue);
153 EXPECT_EVENT(w.update(39), State::WarningHigh);
154 EXPECT_EVENT(w.update(std::nullopt), State::NoValue);
155
156 thr.criticalHigh.reset();
157 EXPECT_NONE(w.setThresholds(thr));
158
159 thr.criticalHigh = OneThr{40, 2};
160 EXPECT_NONE(w.setThresholds(thr));
161 EXPECT_EVENT(w.update(41), State::CriticalHigh);
Tomáš Pecka50584c52023-04-19 10:17:01 +0200162}