FairMQ: Add test coverage for PluginServices

This commit is contained in:
Dennis Klein 2017-07-26 00:34:07 +02:00 committed by Mohammad Al-Turany
parent 052ac8487d
commit dfb2bac4bc
5 changed files with 239 additions and 0 deletions

View File

@ -131,6 +131,17 @@ add_testsuite(FairMQ.PluginsPrelinked
TIMEOUT 10 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 # # Aggregate all test targets #
############################## ##############################

View File

@ -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 <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()
: 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 */

View File

@ -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 <fairmq/Tools.h>
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;
}
});
}
} /* namespace */

View File

@ -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 <condition_variable>
#include <mutex>
// #include <thread>
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<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(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 */

View 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();
}