mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-14 09:06:47 +00:00
Use process tools for WaitFor test
This commit is contained in:
parent
6809d60fad
commit
99ffb732f4
|
@ -189,7 +189,7 @@ try {
|
||||||
bool keepRunning = true;
|
bool keepRunning = true;
|
||||||
|
|
||||||
while (keepRunning) {
|
while (keepRunning) {
|
||||||
if (poll(cinfd, 1, 500)) {
|
if (poll(cinfd, 1, 100)) {
|
||||||
if (fDeviceShutdownRequested) {
|
if (fDeviceShutdownRequested) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,66 +9,65 @@
|
||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <boost/process.hpp>
|
#include <fairmq/Tools.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <signal.h> // kill
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <future> // std::async, std::future
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace fair::mq::test;
|
using namespace fair::mq::test;
|
||||||
|
using namespace fair::mq::tools;
|
||||||
|
|
||||||
void RunWaitFor()
|
void RunWaitFor(const string& state, const string& control)
|
||||||
{
|
{
|
||||||
std::mutex mtx;
|
execute_result result;
|
||||||
std::condition_variable cv;
|
thread device_thread([&] {
|
||||||
|
|
||||||
int pid = 0;
|
|
||||||
int exit_code = 0;
|
|
||||||
|
|
||||||
thread deviceThread([&]() {
|
|
||||||
stringstream cmd;
|
stringstream cmd;
|
||||||
cmd << runTestDevice << " --id waitfor_" << " --control static " << " --severity nolog";
|
cmd << runTestDevice
|
||||||
|
<< " --id waitfor_" << state
|
||||||
boost::process::ipstream stdout;
|
<< " --control " << control
|
||||||
boost::process::child c(cmd.str(), boost::process::std_out > stdout);
|
<< " --session " << UuidHash()
|
||||||
string line;
|
<< " --severity debug"
|
||||||
getline(stdout, line);
|
<< " --color false";
|
||||||
|
result = execute(cmd.str(), "[WaitFor]", "", SIGINT);
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
pid = c.id();
|
|
||||||
}
|
|
||||||
cv.notify_one();
|
|
||||||
|
|
||||||
c.wait();
|
|
||||||
|
|
||||||
exit_code = c.exit_code();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
device_thread.join();
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
cv.wait(lock, [&pid]{ return pid != 0; });
|
|
||||||
}
|
|
||||||
|
|
||||||
kill(pid, SIGINT);
|
ASSERT_NE(string::npos, result.console_out.find("Sleeping Done. Interrupted."));
|
||||||
|
|
||||||
deviceThread.join();
|
exit(result.exit_code);
|
||||||
|
|
||||||
exit(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
|
} // namespace
|
||||||
|
|
|
@ -32,6 +32,7 @@ class ErrorState : public FairMQDevice
|
||||||
ChangeState(fair::mq::Transition::ErrorFound);
|
ChangeState(fair::mq::Transition::ErrorFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bind() override
|
void Bind() override
|
||||||
{
|
{
|
||||||
std::string state("Bind");
|
std::string state("Bind");
|
||||||
|
@ -40,6 +41,7 @@ class ErrorState : public FairMQDevice
|
||||||
ChangeState(fair::mq::Transition::ErrorFound);
|
ChangeState(fair::mq::Transition::ErrorFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connect() override
|
void Connect() override
|
||||||
{
|
{
|
||||||
std::string state("Connect");
|
std::string state("Connect");
|
||||||
|
|
|
@ -32,6 +32,7 @@ class Exceptions : public FairMQDevice
|
||||||
throw std::runtime_error("exception in " + state + "()");
|
throw std::runtime_error("exception in " + state + "()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Bind() -> void override
|
auto Bind() -> void override
|
||||||
{
|
{
|
||||||
std::string state("Bind");
|
std::string state("Bind");
|
||||||
|
@ -39,6 +40,7 @@ class Exceptions : public FairMQDevice
|
||||||
throw std::runtime_error("exception in " + state + "()");
|
throw std::runtime_error("exception in " + state + "()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Connect() -> void override
|
auto Connect() -> void override
|
||||||
{
|
{
|
||||||
std::string state("Connect");
|
std::string state("Connect");
|
||||||
|
|
|
@ -24,10 +24,34 @@ namespace test
|
||||||
class TestWaitFor : public FairMQDevice
|
class TestWaitFor : public FairMQDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Run()
|
void PreRun() override
|
||||||
{
|
{
|
||||||
std::cout << "hello" << std::endl;
|
std::string state("PreRun");
|
||||||
WaitFor(std::chrono::seconds(60));
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user