test: parameterize the device state tests by state and control

- Add test::StateParam (state, control mode, stdin input) and
  WithControls() for INSTANTIATE_TEST_SUITE_P()
- Turn the 60 TEST() copies in the Exceptions, ErrorState, Signals and
  WaitFor files into one TEST_P() each
- The test names change, e.g. Exceptions.interactive_InRun is now
  Exceptions.Throw/interactive_Run, Signal_SIGINT.* is Signals.RaiseSIGINT/*
  and ErrorState.OrphanMessages is OrphanMessages.shmem

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-03 14:42:38 +00:00
committed by Dennis Klein
parent 5da5a35b4d
commit 8ff2214f9c
5 changed files with 114 additions and 309 deletions

View File

@@ -7,6 +7,7 @@
********************************************************************************/
#include <helper/Devices.h>
#include <helper/StateParam.h>
#include <fairmq/Device.h>
@@ -61,108 +62,23 @@ class BadDevice : public Device
Parts parts;
};
void RunErrorStateIn(string const& state, string const& control, string const& input = "")
using ErrorState = StateTest;
TEST_P(ErrorState, ChangeState)
{
auto const& state = GetParam().state;
RunOptions options;
options.control = control;
options.input = input;
options.control = GetParam().control;
options.input = GetParam().Input();
auto const results = RunDevices({{"error_state_" + state + "_"}}, options);
EXPECT_TRUE(ExitedWith(results.at(0), 1, "going to change to Error state from " + state + "()"));
}
TEST(ErrorState, static_InInit)
{
RunErrorStateIn("Init", "static");
}
TEST(ErrorState, static_InBind)
{
RunErrorStateIn("Bind", "static");
}
TEST(ErrorState, static_InConnect)
{
RunErrorStateIn("Connect", "static");
}
TEST(ErrorState, static_InInitTask)
{
RunErrorStateIn("InitTask", "static");
}
TEST(ErrorState, static_InPreRun)
{
RunErrorStateIn("PreRun", "static");
}
TEST(ErrorState, static_InRun)
{
RunErrorStateIn("Run", "static");
}
TEST(ErrorState, static_InPostRun)
{
RunErrorStateIn("PostRun", "static");
}
TEST(ErrorState, static_InResetTask)
{
RunErrorStateIn("ResetTask", "static");
}
TEST(ErrorState, static_InReset)
{
RunErrorStateIn("Reset", "static");
}
TEST(ErrorState, interactive_InInit)
{
RunErrorStateIn("Init", "interactive");
}
TEST(ErrorState, interactive_InBind)
{
RunErrorStateIn("Bind", "interactive");
}
TEST(ErrorState, interactive_InConnect)
{
RunErrorStateIn("Connect", "interactive");
}
TEST(ErrorState, interactive_InInitTask)
{
RunErrorStateIn("InitTask", "interactive");
}
TEST(ErrorState, interactive_InPreRun)
{
RunErrorStateIn("PreRun", "interactive");
}
TEST(ErrorState, interactive_InRun)
{
RunErrorStateIn("Run", "interactive");
}
TEST(ErrorState, interactive_InPostRun)
{
RunErrorStateIn("PostRun", "interactive");
}
TEST(ErrorState, interactive_InResetTask)
{
RunErrorStateIn("ResetTask", "interactive", "q");
}
TEST(ErrorState, interactive_InReset)
{
RunErrorStateIn("Reset", "interactive", "q");
}
INSTANTIATE_TEST_SUITE_P(, ErrorState, WithControls(HookStates()), StateParamName());
#ifdef FAIRMQ_DEBUG_MODE
TEST(ErrorState, OrphanMessages)
TEST(OrphanMessages, shmem)
{
BadDevice badDevice;
}

View File

