| /* |
| * Copyright (C) 2016-2021 CESNET, https://photonics.cesnet.cz/ |
| * |
| * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| * |
| */ |
| |
| #include "trompeloeil_doctest.h" |
| #include "ietf-hardware/thresholds.h" |
| |
| using namespace velia::ietf_hardware; |
| using Thrs = Thresholds<int16_t>; |
| using OneThr = OneThreshold<int16_t>; |
| |
| class Transitions { |
| public: |
| MAKE_MOCK1(record, void(State)); |
| }; |
| |
| #define EVENTS_INIT \ |
| trompeloeil::sequence seq; \ |
| Transitions eventLog; \ |
| Watcher w(thr); |
| |
| |
| #define EXPECT_EVENT(E, STATE) \ |
| { \ |
| auto statusChange = E; \ |
| REQUIRE(statusChange); \ |
| REQUIRE(statusChange == STATE); \ |
| } |
| |
| #define EXPECT_NONE(E) \ |
| { \ |
| auto statusChange = E; \ |
| REQUIRE(!statusChange); \ |
| } |
| |
| TEST_CASE("just one threshold") |
| { |
| Thrs thr; |
| |
| SECTION("critical-low ok -> failed -> ok") |
| { |
| thr.criticalLow = OneThr{0, 1}; |
| EVENTS_INIT |
| EXPECT_EVENT(w.update(10), State::Normal); |
| EXPECT_EVENT(w.update(-10), State::CriticalLow); |
| EXPECT_EVENT(w.update(10), State::Normal); |
| } |
| |
| SECTION("critical-low failed") |
| { |
| thr.criticalLow = OneThr{0, 1}; |
| EVENTS_INIT |
| EXPECT_EVENT(w.update(-10), State::CriticalLow); |
| } |
| |
| SECTION("warning-low ok") |
| { |
| thr.warningLow = OneThr{0, 1}; |
| EVENTS_INIT |
| EXPECT_EVENT(w.update(10), State::Normal); |
| } |
| |
| SECTION("warning-low failed -> ignoring") |
| { |
| thr.warningLow = OneThr{0, 1}; |
| EVENTS_INIT |
| EXPECT_EVENT(w.update(-10), State::WarningLow); |
| thr.warningLow.reset(); |
| EXPECT_EVENT(w.setThresholds(thr), State::Disabled); |
| |
| EXPECT_NONE(w.update(-20)); |
| EXPECT_NONE(w.update(-10)); |
| EXPECT_NONE(w.update(0)); |
| EXPECT_NONE(w.update(10)); |
| EXPECT_NONE(w.update(-10)); |
| } |
| |
| SECTION("setting thresholds before updating first value does not trigger any events") |
| { |
| EVENTS_INIT; |
| thr.criticalLow = OneThr{0, 1}; |
| EXPECT_NONE(w.setThresholds(thr)); |
| } |
| } |
| |
| TEST_CASE("state transitions") |
| { |
| Thrs thr; |
| thr.criticalLow = OneThr{10, 1}; |
| |
| EVENTS_INIT |
| |
| EXPECT_EVENT(w.update(10), State::Normal); |
| EXPECT_NONE(w.update(12)); |
| EXPECT_EVENT(w.update(8), State::CriticalLow); |
| |
| EXPECT_EVENT(w.update(std::nullopt), State::NoValue); |
| EXPECT_NONE(w.update(std::nullopt)); |
| EXPECT_EVENT(w.update(10), State::Normal); |
| EXPECT_EVENT(w.update(std::nullopt), State::NoValue); |
| EXPECT_EVENT(w.update(6), State::CriticalLow); |
| |
| thr.warningHigh = OneThr{20, 1}; |
| EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow); |
| |
| EXPECT_EVENT(w.setThresholds(thr), State::CriticalLow); |
| EXPECT_EVENT(w.update(10), State::Normal); |
| |
| thr.warningLow = OneThr{13, 1}; |
| thr.criticalHigh = OneThr{30, 1}; |
| EXPECT_EVENT( w.setThresholds(thr), State::WarningLow); |
| |
| EXPECT_NONE(w.update(12)); |
| } |
| |
| TEST_CASE("hysteresis") |
| { |
| Thrs thr; |
| thr.criticalHigh = OneThr{40, 2}; |
| thr.warningHigh = OneThr{30, 2}; |
| thr.warningLow = OneThr{20, 2}; |
| thr.criticalLow = OneThr{10, 2}; |
| EVENTS_INIT |
| |
| EXPECT_EVENT(w.update(25), State::Normal); |
| EXPECT_EVENT(w.update(31), State::WarningHigh); |
| |
| EXPECT_NONE(w.update(31)); |
| EXPECT_NONE(w.update(31)); |
| EXPECT_NONE(w.update(29)); |
| EXPECT_NONE(w.update(29)); |
| EXPECT_NONE(w.update(29)); |
| EXPECT_NONE(w.update(31)); |
| EXPECT_NONE(w.update(29)); |
| EXPECT_NONE(w.update(31)); |
| EXPECT_NONE(w.update(29)); |
| |
| EXPECT_EVENT(w.update(41), State::CriticalHigh); |
| EXPECT_EVENT(w.update(37), State::WarningHigh); |
| |
| EXPECT_NONE(w.update(38)); |
| EXPECT_NONE(w.update(39)); |
| EXPECT_NONE(w.update(40)); |
| |
| EXPECT_EVENT(w.update(41), State::CriticalHigh); |
| EXPECT_NONE(w.update(39)); |
| EXPECT_EVENT(w.update(std::nullopt), State::NoValue); |
| EXPECT_EVENT(w.update(41), State::CriticalHigh); |
| EXPECT_EVENT(w.update(std::nullopt), State::NoValue); |
| EXPECT_EVENT(w.update(39), State::WarningHigh); |
| EXPECT_EVENT(w.update(std::nullopt), State::NoValue); |
| |
| thr.criticalHigh.reset(); |
| EXPECT_NONE(w.setThresholds(thr)); |
| |
| thr.criticalHigh = OneThr{40, 2}; |
| EXPECT_NONE(w.setThresholds(thr)); |
| EXPECT_EVENT(w.update(41), State::CriticalHigh); |
| } |