testing/mozbase/mozrunner/tests/test_threads.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 #!/usr/bin/env python
     2 # This Source Code Form is subject to the terms of the Mozilla Public
     3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4 # You can obtain one at http://mozilla.org/MPL/2.0/.
     6 import threading
     7 from time import sleep
     9 import mozrunnertest
    12 class RunnerThread(threading.Thread):
    13     def __init__(self, runner, do_start, timeout=10):
    14         threading.Thread.__init__(self)
    15         self.runner = runner
    16         self.timeout = timeout
    17         self.do_start = do_start
    19     def run(self):
    20         sleep(self.timeout)
    21         if self.do_start:
    22             self.runner.start()
    23         else:
    24             self.runner.stop()
    27 class MozrunnerThreadsTestCase(mozrunnertest.MozrunnerTestCase):
    29     def test_process_start_via_thread(self):
    30         """Start the runner via a thread"""
    31         thread = RunnerThread(self.runner, True, 2)
    32         self.threads.append(thread)
    34         thread.start()
    35         thread.join()
    37         self.assertTrue(self.runner.is_running())
    39     def test_process_stop_via_multiple_threads(self):
    40         """Stop the runner via multiple threads"""
    41         self.runner.start()
    42         for i in range(5):
    43             thread = RunnerThread(self.runner, False, 5)
    44             self.threads.append(thread)
    45             thread.start()
    47         # Wait until the process has been stopped by another thread
    48         for thread in self.threads:
    49             thread.join()
    50         returncode = self.runner.wait(2)
    52         self.assertNotIn(returncode, [None, 0])
    53         self.assertEqual(self.runner.returncode, returncode)
    54         self.assertIsNotNone(self.runner.process_handler)
    55         self.assertEqual(self.runner.wait(10), returncode)
    57     def test_process_post_stop_via_thread(self):
    58         """Stop the runner and try it again with a thread a bit later"""
    59         self.runner.start()
    60         thread = RunnerThread(self.runner, False, 5)
    61         self.threads.append(thread)
    62         thread.start()
    64         # Wait a bit to start the application gets started
    65         self.runner.wait(2)
    66         returncode = self.runner.stop()
    67         thread.join()
    69         self.assertNotIn(returncode, [None, 0])
    70         self.assertEqual(self.runner.returncode, returncode)
    71         self.assertIsNotNone(self.runner.process_handler)
    72         self.assertEqual(self.runner.wait(10), returncode)

mercurial