polybar-dwm/tests/unit_tests/utils/process.cpp
patrick96 47483a94f1 fix(process): fork_detached created zombie processes
Since the forked processes are still our children, we need to wait on
them, otherwise they become zombie processes.

We now fork twice, let the first fork immediately return and wait on it.
This reparents the second fork, which runs the actual code, to the init
process which then collects it.

Ref #770
2020-12-12 02:37:21 +01:00

35 lines
658 B
C++

#include "utils/process.hpp"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <chrono>
#include <thread>
#include "common/test.hpp"
using namespace polybar;
using namespace process_util;
TEST(SpawnAsync, is_async) {
pid_t pid = spawn_async([] { exec_sh("sleep 0.1"); });
int status;
pid_t res = process_util::wait_for_completion_nohang(pid, &status);
ASSERT_NE(res, -1);
EXPECT_FALSE(WIFEXITED(status));
}
TEST(SpawnAsync, exit_code) {
pid_t pid = spawn_async([] { exec_sh("exit 42"); });
int status = 0;
pid_t res = waitpid(pid, &status, 0);
EXPECT_EQ(res, pid);
EXPECT_EQ(WEXITSTATUS(status), 42);
}