|
1 #!/usr/bin/env python |
|
2 |
|
3 import os |
|
4 import signal |
|
5 import unittest |
|
6 |
|
7 import mozinfo |
|
8 from mozprocess import processhandler |
|
9 |
|
10 import proctest |
|
11 |
|
12 |
|
13 here = os.path.dirname(os.path.abspath(__file__)) |
|
14 |
|
15 |
|
16 class ProcTestPoll(proctest.ProcTest): |
|
17 """ Class to test process poll """ |
|
18 |
|
19 def test_poll_before_run(self): |
|
20 """Process is not started, and poll() is called""" |
|
21 |
|
22 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
23 "process_normal_finish_python.ini"], |
|
24 cwd=here) |
|
25 self.assertRaises(AttributeError, p.poll) |
|
26 |
|
27 def test_poll_while_running(self): |
|
28 """Process is started, and poll() is called""" |
|
29 |
|
30 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
31 "process_normal_finish_python.ini"], |
|
32 cwd=here) |
|
33 p.run() |
|
34 returncode = p.poll() |
|
35 |
|
36 self.assertEqual(returncode, None) |
|
37 |
|
38 detected, output = proctest.check_for_process(self.proclaunch) |
|
39 self.determine_status(detected, |
|
40 output, |
|
41 returncode, |
|
42 p.didTimeout, |
|
43 True) |
|
44 p.kill() |
|
45 |
|
46 def test_poll_after_kill(self): |
|
47 """Process is killed, and poll() is called""" |
|
48 |
|
49 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
50 "process_normal_finish_python.ini"], |
|
51 cwd=here) |
|
52 p.run() |
|
53 returncode = p.kill() |
|
54 |
|
55 # We killed the process, so the returncode should be < 0 |
|
56 self.assertLess(returncode, 0) |
|
57 self.assertEqual(returncode, p.poll()) |
|
58 |
|
59 detected, output = proctest.check_for_process(self.proclaunch) |
|
60 self.determine_status(detected, |
|
61 output, |
|
62 returncode, |
|
63 p.didTimeout) |
|
64 |
|
65 def test_poll_after_kill_no_process_group(self): |
|
66 """Process (no group) is killed, and poll() is called""" |
|
67 |
|
68 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
69 "process_normal_finish_no_process_group.ini"], |
|
70 cwd=here, |
|
71 ignore_children=True |
|
72 ) |
|
73 p.run() |
|
74 returncode = p.kill() |
|
75 |
|
76 # We killed the process, so the returncode should be < 0 |
|
77 self.assertLess(returncode, 0) |
|
78 self.assertEqual(returncode, p.poll()) |
|
79 |
|
80 detected, output = proctest.check_for_process(self.proclaunch) |
|
81 self.determine_status(detected, |
|
82 output, |
|
83 returncode, |
|
84 p.didTimeout) |
|
85 |
|
86 def test_poll_after_double_kill(self): |
|
87 """Process is killed twice, and poll() is called""" |
|
88 |
|
89 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
90 "process_normal_finish_python.ini"], |
|
91 cwd=here) |
|
92 p.run() |
|
93 p.kill() |
|
94 returncode = p.kill() |
|
95 |
|
96 # We killed the process, so the returncode should be < 0 |
|
97 self.assertLess(returncode, 0) |
|
98 self.assertEqual(returncode, p.poll()) |
|
99 |
|
100 detected, output = proctest.check_for_process(self.proclaunch) |
|
101 self.determine_status(detected, |
|
102 output, |
|
103 returncode, |
|
104 p.didTimeout) |
|
105 |
|
106 def test_poll_after_external_kill(self): |
|
107 """Process is killed externally, and poll() is called""" |
|
108 |
|
109 p = processhandler.ProcessHandler([self.python, self.proclaunch, |
|
110 "process_normal_finish_python.ini"], |
|
111 cwd=here) |
|
112 p.run() |
|
113 os.kill(p.pid, signal.SIGTERM) |
|
114 returncode = p.wait() |
|
115 |
|
116 # We killed the process, so the returncode should be < 0 |
|
117 self.assertEqual(returncode, -signal.SIGTERM) |
|
118 self.assertEqual(returncode, p.poll()) |
|
119 |
|
120 detected, output = proctest.check_for_process(self.proclaunch) |
|
121 self.determine_status(detected, |
|
122 output, |
|
123 returncode, |
|
124 p.didTimeout) |
|
125 |
|
126 if __name__ == '__main__': |
|
127 unittest.main() |