Skip to content

Commit 2233d74

Browse files
committed
new unit test for procfs related function
1 parent ea7e173 commit 2233d74

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/python/Utils_t/ProcFS_t.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
"""
3+
Unittests for ProcFS functions
4+
"""
5+
6+
# system modules
7+
import unittest
8+
import os
9+
import time
10+
import threading
11+
12+
# WMCore modules
13+
from Utils.ProcFS import processStatus
14+
15+
16+
def daemon(nloops=2):
17+
"""
18+
Dummy daemon with sleep loops
19+
:param: number of loops to perform, default 2
20+
"""
21+
for _ in range(nloops):
22+
time.sleep(1)
23+
24+
25+
class TestProcessStatus(unittest.TestCase):
26+
"""
27+
TestProcessStauts unit test class
28+
"""
29+
def testProcessStatus(self):
30+
"""Test processStatus function with the current process PID."""
31+
pid = os.getpid() # Get the current process ID
32+
thread = threading.Thread(target=daemon, args={}) # Create a dummy thread
33+
thread.start()
34+
try:
35+
statusList = processStatus(pid)
36+
37+
# Check that at least one entry exists (main process)
38+
self.assertGreater(len(statusList), 0, "Status list should not be empty")
39+
40+
# Ensure the first entry is the main process
41+
mainProcess = statusList[0]
42+
self.assertEqual(mainProcess["pid"], str(pid), "Main process PID should match")
43+
self.assertIn(mainProcess["status"][0], "RSDTZ", "Unexpected process state")
44+
45+
# Ensure that we have two pids: one for main process and another for daemon thread
46+
pids = [entry["pid"] for entry in statusList]
47+
self.assertTrue(len(pids), 2)
48+
self.assertTrue(pids[0] != pids[1], True)
49+
50+
finally:
51+
thread.join() # Ensure thread finishes execution
52+
53+
def worker(self):
54+
"""A simple worker thread that runs for a short time."""
55+
time.sleep(2) # Keep the thread alive for 2 seconds
56+
57+
def testProcessStatusWithStoppedThread(self):
58+
"""Test processStatus when a thread is running and then stopped."""
59+
pid = os.getpid() # Get current process ID
60+
thread = threading.Thread(target=self.worker)
61+
thread.start() # Start the thread
62+
63+
time.sleep(0.5) # Wait for the thread to be fully initialized
64+
65+
# Check status while the thread is running
66+
statusList = processStatus(pid)
67+
pids = [entry["pid"] for entry in statusList]
68+
self.assertTrue(len(pids), 2)
69+
70+
# Let the thread finish (simulate stopping)
71+
thread.join()
72+
73+
time.sleep(1) # Allow time for the system to reflect thread termination
74+
75+
# Check status after thread stops
76+
statusList = processStatus(pid)
77+
pids = [entry["pid"] for entry in statusList]
78+
self.assertTrue(len(pids), 1)
79+
80+
81+
if __name__ == "__main__":
82+
unittest.main()

0 commit comments

Comments
 (0)