From 788ca57859a54476ceea0a75a9978157480604ae Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 14:53:55 +0000 Subject: [PATCH] test(tools): add tests for the tools library - RateLimiter: lower bound of the duration at 200 Hz; rate 0 does not sleep - InstanceLimiter: throws above the limit, counts down on destruction - Semaphore and SharedSemaphore: counts, blocking Wait(), shared count - ToString(), StrStartsWith(), StrEndsWith(), ToStrVector() - Uuid() and UuidHash() are unique - execute(): output with prefix, stderr, exit code, stdin input, signal, timeout - Skip Tools.NetworkDefaultIP on a machine without a default route Co-Authored-By: Claude Opus 5.5 --- test/CMakeLists.txt | 1 + test/tools/_network.cxx | 4 +- test/tools/_tools.cxx | 191 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 test/tools/_tools.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 21226a09..a482a5a8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -263,6 +263,7 @@ add_testsuite(Tools SOURCES ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx tools/_network.cxx + tools/_tools.cxx LINKS FairMQ INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/test/tools/_network.cxx b/test/tools/_network.cxx index e63d73c7..e24e4ed0 100644 --- a/test/tools/_network.cxx +++ b/test/tools/_network.cxx @@ -16,7 +16,9 @@ namespace TEST(Tools, NetworkDefaultIP) { auto const interface = fair::mq::tools::getDefaultRouteNetworkInterface(); - EXPECT_NE(interface, ""); + if (interface.empty()) { + GTEST_SKIP() << "no default route on this machine"; + } auto const interfaceIP = fair::mq::tools::getInterfaceIP(interface); EXPECT_NE(interfaceIP, ""); } diff --git a/test/tools/_tools.cxx b/test/tools/_tools.cxx new file mode 100644 index 00000000..acf83608 --- /dev/null +++ b/test/tools/_tools.cxx @@ -0,0 +1,191 @@ +/******************************************************************************** + * Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +using namespace std; +using namespace fair::mq::tools; + +TEST(RateLimiter, LimitsTheRate) +{ + // 100 calls at 200 Hz take at least approx. 0.5 s. Check only a loose + // lower bound: a slow machine can make the loop slower, never faster. + RateLimiter limiter(200); + auto const start = chrono::steady_clock::now(); + for (int i = 0; i < 100; ++i) { + limiter.maybe_sleep(); + } + auto const elapsed = chrono::steady_clock::now() - start; + EXPECT_GE(elapsed, chrono::milliseconds(350)); + EXPECT_LT(elapsed, chrono::seconds(5)); +} + +TEST(RateLimiter, ZeroRateDoesNotSleep) +{ + RateLimiter limiter(0); + auto const start = chrono::steady_clock::now(); + for (int i = 0; i < 100000; ++i) { + limiter.maybe_sleep(); + } + EXPECT_LT(chrono::steady_clock::now() - start, chrono::seconds(1)); +} + +struct TestTag +{ + friend auto operator<<(ostream& os, TestTag) -> ostream& { return os << "TestTag"; } +}; + +TEST(InstanceLimiter, ThrowsAboveTheLimit) +{ + using Limiter = InstanceLimiter; + { + Limiter first; + Limiter second; + EXPECT_EQ(first.GetCount(), 2); + try { + Limiter third; + ADD_FAILURE() << "no exception for the third instance"; + } catch (runtime_error const& e) { + EXPECT_EQ(string(e.what()), "More than 2 instances of TestTag in parallel not supported"); + } + EXPECT_EQ(first.GetCount(), 2); + } + Limiter again; + EXPECT_EQ(again.GetCount(), 1); +} + +TEST(Semaphore, CountsSignalsAndWaits) +{ + Semaphore sem(2); + EXPECT_EQ(sem.GetCount(), 2U); + sem.Wait(); + sem.Wait(); + EXPECT_EQ(sem.GetCount(), 0U); + sem.Signal(); + EXPECT_EQ(sem.GetCount(), 1U); + EXPECT_EQ(Semaphore().GetCount(), 0U); +} + +TEST(Semaphore, WaitBlocksUntilSignal) +{ + Semaphore sem; + atomic done{false}; + fair::mq::test::JoiningThread waiter([&] { + sem.Wait(); + done = true; + }); + this_thread::sleep_for(chrono::milliseconds(20)); + EXPECT_FALSE(done); + sem.Signal(); + EXPECT_TRUE(fair::mq::test::WaitUntil([&] { return done.load(); })); +} + +TEST(SharedSemaphore, CopiesShareTheCount) +{ + SharedSemaphore sem(1); + SharedSemaphore copy = sem; // NOLINT(performance-unnecessary-copy-initialization) + copy.Signal(); + EXPECT_EQ(sem.GetCount(), 2U); + sem.Wait(); + EXPECT_EQ(copy.GetCount(), 1U); +} + +TEST(Strings, ToString) +{ + EXPECT_EQ(ToString(), ""); + EXPECT_EQ(ToString("a", 1, '-', 2.5, string("z")), "a1-2.5z"); +} + +TEST(Strings, StartsAndEndsWith) +{ + EXPECT_TRUE(StrStartsWith("fairmq", "fair")); + EXPECT_TRUE(StrStartsWith("fairmq", "")); + EXPECT_FALSE(StrStartsWith("fair", "fairmq")); + EXPECT_FALSE(StrStartsWith("fairmq", "mq")); + EXPECT_TRUE(StrEndsWith("fairmq", "mq")); + EXPECT_TRUE(StrEndsWith("fairmq", "")); + EXPECT_FALSE(StrEndsWith("mq", "fairmq")); + EXPECT_FALSE(StrEndsWith("fairmq", "fair")); +} + +TEST(Strings, ToStrVector) +{ + char prog[] = "prog"; + char a[] = "--a"; + char b[] = "b"; + char* argv[] = {prog, a, b}; + EXPECT_EQ(ToStrVector(3, argv), (vector{"--a", "b"})); + EXPECT_EQ(ToStrVector(3, argv, false), (vector{"prog", "--a", "b"})); +} + +TEST(Unique, UuidsAreUnique) +{ + set uuids; + set hashes; + for (int i = 0; i < 1000; ++i) { + auto const uuid = Uuid(); + EXPECT_EQ(uuid.size(), 36U) << uuid; + uuids.insert(uuid); + hashes.insert(UuidHash()); + } + EXPECT_EQ(uuids.size(), 1000U); + EXPECT_EQ(hashes.size(), 1000U); +} + +TEST(Execute, OutputAndExitCode) +{ + auto const result = execute(R"(sh -c "echo out; echo err 1>&2; exit 3")", "[p] "); + EXPECT_EQ(result.exit_code, 3); + EXPECT_NE(result.console_out.find("[p] out\n"), string::npos) << result.console_out; + EXPECT_NE(result.console_out.find("[p] error: err\n"), string::npos) << result.console_out; +} + +TEST(Execute, Input) +{ + auto const result = execute("head -n 1", "", "from stdin\n"); + EXPECT_EQ(result.exit_code, 0); + EXPECT_NE(result.console_out.find("from stdin"), string::npos) << result.console_out; +} + +TEST(Execute, Signal) +{ + // execute() sends the signal approx. 2 s after the start. + auto const result = execute("sleep 30", "", "", SIGTERM); + EXPECT_EQ(result.exit_code, SIGTERM); +} + +TEST(Execute, Timeout) +{ + auto const start = chrono::steady_clock::now(); + auto const result = execute("sleep 30", "", "", -1, chrono::milliseconds(200)); + EXPECT_LT(chrono::steady_clock::now() - start, chrono::seconds(10)); + EXPECT_EQ(result.exit_code, SIGKILL); + EXPECT_NE(result.console_out.find("timeout after 200 ms"), string::npos) << result.console_out; +} + +} // namespace