Skip to content

Commit

Permalink
TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 13, 2024
1 parent 6e1d203 commit 62e9221
Showing 1 changed file with 32 additions and 63 deletions.
95 changes: 32 additions & 63 deletions nano/core_test/signal_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,94 +12,63 @@
*/

#include <nano/lib/signal_manager.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>

#include <csignal>
#include <iostream>
#include <thread>

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<void (int)> 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<void (int)> 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);
}

0 comments on commit 62e9221

Please sign in to comment.