test: do not accidentally kill parent process

Use `kill(getpid(), ...)` instead of `kill(0, ...)`.
`kill(0, ...)` sends a signal to the whole process group, not just the
currently executing process. The process group may include a parent
process, for example a bash subshell spawned for a script.
This commit is contained in:
Tom 2020-08-01 02:05:43 +02:00
parent 7857a565b4
commit 63535dc16c
2 changed files with 22 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <unistd.h>
void Worker(const sgnl::AtomicCondition<bool>& exit_condition) void Worker(const sgnl::AtomicCondition<bool>& exit_condition)
@ -53,10 +54,10 @@ int main()
// SIGUSR1 // SIGUSR1
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
kill(0, SIGUSR1); kill(getpid(), SIGUSR1);
// SIGTERM // SIGTERM
kill(0, SIGTERM); kill(getpid(), SIGTERM);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
for(auto& future : futures) for(auto& future : futures)

View File

@ -11,6 +11,7 @@
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <unistd.h>
#include <vector> #include <vector>
@ -163,14 +164,22 @@ TEST_CASE("wait-until-predicate")
{ {
sgnl::AtomicCondition condition(23); sgnl::AtomicCondition condition(23);
auto pred = [&condition](){ return condition.get() == 42; }; auto pred = [&condition](){ return condition.get() == 42; };
sgnl::AtomicCondition<bool> fence(false);
std::future<void> future = std::future<void> future =
std::async( std::async(
std::launch::async, std::launch::async,
[&condition, &pred](){ [&condition, &pred, &fence](){
auto duration = std::chrono::system_clock::now()
+ std::chrono::hours(1);
fence.set_and_notify_all(true);
condition.wait_until( condition.wait_until(
std::chrono::system_clock::now() + std::chrono::hours(1), duration,
pred); }); pred); });
fence.wait_value(true);
REQUIRE( fence.get() == true );
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::yield(); std::this_thread::yield();
@ -208,7 +217,7 @@ TEST_CASE("wait-until-value")
TEST_CASE("sigwait") TEST_CASE("sigwait")
{ {
sgnl::SignalHandler signal_handler({SIGUSR1}); sgnl::SignalHandler signal_handler({SIGUSR1});
kill(0, SIGUSR1); kill(getpid(), SIGUSR1);
REQUIRE( signal_handler.sigwait() == SIGUSR1 ); REQUIRE( signal_handler.sigwait() == SIGUSR1 );
} }
@ -218,7 +227,7 @@ TEST_CASE("sigwait_handler")
return true; return true;
}; };
sgnl::SignalHandler signal_handler({SIGUSR2}); sgnl::SignalHandler signal_handler({SIGUSR2});
kill(0, SIGUSR2); kill(getpid(), SIGUSR2);
REQUIRE( signal_handler.sigwait_handler(handler) == SIGUSR2 ); REQUIRE( signal_handler.sigwait_handler(handler) == SIGUSR2 );
} }
@ -231,7 +240,7 @@ TEST_CASE("async_sigwait_handler")
}; };
auto future = signal_handler.async_sigwait_handler(handler); auto future = signal_handler.async_sigwait_handler(handler);
kill(0, SIGUSR1); kill(getpid(), SIGUSR1);
REQUIRE( future.get() == SIGUSR1 ); REQUIRE( future.get() == SIGUSR1 );
} }
@ -248,7 +257,7 @@ TEST_CASE("async_sigwait_handler-condition")
}; };
auto future = signal_handler.async_sigwait_handler(handler); auto future = signal_handler.async_sigwait_handler(handler);
kill(0, SIGUSR1); kill(getpid(), SIGUSR1);
REQUIRE( future.get() == SIGUSR1 ); REQUIRE( future.get() == SIGUSR1 );
REQUIRE( condition.get() == SIGUSR1 ); REQUIRE( condition.get() == SIGUSR1 );
} }
@ -273,13 +282,13 @@ TEST_CASE("constructor-thread-blocks-signals")
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::yield(); std::this_thread::yield();
REQUIRE( kill(0, SIGTERM) == 0 ); REQUIRE( kill(getpid(), SIGTERM) == 0 );
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::yield(); std::this_thread::yield();
CHECK( last_signal.load() == SIGTERM ); CHECK( last_signal.load() == SIGTERM );
REQUIRE( kill(0, SIGINT) == 0 ); REQUIRE( kill(getpid(), SIGINT) == 0 );
REQUIRE( ft_sig_handler.get() == SIGINT ); REQUIRE( ft_sig_handler.get() == SIGINT );
REQUIRE( last_signal.load() == SIGINT ); REQUIRE( last_signal.load() == SIGINT );
@ -317,7 +326,7 @@ TEST_CASE("sleeping-workers-with-exit-condition")
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::yield(); std::this_thread::yield();
REQUIRE( kill(0, test_signal) == 0 ); REQUIRE( kill(getpid(), test_signal) == 0 );
for(auto& future : futures) for(auto& future : futures)
REQUIRE(future.get() == true); REQUIRE(future.get() == true);
@ -359,7 +368,7 @@ TEST_CASE("looping-workers-with-exit-condition")
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::yield(); std::this_thread::yield();
REQUIRE( kill(0, test_signal) == 0 ); REQUIRE( kill(getpid(), test_signal) == 0 );
for(auto& future : futures) for(auto& future : futures)
// After 100 milliseconds, each worker thread should // After 100 milliseconds, each worker thread should