diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 74853ac6..eea33b29 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -134,6 +134,7 @@ add_testsuite(Device device/_version.cxx device/_config.cxx device/_data_callbacks.cxx + device/_runner.cxx device/_waitfor.cxx device/_exceptions.cxx device/_error_state.cxx diff --git a/test/device/_runner.cxx b/test/device/_runner.cxx new file mode 100644 index 00000000..fa18df24 --- /dev/null +++ b/test/device/_runner.cxx @@ -0,0 +1,112 @@ +/******************************************************************************** + * 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 + +namespace +{ + +using namespace std; +using namespace fair::mq::test; + +// The StateAction device with an id that selects no state: it does nothing. +constexpr auto kIdleDevice = "waitfor_None_"; + +TEST(DeviceRunner, PrintOptions) +{ + auto const results = RunDevices({{kIdleDevice, {}, "--print-options"}}, RunOptions("shmem")); + auto const& result = results.at(0); + EXPECT_TRUE(ExitedWith(result, 0, string("id:") + kIdleDevice + ":string:Device ID.")); + EXPECT_TRUE(ExitedWith(result, 0, "transport:shmem:string:")); + EXPECT_TRUE(ExitedWith(result, 0, "control:static:string:")); + // The device does not start. + EXPECT_EQ(result.output.find("INITIALIZING DEVICE"), string::npos) << result.output; +} + +// Known defect: DeviceRunner sets the console severity to "nolog" for +// --print-channels and --version, and then prints the output with LOGV(info). +// Both options print nothing. Enable these tests with the fix. +TEST(DeviceRunner, DISABLED_PrintChannels) +{ + auto const results = RunDevices({{kIdleDevice, {}, "--print-channels"}}); + auto const& result = results.at(0); + EXPECT_TRUE(ExitedWith(result, 0, "data:1:1")); + EXPECT_TRUE(ExitedWith(result, 0, "fan:2:4")); +} + +TEST(DeviceRunner, DISABLED_Version) +{ + auto const results = RunDevices({{kIdleDevice, {}, "--version"}}); + EXPECT_TRUE(ExitedWith(results.at(0), 0, "FairMQ version: ")); + EXPECT_TRUE(ExitedWith(results.at(0), 0, "User device version: ")); +} + +// PrintChannels and Version must not start the device, also while they print nothing. +TEST(DeviceRunner, PrintChannelsAndVersionDoNotStartTheDevice) +{ + for (auto const* option : {"--print-channels", "--version"}) { + auto const results = RunDevices({{kIdleDevice, {}, option}}); + EXPECT_TRUE(ExitedWith(results.at(0), 0)) << option; + EXPECT_EQ(results.at(0).output.find("INITIALIZING DEVICE"), string::npos) << option; + } +} + +struct KeyParam +{ + char key; + string expected; +}; + +auto operator<<(std::ostream& os, KeyParam const& param) -> std::ostream& +{ + return os << "key '" << param.key << "'"; +} + +class InteractiveKey : public testing::TestWithParam +{}; + +// One key, then SIGINT ends the device (see the defect below). +TEST_P(InteractiveKey, PrintsItsAction) +{ + RunOptions options; + options.control = "interactive"; + options.input = string(1, GetParam().key); + options.signal = SIGINT; + auto const results = RunDevices({{kIdleDevice}}, options); + EXPECT_TRUE(ExitedWith(results.at(0), 0, GetParam().expected)); +} + +INSTANTIATE_TEST_SUITE_P(, InteractiveKey, + testing::Values(KeyParam{'h', "--> [h] help"}, + KeyParam{'c', "check current state"}, + KeyParam{'p', "--> [p] print number of connected peers"}, + KeyParam{'k', "--> [k] increase log severity"}, + KeyParam{'z', "Invalid input: [z]"}), + [](testing::TestParamInfo const& p) { return string(1, p.param.key); }); + +// Known defect: the control plugin polls the stdin file descriptor before it +// reads the next key from std::cin. Keys that arrive in one read (e.g. "cq" +// from a pipe) stay in the std::cin buffer, and the plugin never handles them. +// Enable this test with the fix. +TEST(ControlPlugin, DISABLED_SeveralKeysInOneInput) +{ + RunOptions options; + options.control = "interactive"; + options.input = "cq"; + auto const results = RunDevices({{kIdleDevice}}, options); + EXPECT_TRUE(ExitedWith(results.at(0), 0, "check current state")); + EXPECT_TRUE(ExitedWith(results.at(0), 0, "--> [q] end")); +} + +} // namespace diff --git a/test/helper/devices/TestStateAction.h b/test/helper/devices/TestStateAction.h index 0c4d95cc..f6d4ed0c 100644 --- a/test/helper/devices/TestStateAction.h +++ b/test/helper/devices/TestStateAction.h @@ -41,6 +41,13 @@ class StateAction : public Device void ResetTask() override { ActIfRequested("ResetTask"); } void Reset() override { ActIfRequested("Reset"); } + // For the --print-channels test. + void RegisterChannelEndpoints() override + { + RegisterChannelEndpoint("data"); + RegisterChannelEndpoint("fan", 2, 4); + } + private: void ActIfRequested(std::string_view state) {