tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_wait.py (3966B)


      1 #!/usr/bin/env python
      2 
      3 import os
      4 import signal
      5 
      6 import mozinfo
      7 import mozunit
      8 import proctest
      9 from mozprocess import processhandler
     10 
     11 here = os.path.dirname(os.path.abspath(__file__))
     12 
     13 
     14 class ProcTestWait(proctest.ProcTest):
     15    """Class to test process waits and timeouts"""
     16 
     17    def test_normal_finish(self):
     18        """Process is started, runs to completion while we wait for it"""
     19 
     20        p = processhandler.ProcessHandler(
     21            [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here
     22        )
     23        p.run()
     24        p.wait()
     25 
     26        self.determine_status(p)
     27 
     28    def test_wait(self):
     29        """Process is started runs to completion while we wait indefinitely"""
     30 
     31        p = processhandler.ProcessHandler(
     32            [self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here
     33        )
     34        p.run()
     35        p.wait()
     36 
     37        self.determine_status(p)
     38 
     39    def test_timeout(self):
     40        """Process is started, runs but we time out waiting on it
     41        to complete
     42        """
     43 
     44        p = processhandler.ProcessHandler(
     45            [self.python, self.proclaunch, "process_waittimeout.ini"],
     46            cwd=here,
     47        )
     48        p.run(timeout=10)
     49        p.wait()
     50 
     51        if mozinfo.isLinux:
     52            # process was killed, so returncode should be negative
     53            self.assertLess(p.proc.returncode, 0)
     54 
     55        self.determine_status(p, False, ["returncode", "didtimeout"])
     56 
     57    def test_waittimeout(self):
     58        """
     59        Process is started, then wait is called and times out.
     60        Process is still running and didn't timeout
     61        """
     62        p = processhandler.ProcessHandler(
     63            [self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here
     64        )
     65 
     66        p.run()
     67        p.wait(timeout=0)
     68 
     69        self.determine_status(p, True, ())
     70 
     71    def test_waitnotimeout(self):
     72        """Process is started, runs to completion before our wait times out"""
     73        p = processhandler.ProcessHandler(
     74            [self.python, self.proclaunch, "process_waittimeout_10s.ini"], cwd=here
     75        )
     76        p.run(timeout=30)
     77        p.wait()
     78 
     79        self.determine_status(p)
     80 
     81    def test_wait_twice_after_kill(self):
     82        """Bug 968718: Process is started and stopped. wait() twice afterward."""
     83        p = processhandler.ProcessHandler(
     84            [self.python, self.proclaunch, "process_waittimeout.ini"], cwd=here
     85        )
     86        p.run()
     87        p.kill()
     88        returncode1 = p.wait()
     89        returncode2 = p.wait()
     90 
     91        self.determine_status(p)
     92 
     93        # We killed the process, so the returncode should be non-zero
     94        if mozinfo.isWin:
     95            self.assertGreater(
     96                returncode2, 0, 'Positive returncode expected, got "%s"' % returncode2
     97            )
     98        else:
     99            self.assertLess(
    100                returncode2, 0, 'Negative returncode expected, got "%s"' % returncode2
    101            )
    102        self.assertEqual(
    103            returncode1, returncode2, "Expected both returncodes of wait() to be equal"
    104        )
    105 
    106    def test_wait_after_external_kill(self):
    107        """Process is killed externally, and poll() is called."""
    108        p = processhandler.ProcessHandler(
    109            [self.python, self.proclaunch, "process_normal_finish.ini"], cwd=here
    110        )
    111        p.run()
    112        os.kill(p.pid, signal.SIGTERM)
    113        returncode = p.wait()
    114 
    115        # We killed the process, so the returncode should be non-zero
    116        if mozinfo.isWin:
    117            self.assertEqual(
    118                returncode,
    119                signal.SIGTERM,
    120                'Positive returncode expected, got "%s"' % returncode,
    121            )
    122        else:
    123            self.assertEqual(
    124                returncode,
    125                -signal.SIGTERM,
    126                '%s expected, got "%s"' % (-signal.SIGTERM, returncode),
    127            )
    128 
    129        self.assertEqual(returncode, p.poll())
    130 
    131        self.determine_status(p)
    132 
    133 
    134 if __name__ == "__main__":
    135    mozunit.main()