test-watch-pid: use pidref_safe_fork() with FORK_FREEZE

Then, use pidref version of functions.
This commit is contained in:
Yu Watanabe
2025-03-27 04:13:54 +09:00
parent 017f9bc1d7
commit 56691ea753
+27 -34
View File
@@ -34,60 +34,53 @@ TEST(watch_pid) {
ASSERT_TRUE(set_isempty(c->pids));
/* Fork off a child so that we have a PID to watch */
_cleanup_(sigkill_waitp) pid_t pid = 0;
pid = fork();
if (pid == 0) {
/* Child */
pause();
_exit(EXIT_SUCCESS);
}
ASSERT_OK(pid);
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
ASSERT_OK_POSITIVE(pidref_safe_fork("(child)", FORK_FREEZE, &pidref));
ASSERT_TRUE(hashmap_isempty(m->watch_pids));
ASSERT_NULL(manager_get_unit_by_pid(m, pid));
ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
ASSERT_OK(unit_watch_pid(a, pid, false));
ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), a);
ASSERT_OK(unit_watch_pidref(a, &pidref, false));
ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
ASSERT_OK(unit_watch_pid(a, pid, false));
ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), a);
ASSERT_OK(unit_watch_pidref(a, &pidref, false));
ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
ASSERT_OK(unit_watch_pid(b, pid, false));
u = manager_get_unit_by_pid(m, pid);
ASSERT_OK(unit_watch_pidref(b, &pidref, false));
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == b);
ASSERT_OK(unit_watch_pid(b, pid, false));
u = manager_get_unit_by_pid(m, pid);
ASSERT_OK(unit_watch_pidref(b, &pidref, false));
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == b);
ASSERT_OK(unit_watch_pid(c, pid, false));
u = manager_get_unit_by_pid(m, pid);
ASSERT_OK(unit_watch_pidref(c, &pidref, false));
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == b || u == c);
ASSERT_OK(unit_watch_pid(c, pid, false));
u = manager_get_unit_by_pid(m, pid);
ASSERT_OK(unit_watch_pidref(c, &pidref, false));
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == b || u == c);
unit_unwatch_pid(b, pid);
u = manager_get_unit_by_pid(m, pid);
unit_unwatch_pidref(b, &pidref);
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == c);
unit_unwatch_pid(b, pid);
u = manager_get_unit_by_pid(m, pid);
unit_unwatch_pidref(b, &pidref);
u = manager_get_unit_by_pidref(m, &pidref);
ASSERT_TRUE(u == a || u == c);
unit_unwatch_pid(a, pid);
ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), c);
unit_unwatch_pidref(a, &pidref);
ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
unit_unwatch_pid(a, pid);
ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), c);
unit_unwatch_pidref(a, &pidref);
ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
unit_unwatch_pid(c, pid);
ASSERT_NULL(manager_get_unit_by_pid(m, pid));
unit_unwatch_pidref(c, &pidref);
ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
unit_unwatch_pid(c, pid);
ASSERT_NULL(manager_get_unit_by_pid(m, pid));
unit_unwatch_pidref(c, &pidref);
ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
}
static int intro(void) {