Commit 82917ea4 authored by Dominik Charousset's avatar Dominik Charousset

Print output of child process in unit tests

parent c44db8a4
...@@ -74,7 +74,7 @@ void set_default_test_settings() { ...@@ -74,7 +74,7 @@ void set_default_test_settings() {
cout.unsetf(ios_base::unitbuf); cout.unsetf(ios_base::unitbuf);
} }
std::thread run_program_impl(const char* cpath, std::vector<std::string> args) { std::thread run_program_impl(actor rc, const char* cpath, vector<string> args) {
string path = cpath; string path = cpath;
replace_all(path, "'", "\\'"); replace_all(path, "'", "\\'");
ostringstream oss; ostringstream oss;
...@@ -82,12 +82,20 @@ std::thread run_program_impl(const char* cpath, std::vector<std::string> args) { ...@@ -82,12 +82,20 @@ std::thread run_program_impl(const char* cpath, std::vector<std::string> args) {
for (auto& arg : args) { for (auto& arg : args) {
oss << " " << arg; oss << " " << arg;
} }
oss << to_dev_null; oss << " 2>&1";
string cmdstr = oss.str(); string cmdstr = oss.str();
return thread([cmdstr] { return std::thread([cmdstr, rc] {
if (system(cmdstr.c_str()) != 0) { string output;
CAF_PRINTERR("FATAL: command line failed: " << cmdstr); auto fp = popen(cmdstr.c_str(), "r");
abort(); if (!fp) {
CAF_PRINTERR("FATAL: command line failed: " << cmdstr);
abort();
} }
char buf[512];
while (fgets(buf, sizeof(buf), fp)) {
output += buf;
}
pclose(fp);
anon_send(rc, output);
}); });
} }
...@@ -205,7 +205,7 @@ caf::optional<T> spro(const std::string& str) { ...@@ -205,7 +205,7 @@ caf::optional<T> spro(const std::string& str) {
return caf::none; return caf::none;
} }
std::thread run_program_impl(const char* path, std::vector<std::string> args); std::thread run_program_impl(caf::actor, const char*, std::vector<std::string>);
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<
...@@ -221,9 +221,9 @@ inline std::string convert_to_str(std::string value) { ...@@ -221,9 +221,9 @@ inline std::string convert_to_str(std::string value) {
} }
template <class... Ts> template <class... Ts>
std::thread run_program(const char* path, Ts&&... args) { std::thread run_program(caf::actor listener, const char* path, Ts&&... args) {
std::vector<std::string> vec{convert_to_str(std::forward<Ts>(args))...}; std::vector<std::string> vec{convert_to_str(std::forward<Ts>(args))...};
return run_program_impl(path, std::move(vec)); return run_program_impl(listener, path, std::move(vec));
} }
#endif // TEST_HPP #endif // TEST_HPP
...@@ -153,12 +153,19 @@ void run_server(bool spawn_client, const char* bin_path) { ...@@ -153,12 +153,19 @@ void run_server(bool spawn_client, const char* bin_path) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
cout << "server is running on port " << port << endl; cout << "server is running on port " << port << endl;
if (spawn_client) { if (spawn_client) {
auto child = run_program(bin_path, "-c", port); auto child = run_program(self, bin_path, "-c", port);
CAF_CHECKPOINT(); CAF_CHECKPOINT();
child.join(); child.join();
} }
} }
); );
self->await_all_other_actors_done();
self->receive(
[](const std::string& output) {
cout << endl << endl << "*** output of client program ***"
<< endl << output << endl;
}
);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
......
...@@ -348,7 +348,7 @@ void test_remote_actor(const char* app_path, bool run_remote_actor) { ...@@ -348,7 +348,7 @@ void test_remote_actor(const char* app_path, bool run_remote_actor) {
CAF_CHECK(serv == serv2); CAF_CHECK(serv == serv2);
thread child; thread child;
if (run_remote_actor) { if (run_remote_actor) {
child = run_program(app_path, "-c", port2, port1, gport); child = run_program(self, app_path, "-c", port2, port1, gport);
} else { } else {
CAF_PRINT("please run client with: " CAF_PRINT("please run client with: "
<< "-c " << port2 << " " << port1 << " " << gport); << "-c " << port2 << " " << port1 << " " << gport);
...@@ -362,11 +362,17 @@ void test_remote_actor(const char* app_path, bool run_remote_actor) { ...@@ -362,11 +362,17 @@ void test_remote_actor(const char* app_path, bool run_remote_actor) {
); );
// wait until separate process (in sep. thread) finished execution // wait until separate process (in sep. thread) finished execution
CAF_CHECKPOINT(); CAF_CHECKPOINT();
self->await_all_other_actors_done();
CAF_CHECKPOINT();
if (run_remote_actor) { if (run_remote_actor) {
child.join(); child.join();
self->receive(
[](const std::string& output) {
cout << endl << endl << "*** output of client program ***"
<< endl << output << endl;
}
);
} }
CAF_CHECKPOINT();
self->await_all_other_actors_done();
} }
} // namespace <anonymous> } // namespace <anonymous>
......
...@@ -91,13 +91,19 @@ int main(int argc, char** argv) { ...@@ -91,13 +91,19 @@ int main(int argc, char** argv) {
CAF_CHECKPOINT(); CAF_CHECKPOINT();
// execute client_part() in a separate process, // execute client_part() in a separate process,
// connected via localhost socket // connected via localhost socket
auto child = run_program(argv[0], "-c", port); scoped_actor self;
auto child = run_program(self, argv[0], "-c", port);
CAF_CHECKPOINT(); CAF_CHECKPOINT();
child.join(); child.join();
self->await_all_other_actors_done();
self->receive(
[](const std::string& output) {
cout << endl << endl << "*** output of client program ***"
<< endl << output << endl;
}
);
} }
}); });
CAF_CHECKPOINT();
await_all_actors_done();
shutdown(); shutdown();
return CAF_TEST_RESULT(); return CAF_TEST_RESULT();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment