| OLD | NEW |
| 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 decorators.py. | 6 Unit tests for decorators.py. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
| 10 | 10 |
| 11 import os | 11 import os |
| 12 import sys | 12 import sys |
| 13 import time | 13 import time |
| 14 import traceback | 14 import traceback |
| 15 import unittest | 15 import unittest |
| 16 | 16 |
| 17 from pylib import constants | 17 from pylib import constants |
| 18 from pylib.device import decorators | 18 from pylib.device import decorators |
| 19 from pylib.device import device_errors | 19 from pylib.device import device_errors |
| 20 from pylib.utils import reraiser_thread | 20 from pylib.utils import reraiser_thread |
| 21 | 21 |
| 22 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer | |
| 23 # backed by AndroidCommands / android_testrunner. | |
| 24 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | |
| 25 'android_testrunner')) | |
| 26 import errors as old_errors | |
| 27 | |
| 28 _DEFAULT_TIMEOUT = 30 | 22 _DEFAULT_TIMEOUT = 30 |
| 29 _DEFAULT_RETRIES = 3 | 23 _DEFAULT_RETRIES = 3 |
| 30 | 24 |
| 31 class DecoratorsTest(unittest.TestCase): | 25 class DecoratorsTest(unittest.TestCase): |
| 32 _decorated_function_called_count = 0 | 26 _decorated_function_called_count = 0 |
| 33 | 27 |
| 34 def testFunctionDecoratorDoesTimeouts(self): | 28 def testFunctionDecoratorDoesTimeouts(self): |
| 35 """Tests that the base decorator handles the timeout logic.""" | 29 """Tests that the base decorator handles the timeout logic.""" |
| 36 DecoratorsTest._decorated_function_called_count = 0 | 30 DecoratorsTest._decorated_function_called_count = 0 |
| 37 @decorators.WithTimeoutAndRetries | 31 @decorators.WithTimeoutAndRetries |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 with self.assertRaises(KeyError): | 65 with self.assertRaises(KeyError): |
| 72 requiresExplicitTimeoutAndRetries(retries=0) | 66 requiresExplicitTimeoutAndRetries(retries=0) |
| 73 expected_timeout = 10 | 67 expected_timeout = 10 |
| 74 expected_retries = 1 | 68 expected_retries = 1 |
| 75 (actual_timeout, actual_retries) = ( | 69 (actual_timeout, actual_retries) = ( |
| 76 requiresExplicitTimeoutAndRetries(timeout=expected_timeout, | 70 requiresExplicitTimeoutAndRetries(timeout=expected_timeout, |
| 77 retries=expected_retries)) | 71 retries=expected_retries)) |
| 78 self.assertEquals(expected_timeout, actual_timeout) | 72 self.assertEquals(expected_timeout, actual_timeout) |
| 79 self.assertEquals(expected_retries, actual_retries) | 73 self.assertEquals(expected_retries, actual_retries) |
| 80 | 74 |
| 81 def testFunctionDecoratorTranslatesOldExceptions(self): | |
| 82 """Tests that the explicit decorator translates old exceptions.""" | |
| 83 @decorators.WithTimeoutAndRetries | |
| 84 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | |
| 85 raise exception | |
| 86 | |
| 87 exception_desc = 'Old response timeout error' | |
| 88 with self.assertRaises(device_errors.CommandTimeoutError) as e: | |
| 89 alwaysRaisesProvidedException( | |
| 90 old_errors.WaitForResponseTimedOutError(exception_desc), | |
| 91 timeout=10, retries=1) | |
| 92 self.assertEquals(exception_desc, str(e.exception)) | |
| 93 | |
| 94 exception_desc = 'Old device error' | |
| 95 with self.assertRaises(device_errors.DeviceUnreachableError) as e: | |
| 96 alwaysRaisesProvidedException( | |
| 97 old_errors.DeviceUnresponsiveError(exception_desc), | |
| 98 timeout=10, retries=1) | |
| 99 self.assertEquals(exception_desc, str(e.exception)) | |
| 100 | |
| 101 def testFunctionDecoratorTranslatesReraiserExceptions(self): | 75 def testFunctionDecoratorTranslatesReraiserExceptions(self): |
| 102 """Tests that the explicit decorator translates reraiser exceptions.""" | 76 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 103 @decorators.WithTimeoutAndRetries | 77 @decorators.WithTimeoutAndRetries |
| 104 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | 78 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
| 105 raise exception | 79 raise exception |
| 106 | 80 |
| 107 exception_desc = 'Reraiser thread timeout error' | 81 exception_desc = 'Reraiser thread timeout error' |
| 108 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 82 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 109 alwaysRaisesProvidedException( | 83 alwaysRaisesProvidedException( |
| 110 reraiser_thread.TimeoutError(exception_desc), | 84 reraiser_thread.TimeoutError(exception_desc), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 self.assertEquals(30, alwaysReturnsTimeouts()) | 133 self.assertEquals(30, alwaysReturnsTimeouts()) |
| 160 self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) | 134 self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) |
| 161 | 135 |
| 162 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | 136 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 163 def alwaysReturnsRetries(timeout=None, retries=None): | 137 def alwaysReturnsRetries(timeout=None, retries=None): |
| 164 return retries | 138 return retries |
| 165 | 139 |
| 166 self.assertEquals(10, alwaysReturnsRetries()) | 140 self.assertEquals(10, alwaysReturnsRetries()) |
| 167 self.assertEquals(1, alwaysReturnsRetries(retries=1)) | 141 self.assertEquals(1, alwaysReturnsRetries(retries=1)) |
| 168 | 142 |
| 169 def testDefaultsFunctionDecoratorTranslatesOldExceptions(self): | |
| 170 """Tests that the explicit decorator translates old exceptions.""" | |
| 171 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | |
| 172 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | |
| 173 raise exception | |
| 174 | |
| 175 exception_desc = 'Old response timeout error' | |
| 176 with self.assertRaises(device_errors.CommandTimeoutError) as e: | |
| 177 alwaysRaisesProvidedException( | |
| 178 old_errors.WaitForResponseTimedOutError(exception_desc)) | |
| 179 self.assertEquals(exception_desc, str(e.exception)) | |
| 180 | |
| 181 exception_desc = 'Old device error' | |
| 182 with self.assertRaises(device_errors.DeviceUnreachableError) as e: | |
| 183 alwaysRaisesProvidedException( | |
| 184 old_errors.DeviceUnresponsiveError(exception_desc)) | |
| 185 self.assertEquals(exception_desc, str(e.exception)) | |
| 186 | |
| 187 def testDefaultsFunctionDecoratorTranslatesReraiserExceptions(self): | 143 def testDefaultsFunctionDecoratorTranslatesReraiserExceptions(self): |
| 188 """Tests that the explicit decorator translates reraiser exceptions.""" | 144 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 189 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | 145 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 190 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | 146 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
| 191 raise exception | 147 raise exception |
| 192 | 148 |
| 193 exception_desc = 'Reraiser thread timeout error' | 149 exception_desc = 'Reraiser thread timeout error' |
| 194 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 150 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 195 alwaysRaisesProvidedException( | 151 alwaysRaisesProvidedException( |
| 196 reraiser_thread.TimeoutError(exception_desc)) | 152 reraiser_thread.TimeoutError(exception_desc)) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 216 DecoratorsTest._decorated_function_called_count = 0 | 172 DecoratorsTest._decorated_function_called_count = 0 |
| 217 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 173 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
| 218 def alwaysRaisesCommandFailedError(): | 174 def alwaysRaisesCommandFailedError(): |
| 219 DecoratorsTest._decorated_function_called_count += 1 | 175 DecoratorsTest._decorated_function_called_count += 1 |
| 220 raise device_errors.CommandFailedError('testCommand failed') | 176 raise device_errors.CommandFailedError('testCommand failed') |
| 221 | 177 |
| 222 with self.assertRaises(device_errors.CommandFailedError): | 178 with self.assertRaises(device_errors.CommandFailedError): |
| 223 alwaysRaisesCommandFailedError() | 179 alwaysRaisesCommandFailedError() |
| 224 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 180 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
| 225 | 181 |
| 226 def testExplicitDecoratorTranslatesOldExceptions(self): | |
| 227 """Tests that the explicit decorator translates old exceptions.""" | |
| 228 @decorators.WithExplicitTimeoutAndRetries(30, 10) | |
| 229 def alwaysRaisesProvidedException(exception): | |
| 230 raise exception | |
| 231 | |
| 232 exception_desc = 'Old response timeout error' | |
| 233 with self.assertRaises(device_errors.CommandTimeoutError) as e: | |
| 234 alwaysRaisesProvidedException( | |
| 235 old_errors.WaitForResponseTimedOutError(exception_desc)) | |
| 236 self.assertEquals(exception_desc, str(e.exception)) | |
| 237 | |
| 238 exception_desc = 'Old device error' | |
| 239 with self.assertRaises(device_errors.DeviceUnreachableError) as e: | |
| 240 alwaysRaisesProvidedException( | |
| 241 old_errors.DeviceUnresponsiveError(exception_desc)) | |
| 242 self.assertEquals(exception_desc, str(e.exception)) | |
| 243 | |
| 244 def testExplicitDecoratorTranslatesReraiserExceptions(self): | 182 def testExplicitDecoratorTranslatesReraiserExceptions(self): |
| 245 """Tests that the explicit decorator translates reraiser exceptions.""" | 183 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 246 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 184 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
| 247 def alwaysRaisesProvidedException(exception): | 185 def alwaysRaisesProvidedException(exception): |
| 248 raise exception | 186 raise exception |
| 249 | 187 |
| 250 exception_desc = 'Reraiser thread timeout error' | 188 exception_desc = 'Reraiser thread timeout error' |
| 251 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 189 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 252 alwaysRaisesProvidedException( | 190 alwaysRaisesProvidedException( |
| 253 reraiser_thread.TimeoutError(exception_desc)) | 191 reraiser_thread.TimeoutError(exception_desc)) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 267 |
| 330 def testMethodDecoratorPassesValues(self): | 268 def testMethodDecoratorPassesValues(self): |
| 331 """Tests that the method decorator passes timeout and retries kwargs.""" | 269 """Tests that the method decorator passes timeout and retries kwargs.""" |
| 332 test_obj = self._MethodDecoratorTestObject( | 270 test_obj = self._MethodDecoratorTestObject( |
| 333 self, default_timeout=42, default_retries=31) | 271 self, default_timeout=42, default_retries=31) |
| 334 self.assertEquals(42, test_obj.alwaysReturnsTimeout()) | 272 self.assertEquals(42, test_obj.alwaysReturnsTimeout()) |
| 335 self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) | 273 self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) |
| 336 self.assertEquals(31, test_obj.alwaysReturnsRetries()) | 274 self.assertEquals(31, test_obj.alwaysReturnsRetries()) |
| 337 self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) | 275 self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) |
| 338 | 276 |
| 339 def testMethodDecoratorTranslatesOldExceptions(self): | |
| 340 test_obj = self._MethodDecoratorTestObject(self) | |
| 341 | |
| 342 exception_desc = 'Old response timeout error' | |
| 343 with self.assertRaises(device_errors.CommandTimeoutError) as e: | |
| 344 test_obj.alwaysRaisesProvidedException( | |
| 345 old_errors.WaitForResponseTimedOutError(exception_desc)) | |
| 346 self.assertEquals(exception_desc, str(e.exception)) | |
| 347 | |
| 348 exception_desc = 'Old device error' | |
| 349 with self.assertRaises(device_errors.DeviceUnreachableError) as e: | |
| 350 test_obj.alwaysRaisesProvidedException( | |
| 351 old_errors.DeviceUnresponsiveError(exception_desc)) | |
| 352 self.assertEquals(exception_desc, str(e.exception)) | |
| 353 | |
| 354 def testMethodDecoratorTranslatesReraiserExceptions(self): | 277 def testMethodDecoratorTranslatesReraiserExceptions(self): |
| 355 test_obj = self._MethodDecoratorTestObject(self) | 278 test_obj = self._MethodDecoratorTestObject(self) |
| 356 | 279 |
| 357 exception_desc = 'Reraiser thread timeout error' | 280 exception_desc = 'Reraiser thread timeout error' |
| 358 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 281 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 359 test_obj.alwaysRaisesProvidedException( | 282 test_obj.alwaysRaisesProvidedException( |
| 360 reraiser_thread.TimeoutError(exception_desc)) | 283 reraiser_thread.TimeoutError(exception_desc)) |
| 361 self.assertEquals(exception_desc, str(e.exception)) | 284 self.assertEquals(exception_desc, str(e.exception)) |
| 362 | 285 |
| 363 if __name__ == '__main__': | 286 if __name__ == '__main__': |
| 364 unittest.main(verbosity=2) | 287 unittest.main(verbosity=2) |
| 365 | 288 |
| OLD | NEW |