testing/mozbase/mozrunner/tests/test_threads.py

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:34808db8cc4a
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/.
5
6 import threading
7 from time import sleep
8
9 import mozrunnertest
10
11
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
18
19 def run(self):
20 sleep(self.timeout)
21 if self.do_start:
22 self.runner.start()
23 else:
24 self.runner.stop()
25
26
27 class MozrunnerThreadsTestCase(mozrunnertest.MozrunnerTestCase):
28
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)
33
34 thread.start()
35 thread.join()
36
37 self.assertTrue(self.runner.is_running())
38
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()
46
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)
51
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)
56
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()
63
64 # Wait a bit to start the application gets started
65 self.runner.wait(2)
66 returncode = self.runner.stop()
67 thread.join()
68
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