test_poll.py (4557B)
1 #!/usr/bin/env python 2 3 import os 4 import signal 5 import sys 6 import time 7 import unittest 8 9 import mozinfo 10 import mozunit 11 import proctest 12 from mozprocess import processhandler 13 14 here = os.path.dirname(os.path.abspath(__file__)) 15 16 17 class ProcTestPoll(proctest.ProcTest): 18 """Class to test process poll.""" 19 20 def test_poll_before_run(self): 21 """Process is not started, and poll() is called.""" 22 p = processhandler.ProcessHandler( 23 [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here 24 ) 25 self.assertRaises(RuntimeError, p.poll) 26 27 def test_poll_while_running(self): 28 """Process is started, and poll() is called.""" 29 p = processhandler.ProcessHandler( 30 [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here 31 ) 32 p.run() 33 returncode = p.poll() 34 35 self.assertEqual(returncode, None) 36 37 self.determine_status(p, True) 38 p.kill() 39 40 def test_poll_after_kill(self): 41 """Process is killed, and poll() is called.""" 42 p = processhandler.ProcessHandler( 43 [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here 44 ) 45 p.run() 46 returncode = p.kill() 47 48 # We killed the process, so the returncode should be non-zero 49 if mozinfo.isWin: 50 self.assertGreater( 51 returncode, 0, 'Positive returncode expected, got "%s"' % returncode 52 ) 53 else: 54 self.assertLess( 55 returncode, 0, 'Negative returncode expected, got "%s"' % returncode 56 ) 57 58 self.assertEqual(returncode, p.poll()) 59 60 self.determine_status(p) 61 62 def test_poll_after_kill_no_process_group(self): 63 """Process (no group) is killed, and poll() is called.""" 64 p = processhandler.ProcessHandler( 65 [ 66 self.python, 67 self.proclaunch, 68 "process_normal_finish_no_process_group.ini", 69 ], 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 non-zero 77 if mozinfo.isWin: 78 self.assertGreater( 79 returncode, 0, 'Positive returncode expected, got "%s"' % returncode 80 ) 81 else: 82 self.assertLess( 83 returncode, 0, 'Negative returncode expected, got "%s"' % returncode 84 ) 85 86 self.assertEqual(returncode, p.poll()) 87 88 self.determine_status(p) 89 90 def test_poll_after_double_kill(self): 91 """Process is killed twice, and poll() is called.""" 92 p = processhandler.ProcessHandler( 93 [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here 94 ) 95 p.run() 96 p.kill() 97 returncode = p.kill() 98 99 # We killed the process, so the returncode should be non-zero 100 if mozinfo.isWin: 101 self.assertGreater( 102 returncode, 0, 'Positive returncode expected, got "%s"' % returncode 103 ) 104 else: 105 self.assertLess( 106 returncode, 0, 'Negative returncode expected, got "%s"' % returncode 107 ) 108 109 self.assertEqual(returncode, p.poll()) 110 111 self.determine_status(p) 112 113 @unittest.skipIf(sys.platform.startswith("win"), "Bug 1493796") 114 def test_poll_after_external_kill(self): 115 """Process is killed externally, and poll() is called.""" 116 p = processhandler.ProcessHandler( 117 [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here 118 ) 119 p.run() 120 121 os.kill(p.pid, signal.SIGTERM) 122 123 # Allow the output reader thread to finish processing remaining data 124 for i in range(0, 100): 125 time.sleep(processhandler.INTERVAL_PROCESS_ALIVE_CHECK) 126 returncode = p.poll() 127 if returncode is not None: 128 break 129 130 # We killed the process, so the returncode should be non-zero 131 if mozinfo.isWin: 132 self.assertEqual( 133 returncode, 134 signal.SIGTERM, 135 'Positive returncode expected, got "%s"' % returncode, 136 ) 137 else: 138 self.assertEqual( 139 returncode, 140 -signal.SIGTERM, 141 '%s expected, got "%s"' % (-signal.SIGTERM, returncode), 142 ) 143 144 self.assertEqual(returncode, p.wait()) 145 146 self.determine_status(p) 147 148 149 if __name__ == "__main__": 150 mozunit.main()