diff --git a/fairmq/test/CMakeLists.txt b/fairmq/test/CMakeLists.txt index b5f955f6..5a112813 100644 --- a/fairmq/test/CMakeLists.txt +++ b/fairmq/test/CMakeLists.txt @@ -131,6 +131,17 @@ add_testsuite(FairMQ.PluginsPrelinked TIMEOUT 10 ) +add_testsuite(FairMQ.PluginServices + SOURCES + plugin_services/runner.cxx + plugin_services/_config.cxx + plugin_services/_control.cxx + plugin_services/Fixture.h + + LINKS FairMQ + TIMEOUT 10 +) + ############################## # Aggregate all test targets # ############################## diff --git a/fairmq/test/plugin_services/Fixture.h b/fairmq/test/plugin_services/Fixture.h new file mode 100644 index 00000000..e2dc9e21 --- /dev/null +++ b/fairmq/test/plugin_services/Fixture.h @@ -0,0 +1,59 @@ +/******************************************************************************** + * Copyright (C) 2017 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_FIXTURE +#define FAIR_MQ_TEST_FIXTURE + +#include +#include +#include +#include +#include + +namespace fair +{ +namespace mq +{ +namespace test +{ + +inline auto control(std::shared_ptr device) -> void +{ + for (const auto event : { + FairMQDevice::INIT_DEVICE, + FairMQDevice::RESET_DEVICE, + FairMQDevice::END, + }) { + device->ChangeState(event); + if (event != FairMQDevice::END) device->WaitForEndOfState(event); + } +} + +struct PluginServices : ::testing::Test { + PluginServices() + : mDevice{std::make_shared()} + , mServices{&mConfig, mDevice} + { + mDevice->SetTransport("zeromq"); + } + + ~PluginServices() + { + if(mDevice->GetCurrentState() == FairMQDevice::IDLE) control(mDevice); + } + + FairMQProgOptions mConfig; + std::shared_ptr mDevice; + fair::mq::PluginServices mServices; +}; + +} /* namespace test */ +} /* namespace mq */ +} /* namespace fair */ + +#endif /* FAIR_MQ_TEST_FIXTURE */ diff --git a/fairmq/test/plugin_services/_config.cxx b/fairmq/test/plugin_services/_config.cxx new file mode 100644 index 00000000..5f976523 --- /dev/null +++ b/fairmq/test/plugin_services/_config.cxx @@ -0,0 +1,50 @@ +/******************************************************************************** + * Copyright (C) 2017 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 "Fixture.h" +#include + +namespace +{ + +using namespace std; +using fair::mq::test::PluginServices; +using DeviceState = fair::mq::PluginServices::DeviceState; +using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition; + +TEST_F(PluginServices, ConfigSynchronous) +{ + mServices.SubscribeToDeviceStateChange("test",[&](DeviceState newState){ + switch (newState) { + case DeviceState::InitializingDevice: + mServices.SetProperty("blubb", 42); + break; + case DeviceState::DeviceReady: + EXPECT_EQ(mServices.GetProperty("blubb"), 42); + EXPECT_EQ(mServices.GetPropertyAsString("blubb"), fair::mq::tools::ToString(42)); + break; + default: + break; + } + }); +} + +TEST_F(PluginServices, ConfigInvalidStateError) +{ + mServices.SubscribeToDeviceStateChange("test",[&](DeviceState newState){ + switch (newState) { + case DeviceState::DeviceReady: + ASSERT_THROW(mServices.SetProperty("blubb", 42), fair::mq::PluginServices::InvalidStateError); + break; + default: + break; + } + }); +} + +} /* namespace */ diff --git a/fairmq/test/plugin_services/_control.cxx b/fairmq/test/plugin_services/_control.cxx new file mode 100644 index 00000000..a4373b7d --- /dev/null +++ b/fairmq/test/plugin_services/_control.cxx @@ -0,0 +1,103 @@ +/******************************************************************************** + * Copyright (C) 2017 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 "Fixture.h" +#include +#include +// #include + +namespace +{ + +using namespace std; +using fair::mq::test::PluginServices; +using DeviceState = fair::mq::PluginServices::DeviceState; +using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition; + +TEST_F(PluginServices, Control) +{ + ASSERT_EQ(mServices.GetCurrentDeviceState(), DeviceState::Idle); + ASSERT_NO_THROW(mServices.ChangeDeviceState(DeviceStateTransition::InitDevice)); + + DeviceState nextState; + condition_variable cv; + mutex cv_m; + mServices.SubscribeToDeviceStateChange("test", [&](DeviceState newState){ + ASSERT_NE(newState, DeviceState::ResettingDevice); // check UnsubscribeFromDeviceStateChange + + lock_guard lock{cv_m}; + nextState = newState; + if (newState == DeviceState::DeviceReady) + { + cv.notify_one(); + mServices.UnsubscribeFromDeviceStateChange("test"); + } + }); + { + unique_lock lock{cv_m}; + cv.wait(lock); + } + + ASSERT_EQ(mServices.GetCurrentDeviceState(), DeviceState::DeviceReady); + + mServices.ChangeDeviceState(DeviceStateTransition::ResetDevice); + mDevice->WaitForEndOfState(FairMQDevice::RESET_DEVICE); + mServices.ChangeDeviceState(DeviceStateTransition::End); +} + +TEST_F(PluginServices, ControlStateConversions) +{ + EXPECT_NO_THROW(mServices.ToDeviceState("OK")); + EXPECT_NO_THROW(mServices.ToDeviceState("ERROR")); + EXPECT_NO_THROW(mServices.ToDeviceState("IDLE")); + EXPECT_NO_THROW(mServices.ToDeviceState("INITIALIZING DEVICE")); + EXPECT_NO_THROW(mServices.ToDeviceState("DEVICE READY")); + EXPECT_NO_THROW(mServices.ToDeviceState("INITIALIZING TASK")); + EXPECT_NO_THROW(mServices.ToDeviceState("READY")); + EXPECT_NO_THROW(mServices.ToDeviceState("RUNNING")); + EXPECT_NO_THROW(mServices.ToDeviceState("PAUSED")); + EXPECT_NO_THROW(mServices.ToDeviceState("RESETTING TASK")); + EXPECT_NO_THROW(mServices.ToDeviceState("RESETTING DEVICE")); + EXPECT_NO_THROW(mServices.ToDeviceState("EXITING")); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Ok)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Error)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Idle)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::InitializingDevice)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::DeviceReady)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::InitializingTask)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Ready)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Running)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Paused)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::ResettingTask)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::ResettingDevice)); + EXPECT_NO_THROW(mServices.ToStr(DeviceState::Exiting)); +} + +TEST_F(PluginServices, ControlStateTransitionConversions) +{ + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("INIT DEVICE")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("INIT TASK")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("RUN")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("PAUSE")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("STOP")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("RESET TASK")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("RESET DEVICE")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("END")); + EXPECT_NO_THROW(mServices.ToDeviceStateTransition("ERROR FOUND")); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::InitDevice)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::InitTask)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::Run)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::Pause)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::Stop)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::ResetTask)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::ResetDevice)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::End)); + EXPECT_NO_THROW(mServices.ToStr(DeviceStateTransition::ErrorFound)); +} + +} /* namespace */ diff --git a/fairmq/test/plugin_services/runner.cxx b/fairmq/test/plugin_services/runner.cxx new file mode 100644 index 00000000..2357bcc3 --- /dev/null +++ b/fairmq/test/plugin_services/runner.cxx @@ -0,0 +1,16 @@ +/******************************************************************************** + * Copyright (C) 2017 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 + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + ::testing::FLAGS_gtest_death_test_style = "threadsafe"; + return RUN_ALL_TESTS(); +}