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

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 290573004: [Android] Support generic parallel execution across devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 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 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 """ 5 """
6 Unit tests for the contents of device_utils.py (mostly DeviceUtils). 6 Unit tests for the contents of device_utils.py (mostly DeviceUtils).
7 """ 7 """
8 8
9 # pylint: disable=W0212 9 # pylint: disable=W0212
10 # pylint: disable=W0613 10 # pylint: disable=W0613
11 11
12 import os
13 import tempfile
14 import time
12 import unittest 15 import unittest
16
13 from pylib import android_commands 17 from pylib import android_commands
14 from pylib import cmd_helper 18 from pylib import cmd_helper
15 from pylib.device import adb_wrapper 19 from pylib.device import adb_wrapper
16 from pylib.device import device_utils 20 from pylib.device import device_utils
17 21
18 22
23 class _ParallelTestObject(object):
24 """ Class used to test device_utils._ParallelExecutor.
25
26 Note that this has to live at top-level so it can be pickled.
craigdh 2014/05/16 21:59:35 out of date?
jbudorick 2014/05/21 16:42:48 Done.
27 """
28
29 parallel = device_utils._ParallelExecutor
30
31 def __init__(self, thing, completion_file_name=None):
32 self._thing = thing
33 self._completion_file_name = completion_file_name
34 self.helper = _ParallelTestObjectHelper(thing)
35
36 @staticmethod
37 def doReturn(what):
38 return what
39
40 @classmethod
41 def doRaise(cls, what):
42 raise what
43
44 def doReturnTheThing(self):
45 return self._thing
46
47 def doRaiseTheThing(self):
48 raise self._thing
49
50 def doRaiseIfExceptionElseSleepFor(self, sleep_duration):
51 if isinstance(self._thing, Exception):
52 raise self._thing
53 time.sleep(sleep_duration)
54 self._write_completion_file()
55 return self._thing
56
57 def _write_completion_file(self):
58 if self._completion_file_name and len(self._completion_file_name):
59 with open(self._completion_file_name, 'w+b') as completion_file:
60 completion_file.write('complete')
61
62 def __getitem__(self, index):
63 return self._thing[index]
64
65 def __str__(self):
66 return type(self).__name__
67
68
69 class _ParallelTestObjectHelper(object):
70
71 def __init__(self, thing):
72 self._thing = thing
73
74 def doReturnStringThing(self):
75 return str(self._thing)
76
77
19 class DeviceUtilsTest(unittest.TestCase): 78 class DeviceUtilsTest(unittest.TestCase):
20 def testGetAVDs(self): 79 def testGetAVDs(self):
21 pass 80 pass
22 81
23 def testRestartServerNotRunning(self): 82 def testRestartServerNotRunning(self):
24 # TODO(jbudorick) If these fail, it's not DeviceUtils's fault. 83 # TODO(jbudorick) If these fail, it's not DeviceUtils's fault.
25 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb'])) 84 self.assertEqual(0, cmd_helper.RunCmd(['pkill', 'adb']))
26 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) 85 self.assertNotEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']))
27 device_utils.RestartServer() 86 device_utils.RestartServer()
28 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb'])) 87 self.assertEqual(0, cmd_helper.RunCmd(['pgrep', 'adb']))
(...skipping 27 matching lines...) Expand all
56 def testInitWithAndroidCommands(self): 115 def testInitWithAndroidCommands(self):
57 serial = '0fedcba987654321' 116 serial = '0fedcba987654321'
58 a = android_commands.AndroidCommands(device=serial) 117 a = android_commands.AndroidCommands(device=serial)
59 d = device_utils.DeviceUtils(a) 118 d = device_utils.DeviceUtils(a)
60 self.assertEqual(serial, d.old_interface.GetDevice()) 119 self.assertEqual(serial, d.old_interface.GetDevice())
61 120
62 def testInitWithNone(self): 121 def testInitWithNone(self):
63 d = device_utils.DeviceUtils(None) 122 d = device_utils.DeviceUtils(None)
64 self.assertIsNone(d.old_interface.GetDevice()) 123 self.assertIsNone(d.old_interface.GetDevice())
65 124
125 def testParallelAllReturn(self):
126 devices = [_ParallelTestObject(True) for _ in xrange(0, 10)]
127 results = _ParallelTestObject.parallel(devices).doReturnTheThing().get(1)
128 self.assertTrue(isinstance(results, list))
129 self.assertEquals(10, len(results))
130 self.assertTrue(all(results))
131
132 def testParallelAllRaise(self):
133 devices = [_ParallelTestObject(Exception('thing %d' % i))
134 for i in xrange(0, 10)]
135 p = _ParallelTestObject.parallel(devices).doRaiseTheThing()
136 with self.assertRaises(Exception):
137 p.get(1)
138
139 def testParallelOneFailOthersComplete(self):
140 parallel_device_count = 10
141 exception_index = 7
142 exception_msg = 'thing %d' % exception_index
143
144 try:
145 completion_files = [tempfile.NamedTemporaryFile(delete=False)
146 for _ in xrange(0, parallel_device_count)]
147 devices = [
148 _ParallelTestObject(
149 i if i != exception_index else Exception(exception_msg),
150 completion_files[i].name)
151 for i in xrange(0, parallel_device_count)]
152 for f in completion_files:
153 f.close()
154 p = _ParallelTestObject.parallel(devices)
155 with self.assertRaises(Exception) as e:
156 p.doRaiseIfExceptionElseSleepFor(2).get(3)
157 self.assertTrue(exception_msg in str(e.exception))
158 for i in xrange(0, parallel_device_count):
159 with open(completion_files[i].name) as f:
160 if i == exception_index:
161 self.assertEquals('', f.read())
162 else:
163 self.assertEquals('complete', f.read())
164 finally:
165 for f in completion_files:
166 os.remove(f.name)
167
168 def testParallelReusable(self):
169 devices = [_ParallelTestObject(True) for _ in xrange(0, 10)]
170 p = _ParallelTestObject.parallel(devices)
171 results = p.doReturn(True).get(1)
172 self.assertTrue(all(results))
173 results = p.doReturn(True).get(1)
174 self.assertTrue(all(results))
175 with self.assertRaises(Exception):
176 results = p.doRaise(Exception('reusableTest')).get(1)
177
178 def testParallelContained(self):
179 devices = [_ParallelTestObject(i) for i in xrange(0, 10)]
180 results = (_ParallelTestObject.parallel(devices).helper
181 .doReturnStringThing().get(1))
182 self.assertTrue(isinstance(results, list))
183 self.assertEquals(10, len(results))
184 for i in xrange(0, 10):
185 self.assertEquals(str(i), results[i])
186
187 def testParallelGetItem(self):
188 devices = [_ParallelTestObject(range(i, i+10)) for i in xrange(0, 10)]
189 results = _ParallelTestObject.parallel(devices)[9].get(1)
190 self.assertEquals(range(9, 19), results)
191
66 192
67 if __name__ == '__main__': 193 if __name__ == '__main__':
68 unittest.main() 194 unittest.main(verbosity=2)
69 195
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698