Debian Codegraph @

hyprtester/src/tests/main/exec.cpp

raw
1 #include "tests.hpp"
2 #include "../../shared.hpp"
3 #include "../../hyprctlCompat.hpp"
4 #include <chrono>
5 #include <format>
6 #include <thread>
7 #include <hyprutils/os/Process.hpp>
8 #include <hyprutils/memory/WeakPtr.hpp>
9 #include "../shared.hpp"
10
11 using namespace Hyprutils::OS;
12 using namespace Hyprutils::Memory;
13
14 #define UP CUniquePointer
15 #define SP CSharedPointer
16
17 const static auto SLEEP_DURATIONS = std::array{1, 10};
18
19 TEST_CASE(processSpawning) {
20 for (const auto duration : SLEEP_DURATIONS) {
21 // Note: POSIX sleep does not support fractional seconds, so
22 // can't sleep for less than 1 second.
23 OK(getFromSocket(std::format("/dispatch hl.dsp.exec_cmd('sleep {}')", duration)));
24
25 // Ensure that sleep is our child
26 const std::string sleepPidS = Tests::execAndGet("pgrep sleep");
27 pid_t sleepPid;
28 try {
29 sleepPid = std::stoull(sleepPidS);
30 } catch (...) {
31 NLog::log("{}Sleep was not spawned or several sleeps are running: pgrep returned '{}'", Colors::RED, sleepPidS);
32 continue;
33 }
34
35 const std::string sleepParentComm = Tests::execAndGet("cat \"/proc/$(ps -o ppid:1= -p " + sleepPidS + ")/comm\"");
36 NLog::log("{}Expecting that sleep's parent is Hyprland", Colors::YELLOW);
37 EXPECT_CONTAINS(sleepParentComm, "Hyprland");
38
39 std::this_thread::sleep_for(std::chrono::seconds(duration));
40
41 // Ensure that sleep did not become a zombie
42 EXPECT(Tests::processAlive(sleepPid), false);
43
44 // Test succeeded
45 return;
46 }
47
48 FAIL_TEST_SILENT();
49 }