michael@0: #!/usr/bin/env python michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import threading michael@0: from time import sleep michael@0: michael@0: import mozrunnertest michael@0: michael@0: michael@0: class RunnerThread(threading.Thread): michael@0: def __init__(self, runner, do_start, timeout=10): michael@0: threading.Thread.__init__(self) michael@0: self.runner = runner michael@0: self.timeout = timeout michael@0: self.do_start = do_start michael@0: michael@0: def run(self): michael@0: sleep(self.timeout) michael@0: if self.do_start: michael@0: self.runner.start() michael@0: else: michael@0: self.runner.stop() michael@0: michael@0: michael@0: class MozrunnerThreadsTestCase(mozrunnertest.MozrunnerTestCase): michael@0: michael@0: def test_process_start_via_thread(self): michael@0: """Start the runner via a thread""" michael@0: thread = RunnerThread(self.runner, True, 2) michael@0: self.threads.append(thread) michael@0: michael@0: thread.start() michael@0: thread.join() michael@0: michael@0: self.assertTrue(self.runner.is_running()) michael@0: michael@0: def test_process_stop_via_multiple_threads(self): michael@0: """Stop the runner via multiple threads""" michael@0: self.runner.start() michael@0: for i in range(5): michael@0: thread = RunnerThread(self.runner, False, 5) michael@0: self.threads.append(thread) michael@0: thread.start() michael@0: michael@0: # Wait until the process has been stopped by another thread michael@0: for thread in self.threads: michael@0: thread.join() michael@0: returncode = self.runner.wait(2) michael@0: michael@0: self.assertNotIn(returncode, [None, 0]) michael@0: self.assertEqual(self.runner.returncode, returncode) michael@0: self.assertIsNotNone(self.runner.process_handler) michael@0: self.assertEqual(self.runner.wait(10), returncode) michael@0: michael@0: def test_process_post_stop_via_thread(self): michael@0: """Stop the runner and try it again with a thread a bit later""" michael@0: self.runner.start() michael@0: thread = RunnerThread(self.runner, False, 5) michael@0: self.threads.append(thread) michael@0: thread.start() michael@0: michael@0: # Wait a bit to start the application gets started michael@0: self.runner.wait(2) michael@0: returncode = self.runner.stop() michael@0: thread.join() michael@0: michael@0: self.assertNotIn(returncode, [None, 0]) michael@0: self.assertEqual(self.runner.returncode, returncode) michael@0: self.assertIsNotNone(self.runner.process_handler) michael@0: self.assertEqual(self.runner.wait(10), returncode)