From c4145e9ef10887a911dba5c5dff40190c59892cc Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Wed, 12 Sep 2018 17:29:45 +0200 Subject: [PATCH] Add test for FairMQDevice::WaitFor() --- test/CMakeLists.txt | 3 ++ test/helper/devices/TestWaitFor.cxx | 33 +++++++++++++ test/helper/runTestDevice.cxx | 9 +++- test/protocols/_waitfor.cxx | 74 +++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 test/helper/devices/TestWaitFor.cxx create mode 100644 test/protocols/_waitfor.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8b5daa08..1d15ed35 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ add_testhelper(runTestDevice helper/devices/TestReq.cxx helper/devices/TestSub.cxx helper/devices/TestTransferTimeout.cxx + helper/devices/TestWaitFor.cxx LINKS FairMQ ) @@ -51,6 +52,7 @@ add_testsuite(FairMQ.Protocols protocols/_req_rep.cxx protocols/_transfer_timeout.cxx protocols/_push_pull_multipart.cxx + protocols/_waitfor.cxx LINKS FairMQ DEPENDS testhelper_runTestDevice @@ -94,6 +96,7 @@ add_testsuite(FairMQ.Device device/_device_config.cxx LINKS FairMQ + DEPENDS testhelper_runTestDevice INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/device ${CMAKE_CURRENT_BINARY_DIR} TIMEOUT 5 diff --git a/test/helper/devices/TestWaitFor.cxx b/test/helper/devices/TestWaitFor.cxx new file mode 100644 index 00000000..1d6870ef --- /dev/null +++ b/test/helper/devices/TestWaitFor.cxx @@ -0,0 +1,33 @@ +/******************************************************************************** + * Copyright (C) 2015-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 +#include + +#include + +namespace fair +{ +namespace mq +{ +namespace test +{ + +class TestWaitFor : public FairMQDevice +{ + public: + void Run() + { + std::cout << "hello" << std::endl; + WaitFor(std::chrono::seconds(60)); + } +}; + +} // namespace test +} // namespace mq +} // namespace fair diff --git a/test/helper/runTestDevice.cxx b/test/helper/runTestDevice.cxx index ade8b9b6..9be10098 100644 --- a/test/helper/runTestDevice.cxx +++ b/test/helper/runTestDevice.cxx @@ -17,9 +17,12 @@ #include "devices/TestReq.cxx" #include "devices/TestSub.cxx" #include "devices/TestTransferTimeout.cxx" +#include "devices/TestWaitFor.cxx" + +#include + #include #include -#include #include namespace bpo = boost::program_options; @@ -80,6 +83,10 @@ auto getDevice(const FairMQProgOptions& config) -> FairMQDevicePtr { return new PairRight; } + else if (0 == id.find("waitfor_")) + { + return new TestWaitFor; + } else { cerr << "Don't know id '" << id << "'" << endl; diff --git a/test/protocols/_waitfor.cxx b/test/protocols/_waitfor.cxx new file mode 100644 index 00000000..8b241418 --- /dev/null +++ b/test/protocols/_waitfor.cxx @@ -0,0 +1,74 @@ +/******************************************************************************** + * Copyright (C) 2014-2018 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 "runner.h" + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include // std::async, std::future + +namespace +{ + +using namespace std; +using namespace fair::mq::test; + +void RunWaitFor() +{ + std::mutex mtx; + std::condition_variable cv; + + int pid = 0; + int exit_code = 0; + + thread deviceThread([&]() { + stringstream cmd; + cmd << runTestDevice << " --id waitfor_" << " --control static " << " --severity nolog"; + + boost::process::ipstream stdout; + boost::process::child c(cmd.str(), boost::process::std_out > stdout); + string line; + getline(stdout, line); + + { + std::lock_guard lock(mtx); + pid = c.id(); + } + cv.notify_one(); + + c.wait(); + + exit_code = c.exit_code(); + }); + + { + std::unique_lock lock(mtx); + cv.wait(lock, [&pid]{ return pid != 0; }); + } + + kill(pid, SIGINT); + + deviceThread.join(); + + exit(exit_code); +} + +TEST(Device, WaitFor) +{ + EXPECT_EXIT(RunWaitFor(), ::testing::ExitedWithCode(0), ""); +} + +} // namespace