| OLD | NEW | 
| (Empty) |  | 
 |    1 #!/usr/bin/env python | 
 |    2  | 
 |    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/. | 
 |    6  | 
 |    7 import os | 
 |    8 import subprocess | 
 |    9 import sys | 
 |   10 import unittest | 
 |   11 from time import sleep | 
 |   12  | 
 |   13 from mozprocess import processhandler | 
 |   14  | 
 |   15 here = os.path.dirname(os.path.abspath(__file__)) | 
 |   16  | 
 |   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. | 
 |   21  | 
 |   22 def make_proclaunch(aDir): | 
 |   23     """ | 
 |   24         Makes the proclaunch executable. | 
 |   25         Params: | 
 |   26             aDir - the directory in which to issue the make commands | 
 |   27         Returns: | 
 |   28             the path to the proclaunch executable that is generated | 
 |   29     """ | 
 |   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"],stdout=subprocess.PIPE, stde
     rr=subprocess.PIPE, cwd=aDir) | 
 |   33     p = subprocess.call(["make"],stdout=subprocess.PIPE, stderr=subprocess.PIPE,
      cwd=aDir) | 
 |   34     if sys.platform == "win32": | 
 |   35         exepath = os.path.join(aDir, "proclaunch.exe") | 
 |   36     else: | 
 |   37         exepath = os.path.join(aDir, "proclaunch") | 
 |   38     return exepath | 
 |   39  | 
 |   40 def check_for_process(processName): | 
 |   41     """ | 
 |   42         Use to determine if process is still running. | 
 |   43  | 
 |   44         Returns: | 
 |   45         detected -- True if process is detected to exist, False otherwise | 
 |   46         output -- if process exists, stdout of the process, '' otherwise | 
 |   47     """ | 
 |   48     output = '' | 
 |   49     if sys.platform == "win32": | 
 |   50         # On windows we use tasklist | 
 |   51         p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE) | 
 |   52         output = p1.communicate()[0] | 
 |   53         detected = False | 
 |   54         for line in output.splitlines(): | 
 |   55             if processName in line: | 
 |   56                 detected = True | 
 |   57                 break | 
 |   58     else: | 
 |   59         p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE) | 
 |   60         p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=sub
     process.PIPE) | 
 |   61         p1.stdout.close() | 
 |   62         output = p2.communicate()[0] | 
 |   63         detected = False | 
 |   64         for line in output.splitlines(): | 
 |   65             if "grep %s" % processName in line: | 
 |   66                 continue | 
 |   67             elif processName in line and not 'defunct' in line: | 
 |   68                 detected = True | 
 |   69                 break | 
 |   70  | 
 |   71     return detected, output | 
 |   72  | 
 |   73 class ProcTest2(unittest.TestCase): | 
 |   74  | 
 |   75     def __init__(self, *args, **kwargs): | 
 |   76  | 
 |   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) | 
 |   81  | 
 |   82     def test_process_waitnotimeout(self): | 
 |   83         """ Process is started, runs to completion before our wait times out | 
 |   84         """ | 
 |   85         p = processhandler.ProcessHandler([self.proclaunch, | 
 |   86                                           "process_waittimeout_10s.ini"], | 
 |   87                                           cwd=here) | 
 |   88         p.run(timeout=30) | 
 |   89         p.wait() | 
 |   90  | 
 |   91         detected, output = check_for_process(self.proclaunch) | 
 |   92         self.determine_status(detected, | 
 |   93                               output, | 
 |   94                               p.proc.returncode, | 
 |   95                               p.didTimeout) | 
 |   96  | 
 |   97     def test_process_wait(self): | 
 |   98         """ Process is started runs to completion while we wait indefinitely | 
 |   99         """ | 
 |  100  | 
 |  101         p = processhandler.ProcessHandler([self.proclaunch, | 
 |  102                                           "process_waittimeout_10s.ini"], | 
 |  103                                           cwd=here) | 
 |  104         p.run() | 
 |  105         p.wait() | 
 |  106  | 
 |  107         detected, output = check_for_process(self.proclaunch) | 
 |  108         self.determine_status(detected, | 
 |  109                               output, | 
 |  110                               p.proc.returncode, | 
 |  111                               p.didTimeout) | 
 |  112  | 
 |  113     def test_process_waittimeout(self): | 
 |  114         """ | 
 |  115         Process is started, then wait is called and times out. | 
 |  116         Process is still running and didn't timeout | 
 |  117         """ | 
 |  118         p = processhandler.ProcessHandler([self.proclaunch, | 
 |  119                                           "process_waittimeout_10s.ini"], | 
 |  120                                           cwd=here) | 
 |  121  | 
 |  122         p.run() | 
 |  123         p.wait(timeout=5) | 
 |  124  | 
 |  125         detected, output = check_for_process(self.proclaunch) | 
 |  126         self.determine_status(detected, | 
 |  127                               output, | 
 |  128                               p.proc.returncode, | 
 |  129                               p.didTimeout, | 
 |  130                               True, | 
 |  131                               []) | 
 |  132  | 
 |  133     def test_process_output_twice(self): | 
 |  134         """ | 
 |  135         Process is started, then processOutput is called a second time explicitl
     y | 
 |  136         """ | 
 |  137         p = processhandler.ProcessHandler([self.proclaunch, | 
 |  138                                           "process_waittimeout_10s.ini"], | 
 |  139                                           cwd=here) | 
 |  140  | 
 |  141         p.run() | 
 |  142         p.processOutput(timeout=5) | 
 |  143         p.wait() | 
 |  144  | 
 |  145         detected, output = check_for_process(self.proclaunch) | 
 |  146         self.determine_status(detected, | 
 |  147                               output, | 
 |  148                               p.proc.returncode, | 
 |  149                               p.didTimeout, | 
 |  150                               False, | 
 |  151                               []) | 
 |  152  | 
 |  153  | 
 |  154     def determine_status(self, | 
 |  155                          detected=False, | 
 |  156                          output = '', | 
 |  157                          returncode = 0, | 
 |  158                          didtimeout = False, | 
 |  159                          isalive=False, | 
 |  160                          expectedfail=[]): | 
 |  161         """ | 
 |  162         Use to determine if the situation has failed. | 
 |  163         Parameters: | 
 |  164             detected -- value from check_for_process to determine if the process
      is detected | 
 |  165             output -- string of data from detected process, can be '' | 
 |  166             returncode -- return code from process, defaults to 0 | 
 |  167             didtimeout -- True if process timed out, defaults to False | 
 |  168             isalive -- Use True to indicate we pass if the process exists; howev
     er, by default | 
 |  169                        the test will pass if the process does not exist (isalive
      == False) | 
 |  170             expectedfail -- Defaults to [], used to indicate a list of fields th
     at are expected to fail | 
 |  171         """ | 
 |  172         if 'returncode' in expectedfail: | 
 |  173             self.assertTrue(returncode, "Detected an unexpected return code of: 
     %s" % returncode) | 
 |  174         elif not isalive: | 
 |  175             self.assertTrue(returncode == 0, "Detected non-zero return code of: 
     %d" % returncode) | 
 |  176  | 
 |  177         if 'didtimeout' in expectedfail: | 
 |  178             self.assertTrue(didtimeout, "Detected that process didn't time out") | 
 |  179         else: | 
 |  180             self.assertTrue(not didtimeout, "Detected that process timed out") | 
 |  181  | 
 |  182         if isalive: | 
 |  183             self.assertTrue(detected, "Detected process is not running, process 
     output: %s" % output) | 
 |  184         else: | 
 |  185             self.assertTrue(not detected, "Detected process is still running, pr
     ocess output: %s" % output) | 
 |  186  | 
 |  187 if __name__ == '__main__': | 
 |  188     unittest.main() | 
| OLD | NEW |