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

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

Issue 12378048: [Android] Switch instrumentation tests to the new shard/runner paradigm. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add retries to the end of the queue instead of the beginning Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « build/android/pylib/base/shard.py ('k') | build/android/pylib/browsertests/dispatch.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 shard.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 17
18 import shard 18 import shard
19 import test_result 19 import test_result
20 20
21 21
22 class TestException(Exception): 22 class TestException(Exception):
23 pass 23 pass
24 24
25 25
26 class MockRunner(object): 26 class MockRunner(object):
27 """A mock TestRunner.""" 27 """A mock TestRunner."""
28 def __init__(self, device='0'): 28 def __init__(self, device='0', shard_index=0):
29 self.device = device 29 self.device = device
30 self.shard_index = shard_index
30 self.setups = 0 31 self.setups = 0
31 self.teardowns = 0 32 self.teardowns = 0
32 33
33 def RunTest(self, test): 34 def RunTest(self, test):
34 return (test_result.TestResults.FromRun( 35 return (test_result.TestResults.FromRun(
35 ok=[test_result.BaseTestResult(test, '')]), 36 ok=[test_result.BaseTestResult(test, '')]),
36 None) 37 None)
37 38
38 def SetUp(self): 39 def SetUp(self):
39 self.setups += 1 40 self.setups += 1
40 41
41 def TearDown(self): 42 def TearDown(self):
42 self.teardowns += 1 43 self.teardowns += 1
43 44
44 45
45 class MockRunnerFail(MockRunner): 46 class MockRunnerFail(MockRunner):
46 def RunTest(self, test): 47 def RunTest(self, test):
47 return (test_result.TestResults.FromRun( 48 return (test_result.TestResults.FromRun(
48 failed=[test_result.BaseTestResult(test, '')]), 49 failed=[test_result.BaseTestResult(test, '')]),
49 test) 50 test)
50 51
51 52
52 class MockRunnerFailTwice(MockRunner): 53 class MockRunnerFailTwice(MockRunner):
53 def __init__(self, device='0'): 54 def __init__(self, device='0', shard_index=0):
54 super(MockRunnerFailTwice, self).__init__(device) 55 super(MockRunnerFailTwice, self).__init__(device, shard_index)
55 self._fails = 0 56 self._fails = 0
56 57
57 def RunTest(self, test): 58 def RunTest(self, test):
58 self._fails += 1 59 self._fails += 1
59 if self._fails <= 2: 60 if self._fails <= 2:
60 return (test_result.TestResults.FromRun( 61 return (test_result.TestResults.FromRun(
61 failed=[test_result.BaseTestResult(test, '')]), 62 failed=[test_result.BaseTestResult(test, '')]),
62 test) 63 test)
63 else: 64 else:
64 return (test_result.TestResults.FromRun( 65 return (test_result.TestResults.FromRun(
(...skipping 25 matching lines...) Expand all
90 self.assertEqual(len(results.ok), 0) 91 self.assertEqual(len(results.ok), 0)
91 self.assertEqual(len(results.failed), 2) 92 self.assertEqual(len(results.failed), 2)
92 93
93 def testRunTestsFromQueueFailTwice(self): 94 def testRunTestsFromQueueFailTwice(self):
94 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) 95 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b'])
95 self.assertEqual(len(results.ok), 2) 96 self.assertEqual(len(results.ok), 2)
96 self.assertEqual(len(results.GetAllBroken()), 0) 97 self.assertEqual(len(results.GetAllBroken()), 0)
97 98
98 def testSetUp(self): 99 def testSetUp(self):
99 runners = [] 100 runners = []
100 shard._SetUp(MockRunner, '0', runners) 101 counter = shard._ThreadSafeCounter()
102 shard._SetUp(MockRunner, '0', runners, counter)
101 self.assertEqual(len(runners), 1) 103 self.assertEqual(len(runners), 1)
102 self.assertEqual(runners[0].setups, 1) 104 self.assertEqual(runners[0].setups, 1)
103 105
106 def testThreadSafeCounter(self):
107 counter = shard._ThreadSafeCounter()
108 for i in xrange(5):
109 self.assertEqual(counter.GetAndIncrement(), i)
110
104 111
105 class TestThreadGroupFunctions(unittest.TestCase): 112 class TestThreadGroupFunctions(unittest.TestCase):
106 """Tests for shard._RunAllTests and shard._CreateRunners.""" 113 """Tests for shard._RunAllTests and shard._CreateRunners."""
107 def setUp(self): 114 def setUp(self):
108 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 115 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
109 116
110 def testCreate(self): 117 def testCreate(self):
111 runners = shard._CreateRunners(MockRunner, ['0', '1']) 118 runners = shard._CreateRunners(MockRunner, ['0', '1'])
112 for runner in runners: 119 for runner in runners:
113 self.assertEqual(runner.setups, 1) 120 self.assertEqual(runner.setups, 1)
121 self.assertEqual(set([r.device for r in runners]),
122 set(['0', '1']))
123 self.assertEqual(set([r.shard_index for r in runners]),
124 set([0, 1]))
114 125
115 def testRun(self): 126 def testRun(self):
116 runners = [MockRunner('0'), MockRunner('1')] 127 runners = [MockRunner('0'), MockRunner('1')]
117 results = shard._RunAllTests(runners, self.tests) 128 results = shard._RunAllTests(runners, self.tests)
118 self.assertEqual(len(results.ok), len(self.tests)) 129 self.assertEqual(len(results.ok), len(self.tests))
119 130
120 def testTearDown(self): 131 def testTearDown(self):
121 runners = [MockRunner('0'), MockRunner('1')] 132 runners = [MockRunner('0'), MockRunner('1')]
122 shard._TearDownRunners(runners) 133 shard._TearDownRunners(runners)
123 for runner in runners: 134 for runner in runners:
(...skipping 21 matching lines...) Expand all
145 self.assertEqual(len(results.ok), 3) 156 self.assertEqual(len(results.ok), 3)
146 157
147 def testFailing(self): 158 def testFailing(self):
148 results = TestShard._RunShard(MockRunnerFail) 159 results = TestShard._RunShard(MockRunnerFail)
149 self.assertEqual(len(results.ok), 0) 160 self.assertEqual(len(results.ok), 0)
150 self.assertEqual(len(results.failed), 3) 161 self.assertEqual(len(results.failed), 3)
151 162
152 163
153 if __name__ == '__main__': 164 if __name__ == '__main__':
154 unittest.main() 165 unittest.main()
OLDNEW
« no previous file with comments | « build/android/pylib/base/shard.py ('k') | build/android/pylib/browsertests/dispatch.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698