mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 17:41:45 +00:00
Move test directory one up
This commit is contained in:
60
test/plugin_services/Fixture.h
Normal file
60
test/plugin_services/Fixture.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/********************************************************************************
|
||||
* 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 <gtest/gtest.h>
|
||||
#include <fairmq/PluginServices.h>
|
||||
#include <FairMQDevice.h>
|
||||
#include <options/FairMQProgOptions.h>
|
||||
#include <memory>
|
||||
|
||||
namespace fair
|
||||
{
|
||||
namespace mq
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
|
||||
inline auto control(std::shared_ptr<FairMQDevice> 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()
|
||||
: mConfig()
|
||||
, mDevice{std::make_shared<FairMQDevice>()}
|
||||
, mServices{&mConfig, mDevice}
|
||||
{
|
||||
mDevice->SetTransport("zeromq");
|
||||
}
|
||||
|
||||
~PluginServices()
|
||||
{
|
||||
if(mDevice->GetCurrentState() == FairMQDevice::IDLE) control(mDevice);
|
||||
}
|
||||
|
||||
FairMQProgOptions mConfig;
|
||||
std::shared_ptr<FairMQDevice> mDevice;
|
||||
fair::mq::PluginServices mServices;
|
||||
};
|
||||
|
||||
} /* namespace test */
|
||||
} /* namespace mq */
|
||||
} /* namespace fair */
|
||||
|
||||
#endif /* FAIR_MQ_TEST_FIXTURE */
|
91
test/plugin_services/_config.cxx
Normal file
91
test/plugin_services/_config.cxx
Normal file
@@ -0,0 +1,91 @@
|
||||
/********************************************************************************
|
||||
* 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 <fairmq/Tools.h>
|
||||
#include <algorithm>
|
||||
|
||||
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<int>("blubb", 42);
|
||||
break;
|
||||
case DeviceState::DeviceReady:
|
||||
EXPECT_EQ(mServices.GetProperty<int>("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<int>("blubb", 42), fair::mq::PluginServices::InvalidStateError);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(PluginServices, KeyDiscovery)
|
||||
{
|
||||
mConfig.SetValue("foo", 0);
|
||||
|
||||
auto keys(mServices.GetPropertyKeys());
|
||||
|
||||
EXPECT_TRUE(find(keys.begin(), keys.end(), "foo") != keys.end());
|
||||
}
|
||||
|
||||
TEST_F(PluginServices, ConfigCallbacks)
|
||||
{
|
||||
mServices.SubscribeToPropertyChange<string>("test", [](const string& key, string value) {
|
||||
if (key == "chans.data.0.address") { ASSERT_EQ(value, "tcp://localhost:4321"); }
|
||||
});
|
||||
|
||||
mServices.SubscribeToPropertyChange<int>("test", [](const string& key, int value) {
|
||||
if(key == "chans.data.0.rcvBufSize") {
|
||||
FAIL(); // should not be called because we unsubscribed
|
||||
}
|
||||
});
|
||||
|
||||
mServices.SubscribeToPropertyChange<double>("test", [](const string& key, double value) {
|
||||
if (key == "data-rate") { ASSERT_EQ(value, 0.9); }
|
||||
});
|
||||
|
||||
mServices.SubscribeToDeviceStateChange("test",[&](DeviceState newState){
|
||||
switch (newState) {
|
||||
case DeviceState::InitializingDevice:
|
||||
mServices.SetProperty<string>("chans.data.0.address", "tcp://localhost:4321");
|
||||
mServices.SetProperty<int>("chans.data.0.rcvBufSize", 100);
|
||||
mServices.SetProperty<double>("data-rate", 0.9);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
mServices.UnsubscribeFromPropertyChange<int>("test");
|
||||
}
|
||||
|
||||
} /* namespace */
|
129
test/plugin_services/_control.cxx
Normal file
129
test/plugin_services/_control.cxx
Normal file
@@ -0,0 +1,129 @@
|
||||
/********************************************************************************
|
||||
* 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 <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using namespace std;
|
||||
using fair::mq::test::PluginServices;
|
||||
using DeviceState = fair::mq::PluginServices::DeviceState;
|
||||
using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;
|
||||
|
||||
TEST_F(PluginServices, OnlySingleController)
|
||||
{
|
||||
ASSERT_NO_THROW(mServices.TakeDeviceControl("foo"));
|
||||
ASSERT_NO_THROW(mServices.TakeDeviceControl("foo")); // noop
|
||||
ASSERT_THROW( // no control for bar
|
||||
mServices.ChangeDeviceState("bar", DeviceStateTransition::InitDevice),
|
||||
fair::mq::PluginServices::DeviceControlError
|
||||
);
|
||||
ASSERT_THROW( // no control for bar
|
||||
mServices.ReleaseDeviceControl("bar"),
|
||||
fair::mq::PluginServices::DeviceControlError
|
||||
);
|
||||
|
||||
ASSERT_NO_THROW(mServices.ReleaseDeviceControl("foo"));
|
||||
ASSERT_FALSE(mServices.GetDeviceController());
|
||||
// take control implicitely
|
||||
ASSERT_NO_THROW(mServices.TakeDeviceControl("foo"));
|
||||
ASSERT_NO_THROW(mServices.ChangeDeviceState("foo", DeviceStateTransition::InitDevice));
|
||||
EXPECT_EQ(mServices.GetDeviceController(), string{"foo"});
|
||||
|
||||
// park device
|
||||
mDevice->WaitForEndOfState(FairMQDevice::DEVICE_READY);
|
||||
mServices.ChangeDeviceState("foo", DeviceStateTransition::ResetDevice);
|
||||
mDevice->WaitForEndOfState(FairMQDevice::RESET_DEVICE);
|
||||
mServices.ChangeDeviceState("foo", DeviceStateTransition::End);
|
||||
}
|
||||
|
||||
TEST_F(PluginServices, Control)
|
||||
{
|
||||
ASSERT_EQ(mServices.GetCurrentDeviceState(), DeviceState::Idle);
|
||||
ASSERT_NO_THROW(mServices.TakeDeviceControl("foo"));
|
||||
ASSERT_NO_THROW(mServices.ChangeDeviceState("foo", DeviceStateTransition::InitDevice));
|
||||
|
||||
DeviceState nextState;
|
||||
condition_variable cv;
|
||||
mutex cv_m;
|
||||
mServices.SubscribeToDeviceStateChange("test", [&](DeviceState newState){
|
||||
ASSERT_NE(newState, DeviceState::ResettingDevice); // check UnsubscribeFromDeviceStateChange
|
||||
|
||||
lock_guard<mutex> lock{cv_m};
|
||||
nextState = newState;
|
||||
if (newState == DeviceState::DeviceReady)
|
||||
{
|
||||
cv.notify_one();
|
||||
mServices.UnsubscribeFromDeviceStateChange("test");
|
||||
}
|
||||
});
|
||||
|
||||
unique_lock<mutex> lock{cv_m};
|
||||
cv.wait(lock);
|
||||
|
||||
ASSERT_EQ(mServices.GetCurrentDeviceState(), DeviceState::DeviceReady);
|
||||
|
||||
mServices.ChangeDeviceState("foo", DeviceStateTransition::ResetDevice);
|
||||
mDevice->WaitForEndOfState(FairMQDevice::RESET_DEVICE);
|
||||
mServices.ChangeDeviceState("foo", 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 */
|
16
test/plugin_services/runner.cxx
Normal file
16
test/plugin_services/runner.cxx
Normal file
@@ -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 <gtest/gtest.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
::testing::FLAGS_gtest_death_test_style = "threadsafe";
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
Reference in New Issue
Block a user