| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import os |
| 4 |
import signal |
| 5 |
import sys |
| 6 |
|
| 7 |
import mozinfo |
| 8 |
import mozunit |
| 9 |
import proctest |
| 10 |
from mozprocess import processhandler |
| 11 |
|
| 12 |
here = os.path.dirname(os.path.abspath(__file__)) |
| 13 |
|
| 14 |
|
| 15 |
class ProcTestWait(proctest.ProcTest): |
| 16 |
"""Class to test process waits and timeouts""" |
| 17 |
|
| 18 |
def test_normal_finish(self): |
| 19 |
"""Process is started, runs to completion while we wait for it""" |
| 20 |
|
| 21 |
p = processhandler.ProcessHandler( |
| 22 |
[self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here |
| 23 |
) |
| 24 |
p.run() |
| 25 |
p.wait() |
| 26 |
|
| 27 |
self.determine_status(p) |
| 28 |
|
| 29 |
def test_wait(self): |
| 30 |
"""Process is started runs to completion while we wait indefinitely""" |
| 31 |
|
| 32 |
p = processhandler.ProcessHandler( |
| 33 |
[self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here |
| 34 |
) |
| 35 |
p.run() |
| 36 |
p.wait() |
| 37 |
|
| 38 |
self.determine_status(p) |
| 39 |
|
| 40 |
def test_timeout(self): |
| 41 |
"""Process is started, runs but we time out waiting on it |
| 42 |
to complete |
| 43 |
""" |
| 44 |
myenv = None |
| 45 |
# On macosx1014, subprocess fails to find `six` when run with python3. |
| 46 |
# This ensures that subprocess first looks to sys.path to find `six`. |
| 47 |
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1562083 |
| 48 |
if sys.platform == "darwin" and sys.version_info[0] > 2: |
| 49 |
myenv = os.environ.copy() |
| 50 |
myenv["PYTHONPATH"] = ":".join(sys.path) |
| 51 |
|
| 52 |
p = processhandler.ProcessHandler( |
| 53 |
[self.python, self.proclaunch, "process_waittimeout.ini"], |
| 54 |
cwd=here, |
| 55 |
env=myenv, |
| 56 |
) |
| 57 |
p.run(timeout=10) |
| 58 |
p.wait() |
| 59 |
|
| 60 |
if mozinfo.isLinux: |
| 61 |
# process was killed, so returncode should be negative |
| 62 |
self.assertLess(p.proc.returncode, 0) |
| 63 |
|
| 64 |
self.determine_status(p, False, ["returncode", "didtimeout"]) |
| 65 |
|
| 66 |
def test_waittimeout(self): |
| 67 |
""" |
| 68 |
Process is started, then wait is called and times out. |
| 69 |
Process is still running and didn't timeout |
| 70 |
""" |
| 71 |
p = processhandler.ProcessHandler( |
| 72 |
[self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here |
| 73 |
) |
| 74 |
|
| 75 |
p.run() |
| 76 |
p.wait(timeout=0) |
| 77 |
|
| 78 |
self.determine_status(p, True, ()) |
| 79 |
|
| 80 |
def test_waitnotimeout(self): |
| 81 |
"""Process is started, runs to completion before our wait times out""" |
| 82 |
p = processhandler.ProcessHandler( |
| 83 |
[self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here |
| 84 |
) |
| 85 |
p.run(timeout=30) |
| 86 |
p.wait() |
| 87 |
|
| 88 |
self.determine_status(p) |
| 89 |
|
| 90 |
def test_wait_twice_after_kill(self): |
| 91 |
"""Bug 968718: Process is started and stopped. wait() twice afterward.""" |
| 92 |
p = processhandler.ProcessHandler( |
| 93 |
[self.python, self.proclaunch, "process_waittimeout.ini"], cwd=here |
| 94 |
) |
| 95 |
p.run() |
| 96 |
p.kill() |
| 97 |
returncode1 = p.wait() |
| 98 |
returncode2 = p.wait() |
| 99 |
|
| 100 |
self.determine_status(p) |
| 101 |
|
| 102 |
# We killed the process, so the returncode should be non-zero |
| 103 |
if mozinfo.isWin: |
| 104 |
self.assertGreater( |
| 105 |
returncode2, 0, 'Positive returncode expected, got "%s"' % returncode2 |
| 106 |
) |
| 107 |
else: |
| 108 |
self.assertLess( |
| 109 |
returncode2, 0, 'Negative returncode expected, got "%s"' % returncode2 |
| 110 |
) |
| 111 |
self.assertEqual( |
| 112 |
returncode1, returncode2, "Expected both returncodes of wait() to be equal" |
| 113 |
) |
| 114 |
|
| 115 |
def test_wait_after_external_kill(self): |
| 116 |
"""Process is killed externally, and poll() is called.""" |
| 117 |
p = processhandler.ProcessHandler( |
| 118 |
[self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here |
| 119 |
) |
| 120 |
p.run() |
| 121 |
os.kill(p.pid, signal.SIGTERM) |
| 122 |
returncode = p.wait() |
| 123 |
|
| 124 |
# We killed the process, so the returncode should be non-zero |
| 125 |
if mozinfo.isWin: |
| 126 |
self.assertEqual( |
| 127 |
returncode, |
| 128 |
signal.SIGTERM, |
| 129 |
'Positive returncode expected, got "%s"' % returncode, |
| 130 |
) |
| 131 |
else: |
| 132 |
self.assertEqual( |
| 133 |
returncode, |
| 134 |
-signal.SIGTERM, |
| 135 |
'%s expected, got "%s"' % (-signal.SIGTERM, returncode), |
| 136 |
) |
| 137 |
|
| 138 |
self.assertEqual(returncode, p.poll()) |
| 139 |
|
| 140 |
self.determine_status(p) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
mozunit.main() |