@@ -7,6 +7,7 @@
********************************************************************************/
#include <helper/Devices.h>
#include <helper/StateParam.h>
#include <gtest/gtest.h>
@@ -18,104 +19,19 @@ namespace
using namespace std;
using namespace fair::mq::test;
void RunExceptionIn(string const& state, string const& control, string const& input = "")
using Exceptions = StateTest;
TEST_P(Exceptions, Throw)
{
auto const& state = GetParam().state;
RunOptions options;
options.control = control;
options.input = input;
options.control = GetParam().control;
options.input = GetParam().Input();
auto const results = RunDevices({{"exceptions_" + state + "_"}}, options);
EXPECT_TRUE(ExitedWith(results.at(0), 1, "exception in " + state + "()"));
}
TEST(Exceptions, static_InInit)
{
RunExceptionIn("Init", "static");
}
TEST(Exceptions, static_InBind)
{
RunExceptionIn("Bind", "static");
}
TEST(Exceptions, static_InConnect)
{
RunExceptionIn("Connect", "static");
}
TEST(Exceptions, static_InInitTask)
{
RunExceptionIn("InitTask", "static");
}
TEST(Exceptions, static_InPreRun)
{
RunExceptionIn("PreRun", "static");
}
TEST(Exceptions, static_InRun)
{
RunExceptionIn("Run", "static");
}
TEST(Exceptions, static_InPostRun)
{
RunExceptionIn("PostRun", "static");
}
TEST(Exceptions, static_InResetTask)
{
RunExceptionIn("ResetTask", "static");
}
TEST(Exceptions, static_InReset)
{
RunExceptionIn("Reset", "static");
}
TEST(Exceptions, interactive_InInit)
{
RunExceptionIn("Init", "interactive");
}
TEST(Exceptions, interactive_InBind)
{
RunExceptionIn("Bind", "interactive");
}
TEST(Exceptions, interactive_InConnect)
{
RunExceptionIn("Connect", "interactive");
}
TEST(Exceptions, interactive_InInitTask)
{
RunExceptionIn("InitTask", "interactive");
}
TEST(Exceptions, interactive_InPreRun)
{
RunExceptionIn("PreRun", "interactive");
}
TEST(Exceptions, interactive_InRun)
{
RunExceptionIn("Run", "interactive");
}
TEST(Exceptions, interactive_InPostRun)
{
RunExceptionIn("PostRun", "interactive");
}
TEST(Exceptions, interactive_InResetTask)
{
RunExceptionIn("ResetTask", "interactive", "q");
}
TEST(Exceptions, interactive_InReset)
{
RunExceptionIn("Reset", "interactive", "q");
}
INSTANTIATE_TEST_SUITE_P(, Exceptions, WithControls(HookStates()), StateParamName());
} // namespace

View File

@@ -7,6 +7,7 @@
********************************************************************************/
#include <helper/Devices.h>
#include <helper/StateParam.h>
#include <gtest/gtest.h>
@@ -18,104 +19,19 @@ namespace
using namespace std;
using namespace fair::mq::test;
void RunSignalIn(string const& state, string const& control, string const& input = "")
using Signals = StateTest;
TEST_P(Signals, RaiseSIGINT)
{
auto const& state = GetParam().state;
RunOptions options;
options.control = control;
options.input = input;
options.control = GetParam().control;
options.input = GetParam().Input();
auto const results = RunDevices({{"signals_" + state + "_"}}, options);
EXPECT_TRUE(ExitedWith(results.at(0), 0, "raising SIGINT from " + state + "()"));
}
TEST(Signal_SIGINT, static_InInit)
{
RunSignalIn("Init", "static");
}
TEST(Signal_SIGINT, static_InBind)
{
RunSignalIn("Bind", "static");
}
TEST(Signal_SIGINT, static_InConnect)
{
RunSignalIn("Connect", "static");
}
TEST(Signal_SIGINT, static_InInitTask)
{
RunSignalIn("InitTask", "static");
}
TEST(Signal_SIGINT, static_InPreRun)
{
RunSignalIn("PreRun", "static");
}
TEST(Signal_SIGINT, static_InRun)
{
RunSignalIn("Run", "static");
}
TEST(Signal_SIGINT, static_InPostRun)
{
RunSignalIn("PostRun", "static");
}
TEST(Signal_SIGINT, static_InResetTask)
{
RunSignalIn("ResetTask", "static");
}
TEST(Signal_SIGINT, static_InReset)
{
RunSignalIn("Reset", "static");
}
TEST(Signal_SIGINT, interactive_InInit)
{
RunSignalIn("Init", "interactive");
}
TEST(Signal_SIGINT, interactive_InBind)
{
RunSignalIn("Bind", "interactive");
}
TEST(Signal_SIGINT, interactive_InConnect)
{
RunSignalIn("Connect", "interactive");
}
TEST(Signal_SIGINT, interactive_InInitTask)
{
RunSignalIn("InitTask", "interactive");
}
TEST(Signal_SIGINT, interactive_InPreRun)
{
RunSignalIn("PreRun", "interactive");
}
TEST(Signal_SIGINT, interactive_InRun)
{
RunSignalIn("Run", "interactive");
}
TEST(Signal_SIGINT, interactive_InPostRun)
{
RunSignalIn("PostRun", "interactive");
}
TEST(Signal_SIGINT, interactive_InResetTask)
{
RunSignalIn("ResetTask", "interactive", "q");
}
TEST(Signal_SIGINT, interactive_InReset)
{
RunSignalIn("Reset", "interactive", "q");
}
INSTANTIATE_TEST_SUITE_P(, Signals, WithControls(HookStates()), StateParamName());
} // namespace

