3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
11
from time import sleep
13
from mozprocess import processhandler
15
here = os.path.dirname(os.path.abspath(__file__))
17
# This tests specifically the case reported in bug 671316
18
# TODO: Because of the way mutt works we can't just load a utils.py in here.
19
# so, for all process handler tests, copy these two
20
# utility functions to to the top of your source.
22
def make_proclaunch(aDir):
24
Makes the proclaunch executable.
26
aDir - the directory in which to issue the make commands
28
the path to the proclaunch executable that is generated
30
# Ideally make should take care of this, but since it doesn't - on windows,
31
# anyway, let's just call out both targets explicitly.
32
p = subprocess.call(["make", "-C", "iniparser"], cwd=aDir)
33
p = subprocess.call(["make"], cwd=aDir)
34
if sys.platform == "win32":
35
exepath = os.path.join(aDir, "proclaunch.exe")
37
exepath = os.path.join(aDir, "proclaunch")
40
def check_for_process(processName):
42
Use to determine if process is still running.
45
detected -- True if process is detected to exist, False otherwise
46
output -- if process exists, stdout of the process, '' otherwise
49
if sys.platform == "win32":
50
# On windows we use tasklist
51
p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE)
52
output = p1.communicate()[0]
54
for line in output.splitlines():
55
if processName in line:
59
p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE)
60
p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=subprocess.PIPE)
62
output = p2.communicate()[0]
64
for line in output.splitlines():
65
if "grep %s" % processName in line:
67
elif processName in line and not 'defunct' in line:
71
return detected, output
73
class ProcTest2(unittest.TestCase):
75
def __init__(self, *args, **kwargs):
77
# Ideally, I'd use setUpClass but that only exists in 2.7.
78
# So, we'll do this make step now.
79
self.proclaunch = make_proclaunch(here)
80
unittest.TestCase.__init__(self, *args, **kwargs)
82
def test_process_waitnotimeout(self):
83
""" Process is started, runs to completion before our wait times out
85
p = processhandler.ProcessHandler([self.proclaunch,
86
"process_waittimeout_10s.ini"],
89
p.processOutput(timeout=30)
92
detected, output = check_for_process(self.proclaunch)
93
self.determine_status(detected,
98
def test_process_wait(self):
99
""" Process is started runs to completion while we wait indefinitely
102
p = processhandler.ProcessHandler([self.proclaunch,
103
"process_waittimeout_10s.ini"],
108
detected, output = check_for_process(self.proclaunch)
109
self.determine_status(detected,
114
def test_process_waittimeout(self):
116
Process is started, then waitForFinish is called and times out.
117
Process is still running and didn't timeout
119
p = processhandler.ProcessHandler([self.proclaunch,
120
"process_waittimeout_10s.ini"],
125
p.waitForFinish(timeout=5)
127
detected, output = check_for_process(self.proclaunch)
128
self.determine_status(detected,
135
def determine_status(self,
143
Use to determine if the situation has failed.
145
detected -- value from check_for_process to determine if the process is detected
146
output -- string of data from detected process, can be ''
147
returncode -- return code from process, defaults to 0
148
didtimeout -- True if process timed out, defaults to False
149
isalive -- Use True to indicate we pass if the process exists; however, by default
150
the test will pass if the process does not exist (isalive == False)
151
expectedfail -- Defaults to [], used to indicate a list of fields that are expected to fail
153
if 'returncode' in expectedfail:
154
self.assertTrue(returncode, "Detected an unexpected return code of: %s" % returncode)
156
self.assertTrue(returncode == 0, "Detected non-zero return code of: %d" % returncode)
158
if 'didtimeout' in expectedfail:
159
self.assertTrue(didtimeout, "Detected that process didn't time out")
161
self.assertTrue(not didtimeout, "Detected that process timed out")
164
self.assertTrue(detected, "Detected process is not running, process output: %s" % output)
166
self.assertTrue(not detected, "Detected process is still running, process output: %s" % output)
168
if __name__ == '__main__':