Files
FairMQ/test/helper/devices/TestStateAction.h
Claude 4e4d21bbd0 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>
2026-11-03 17:00:01 +01:00

80 lines
3.0 KiB
C++

/********************************************************************************
* 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_STATEACTION_H
#define FAIR_MQ_TEST_STATEACTION_H
#include <fairmq/Device.h>
#include <fairlogger/Logger.h>
#include <chrono>
#include <csignal>
#include <stdexcept>
#include <string>
#include <string_view>
namespace fair::mq::test
{
/// Does one action in one state handler. The device id selects both:
/// "<action>_<State>_", e.g. "exceptions_InitTask_". Actions:
/// - exceptions: throw an exception
/// - error_state: change to the Error state
/// - signals: raise SIGINT
/// - waitfor: wait 60 s in WaitFor(); the test interrupts the wait
class StateAction : public Device
{
public:
void Init() override { ActIfRequested("Init"); }
void Bind() override { ActIfRequested("Bind"); }
void Connect() override { ActIfRequested("Connect"); }
void InitTask() override { ActIfRequested("InitTask"); }
void PreRun() override { ActIfRequested("PreRun"); }
void Run() override { ActIfRequested("Run"); }
void PostRun() override { ActIfRequested("PostRun"); }
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)
{
auto const id = GetId();
if (std::string::npos == id.find(std::string("_").append(state).append("_"))) {
return;
}
if (id.rfind("exceptions_", 0) == 0) {
throw std::runtime_error(std::string("exception in ").append(state).append("()"));
} else if (id.rfind("error_state_", 0) == 0) {
LOG(info) << "going to change to Error state from " << state << "()";
ChangeStateOrThrow(Transition::ErrorFound);
} else if (id.rfind("signals_", 0) == 0) {
LOG(info) << "raising SIGINT from " << state << "()";
std::raise(SIGINT);
} else if (id.rfind("waitfor_", 0) == 0) {
LOG(info) << "Going to sleep via WaitFor() in " << state << "...";
bool const done = WaitFor(std::chrono::seconds(60));
LOG(info) << (done ? "Sleeping Done. Not interrupted." : "Sleeping Done. Interrupted.");
} else {
throw std::runtime_error("unknown action in device id '" + id + "'");
}
}
};
} // namespace fair::mq::test
#endif /* FAIR_MQ_TEST_STATEACTION_H */