OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Unittests for test_dispatcher.py.""" | 5 """Unittests for test_dispatcher.py.""" |
6 # pylint: disable=R0201 | 6 # pylint: disable=R0201 |
7 # pylint: disable=W0212 | 7 # pylint: disable=W0212 |
8 | 8 |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import unittest | 11 import unittest |
12 | 12 |
13 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | 13 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), |
14 os.pardir, os.pardir)) | 14 os.pardir, os.pardir)) |
15 | 15 |
16 # Mock out android_commands.GetAttachedDevices(). | 16 # Mock out android_commands.GetAttachedDevices(). |
17 from pylib import android_commands | 17 from pylib import android_commands |
18 android_commands.GetAttachedDevices = lambda: ['0', '1'] | 18 android_commands.GetAttachedDevices = lambda: ['0', '1'] |
19 from pylib import constants | 19 from pylib import constants |
20 from pylib.base import base_test_result | 20 from pylib.base import base_test_result |
21 from pylib.base import test_dispatcher | 21 from pylib.base import test_dispatcher |
22 from pylib.utils import watchdog_timer | 22 from pylib.utils import watchdog_timer |
23 | 23 |
24 | 24 |
25 | |
26 class TestException(Exception): | 25 class TestException(Exception): |
27 pass | 26 pass |
28 | 27 |
29 | 28 |
30 class MockRunner(object): | 29 class MockRunner(object): |
31 """A mock TestRunner.""" | 30 """A mock TestRunner.""" |
32 def __init__(self, device='0', shard_index=0): | 31 def __init__(self, device='0', shard_index=0): |
33 self.device_serial = device | 32 self.device_serial = device |
34 self.shard_index = shard_index | 33 self.shard_index = shard_index |
35 self.setups = 0 | 34 self.setups = 0 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 counter = test_dispatcher._ThreadSafeCounter() | 112 counter = test_dispatcher._ThreadSafeCounter() |
114 test_dispatcher._SetUp(MockRunner, '0', runners, counter) | 113 test_dispatcher._SetUp(MockRunner, '0', runners, counter) |
115 self.assertEqual(len(runners), 1) | 114 self.assertEqual(len(runners), 1) |
116 self.assertEqual(runners[0].setups, 1) | 115 self.assertEqual(runners[0].setups, 1) |
117 | 116 |
118 def testThreadSafeCounter(self): | 117 def testThreadSafeCounter(self): |
119 counter = test_dispatcher._ThreadSafeCounter() | 118 counter = test_dispatcher._ThreadSafeCounter() |
120 for i in xrange(5): | 119 for i in xrange(5): |
121 self.assertEqual(counter.GetAndIncrement(), i) | 120 self.assertEqual(counter.GetAndIncrement(), i) |
122 | 121 |
| 122 def testApplyMaxPerRun(self): |
| 123 self.assertEqual( |
| 124 ['A:B', 'C:D', 'E', 'F:G', 'H:I'], |
| 125 test_dispatcher.ApplyMaxPerRun(['A:B', 'C:D:E', 'F:G:H:I'], 2)) |
| 126 |
123 | 127 |
124 class TestThreadGroupFunctions(unittest.TestCase): | 128 class TestThreadGroupFunctions(unittest.TestCase): |
125 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" | 129 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" |
126 def setUp(self): | 130 def setUp(self): |
127 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | 131 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
128 shared_test_collection = test_dispatcher._TestCollection( | 132 shared_test_collection = test_dispatcher._TestCollection( |
129 [test_dispatcher._Test(t) for t in self.tests]) | 133 [test_dispatcher._Test(t) for t in self.tests]) |
130 self.test_collection_factory = lambda: shared_test_collection | 134 self.test_collection_factory = lambda: shared_test_collection |
131 | 135 |
132 def testCreate(self): | 136 def testCreate(self): |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 self.assertEqual(len(results.GetPass()), 0) | 185 self.assertEqual(len(results.GetPass()), 0) |
182 self.assertEqual(len(results.GetFail()), 3) | 186 self.assertEqual(len(results.GetFail()), 3) |
183 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 187 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
184 | 188 |
185 def testNoTests(self): | 189 def testNoTests(self): |
186 results, exit_code = test_dispatcher.RunTests( | 190 results, exit_code = test_dispatcher.RunTests( |
187 [], MockRunner, ['0', '1'], shard=True) | 191 [], MockRunner, ['0', '1'], shard=True) |
188 self.assertEqual(len(results.GetAll()), 0) | 192 self.assertEqual(len(results.GetAll()), 0) |
189 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 193 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
190 | 194 |
191 def testTestsRemainWithAllDevicesOffline(self): | |
192 attached_devices = android_commands.GetAttachedDevices | |
193 android_commands.GetAttachedDevices = lambda: [] | |
194 try: | |
195 with self.assertRaises(AssertionError): | |
196 _results, _exit_code = TestShard._RunShard(MockRunner) | |
197 finally: | |
198 android_commands.GetAttachedDevices = attached_devices | |
199 | |
200 | 195 |
201 class TestReplicate(unittest.TestCase): | 196 class TestReplicate(unittest.TestCase): |
202 """Tests test_dispatcher.RunTests with replication.""" | 197 """Tests test_dispatcher.RunTests with replication.""" |
203 @staticmethod | 198 @staticmethod |
204 def _RunReplicate(runner_factory): | 199 def _RunReplicate(runner_factory): |
205 return test_dispatcher.RunTests( | 200 return test_dispatcher.RunTests( |
206 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=False) | 201 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=False) |
207 | 202 |
208 def testReplicate(self): | 203 def testReplicate(self): |
209 results, exit_code = TestReplicate._RunReplicate(MockRunner) | 204 results, exit_code = TestReplicate._RunReplicate(MockRunner) |
210 # We expect 6 results since each test should have been run on every device | 205 # We expect 6 results since each test should have been run on every device |
211 self.assertEqual(len(results.GetPass()), 6) | 206 self.assertEqual(len(results.GetPass()), 6) |
212 self.assertEqual(exit_code, 0) | 207 self.assertEqual(exit_code, 0) |
213 | 208 |
214 def testFailing(self): | 209 def testFailing(self): |
215 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) | 210 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) |
216 self.assertEqual(len(results.GetPass()), 0) | 211 self.assertEqual(len(results.GetPass()), 0) |
217 self.assertEqual(len(results.GetFail()), 6) | 212 self.assertEqual(len(results.GetFail()), 6) |
218 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 213 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
219 | 214 |
220 def testNoTests(self): | 215 def testNoTests(self): |
221 results, exit_code = test_dispatcher.RunTests( | 216 results, exit_code = test_dispatcher.RunTests( |
222 [], MockRunner, ['0', '1'], shard=False) | 217 [], MockRunner, ['0', '1'], shard=False) |
223 self.assertEqual(len(results.GetAll()), 0) | 218 self.assertEqual(len(results.GetAll()), 0) |
224 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 219 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
225 | 220 |
226 | 221 |
227 if __name__ == '__main__': | 222 if __name__ == '__main__': |
228 unittest.main() | 223 unittest.main() |
OLD | NEW |