1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozprocess/tests/test_mozprocess_poll.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,127 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +import os 1.7 +import signal 1.8 +import unittest 1.9 + 1.10 +import mozinfo 1.11 +from mozprocess import processhandler 1.12 + 1.13 +import proctest 1.14 + 1.15 + 1.16 +here = os.path.dirname(os.path.abspath(__file__)) 1.17 + 1.18 + 1.19 +class ProcTestPoll(proctest.ProcTest): 1.20 + """ Class to test process poll """ 1.21 + 1.22 + def test_poll_before_run(self): 1.23 + """Process is not started, and poll() is called""" 1.24 + 1.25 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.26 + "process_normal_finish_python.ini"], 1.27 + cwd=here) 1.28 + self.assertRaises(AttributeError, p.poll) 1.29 + 1.30 + def test_poll_while_running(self): 1.31 + """Process is started, and poll() is called""" 1.32 + 1.33 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.34 + "process_normal_finish_python.ini"], 1.35 + cwd=here) 1.36 + p.run() 1.37 + returncode = p.poll() 1.38 + 1.39 + self.assertEqual(returncode, None) 1.40 + 1.41 + detected, output = proctest.check_for_process(self.proclaunch) 1.42 + self.determine_status(detected, 1.43 + output, 1.44 + returncode, 1.45 + p.didTimeout, 1.46 + True) 1.47 + p.kill() 1.48 + 1.49 + def test_poll_after_kill(self): 1.50 + """Process is killed, and poll() is called""" 1.51 + 1.52 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.53 + "process_normal_finish_python.ini"], 1.54 + cwd=here) 1.55 + p.run() 1.56 + returncode = p.kill() 1.57 + 1.58 + # We killed the process, so the returncode should be < 0 1.59 + self.assertLess(returncode, 0) 1.60 + self.assertEqual(returncode, p.poll()) 1.61 + 1.62 + detected, output = proctest.check_for_process(self.proclaunch) 1.63 + self.determine_status(detected, 1.64 + output, 1.65 + returncode, 1.66 + p.didTimeout) 1.67 + 1.68 + def test_poll_after_kill_no_process_group(self): 1.69 + """Process (no group) is killed, and poll() is called""" 1.70 + 1.71 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.72 + "process_normal_finish_no_process_group.ini"], 1.73 + cwd=here, 1.74 + ignore_children=True 1.75 + ) 1.76 + p.run() 1.77 + returncode = p.kill() 1.78 + 1.79 + # We killed the process, so the returncode should be < 0 1.80 + self.assertLess(returncode, 0) 1.81 + self.assertEqual(returncode, p.poll()) 1.82 + 1.83 + detected, output = proctest.check_for_process(self.proclaunch) 1.84 + self.determine_status(detected, 1.85 + output, 1.86 + returncode, 1.87 + p.didTimeout) 1.88 + 1.89 + def test_poll_after_double_kill(self): 1.90 + """Process is killed twice, and poll() is called""" 1.91 + 1.92 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.93 + "process_normal_finish_python.ini"], 1.94 + cwd=here) 1.95 + p.run() 1.96 + p.kill() 1.97 + returncode = p.kill() 1.98 + 1.99 + # We killed the process, so the returncode should be < 0 1.100 + self.assertLess(returncode, 0) 1.101 + self.assertEqual(returncode, p.poll()) 1.102 + 1.103 + detected, output = proctest.check_for_process(self.proclaunch) 1.104 + self.determine_status(detected, 1.105 + output, 1.106 + returncode, 1.107 + p.didTimeout) 1.108 + 1.109 + def test_poll_after_external_kill(self): 1.110 + """Process is killed externally, and poll() is called""" 1.111 + 1.112 + p = processhandler.ProcessHandler([self.python, self.proclaunch, 1.113 + "process_normal_finish_python.ini"], 1.114 + cwd=here) 1.115 + p.run() 1.116 + os.kill(p.pid, signal.SIGTERM) 1.117 + returncode = p.wait() 1.118 + 1.119 + # We killed the process, so the returncode should be < 0 1.120 + self.assertEqual(returncode, -signal.SIGTERM) 1.121 + self.assertEqual(returncode, p.poll()) 1.122 + 1.123 + detected, output = proctest.check_for_process(self.proclaunch) 1.124 + self.determine_status(detected, 1.125 + output, 1.126 + returncode, 1.127 + p.didTimeout) 1.128 + 1.129 +if __name__ == '__main__': 1.130 + unittest.main()