OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 shard.py.""" | 5 """Unittests for test_dispatcher.py.""" |
6 | 6 |
7 import os | 7 import os |
8 import sys | 8 import sys |
9 import unittest | 9 import unittest |
10 | 10 |
11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | 11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), |
12 os.pardir, os.pardir)) | 12 os.pardir, os.pardir)) |
13 | 13 |
14 # Mock out android_commands.GetAttachedDevices(). | 14 # Mock out android_commands.GetAttachedDevices(). |
15 from pylib import android_commands | 15 from pylib import android_commands |
16 android_commands.GetAttachedDevices = lambda: ['0', '1'] | 16 android_commands.GetAttachedDevices = lambda: ['0', '1'] |
17 from pylib import constants | 17 from pylib import constants |
18 from pylib.utils import watchdog_timer | 18 from pylib.utils import watchdog_timer |
19 | 19 |
20 import base_test_result | 20 import base_test_result |
21 import shard | 21 import test_dispatcher |
22 | 22 |
23 | 23 |
24 class TestException(Exception): | 24 class TestException(Exception): |
25 pass | 25 pass |
26 | 26 |
27 | 27 |
28 class MockRunner(object): | 28 class MockRunner(object): |
29 """A mock TestRunner.""" | 29 """A mock TestRunner.""" |
30 def __init__(self, device='0', shard_index=0): | 30 def __init__(self, device='0', shard_index=0): |
31 self.device = device | 31 self.device = device |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 test, base_test_result.ResultType.PASS)) | 71 test, base_test_result.ResultType.PASS)) |
72 return (results, None) | 72 return (results, None) |
73 | 73 |
74 | 74 |
75 class MockRunnerException(MockRunner): | 75 class MockRunnerException(MockRunner): |
76 def RunTest(self, test): | 76 def RunTest(self, test): |
77 raise TestException | 77 raise TestException |
78 | 78 |
79 | 79 |
80 class TestFunctions(unittest.TestCase): | 80 class TestFunctions(unittest.TestCase): |
81 """Tests for shard._RunTestsFromQueue.""" | 81 """Tests test_dispatcher._RunTestsFromQueue.""" |
82 @staticmethod | 82 @staticmethod |
83 def _RunTests(mock_runner, tests): | 83 def _RunTests(mock_runner, tests): |
84 results = [] | 84 results = [] |
85 tests = shard._TestCollection([shard._Test(t) for t in tests]) | 85 tests = test_dispatcher._TestCollection( |
86 shard._RunTestsFromQueue(mock_runner, tests, results, | 86 [test_dispatcher._Test(t) for t in tests]) |
87 watchdog_timer.WatchdogTimer(None), 2) | 87 test_dispatcher._RunTestsFromQueue(mock_runner, tests, results, |
| 88 watchdog_timer.WatchdogTimer(None), 2) |
88 run_results = base_test_result.TestRunResults() | 89 run_results = base_test_result.TestRunResults() |
89 for r in results: | 90 for r in results: |
90 run_results.AddTestRunResults(r) | 91 run_results.AddTestRunResults(r) |
91 return run_results | 92 return run_results |
92 | 93 |
93 def testRunTestsFromQueue(self): | 94 def testRunTestsFromQueue(self): |
94 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) | 95 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) |
95 self.assertEqual(len(results.GetPass()), 2) | 96 self.assertEqual(len(results.GetPass()), 2) |
96 self.assertEqual(len(results.GetNotPass()), 0) | 97 self.assertEqual(len(results.GetNotPass()), 0) |
97 | 98 |
98 def testRunTestsFromQueueRetry(self): | 99 def testRunTestsFromQueueRetry(self): |
99 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) | 100 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) |
100 self.assertEqual(len(results.GetPass()), 0) | 101 self.assertEqual(len(results.GetPass()), 0) |
101 self.assertEqual(len(results.GetFail()), 2) | 102 self.assertEqual(len(results.GetFail()), 2) |
102 | 103 |
103 def testRunTestsFromQueueFailTwice(self): | 104 def testRunTestsFromQueueFailTwice(self): |
104 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) | 105 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) |
105 self.assertEqual(len(results.GetPass()), 2) | 106 self.assertEqual(len(results.GetPass()), 2) |
106 self.assertEqual(len(results.GetNotPass()), 0) | 107 self.assertEqual(len(results.GetNotPass()), 0) |
107 | 108 |
108 def testSetUp(self): | 109 def testSetUp(self): |
109 runners = [] | 110 runners = [] |
110 counter = shard._ThreadSafeCounter() | 111 counter = test_dispatcher._ThreadSafeCounter() |
111 shard._SetUp(MockRunner, '0', runners, counter) | 112 test_dispatcher._SetUp(MockRunner, '0', runners, counter) |
112 self.assertEqual(len(runners), 1) | 113 self.assertEqual(len(runners), 1) |
113 self.assertEqual(runners[0].setups, 1) | 114 self.assertEqual(runners[0].setups, 1) |
114 | 115 |
115 def testThreadSafeCounter(self): | 116 def testThreadSafeCounter(self): |
116 counter = shard._ThreadSafeCounter() | 117 counter = test_dispatcher._ThreadSafeCounter() |
117 for i in xrange(5): | 118 for i in xrange(5): |
118 self.assertEqual(counter.GetAndIncrement(), i) | 119 self.assertEqual(counter.GetAndIncrement(), i) |
119 | 120 |
120 | 121 |
121 class TestThreadGroupFunctions(unittest.TestCase): | 122 class TestThreadGroupFunctions(unittest.TestCase): |
122 """Tests for shard._RunAllTests and shard._CreateRunners.""" | 123 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" |
123 def setUp(self): | 124 def setUp(self): |
124 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | 125 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
| 126 shared_test_collection = test_dispatcher._TestCollection( |
| 127 [test_dispatcher._Test(t) for t in self.tests]) |
| 128 self.test_collection_factory = lambda: shared_test_collection |
125 | 129 |
126 def testCreate(self): | 130 def testCreate(self): |
127 runners = shard._CreateRunners(MockRunner, ['0', '1']) | 131 runners = test_dispatcher._CreateRunners(MockRunner, ['0', '1']) |
128 for runner in runners: | 132 for runner in runners: |
129 self.assertEqual(runner.setups, 1) | 133 self.assertEqual(runner.setups, 1) |
130 self.assertEqual(set([r.device for r in runners]), | 134 self.assertEqual(set([r.device for r in runners]), |
131 set(['0', '1'])) | 135 set(['0', '1'])) |
132 self.assertEqual(set([r.shard_index for r in runners]), | 136 self.assertEqual(set([r.shard_index for r in runners]), |
133 set([0, 1])) | 137 set([0, 1])) |
134 | 138 |
135 def testRun(self): | 139 def testRun(self): |
136 runners = [MockRunner('0'), MockRunner('1')] | 140 runners = [MockRunner('0'), MockRunner('1')] |
137 results, exit_code = shard._RunAllTests(runners, self.tests, 0) | 141 results, exit_code = test_dispatcher._RunAllTests( |
| 142 runners, self.test_collection_factory, 0) |
138 self.assertEqual(len(results.GetPass()), len(self.tests)) | 143 self.assertEqual(len(results.GetPass()), len(self.tests)) |
139 self.assertEqual(exit_code, 0) | 144 self.assertEqual(exit_code, 0) |
140 | 145 |
141 def testTearDown(self): | 146 def testTearDown(self): |
142 runners = [MockRunner('0'), MockRunner('1')] | 147 runners = [MockRunner('0'), MockRunner('1')] |
143 shard._TearDownRunners(runners) | 148 test_dispatcher._TearDownRunners(runners) |
144 for runner in runners: | 149 for runner in runners: |
145 self.assertEqual(runner.teardowns, 1) | 150 self.assertEqual(runner.teardowns, 1) |
146 | 151 |
147 def testRetry(self): | 152 def testRetry(self): |
148 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) | 153 runners = test_dispatcher._CreateRunners(MockRunnerFail, ['0', '1']) |
149 results, exit_code = shard._RunAllTests(runners, self.tests, 0) | 154 results, exit_code = test_dispatcher._RunAllTests( |
| 155 runners, self.test_collection_factory, 0) |
150 self.assertEqual(len(results.GetFail()), len(self.tests)) | 156 self.assertEqual(len(results.GetFail()), len(self.tests)) |
151 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 157 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
152 | 158 |
153 def testReraise(self): | 159 def testReraise(self): |
154 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) | 160 runners = test_dispatcher._CreateRunners(MockRunnerException, ['0', '1']) |
155 with self.assertRaises(TestException): | 161 with self.assertRaises(TestException): |
156 shard._RunAllTests(runners, self.tests, 0) | 162 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) |
157 | 163 |
158 | 164 |
159 class TestShard(unittest.TestCase): | 165 class TestShard(unittest.TestCase): |
160 """Tests for shard.Shard.""" | 166 """Tests test_dispatcher.RunTests with sharding.""" |
161 @staticmethod | 167 @staticmethod |
162 def _RunShard(runner_factory): | 168 def _RunShard(runner_factory): |
163 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) | 169 return test_dispatcher.RunTests( |
| 170 ['a', 'b', 'c'], runner_factory, False, None, shard=True) |
164 | 171 |
165 def testShard(self): | 172 def testShard(self): |
166 results, exit_code = TestShard._RunShard(MockRunner) | 173 results, exit_code = TestShard._RunShard(MockRunner) |
167 self.assertEqual(len(results.GetPass()), 3) | 174 self.assertEqual(len(results.GetPass()), 3) |
168 self.assertEqual(exit_code, 0) | 175 self.assertEqual(exit_code, 0) |
169 | 176 |
170 def testFailing(self): | 177 def testFailing(self): |
171 results, exit_code = TestShard._RunShard(MockRunnerFail) | 178 results, exit_code = TestShard._RunShard(MockRunnerFail) |
172 self.assertEqual(len(results.GetPass()), 0) | 179 self.assertEqual(len(results.GetPass()), 0) |
173 self.assertEqual(len(results.GetFail()), 3) | 180 self.assertEqual(len(results.GetFail()), 3) |
174 self.assertEqual(exit_code, 0) | 181 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
175 | 182 |
176 def testNoTests(self): | 183 def testNoTests(self): |
177 results, exit_code = shard.ShardAndRunTests(MockRunner, ['0', '1'], []) | 184 results, exit_code = test_dispatcher.RunTests( |
| 185 [], MockRunner, False, None, shard=True) |
178 self.assertEqual(len(results.GetAll()), 0) | 186 self.assertEqual(len(results.GetAll()), 0) |
179 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 187 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
180 | 188 |
| 189 |
| 190 class TestReplicate(unittest.TestCase): |
| 191 """Tests test_dispatcher.RunTests with replication.""" |
| 192 @staticmethod |
| 193 def _RunReplicate(runner_factory): |
| 194 return test_dispatcher.RunTests( |
| 195 ['a', 'b', 'c'], runner_factory, False, None, shard=False) |
| 196 |
| 197 def testReplicate(self): |
| 198 results, exit_code = TestReplicate._RunReplicate(MockRunner) |
| 199 # We expect 6 results since each test should have been run on every device |
| 200 self.assertEqual(len(results.GetPass()), 6) |
| 201 self.assertEqual(exit_code, 0) |
| 202 |
| 203 def testFailing(self): |
| 204 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) |
| 205 self.assertEqual(len(results.GetPass()), 0) |
| 206 self.assertEqual(len(results.GetFail()), 6) |
| 207 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
| 208 |
| 209 def testNoTests(self): |
| 210 results, exit_code = test_dispatcher.RunTests( |
| 211 [], MockRunner, False, None, shard=False) |
| 212 self.assertEqual(len(results.GetAll()), 0) |
| 213 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
| 214 |
181 | 215 |
182 if __name__ == '__main__': | 216 if __name__ == '__main__': |
183 unittest.main() | 217 unittest.main() |
OLD | NEW |