michael@0: #!/usr/bin/env python michael@0: michael@0: import os michael@0: import unittest michael@0: import proctest michael@0: import mozinfo michael@0: from mozprocess import processhandler michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: class ProcTestWait(proctest.ProcTest): michael@0: """ Class to test process waits and timeouts """ michael@0: michael@0: def test_normal_finish(self): michael@0: """Process is started, runs to completion while we wait for it""" michael@0: michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, "process_normal_finish_python.ini"], michael@0: cwd=here) michael@0: p.run() michael@0: p.wait() michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: self.determine_status(detected, michael@0: output, michael@0: p.proc.returncode, michael@0: p.didTimeout) michael@0: michael@0: def test_wait(self): michael@0: """Process is started runs to completion while we wait indefinitely""" michael@0: michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, michael@0: "process_waittimeout_10s_python.ini"], michael@0: cwd=here) michael@0: p.run() michael@0: p.wait() michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: self.determine_status(detected, michael@0: output, michael@0: p.proc.returncode, michael@0: p.didTimeout) michael@0: michael@0: michael@0: def test_timeout(self): michael@0: """ Process is started, runs but we time out waiting on it michael@0: to complete michael@0: """ michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, "process_waittimeout_python.ini"], michael@0: cwd=here) michael@0: p.run(timeout=10) michael@0: p.wait() michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: michael@0: if mozinfo.isUnix: michael@0: # process was killed, so returncode should be negative michael@0: self.assertLess(p.proc.returncode, 0) michael@0: michael@0: self.determine_status(detected, michael@0: output, michael@0: p.proc.returncode, michael@0: p.didTimeout, michael@0: False, michael@0: ['returncode', 'didtimeout']) michael@0: michael@0: def test_waittimeout(self): michael@0: """ michael@0: Process is started, then wait is called and times out. michael@0: Process is still running and didn't timeout michael@0: """ michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, michael@0: "process_waittimeout_10s_python.ini"], michael@0: cwd=here) michael@0: michael@0: p.run() michael@0: p.wait(timeout=5) michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: self.determine_status(detected, michael@0: output, michael@0: p.proc.returncode, michael@0: p.didTimeout, michael@0: True, michael@0: ()) michael@0: michael@0: def test_waitnotimeout(self): michael@0: """ Process is started, runs to completion before our wait times out michael@0: """ michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, michael@0: "process_waittimeout_10s_python.ini"], michael@0: cwd=here) michael@0: p.run(timeout=30) michael@0: p.wait() michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: self.determine_status(detected, michael@0: output, michael@0: p.proc.returncode, michael@0: p.didTimeout) michael@0: michael@0: def test_wait_twice_after_kill(self): michael@0: """Bug 968718: Process is started and stopped. wait() twice afterward.""" michael@0: p = processhandler.ProcessHandler([self.python, self.proclaunch, michael@0: "process_waittimeout_python.ini"], michael@0: cwd=here) michael@0: p.run() michael@0: p.kill() michael@0: returncode1 = p.wait() michael@0: returncode2 = p.wait() michael@0: michael@0: detected, output = proctest.check_for_process(self.proclaunch) michael@0: self.determine_status(detected, michael@0: output, michael@0: returncode2, michael@0: p.didTimeout) michael@0: michael@0: self.assertLess(returncode2, 0, michael@0: 'Negative returncode expected, got "%s"' % returncode2) michael@0: self.assertEqual(returncode1, returncode2, michael@0: 'Expected both returncodes of wait() to be equal') michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()