From 99ffb732f404b1bb043bb6643563d948c18522f8 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Tue, 19 Mar 2019 16:34:51 +0100 Subject: [PATCH] Use process tools for WaitFor test --- fairmq/plugins/Control.cxx | 2 +- test/device/_waitfor.cxx | 77 ++++++++++++++-------------- test/helper/devices/TestErrorState.h | 2 + test/helper/devices/TestExceptions.h | 2 + test/helper/devices/TestWaitFor.h | 30 +++++++++-- 5 files changed, 70 insertions(+), 43 deletions(-) diff --git a/fairmq/plugins/Control.cxx b/fairmq/plugins/Control.cxx index c313668f..f03dcd24 100644 --- a/fairmq/plugins/Control.cxx +++ b/fairmq/plugins/Control.cxx @@ -189,7 +189,7 @@ try { bool keepRunning = true; while (keepRunning) { - if (poll(cinfd, 1, 500)) { + if (poll(cinfd, 1, 100)) { if (fDeviceShutdownRequested) { break; } diff --git a/test/device/_waitfor.cxx b/test/device/_waitfor.cxx index 8b241418..c2884ee6 100644 --- a/test/device/_waitfor.cxx +++ b/test/device/_waitfor.cxx @@ -9,66 +9,65 @@ #include "runner.h" #include -#include +#include -#include -#include +#include // kill #include #include -#include #include -#include // std::async, std::future namespace { using namespace std; using namespace fair::mq::test; +using namespace fair::mq::tools; -void RunWaitFor() +void RunWaitFor(const string& state, const string& control) { - std::mutex mtx; - std::condition_variable cv; - - int pid = 0; - int exit_code = 0; - - thread deviceThread([&]() { + execute_result result; + thread device_thread([&] { 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(); + cmd << runTestDevice + << " --id waitfor_" << state + << " --control " << control + << " --session " << UuidHash() + << " --severity debug" + << " --color false"; + result = execute(cmd.str(), "[WaitFor]", "", SIGINT); }); - { - std::unique_lock lock(mtx); - cv.wait(lock, [&pid]{ return pid != 0; }); - } + device_thread.join(); - kill(pid, SIGINT); + ASSERT_NE(string::npos, result.console_out.find("Sleeping Done. Interrupted.")); - deviceThread.join(); - - exit(exit_code); + exit(result.exit_code); } -TEST(Device, WaitFor) +TEST(WaitFor, static_InPreRun) { - EXPECT_EXIT(RunWaitFor(), ::testing::ExitedWithCode(0), ""); + EXPECT_EXIT(RunWaitFor("PreRun", "static"), ::testing::ExitedWithCode(0), ""); +} +TEST(WaitFor, static_InRun) +{ + EXPECT_EXIT(RunWaitFor("Run", "static"), ::testing::ExitedWithCode(0), ""); +} +TEST(WaitFor, static_InPostRun) +{ + EXPECT_EXIT(RunWaitFor("PostRun", "static"), ::testing::ExitedWithCode(0), ""); +} +TEST(WaitFor, interactive_InPreRun) +{ + EXPECT_EXIT(RunWaitFor("PreRun", "interactive"), ::testing::ExitedWithCode(0), ""); +} +TEST(WaitFor, interactive_InRun) +{ + EXPECT_EXIT(RunWaitFor("Run", "interactive"), ::testing::ExitedWithCode(0), ""); +} +TEST(WaitFor, interactive_InPostRun) +{ + EXPECT_EXIT(RunWaitFor("PostRun", "interactive"), ::testing::ExitedWithCode(0), ""); } } // namespace diff --git a/test/helper/devices/TestErrorState.h b/test/helper/devices/TestErrorState.h index 600bd9cc..42b89dea 100644 --- a/test/helper/devices/TestErrorState.h +++ b/test/helper/devices/TestErrorState.h @@ -32,6 +32,7 @@ class ErrorState : public FairMQDevice ChangeState(fair::mq::Transition::ErrorFound); } } + void Bind() override { std::string state("Bind"); @@ -40,6 +41,7 @@ class ErrorState : public FairMQDevice ChangeState(fair::mq::Transition::ErrorFound); } } + void Connect() override { std::string state("Connect"); diff --git a/test/helper/devices/TestExceptions.h b/test/helper/devices/TestExceptions.h index 6661bb9b..d0a2abf4 100644 --- a/test/helper/devices/TestExceptions.h +++ b/test/helper/devices/TestExceptions.h @@ -32,6 +32,7 @@ class Exceptions : public FairMQDevice throw std::runtime_error("exception in " + state + "()"); } } + auto Bind() -> void override { std::string state("Bind"); @@ -39,6 +40,7 @@ class Exceptions : public FairMQDevice throw std::runtime_error("exception in " + state + "()"); } } + auto Connect() -> void override { std::string state("Connect"); diff --git a/test/helper/devices/TestWaitFor.h b/test/helper/devices/TestWaitFor.h index 7059c91a..b565bb3c 100644 --- a/test/helper/devices/TestWaitFor.h +++ b/test/helper/devices/TestWaitFor.h @@ -24,10 +24,34 @@ namespace test class TestWaitFor : public FairMQDevice { public: - void Run() + void PreRun() override { - std::cout << "hello" << std::endl; - WaitFor(std::chrono::seconds(60)); + std::string state("PreRun"); + if (std::string::npos != GetId().find("_" + state)) { + LOG(info) << "Going to sleep via WaitFor() in " << state << "..."; + bool result = WaitFor(std::chrono::seconds(60)); + LOG(info) << (result == true ? "Sleeping Done. Not interrupted." : "Sleeping Done. Interrupted."); + } + } + + void Run() override + { + std::string state("Run"); + if (std::string::npos != GetId().find("_" + state)) { + LOG(info) << "Going to sleep via WaitFor() in " << state << "..."; + bool result = WaitFor(std::chrono::seconds(60)); + LOG(info) << (result == true ? "Sleeping Done. Not interrupted." : "Sleeping Done. Interrupted."); + } + } + + void PostRun() override + { + std::string state("PostRun"); + if (std::string::npos != GetId().find("_" + state)) { + LOG(info) << "Going to sleep via WaitFor() in " << state << "..."; + bool result = WaitFor(std::chrono::seconds(60)); + LOG(info) << (result == true ? "Sleeping Done. Not interrupted." : "Sleeping Done. Interrupted."); + } } };