test(utils): add SleeperThread reassignment regression
We needed a regression test for reassignment safety after lifecycle fixes. I added a subprocess test that reassigns SleeperThread workers and verifies the process exits normally instead of terminating. Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
@@ -13,6 +13,7 @@ test_src = files(
|
|||||||
'../../src/config.cpp',
|
'../../src/config.cpp',
|
||||||
'JsonParser.cpp',
|
'JsonParser.cpp',
|
||||||
'SafeSignal.cpp',
|
'SafeSignal.cpp',
|
||||||
|
'sleeper_thread.cpp',
|
||||||
'css_reload_helper.cpp',
|
'css_reload_helper.cpp',
|
||||||
'../../src/util/css_reload_helper.cpp',
|
'../../src/util/css_reload_helper.cpp',
|
||||||
)
|
)
|
||||||
|
|||||||
42
test/utils/sleeper_thread.cpp
Normal file
42
test/utils/sleeper_thread.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#if __has_include(<catch2/catch_test_macros.hpp>)
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#else
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <thread>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "util/sleeper_thread.hpp"
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
SafeSignal<bool>& prepare_for_sleep() {
|
||||||
|
static SafeSignal<bool> signal;
|
||||||
|
return signal;
|
||||||
|
}
|
||||||
|
} // namespace waybar::util
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
int run_reassignment_regression() {
|
||||||
|
waybar::util::SleeperThread thread;
|
||||||
|
thread = [] { std::this_thread::sleep_for(std::chrono::milliseconds(10)); };
|
||||||
|
thread = [] { std::this_thread::sleep_for(std::chrono::milliseconds(1)); };
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("SleeperThread reassignment does not terminate process", "[util][sleeper_thread]") {
|
||||||
|
const auto pid = fork();
|
||||||
|
REQUIRE(pid >= 0);
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
_exit(run_reassignment_regression());
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = -1;
|
||||||
|
REQUIRE(waitpid(pid, &status, 0) == pid);
|
||||||
|
REQUIRE(WIFEXITED(status));
|
||||||
|
REQUIRE(WEXITSTATUS(status) == 0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user