From 84de22f80b922a133431b9c2a1bdc9179d6b6004 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Tue, 28 Feb 2023 15:12:40 +0100 Subject: [PATCH] test: Consolidate some device control logic --- test/device/_config.cxx | 62 +++++------------------------ test/device/_multiple_devices.cxx | 29 ++------------ test/helper/ControlDevice.h | 66 +++++++++++++++++++++++++++++++ test/plugin_services/Fixture.h | 32 +++++---------- test/plugins/_plugin.cxx | 26 +++++------- test/plugins/_plugin_manager.cxx | 23 ++++------- 6 files changed, 106 insertions(+), 132 deletions(-) create mode 100644 test/helper/ControlDevice.h diff --git a/test/device/_config.cxx b/test/device/_config.cxx index a56c1546..6be5f7af 100644 --- a/test/device/_config.cxx +++ b/test/device/_config.cxx @@ -1,11 +1,13 @@ /******************************************************************************** - * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2023 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/ControlDevice.h" + #include #include #include @@ -21,51 +23,14 @@ namespace _config using namespace std; using namespace fair::mq; -void control(Device& device) -{ - device.ChangeState(Transition::InitDevice); - device.WaitForState(State::InitializingDevice); - device.ChangeState(Transition::CompleteInit); - device.WaitForState(State::Initialized); - device.ChangeState(Transition::Bind); - device.WaitForState(State::Bound); - device.ChangeState(Transition::Connect); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::InitTask); - device.WaitForState(State::Ready); - - device.ChangeState(Transition::Run); - device.WaitForState(State::Ready); - - device.ChangeState(Transition::ResetTask); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::ResetDevice); - device.WaitForState(State::Idle); - - device.ChangeState(Transition::End); -} - class TestDevice : public Device { public: TestDevice(const string& transport) + : fDeviceThread(&Device::RunStateMachine, this) { - fDeviceThread = thread(&Device::RunStateMachine, this); - SetTransport(transport); - - ChangeState(Transition::InitDevice); - WaitForState(State::InitializingDevice); - ChangeState(Transition::CompleteInit); - WaitForState(State::Initialized); - ChangeState(Transition::Bind); - WaitForState(State::Bound); - ChangeState(Transition::Connect); - WaitForState(State::DeviceReady); - ChangeState(Transition::InitTask); - WaitForState(State::Ready); - - ChangeState(Transition::Run); + test::Control(*this, test::Cycle::ToRun); } TestDevice(const TestDevice&) = delete; @@ -75,16 +40,7 @@ class TestDevice : public Device ~TestDevice() override { - WaitForState(State::Running); - ChangeState(Transition::Stop); - WaitForState(State::Ready); - ChangeState(Transition::ResetTask); - WaitForState(State::DeviceReady); - ChangeState(Transition::ResetDevice); - WaitForState(State::Idle); - - ChangeState(Transition::End); - + test::Control(*this, test::Cycle::ReadyToEnd); if (fDeviceThread.joinable()) { fDeviceThread.join(); } @@ -118,7 +74,7 @@ class Config : public ::testing::Test channel.UpdateAddress("tcp://localhost:5558"); device.AddChannel("data", std::move(channel)); - thread t(control, ref(device)); + thread t([&]() { test::Control(device); }); device.RunStateMachine(); @@ -156,7 +112,7 @@ class Config : public ::testing::Test channel.UpdateAddress("tcp://localhost:5558"); device.AddChannel("data", std::move(channel)); - thread t(control, ref(device)); + thread t([&]() { test::Control(device); }); device.RunStateMachine(); @@ -189,7 +145,7 @@ class Config : public ::testing::Test thread t(&Device::RunStateMachine, &device); - control(device); + test::Control(device); if (t.joinable()) { t.join(); diff --git a/test/device/_multiple_devices.cxx b/test/device/_multiple_devices.cxx index c771f1a4..45247801 100644 --- a/test/device/_multiple_devices.cxx +++ b/test/device/_multiple_devices.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -9,6 +9,8 @@ #include "TestSender.h" #include "TestReceiver.h" +#include "../helper/ControlDevice.h" + #include #include @@ -23,28 +25,7 @@ using namespace fair::mq; void control(Device& device) { - thread t([&] { - device.ChangeState(Transition::InitDevice); - device.WaitForState(State::InitializingDevice); - device.ChangeState(Transition::CompleteInit); - device.WaitForState(State::Initialized); - device.ChangeState(Transition::Bind); - device.WaitForState(State::Bound); - device.ChangeState(Transition::Connect); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::InitTask); - device.WaitForState(State::Ready); - - device.ChangeState(Transition::Run); - device.WaitForState(State::Ready); - - device.ChangeState(Transition::ResetTask); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::ResetDevice); - device.WaitForState(State::Idle); - - device.ChangeState(Transition::End); - }); + thread t([&]() { test::Control(device); }); device.RunStateMachine(); @@ -55,8 +36,6 @@ void control(Device& device) class MultipleDevices : public ::testing::Test { public: - MultipleDevices() {} - bool TestFirst() { test::Sender sender("data"); diff --git a/test/helper/ControlDevice.h b/test/helper/ControlDevice.h new file mode 100644 index 00000000..250a94c6 --- /dev/null +++ b/test/helper/ControlDevice.h @@ -0,0 +1,66 @@ +/******************************************************************************** + * Copyright (C) 2023 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_CONTROL_H +#define FAIR_MQ_TEST_CONTROL_H + +#include + +namespace fair::mq::test +{ + +enum class Cycle { + FullNoStop, + ToDeviceReadyAndBack, + ToRun, + ReadyToEnd +}; + +inline auto Control(Device& device, Cycle cycle = Cycle::FullNoStop) +{ + if( cycle == Cycle::FullNoStop + || cycle == Cycle::ToDeviceReadyAndBack + || cycle == Cycle::ToRun) + { + device.ChangeStateOrThrow(Transition::InitDevice); + device.WaitForState(State::InitializingDevice); + device.ChangeStateOrThrow(Transition::CompleteInit); + device.WaitForState(State::Initialized); + device.ChangeStateOrThrow(Transition::Bind); + device.WaitForState(State::Bound); + device.ChangeStateOrThrow(Transition::Connect); + device.WaitForState(State::DeviceReady); + } + if( cycle == Cycle::FullNoStop + || cycle == Cycle::ToRun) + { + device.ChangeStateOrThrow(Transition::InitTask); + device.WaitForState(State::Ready); + device.ChangeStateOrThrow(Transition::Run); + device.WaitForState(State::Running); + } + if( cycle == Cycle::FullNoStop + || cycle == Cycle::ReadyToEnd) + { + device.WaitForState(State::Ready); + device.ChangeStateOrThrow(Transition::ResetTask); + device.WaitForState(State::DeviceReady); + } + if( cycle == Cycle::FullNoStop + || cycle == Cycle::ToDeviceReadyAndBack + || cycle == Cycle::ReadyToEnd) + { + device.ChangeStateOrThrow(Transition::ResetDevice); + device.WaitForState(State::Idle); + device.ChangeStateOrThrow(Transition::End); + } +} + +} + +#endif // FAIR_MQ_TEST_CONTROL_H diff --git a/test/plugin_services/Fixture.h b/test/plugin_services/Fixture.h index f994cadd..9aad35e9 100644 --- a/test/plugin_services/Fixture.h +++ b/test/plugin_services/Fixture.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -9,6 +9,8 @@ #ifndef FAIR_MQ_TEST_FIXTURE #define FAIR_MQ_TEST_FIXTURE +#include "../helper/ControlDevice.h" + #include #include #include @@ -21,22 +23,6 @@ namespace fair::mq::test { -inline auto control(fair::mq::Device& device) -> void -{ - device.ChangeState(fair::mq::Transition::InitDevice); - device.WaitForState(fair::mq::State::InitializingDevice); - device.ChangeState(fair::mq::Transition::CompleteInit); - device.WaitForState(fair::mq::State::Initialized); - device.ChangeState(fair::mq::Transition::Bind); - device.WaitForState(fair::mq::State::Bound); - device.ChangeState(fair::mq::Transition::Connect); - device.WaitForState(fair::mq::State::DeviceReady); - device.ChangeState(fair::mq::Transition::ResetDevice); - device.WaitForState(fair::mq::State::Idle); - - device.ChangeState(fair::mq::Transition::End); -} - struct PluginServices : ::testing::Test { PluginServices() : mConfig() @@ -48,17 +34,19 @@ struct PluginServices : ::testing::Test { mDevice.SetTransport("zeromq"); } - ~PluginServices() + ~PluginServices() override { - if (mDevice.GetCurrentState() == fair::mq::State::Idle) control(mDevice); + if (mDevice.GetCurrentState() == State::Idle) { + Control(mDevice, Cycle::ToDeviceReadyAndBack); + } if (fRunStateMachineThread.joinable()) { fRunStateMachineThread.join(); } } - fair::mq::ProgOptions mConfig; - fair::mq::Device mDevice; - fair::mq::PluginServices mServices; + ProgOptions mConfig; + Device mDevice; + mq::PluginServices mServices; std::thread fRunStateMachineThread; }; diff --git a/test/plugins/_plugin.cxx b/test/plugins/_plugin.cxx index 2218305e..3dd712d9 100644 --- a/test/plugins/_plugin.cxx +++ b/test/plugins/_plugin.cxx @@ -1,17 +1,21 @@ /******************************************************************************** - * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2023 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 "../helper/ControlDevice.h" + #include #include #include #include #include + +#include + #include #include #include @@ -27,19 +31,7 @@ using namespace fair::mq; auto control(Device& device) -> void { device.SetTransport("zeromq"); - - device.ChangeState(Transition::InitDevice); - device.WaitForState(State::InitializingDevice); - device.ChangeState(Transition::CompleteInit); - device.WaitForState(State::Initialized); - device.ChangeState(Transition::Bind); - device.WaitForState(State::Bound); - device.ChangeState(Transition::Connect); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::ResetDevice); - device.WaitForState(State::Idle); - - device.ChangeState(Transition::End); + test::Control(device, test::Cycle::ToDeviceReadyAndBack); } TEST(Plugin, Operators) @@ -52,7 +44,7 @@ TEST(Plugin, Operators) Plugin p3{"file", {1, 0, 0}, "Foo Bar ", "https://git.test.net/file.git", &services}; EXPECT_EQ(p1, p2); EXPECT_NE(p1, p3); - thread t(control, std::ref(device)); + thread t([&]() { control(device); }); device.RunStateMachine(); if (t.joinable()) { t.join(); @@ -68,7 +60,7 @@ TEST(Plugin, OstreamOperators) stringstream ss; ss << p1; EXPECT_EQ(ss.str(), string{"'dds', version '1.0.0', maintainer 'Foo Bar ', homepage 'https://git.test.net/dds.git'"}); - thread t(control, std::ref(device)); + thread t([&]() { control(device); }); device.RunStateMachine(); if (t.joinable()) { t.join(); diff --git a/test/plugins/_plugin_manager.cxx b/test/plugins/_plugin_manager.cxx index 0916e6ca..b371bc41 100644 --- a/test/plugins/_plugin_manager.cxx +++ b/test/plugins/_plugin_manager.cxx @@ -1,17 +1,22 @@ /******************************************************************************** - * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2023 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 "../helper/ControlDevice.h" + #include #include #include #include + +#include + #include + #include #include #include @@ -28,19 +33,7 @@ using namespace std; auto control(Device& device) -> void { device.SetTransport("zeromq"); - - device.ChangeState(Transition::InitDevice); - device.WaitForState(State::InitializingDevice); - device.ChangeState(Transition::CompleteInit); - device.WaitForState(State::Initialized); - device.ChangeState(Transition::Bind); - device.WaitForState(State::Bound); - device.ChangeState(Transition::Connect); - device.WaitForState(State::DeviceReady); - device.ChangeState(Transition::ResetDevice); - device.WaitForState(State::Idle); - - device.ChangeState(Transition::End); + test::Control(device, test::Cycle::ToDeviceReadyAndBack); } TEST(PluginManager, LoadPluginDynamic)