testing/mozbase/mozrunner/tests/test_threads.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial