From 63916cfa89759d3360616f5255617f28230ebcbe Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Tue, 16 Jan 2018 09:13:33 +0000 Subject: [PATCH] Add process service stop timeout test. --- src/tests/proctests.cc | 49 ++++++++++++++++++++++++++ src/tests/test-bpsys.cc | 10 +++++- src/tests/test-includes/baseproc-sys.h | 7 +--- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/tests/proctests.cc b/src/tests/proctests.cc index 7dc4cb3..f193dbe 100644 --- a/src/tests/proctests.cc +++ b/src/tests/proctests.cc @@ -18,6 +18,7 @@ class base_process_service_test public: static void exec_succeeded(base_process_service *bsp) { + bsp->waiting_for_execstat = false; bsp->exec_succeeded(); } @@ -27,6 +28,11 @@ class base_process_service_test } }; +namespace bp_sys { + // last signal sent: + extern int last_sig_sent; +} + // Regular service start void test_proc_service_start() { @@ -139,6 +145,48 @@ void test_proc_start_timeout() assert(p.get_state() == service_state_t::STOPPED); } +// Test stop timeout +void test_proc_stop_timeout() +{ + using namespace std; + + service_set sset; + + string command = "test-command"; + list> command_offsets; + command_offsets.emplace_back(0, command.length()); + std::list depends; + + process_service p = process_service(&sset, "testproc", std::move(command), command_offsets, depends); + p.start(true); + sset.process_queues(); + + assert(p.get_state() == service_state_t::STARTING); + + base_process_service_test::exec_succeeded(&p); + sset.process_queues(); + + assert(p.get_state() == service_state_t::STARTED); + + p.stop(true); + sset.process_queues(); + + assert(p.get_state() == service_state_t::STOPPING); + assert(bp_sys::last_sig_sent == SIGTERM); + + p.timer_expired(); + sset.process_queues(); + + // kill signal (SIGKILL) should have been sent; process not dead until it's dead, however + assert(p.get_state() == service_state_t::STOPPING); + assert(bp_sys::last_sig_sent == SIGKILL); + + base_process_service_test::handle_exit(&p, 0); + sset.process_queues(); + + assert(p.get_state() == service_state_t::STOPPED); +} + // Smooth recovery void test_proc_smooth_recovery() { @@ -180,5 +228,6 @@ int main(int argc, char **argv) RUN_TEST(test_proc_unexpected_term, " "); RUN_TEST(test_term_via_stop, " "); RUN_TEST(test_proc_start_timeout, " "); + RUN_TEST(test_proc_stop_timeout, " "); RUN_TEST(test_proc_smooth_recovery, " "); } diff --git a/src/tests/test-bpsys.cc b/src/tests/test-bpsys.cc index a244f8c..a117d3a 100644 --- a/src/tests/test-bpsys.cc +++ b/src/tests/test-bpsys.cc @@ -4,7 +4,7 @@ #include "baseproc-sys.h" -std::vector usedfds = {true, true, true}; +static std::vector usedfds = {true, true, true}; // Allocate a file descriptor static int allocfd() @@ -22,6 +22,8 @@ static int allocfd() namespace bp_sys { +int last_sig_sent = -1; // last signal number sent, accessible for tests. + int pipe2(int fds[2], int flags) { fds[0] = allocfd(); @@ -37,4 +39,10 @@ int close(int fd) return 0; } +int kill(pid_t pid, int sig) +{ + last_sig_sent = sig; + return 0; +} + } diff --git a/src/tests/test-includes/baseproc-sys.h b/src/tests/test-includes/baseproc-sys.h index 5df5b7e..638b7c2 100644 --- a/src/tests/test-includes/baseproc-sys.h +++ b/src/tests/test-includes/baseproc-sys.h @@ -7,6 +7,7 @@ namespace bp_sys { int pipe2(int pipefd[2], int flags); int close(int fd); +int kill(pid_t pid, int sig); inline int fcntl(int fd, int cmd, ...) { @@ -14,12 +15,6 @@ inline int fcntl(int fd, int cmd, ...) return 0; } -inline int kill(pid_t pid, int sig) -{ - // No proper mock implemented yet, just return success for now: - return 0; -} - inline pid_t getpgid(pid_t pid) { return pid; -- 2.25.1