Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 26, 2025
1 parent ffb17da commit 7cb3517
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions psutil/tests/test_process_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

"""Tests for psutil.ProcessWatcher class."""

import os
import time

import psutil
from psutil.tests import PsutilTestCase

Expand All @@ -17,6 +20,31 @@ def setUp(self):
def tearDown(self):
self.pw.close()

def test_it(self):
pass
# proc = self.spawn_testproc()
def read_until_pid(self, pid, timeout=2):
stop_at = time.monotonic() + timeout
while time.monotonic() < stop_at:
event = self.pw.read()
if event["pid"] == pid:
return event
raise TimeoutError("timed out")

def test_fork_then_exit(self):
proc = self.spawn_testproc()
event = self.read_until_pid(proc.pid)
assert event == {
"pid": proc.pid,
"event": psutil.PROC_EVENT_FORK,
"parent_pid": os.getpid(),
}

event = self.read_until_pid(proc.pid)
assert event == {"pid": proc.pid, "event": psutil.PROC_EVENT_EXEC}

proc.terminate()
proc.wait()
event = self.read_until_pid(proc.pid)
assert event == {
"pid": proc.pid,
"event": psutil.PROC_EVENT_EXIT,
"exit_code": abs(proc.returncode),
}

0 comments on commit 7cb3517

Please sign in to comment.