Tomáš Pecka | 0a2e890 | 2020-06-09 21:11:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "trompeloeil_doctest.h" |
| 9 | #include <chrono> |
| 10 | #include <functional> |
| 11 | #include <future> |
| 12 | #include "dbus-helpers/dbus_semaphore_server.h" |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 13 | #include "health/inputs/DbusSemaphoreInput.h" |
Tomáš Pecka | 339bc67 | 2020-11-11 15:59:03 +0100 | [diff] [blame] | 14 | #include "mock/health.h" |
Tomáš Pecka | 0a2e890 | 2020-06-09 21:11:20 +0200 | [diff] [blame] | 15 | #include "test_log_setup.h" |
| 16 | |
| 17 | TEST_CASE("Test semaphore input") |
| 18 | { |
| 19 | using namespace std::literals::chrono_literals; |
| 20 | |
| 21 | TEST_INIT_LOGS; |
| 22 | trompeloeil::sequence seq1; |
| 23 | |
| 24 | const std::string dbusObj = "/cz/cesnet/led"; |
| 25 | const std::string dbusProp = "Semaphore"; |
| 26 | const std::string dbusPropIface = "cz.cesnet.Led"; |
| 27 | |
| 28 | std::vector<std::pair<std::string, std::chrono::milliseconds>> stateSequence; |
| 29 | SECTION("Sequence with pauses") |
| 30 | { |
| 31 | stateSequence = { |
| 32 | {"OK", 505ms}, |
| 33 | {"OK", 311ms}, |
| 34 | {"WARNING", 143ms}, |
| 35 | {"ERROR", 87ms}, |
| 36 | {"WARNING", 333ms}, |
| 37 | {"OK", 1ms}, |
| 38 | }; |
| 39 | } |
| 40 | SECTION("Sequence without pauses") |
| 41 | { |
| 42 | stateSequence = { |
| 43 | {"OK", 0ms}, |
| 44 | {"OK", 0ms}, |
| 45 | {"WARNING", 0ms}, |
| 46 | {"ERROR", 0ms}, |
| 47 | {"WARNING", 0ms}, |
| 48 | {"OK", 0ms}, |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | // setup separate connections for both client and server. Can be done using one connection only but this way it is more generic |
| 53 | auto clientConnection = sdbus::createSessionBusConnection(); |
| 54 | auto serverConnection = sdbus::createSessionBusConnection(); |
| 55 | |
| 56 | // enter client and servers event loops |
| 57 | clientConnection->enterEventLoopAsync(); |
| 58 | serverConnection->enterEventLoopAsync(); |
| 59 | |
| 60 | auto mx = std::make_shared<FakeManager>(); |
| 61 | auto server = DbusSemaphoreServer(*serverConnection, dbusObj, dbusProp, dbusPropIface, "ERROR"); // let the first state be ERROR, because why not |
| 62 | |
| 63 | // i1 gets constructed which means: |
| 64 | // - a registration is performed, along with an updateState call (State::OK) |
| 65 | // - i1's constructor queries the current state and performs updateState |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 66 | REQUIRE_CALL(*mx, registerInput(ANY(void*), velia::health::State::OK)).LR_SIDE_EFFECT(mx->updateState(_1, _2)).IN_SEQUENCE(seq1); |
| 67 | REQUIRE_CALL(*mx, updateState(ANY(void*), velia::health::State::OK)).IN_SEQUENCE(seq1); |
| 68 | REQUIRE_CALL(*mx, updateState(ANY(void*), velia::health::State::ERROR)).IN_SEQUENCE(seq1); |
| 69 | auto i1 = std::make_shared<velia::health::DbusSemaphoreInput>(mx, *clientConnection, serverConnection->getUniqueName(), dbusObj, dbusProp, dbusPropIface); |
Tomáš Pecka | 0a2e890 | 2020-06-09 21:11:20 +0200 | [diff] [blame] | 70 | // i1 now listens for dbus events, we can start the semaphore server |
| 71 | |
| 72 | // mux should get notified for every semaphore state change |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame] | 73 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::OK)).IN_SEQUENCE(seq1); |
| 74 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::OK)).IN_SEQUENCE(seq1); |
| 75 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::WARNING)).IN_SEQUENCE(seq1); |
| 76 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::ERROR)).IN_SEQUENCE(seq1); |
| 77 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::WARNING)).IN_SEQUENCE(seq1); |
| 78 | REQUIRE_CALL(*mx, updateState(i1.get(), velia::health::State::OK)).IN_SEQUENCE(seq1); |
Tomáš Pecka | 0a2e890 | 2020-06-09 21:11:20 +0200 | [diff] [blame] | 79 | |
| 80 | |
| 81 | auto a1 = std::async(std::launch::async, [&]() { server.runStateChanges(stateSequence); }); |
| 82 | |
| 83 | waitForCompletionAndBitMore(seq1); // do not leave event loops until all dbus messages are received |
| 84 | serverConnection->leaveEventLoop(); |
| 85 | clientConnection->leaveEventLoop(); |
| 86 | |
| 87 | REQUIRE_CALL(*mx, unregisterInput(i1.get())).IN_SEQUENCE(seq1); |
| 88 | i1.reset(); |
| 89 | } |