test(device): add tests for the runner options and interactive keys

- --print-options prints the computed options and does not start the
  device
- --print-channels and --version do not start the device
- Interactive control: h, c, p, k and an invalid key print their action
- Register two channel endpoints in the StateAction device
- Add disabled tests for known defects: --print-channels and --version
  print nothing (console severity "nolog"); keys that arrive in one read
  stay in the std::cin buffer and are never handled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-03 15:02:12 +00:00
committed by Dennis Klein
parent 89cbc968d8
commit 4e4d21bbd0
3 changed files with 120 additions and 0 deletions

View File

@@ -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

112
test/device/_runner.cxx Normal file
View File

@@ -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 <helper/Devices.h>
#include <gtest/gtest.h>
#include <csignal>
#include <ostream>
#include <string>
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<KeyParam>
{};
// 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<KeyParam> 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

View File

@@ -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)
{