View File

@@ -7,6 +7,7 @@
********************************************************************************/
#include <helper/Devices.h>
#include <helper/StateParam.h>
#include <gtest/gtest.h>
@@ -19,45 +20,20 @@ namespace
using namespace std;
using namespace fair::mq::test;
void RunWaitFor(string const& state, string const& control)
using WaitFor = StateTest;
TEST_P(WaitFor, Interrupt)
{
RunOptions options;
options.control = control;
options.control = GetParam().control;
options.signal = SIGINT;
options.extraArgs = "--severity debug";
auto const results = RunDevices({{"waitfor_" + state + "_"}}, options);
auto const results = RunDevices({{"waitfor_" + GetParam().state + "_"}}, options);
EXPECT_TRUE(ExitedWith(results.at(0), 0, "Sleeping Done. Interrupted."));
}
TEST(WaitFor, static_InPreRun)
{
RunWaitFor("PreRun", "static");
}
TEST(WaitFor, static_InRun)
{
RunWaitFor("Run", "static");
}
TEST(WaitFor, static_InPostRun)
{
RunWaitFor("PostRun", "static");
}
TEST(WaitFor, interactive_InPreRun)
{
RunWaitFor("PreRun", "interactive");
}
TEST(WaitFor, interactive_InRun)
{
RunWaitFor("Run", "interactive");
}
TEST(WaitFor, interactive_InPostRun)
{
RunWaitFor("PostRun", "interactive");
}
// The device waits (and SIGINT interrupts the wait) only in these states.
INSTANTIATE_TEST_SUITE_P(, WaitFor, WithControls({"PreRun", "Run", "PostRun"}), StateParamName());
} // namespace

81
test/helper/StateParam.h Normal file
View File

@@ -0,0 +1,81 @@
/********************************************************************************
* 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" *
********************************************************************************/
#ifndef FAIR_MQ_TEST_STATEPARAM_H
#define FAIR_MQ_TEST_STATEPARAM_H
#include <gtest/gtest.h>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
namespace fair::mq::test
{
/// The parameter of a device test that runs an action (see StateAction) in
/// one state, with one control mode. The test names get the suffix Name(),
/// e.g. "Exceptions.Throw/interactive_InitTask".
struct StateParam
{
StateParam(std::string stateName, std::string controlName)
: state(std::move(stateName))
, control(std::move(controlName))
{}
std::string state;
std::string control;
auto Name() const -> std::string { return control + "_" + state; }
/// The stdin input for the device. With interactive control, a device
/// that fails in ResetTask or Reset waits for "q" to quit.
auto Input() const -> std::string
{
return (control == "interactive" && (state == "ResetTask" || state == "Reset")) ? "q" : "";
}
};
inline auto operator<<(std::ostream& os, StateParam const& param) -> std::ostream&
{
return os << param.Name();
}
/// The device states with a user hook.
inline auto HookStates() -> std::vector<std::string>
{
return {"Init", "Bind", "Connect", "InitTask", "PreRun", "Run", "PostRun", "ResetTask", "Reset"};
}
/// Each state with the static and with the interactive control.
inline auto WithControls(std::vector<std::string> const& states)
{
std::vector<StateParam> params;
for (auto const& control : {"static", "interactive"}) {
for (auto const& state : states) {
params.emplace_back(state, control);
}
}
return testing::ValuesIn(params);
}
/// Name generator for INSTANTIATE_TEST_SUITE_P().
struct StateParamName
{
auto operator()(testing::TestParamInfo<StateParam> const& info) const -> std::string
{
return info.param.Name();
}
};
using StateTest = testing::TestWithParam<StateParam>;
} // namespace fair::mq::test
#endif /* FAIR_MQ_TEST_STATEPARAM_H */