Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: build/android/pylib/base/dispatch_unittest.py

Issue 18770008: [Android] Redesigns the sharder to allow replicated vs distributed tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adds tagging of tests (for replication) Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 dispatch.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 dispatch
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
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 for dispatch._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 = dispatch._TestCollection([dispatch._Test(t) for t in tests])
86 shard._RunTestsFromQueue(mock_runner, tests, results, 86 dispatch._RunTestsFromQueue(mock_runner, tests, results,
87 watchdog_timer.WatchdogTimer(None), 2) 87 watchdog_timer.WatchdogTimer(None), 2)
88 run_results = base_test_result.TestRunResults() 88 run_results = base_test_result.TestRunResults()
89 for r in results: 89 for r in results:
90 run_results.AddTestRunResults(r) 90 run_results.AddTestRunResults(r)
91 return run_results 91 return run_results
92 92
93 def testRunTestsFromQueue(self): 93 def testRunTestsFromQueue(self):
94 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) 94 results = TestFunctions._RunTests(MockRunner(), ['a', 'b'])
95 self.assertEqual(len(results.GetPass()), 2) 95 self.assertEqual(len(results.GetPass()), 2)
96 self.assertEqual(len(results.GetNotPass()), 0) 96 self.assertEqual(len(results.GetNotPass()), 0)
97 97
98 def testRunTestsFromQueueRetry(self): 98 def testRunTestsFromQueueRetry(self):
99 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) 99 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b'])
100 self.assertEqual(len(results.GetPass()), 0) 100 self.assertEqual(len(results.GetPass()), 0)
101 self.assertEqual(len(results.GetFail()), 2) 101 self.assertEqual(len(results.GetFail()), 2)
102 102
103 def testRunTestsFromQueueFailTwice(self): 103 def testRunTestsFromQueueFailTwice(self):
104 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) 104 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b'])
105 self.assertEqual(len(results.GetPass()), 2) 105 self.assertEqual(len(results.GetPass()), 2)
106 self.assertEqual(len(results.GetNotPass()), 0) 106 self.assertEqual(len(results.GetNotPass()), 0)
107 107
108 def testSetUp(self): 108 def testSetUp(self):
109 runners = [] 109 runners = []
110 counter = shard._ThreadSafeCounter() 110 counter = dispatch._ThreadSafeCounter()
111 shard._SetUp(MockRunner, '0', runners, counter) 111 dispatch._SetUp(MockRunner, '0', runners, counter)
112 self.assertEqual(len(runners), 1) 112 self.assertEqual(len(runners), 1)
113 self.assertEqual(runners[0].setups, 1) 113 self.assertEqual(runners[0].setups, 1)
114 114
115 def testThreadSafeCounter(self): 115 def testThreadSafeCounter(self):
116 counter = shard._ThreadSafeCounter() 116 counter = dispatch._ThreadSafeCounter()
117 for i in xrange(5): 117 for i in xrange(5):
118 self.assertEqual(counter.GetAndIncrement(), i) 118 self.assertEqual(counter.GetAndIncrement(), i)
119 119
120 120
121 class TestThreadGroupFunctions(unittest.TestCase): 121 class TestThreadGroupFunctions(unittest.TestCase):
122 """Tests for shard._RunAllTests and shard._CreateRunners.""" 122 """Tests for dispatch._RunAllTests and dispatch._CreateRunners."""
123 def setUp(self): 123 def setUp(self):
124 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 124 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
125 shared_test_collection = dispatch._TestCollection(
126 [dispatch._Test(t) for t in self.tests])
127 self.test_collection_factory = lambda: shared_test_collection
125 128
126 def testCreate(self): 129 def testCreate(self):
127 runners = shard._CreateRunners(MockRunner, ['0', '1']) 130 runners = dispatch._CreateRunners(MockRunner, ['0', '1'])
128 for runner in runners: 131 for runner in runners:
129 self.assertEqual(runner.setups, 1) 132 self.assertEqual(runner.setups, 1)
130 self.assertEqual(set([r.device for r in runners]), 133 self.assertEqual(set([r.device for r in runners]),
131 set(['0', '1'])) 134 set(['0', '1']))
132 self.assertEqual(set([r.shard_index for r in runners]), 135 self.assertEqual(set([r.shard_index for r in runners]),
133 set([0, 1])) 136 set([0, 1]))
134 137
135 def testRun(self): 138 def testRun(self):
136 runners = [MockRunner('0'), MockRunner('1')] 139 runners = [MockRunner('0'), MockRunner('1')]
137 results, exit_code = shard._RunAllTests(runners, self.tests, 0) 140 results, exit_code = dispatch._RunAllTests(
141 runners, self.test_collection_factory, 0)
138 self.assertEqual(len(results.GetPass()), len(self.tests)) 142 self.assertEqual(len(results.GetPass()), len(self.tests))
139 self.assertEqual(exit_code, 0) 143 self.assertEqual(exit_code, 0)
140 144
141 def testTearDown(self): 145 def testTearDown(self):
142 runners = [MockRunner('0'), MockRunner('1')] 146 runners = [MockRunner('0'), MockRunner('1')]
143 shard._TearDownRunners(runners) 147 dispatch._TearDownRunners(runners)
144 for runner in runners: 148 for runner in runners:
145 self.assertEqual(runner.teardowns, 1) 149 self.assertEqual(runner.teardowns, 1)
146 150
147 def testRetry(self): 151 def testRetry(self):
148 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) 152 runners = dispatch._CreateRunners(MockRunnerFail, ['0', '1'])
149 results, exit_code = shard._RunAllTests(runners, self.tests, 0) 153 results, exit_code = dispatch._RunAllTests(
154 runners, self.test_collection_factory, 0)
150 self.assertEqual(len(results.GetFail()), len(self.tests)) 155 self.assertEqual(len(results.GetFail()), len(self.tests))
151 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 156 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
152 157
153 def testReraise(self): 158 def testReraise(self):
154 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) 159 runners = dispatch._CreateRunners(MockRunnerException, ['0', '1'])
155 with self.assertRaises(TestException): 160 with self.assertRaises(TestException):
156 shard._RunAllTests(runners, self.tests, 0) 161 dispatch._RunAllTests(runners, self.test_collection_factory, 0)
157 162
158 163
159 class TestShard(unittest.TestCase): 164 class TestShard(unittest.TestCase):
160 """Tests for shard.Shard.""" 165 """Tests for dispatch.ShardAndRunTests."""
161 @staticmethod 166 @staticmethod
162 def _RunShard(runner_factory): 167 def _RunShard(runner_factory):
163 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) 168 return dispatch.ShardAndRunTests(['a', 'b', 'c'], False, None,
169 runner_factory)
164 170
165 def testShard(self): 171 def testShard(self):
166 results, exit_code = TestShard._RunShard(MockRunner) 172 results, exit_code = TestShard._RunShard(MockRunner)
167 self.assertEqual(len(results.GetPass()), 3) 173 self.assertEqual(len(results.GetPass()), 3)
168 self.assertEqual(exit_code, 0) 174 self.assertEqual(exit_code, 0)
169 175
170 def testFailing(self): 176 def testFailing(self):
171 results, exit_code = TestShard._RunShard(MockRunnerFail) 177 results, exit_code = TestShard._RunShard(MockRunnerFail)
172 self.assertEqual(len(results.GetPass()), 0) 178 self.assertEqual(len(results.GetPass()), 0)
173 self.assertEqual(len(results.GetFail()), 3) 179 self.assertEqual(len(results.GetFail()), 3)
174 self.assertEqual(exit_code, 0) 180 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
175 181
176 def testNoTests(self): 182 def testNoTests(self):
177 results, exit_code = shard.ShardAndRunTests(MockRunner, ['0', '1'], []) 183 results, exit_code = dispatch.ShardAndRunTests([], False, None, MockRunner)
178 self.assertEqual(len(results.GetAll()), 0) 184 self.assertEqual(len(results.GetAll()), 0)
179 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) 185 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
180 186
187
188 class TestReplicate(unittest.TestCase):
189 """Tests for dispatch.ReplicateAndRunTests."""
190 @staticmethod
191 def _RunReplicate(runner_factory):
192 return dispatch.ReplicateAndRunTests(['a', 'b', 'c'], False, None,
193 runner_factory)
194
195 def testReplicate(self):
196 results, exit_code = TestReplicate._RunReplicate(MockRunner)
197 # We expect 6 results since each test should have been run on every device
198 self.assertEqual(len(results.GetPass()), 6)
199 self.assertEqual(exit_code, 0)
200
201 def testFailing(self):
202 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail)
203 self.assertEqual(len(results.GetPass()), 0)
204 self.assertEqual(len(results.GetFail()), 6)
205 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
206
207 def testNoTests(self):
208 results, exit_code = dispatch.ReplicateAndRunTests([], False, None,
209 MockRunner)
210 self.assertEqual(len(results.GetAll()), 0)
211 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE)
212
181 213
182 if __name__ == '__main__': 214 if __name__ == '__main__':
183 unittest.main() 215 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698