|
| 1 | +// SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com> |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#include <BoostTestTargetConfig.h> |
| 5 | +#include "perfdata/opentsdbwriter.hpp" |
| 6 | +#include "test/base-testloggerfixture.hpp" |
| 7 | +#include "test/perfdata-perfdatawriterfixture.hpp" |
| 8 | +#include "test/utils.hpp" |
| 9 | + |
| 10 | +using namespace icinga; |
| 11 | + |
| 12 | +BOOST_FIXTURE_TEST_SUITE(perfdata_opentsdbwriter, PerfdataWriterFixture<OpenTsdbWriter>, |
| 13 | + *boost::unit_test::label("perfdata")) |
| 14 | + |
| 15 | +BOOST_AUTO_TEST_CASE(connect) |
| 16 | +{ |
| 17 | + ResumeWriter(); |
| 18 | + |
| 19 | + ReceiveCheckResults(1, ServiceState::ServiceCritical); |
| 20 | + |
| 21 | + Accept(); |
| 22 | + auto msg = GetDataUntil('\n'); |
| 23 | + std::vector<std::string> splitMsg; |
| 24 | + boost::split(splitMsg, msg, boost::is_any_of(" ")); |
| 25 | + |
| 26 | + // Just some basic sanity tests. It's not important to check if everything is entirely correct here. |
| 27 | + BOOST_REQUIRE_EQUAL(splitMsg.size(), 5); |
| 28 | + BOOST_REQUIRE_EQUAL(splitMsg[0], "put"); |
| 29 | + BOOST_REQUIRE_EQUAL(splitMsg[1], "icinga.host.state"); |
| 30 | + BOOST_REQUIRE_CLOSE(boost::lexical_cast<double>(splitMsg[2]), Utility::GetTime(), 1); |
| 31 | + BOOST_REQUIRE_EQUAL(splitMsg[3], "1"); |
| 32 | + BOOST_REQUIRE_EQUAL(splitMsg[4], "host=h1"); |
| 33 | + PauseWriter(); |
| 34 | +} |
| 35 | + |
| 36 | +BOOST_AUTO_TEST_CASE(pause_with_pending_work) |
| 37 | +{ |
| 38 | + ResumeWriter(); |
| 39 | + |
| 40 | + // Make OpenTsdbWriter send a huge message that fills up the connection's buffer. |
| 41 | + ReceiveCheckResults(1, ServiceState::ServiceCritical, [&](const CheckResult::Ptr& cr) { |
| 42 | + cr->GetPerformanceData()->Add(new PerfdataValue{GetRandomString("aaaaa", 24 * 1024 * 1024), 1}); |
| 43 | + }); |
| 44 | + |
| 45 | + // Accept the connection, and read until OpenTsdbWriter has started sending the large part |
| 46 | + // of the PerfdataValue sent above. |
| 47 | + Accept(); |
| 48 | + GetDataUntil("aaaaa"); |
| 49 | + |
| 50 | + // Now stop reading and try to pause OpenTsdbWriter. |
| 51 | + PauseWriter(); |
| 52 | + |
| 53 | + REQUIRE_LOG_MESSAGE("Operation canceled\\.", 10s); |
| 54 | + REQUIRE_LOG_MESSAGE("'OpenTsdbWriter' paused\\.", 10s); |
| 55 | +} |
| 56 | + |
| 57 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments