testing/mozbase/mozrunner/tests/test_threads.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozrunner/tests/test_threads.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +#!/usr/bin/env python
     1.5 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 +# You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 +
     1.9 +import threading
    1.10 +from time import sleep
    1.11 +
    1.12 +import mozrunnertest
    1.13 +
    1.14 +
    1.15 +class RunnerThread(threading.Thread):
    1.16 +    def __init__(self, runner, do_start, timeout=10):
    1.17 +        threading.Thread.__init__(self)
    1.18 +        self.runner = runner
    1.19 +        self.timeout = timeout
    1.20 +        self.do_start = do_start
    1.21 +
    1.22 +    def run(self):
    1.23 +        sleep(self.timeout)
    1.24 +        if self.do_start:
    1.25 +            self.runner.start()
    1.26 +        else:
    1.27 +            self.runner.stop()
    1.28 +
    1.29 +
    1.30 +class MozrunnerThreadsTestCase(mozrunnertest.MozrunnerTestCase):
    1.31 +
    1.32 +    def test_process_start_via_thread(self):
    1.33 +        """Start the runner via a thread"""
    1.34 +        thread = RunnerThread(self.runner, True, 2)
    1.35 +        self.threads.append(thread)
    1.36 +
    1.37 +        thread.start()
    1.38 +        thread.join()
    1.39 +
    1.40 +        self.assertTrue(self.runner.is_running())
    1.41 +
    1.42 +    def test_process_stop_via_multiple_threads(self):
    1.43 +        """Stop the runner via multiple threads"""
    1.44 +        self.runner.start()
    1.45 +        for i in range(5):
    1.46 +            thread = RunnerThread(self.runner, False, 5)
    1.47 +            self.threads.append(thread)
    1.48 +            thread.start()
    1.49 +
    1.50 +        # Wait until the process has been stopped by another thread
    1.51 +        for thread in self.threads:
    1.52 +            thread.join()
    1.53 +        returncode = self.runner.wait(2)
    1.54 +
    1.55 +        self.assertNotIn(returncode, [None, 0])
    1.56 +        self.assertEqual(self.runner.returncode, returncode)
    1.57 +        self.assertIsNotNone(self.runner.process_handler)
    1.58 +        self.assertEqual(self.runner.wait(10), returncode)
    1.59 +
    1.60 +    def test_process_post_stop_via_thread(self):
    1.61 +        """Stop the runner and try it again with a thread a bit later"""
    1.62 +        self.runner.start()
    1.63 +        thread = RunnerThread(self.runner, False, 5)
    1.64 +        self.threads.append(thread)
    1.65 +        thread.start()
    1.66 +
    1.67 +        # Wait a bit to start the application gets started
    1.68 +        self.runner.wait(2)
    1.69 +        returncode = self.runner.stop()
    1.70 +        thread.join()
    1.71 +
    1.72 +        self.assertNotIn(returncode, [None, 0])
    1.73 +        self.assertEqual(self.runner.returncode, returncode)
    1.74 +        self.assertIsNotNone(self.runner.process_handler)
    1.75 +        self.assertEqual(self.runner.wait(10), returncode)

mercurial