Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | using namespace velia::ietf_hardware; |
| 12 | using Thrs = Thresholds<int16_t>; |
| 13 | using OneThr = OneThreshold<int16_t>; |
| 14 | |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 15 | class Transitions { |
| 16 | public: |
| 17 | MAKE_MOCK1(record, void(State)); |
| 18 | }; |
| 19 | |
Jan Kundrát | c151151 | 2024-01-15 13:47:28 +0100 | [diff] [blame] | 20 | #define EVENTS_INIT \ |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 21 | trompeloeil::sequence seq; \ |
Jan Kundrát | c151151 | 2024-01-15 13:47:28 +0100 | [diff] [blame] | 22 | Transitions eventLog; \ |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 23 | Watcher w(thr); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 24 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 25 | |
Jan Kundrát | c151151 | 2024-01-15 13:47:28 +0100 | [diff] [blame] | 26 | #define EXPECT_EVENT(E, STATE) \ |
| 27 | { \ |
| 28 | auto statusChange = E; \ |
| 29 | REQUIRE(statusChange); \ |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 30 | REQUIRE(statusChange == STATE); \ |
| 31 | } |
| 32 | |
Jan Kundrát | c151151 | 2024-01-15 13:47:28 +0100 | [diff] [blame] | 33 | #define EXPECT_NONE(E) \ |
| 34 | { \ |
| 35 | auto statusChange = E; \ |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 36 | REQUIRE(!statusChange); \ |
| 37 | } |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 38 | |
| 39 | TEST_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áš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 47 | EXPECT_EVENT(w.update(10), State::Normal); |
| 48 | EXPECT_EVENT(w.update(-10), State::CriticalLow); |
| 49 | EXPECT_EVENT(w.update(10), State::Normal); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | SECTION("critical-low failed") |
| 53 | { |
| 54 | thr.criticalLow = OneThr{0, 1}; |
| 55 | EVENTS_INIT |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 56 | EXPECT_EVENT(w.update(-10), State::CriticalLow); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | SECTION("warning-low ok") |
| 60 | { |
| 61 | thr.warningLow = OneThr{0, 1}; |
| 62 | EVENTS_INIT |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 63 | EXPECT_EVENT(w.update(10), State::Normal); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | SECTION("warning-low failed -> ignoring") |
| 67 | { |
| 68 | thr.warningLow = OneThr{0, 1}; |
| 69 | EVENTS_INIT |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 70 | EXPECT_EVENT(w.update(-10), State::WarningLow); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 71 | thr.warningLow.reset(); |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 72 | 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áš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 79 | } |
Tomáš Pecka | 68f08ae | 2023-05-22 14:48:47 +0200 | [diff] [blame] | 80 | |
| 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áš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_CASE("state transitions") |
| 90 | { |
| 91 | Thrs thr; |
| 92 | thr.criticalLow = OneThr{10, 1}; |
| 93 | |
| 94 | EVENTS_INIT |
| 95 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 96 | EXPECT_EVENT(w.update(10), State::Normal); |
| 97 | EXPECT_NONE(w.update(12)); |
| 98 | EXPECT_EVENT(w.update(8), State::CriticalLow); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 99 | |
Tomáš Pecka | 68f08ae | 2023-05-22 14:48:47 +0200 | [diff] [blame] | 100 | 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áš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 106 | thr.warningHigh = OneThr{20, 1}; |
| 107 | EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 108 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 109 | EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow); |
| 110 | EXPECT_EVENT(w.update(10), State::Normal); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 111 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 112 | thr.warningLow = OneThr{13, 1}; |
| 113 | thr.criticalHigh = OneThr{30, 1}; |
| 114 | EXPECT_EVENT( w.setThresholds(thr), State::WarningLow); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 115 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 116 | EXPECT_NONE(w.update(12)); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_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áš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 128 | EXPECT_EVENT(w.update(25), State::Normal); |
| 129 | EXPECT_EVENT(w.update(31), State::WarningHigh); |
Tomáš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 130 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 131 | 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áš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 140 | |
Tomáš Pecka | bacb5da | 2023-05-04 20:14:20 +0200 | [diff] [blame] | 141 | 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áš Pecka | 68f08ae | 2023-05-22 14:48:47 +0200 | [diff] [blame] | 147 | |
| 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áš Pecka | 50584c5 | 2023-04-19 10:17:01 +0200 | [diff] [blame] | 162 | } |