From 675ac17fddca229e391d80eb610959d9c53db6e5 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 26 Mar 2012 11:36:47 -0700 Subject: [PATCH] Sync libtest. --- libtest/cmdline.cc | 32 +++++++++++++++++++++++--------- libtest/cmdline.h | 7 ++++++- libtest/fatal.hpp | 4 ++-- libtest/server.cc | 35 ++++++++++++----------------------- libtest/server.h | 7 ++----- libtest/server_container.cc | 4 ++-- libtest/test.cc | 5 +++++ libtest/unittest.cc | 3 +++ 8 files changed, 55 insertions(+), 42 deletions(-) diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index b93d8aba..cad29467 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -116,7 +116,7 @@ Application::Application(const std::string& arg, const bool _use_libtool_arg) : { if (libtool() == NULL) { - throw "libtool requested, but know libtool was found"; + throw fatal_message("libtool requested, but know libtool was found"); } } @@ -143,6 +143,7 @@ Application::Application(const std::string& arg, const bool _use_libtool_arg) : Application::~Application() { + murder(); delete_argv(); } @@ -234,6 +235,7 @@ Application::error_t Application::run(const char *args[]) if (spawn_ret) { + _pid= -1; return Application::INVALID; } @@ -300,6 +302,7 @@ bool Application::slurp() return false; } + bool data_was_read= false; if (fds[0].revents & POLLIN) { ssize_t read_length; @@ -320,6 +323,8 @@ bool Application::slurp() break; } + + data_was_read= true; _stdout_buffer.reserve(read_length +1); for (size_t x= 0; x < read_length; x++) { @@ -351,6 +356,8 @@ bool Application::slurp() break; } + + data_was_read= true; _stderr_buffer.reserve(read_length +1); for (size_t x= 0; x < read_length; x++) { @@ -360,12 +367,15 @@ bool Application::slurp() } } - return true; + return data_was_read; } -Application::error_t Application::wait() +Application::error_t Application::wait(bool nohang) { - fatal_assert(_pid != -1); + if (_pid == -1) + { + return Application::INVALID; + } slurp(); @@ -373,7 +383,7 @@ Application::error_t Application::wait() { int status= 0; pid_t waited_pid; - if ((waited_pid= waitpid(_pid, &status, 0)) == -1) + if ((waited_pid= waitpid(_pid, &status, nohang ? WNOHANG : 0)) == -1) { switch (errno) { @@ -385,6 +395,10 @@ Application::error_t Application::wait() Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(_pid); } } + else if (waited_pid == 0) + { + exit_code= Application::SUCCESS; + } else { if (waited_pid != _pid) @@ -453,7 +467,7 @@ void Application::Pipe::reset() if (pipe(_fd) == -1) { - throw strerror(errno); + throw fatal_message(strerror(errno)); } _open[0]= true; _open[1]= true; @@ -478,13 +492,13 @@ void Application::Pipe::dup_for_spawn(const close_t& arg, posix_spawn_file_actio if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _fd[type], newfildes )) < 0) { Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")"; - throw strerror(ret); + throw fatal_message(strerror(ret)); } if ((ret= posix_spawn_file_actions_addclose(&file_actions, _fd[type])) < 0) { Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")"; - throw strerror(ret); + throw fatal_message(strerror(ret)); } } @@ -640,7 +654,7 @@ int exec_cmdline(const std::string& command, const char *args[], bool use_libtoo return int(ret); } - return int(app.wait()); + return int(app.wait(false)); } const char *gearmand_binary() diff --git a/libtest/cmdline.h b/libtest/cmdline.h index 0f069925..7b0afc5f 100644 --- a/libtest/cmdline.h +++ b/libtest/cmdline.h @@ -74,7 +74,7 @@ public: void add_option(const std::string&, const std::string&); void add_long_option(const std::string& option_name, const std::string& option_value); error_t run(const char *args[]= NULL); - error_t wait(); + error_t wait(bool nohang= true); libtest::vchar_t stdout_result() const { @@ -120,6 +120,11 @@ public: return _gdb_filename; } + pid_t pid() const + { + return _pid; + } + private: void create_argv(const char *args[]); void delete_argv(); diff --git a/libtest/fatal.hpp b/libtest/fatal.hpp index 23f47bd9..61fe4b02 100644 --- a/libtest/fatal.hpp +++ b/libtest/fatal.hpp @@ -55,5 +55,5 @@ private: } // namespace libtest -#define fatal_message(__mesg) libtest::fatal(LIBYATL_DEFAULT_PARAM, __mesg) -#define fatal_assert(__assert) if((__assert)) {} else { libtest::fatal(LIBYATL_DEFAULT_PARAM, #__assert); } +#define fatal_message(__mesg) libtest::fatal(LIBYATL_DEFAULT_PARAM, "%s", __mesg) +#define fatal_assert(__assert) if((__assert)) {} else { libtest::fatal(LIBYATL_DEFAULT_PARAM, "%s", #__assert); } diff --git a/libtest/server.cc b/libtest/server.cc index 55713d45..da341f95 100644 --- a/libtest/server.cc +++ b/libtest/server.cc @@ -90,16 +90,6 @@ Server::Server(const std::string& host_arg, const in_port_t port_arg, Server::~Server() { - if (has_pid() and not kill(_pid)) - { - Error << "Unable to kill:" << *this; - } - - Application::error_t ret; - if (Application::SUCCESS != (ret= _app.wait())) - { - Error << "Application::wait() " << _running << " " << ret << ": " << _app.stderr_result(); - } } bool Server::check() @@ -119,13 +109,12 @@ bool Server::cycle() uint32_t limit= 3; // Try to ping, and kill the server #limit number of times - pid_t current_pid; while (--limit and - is_pid_valid(current_pid= get_pid())) + is_pid_valid(_app.pid())) { - if (kill(current_pid)) + if (kill()) { - Log << "Killed existing server," << *this << " with pid:" << current_pid; + Log << "Killed existing server," << *this; dream(0, 50000); continue; } @@ -134,7 +123,7 @@ bool Server::cycle() // For whatever reason we could not kill it, and we reached limit if (limit == 0) { - Error << "Reached limit, could not kill server pid:" << current_pid; + Error << "Reached limit, could not kill server"; return false; } @@ -148,15 +137,15 @@ bool Server::wait_for_pidfile() const return wait.successful(); } +bool Server::has_pid() const +{ + return (_pid > 1); +} + + bool Server::start() { // If we find that we already have a pid then kill it. - if (has_pid() and kill(_pid) == false) - { - Error << "Could not kill() existing server during start() pid:" << _pid; - return false; - } - if (has_pid() == false) { fatal_message("has_pid() failed, programer error"); @@ -402,9 +391,9 @@ bool Server::args(Application& app) return true; } -bool Server::kill(pid_t pid_arg) +bool Server::kill() { - if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset + if (check_pid(_app.pid()) and kill_pid(_app.pid())) // If we kill it, reset { if (broken_pid_file() and pid_file().empty() == false) { diff --git a/libtest/server.h b/libtest/server.h index 68d5e226..1aa16ca0 100644 --- a/libtest/server.h +++ b/libtest/server.h @@ -202,10 +202,7 @@ public: return _pid; } - bool has_pid() const - { - return (_pid > 1); - } + bool has_pid() const; virtual bool wait_for_pidfile() const; @@ -228,7 +225,7 @@ public: std::string log_and_pid(); - bool kill(pid_t pid_arg); + bool kill(); bool start(); bool command(libtest::Application& app); diff --git a/libtest/server_container.cc b/libtest/server_container.cc index 1b5229cc..d406c16e 100644 --- a/libtest/server_container.cc +++ b/libtest/server_container.cc @@ -82,7 +82,7 @@ bool server_startup_st::shutdown(uint32_t number_of_host) { Server* tmp= servers[number_of_host]; - if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid())) + if (tmp and tmp->has_pid() and tmp->kill() == false) { } else { @@ -119,7 +119,7 @@ void server_startup_st::shutdown() { for (std::vector::iterator iter= servers.begin(); iter != servers.end(); iter++) { - if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid())) + if ((*iter)->has_pid() and (*iter)->kill() == false) { Error << "Unable to kill:" << *(*iter); } diff --git a/libtest/test.cc b/libtest/test.cc index 3e5713c9..fb18e0ea 100644 --- a/libtest/test.cc +++ b/libtest/test.cc @@ -350,6 +350,11 @@ int main(int argc, char *argv[]) } } + catch (libtest::fatal &e) + { + Error << "Fatal exception was thrown: " << e.what(); + return_code= TEST_FAILURE; + } catch (std::exception &e) { Error << "Exception was thrown: " << e.what(); diff --git a/libtest/unittest.cc b/libtest/unittest.cc index 10d6c062..d295b5a9 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -471,6 +471,8 @@ static test_return_t application_echo_fubar_BINARY(void *) test_compare(Application::SUCCESS, true_app.run(args)); test_compare(Application::SUCCESS, true_app.wait()); + while (true_app.slurp() == false) {} ; + libtest::vchar_t response; make_vector(response, test_literal_param("fubar\n")); test_compare(response, true_app.stdout_result()); @@ -486,6 +488,7 @@ static test_return_t application_echo_fubar_BINARY2(void *) test_compare(Application::SUCCESS, true_app.run()); test_compare(Application::SUCCESS, true_app.wait()); + libtest::vchar_t response; make_vector(response, test_literal_param("fubar\n")); test_compare(response, true_app.stdout_result()); -- 2.30.2