diff --git a/nano/core_test/signal_manager.cpp b/nano/core_test/signal_manager.cpp index fe3383945f..5f41b188d2 100644 --- a/nano/core_test/signal_manager.cpp +++ b/nano/core_test/signal_manager.cpp @@ -12,6 +12,8 @@ */ #include +#include +#include #include @@ -19,87 +21,54 @@ #include #include -static void handler_print_signal (int signum) +TEST (signal_manager, basic) { - std::cerr << "boost signal handler " << signum << std::endl - << std::flush; + nano::signal_manager sigman; } -static int wait_for_sig_received (int millisecs, int & sig_received) +TEST (signal_manager, single) { - for (int i = 0; i < millisecs && sig_received == 0; i++) - { - std::this_thread::sleep_for (std::chrono::microseconds (1)); - } - return sig_received; -} + nano::test::system system; -static int trap (int signum) -{ nano::signal_manager sigman; - int sig_received = 0; - - std::function f = [&sig_received] (int signum) { - std::cerr << "boost signal handler " << signum << std::endl - << std::flush; - sig_received = signum; - }; - sigman.register_signal_handler (signum, f, false); + std::atomic received{ 0 }; + sigman.register_signal_handler (SIGINT, [&] (int sig) { received = sig; }, false); - raise (signum); - - exit (wait_for_sig_received (10000, sig_received)); + raise (SIGINT); + ASSERT_TIMELY (5s, received.load ()); } -static void repeattest (int signum, bool repeat) +TEST (signal_manager, multiple) { + nano::test::system system; + nano::signal_manager sigman; - int sig_received = 0; - - std::function f = [&sig_received] (int signum) { - std::cerr << "boost signal handler" << std::flush; - sig_received = signum; - }; - - sigman.register_signal_handler (signum, f, repeat); - - for (int i = 0; i < 10; i++) - { - sig_received = 0; - raise (signum); - if (wait_for_sig_received (10000, sig_received) != signum) - { - exit (1); - } - } - - exit (0); -} -TEST (DISABLED_signal_manager_test, trap) -{ - int signum; + std::atomic received{ 0 }; + sigman.register_signal_handler (SIGINT, [&] (int sig) { received = sig; }, false); + sigman.register_signal_handler (SIGTERM, [&] (int sig) { received = sig; }, false); - signum = SIGINT; - ASSERT_EXIT (trap (signum), ::testing::ExitedWithCode (signum), ""); + raise (SIGINT); + ASSERT_TIMELY (5s, received.load () == SIGINT); - signum = SIGTERM; - ASSERT_EXIT (trap (signum), ::testing::ExitedWithCode (signum), ""); + raise (SIGTERM); + ASSERT_TIMELY (5s, received.load () == SIGTERM); } -TEST (DISABLED_signal_manager_test, repeat) +TEST (signal_manager, repeat) { - int signum; + nano::test::system system; - signum = SIGINT; - ASSERT_EXIT (repeattest (signum, true), ::testing::ExitedWithCode (0), ""); -} + nano::signal_manager sigman; -TEST (DISABLED_signal_manager_test, norepeat) -{ - int signum; + std::atomic received{ 0 }; + sigman.register_signal_handler (SIGINT, [&] (int sig) { received = sig; }, true); - signum = SIGINT; - ASSERT_DEATH (repeattest (signum, false), "^boost signal handler$"); -} + raise (SIGINT); + ASSERT_TIMELY (5s, received.load () == SIGINT); + + received = 0; + raise (SIGINT); + ASSERT_TIMELY (5s, received.load () == SIGINT); +} \ No newline at end of file