| 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 |
| 22 _DEFAULT_TIMEOUT = 30 | 28 _DEFAULT_TIMEOUT = 30 |
| 23 _DEFAULT_RETRIES = 3 | 29 _DEFAULT_RETRIES = 3 |
| 24 | 30 |
| 25 class DecoratorsTest(unittest.TestCase): | 31 class DecoratorsTest(unittest.TestCase): |
| 26 _decorated_function_called_count = 0 | 32 _decorated_function_called_count = 0 |
| 27 | 33 |
| 28 def testFunctionDecoratorDoesTimeouts(self): | 34 def testFunctionDecoratorDoesTimeouts(self): |
| 29 """Tests that the base decorator handles the timeout logic.""" | 35 """Tests that the base decorator handles the timeout logic.""" |
| 30 DecoratorsTest._decorated_function_called_count = 0 | 36 DecoratorsTest._decorated_function_called_count = 0 |
| 31 @decorators.WithTimeoutAndRetries | 37 @decorators.WithTimeoutAndRetries |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 with self.assertRaises(KeyError): | 71 with self.assertRaises(KeyError): |
| 66 requiresExplicitTimeoutAndRetries(retries=0) | 72 requiresExplicitTimeoutAndRetries(retries=0) |
| 67 expected_timeout = 10 | 73 expected_timeout = 10 |
| 68 expected_retries = 1 | 74 expected_retries = 1 |
| 69 (actual_timeout, actual_retries) = ( | 75 (actual_timeout, actual_retries) = ( |
| 70 requiresExplicitTimeoutAndRetries(timeout=expected_timeout, | 76 requiresExplicitTimeoutAndRetries(timeout=expected_timeout, |
| 71 retries=expected_retries)) | 77 retries=expected_retries)) |
| 72 self.assertEquals(expected_timeout, actual_timeout) | 78 self.assertEquals(expected_timeout, actual_timeout) |
| 73 self.assertEquals(expected_retries, actual_retries) | 79 self.assertEquals(expected_retries, actual_retries) |
| 74 | 80 |
| 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 |
| 75 def testFunctionDecoratorTranslatesReraiserExceptions(self): | 101 def testFunctionDecoratorTranslatesReraiserExceptions(self): |
| 76 """Tests that the explicit decorator translates reraiser exceptions.""" | 102 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 77 @decorators.WithTimeoutAndRetries | 103 @decorators.WithTimeoutAndRetries |
| 78 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | 104 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
| 79 raise exception | 105 raise exception |
| 80 | 106 |
| 81 exception_desc = 'Reraiser thread timeout error' | 107 exception_desc = 'Reraiser thread timeout error' |
| 82 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 108 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 83 alwaysRaisesProvidedException( | 109 alwaysRaisesProvidedException( |
| 84 reraiser_thread.TimeoutError(exception_desc), | 110 reraiser_thread.TimeoutError(exception_desc), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 self.assertEquals(30, alwaysReturnsTimeouts()) | 159 self.assertEquals(30, alwaysReturnsTimeouts()) |
| 134 self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) | 160 self.assertEquals(120, alwaysReturnsTimeouts(timeout=120)) |
| 135 | 161 |
| 136 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | 162 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 137 def alwaysReturnsRetries(timeout=None, retries=None): | 163 def alwaysReturnsRetries(timeout=None, retries=None): |
| 138 return retries | 164 return retries |
| 139 | 165 |
| 140 self.assertEquals(10, alwaysReturnsRetries()) | 166 self.assertEquals(10, alwaysReturnsRetries()) |
| 141 self.assertEquals(1, alwaysReturnsRetries(retries=1)) | 167 self.assertEquals(1, alwaysReturnsRetries(retries=1)) |
| 142 | 168 |
| 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 |
| 143 def testDefaultsFunctionDecoratorTranslatesReraiserExceptions(self): | 187 def testDefaultsFunctionDecoratorTranslatesReraiserExceptions(self): |
| 144 """Tests that the explicit decorator translates reraiser exceptions.""" | 188 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 145 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | 189 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
| 146 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): | 190 def alwaysRaisesProvidedException(exception, timeout=None, retries=None): |
| 147 raise exception | 191 raise exception |
| 148 | 192 |
| 149 exception_desc = 'Reraiser thread timeout error' | 193 exception_desc = 'Reraiser thread timeout error' |
| 150 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 194 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 151 alwaysRaisesProvidedException( | 195 alwaysRaisesProvidedException( |
| 152 reraiser_thread.TimeoutError(exception_desc)) | 196 reraiser_thread.TimeoutError(exception_desc)) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 172 DecoratorsTest._decorated_function_called_count = 0 | 216 DecoratorsTest._decorated_function_called_count = 0 |
| 173 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 217 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
| 174 def alwaysRaisesCommandFailedError(): | 218 def alwaysRaisesCommandFailedError(): |
| 175 DecoratorsTest._decorated_function_called_count += 1 | 219 DecoratorsTest._decorated_function_called_count += 1 |
| 176 raise device_errors.CommandFailedError('testCommand failed') | 220 raise device_errors.CommandFailedError('testCommand failed') |
| 177 | 221 |
| 178 with self.assertRaises(device_errors.CommandFailedError): | 222 with self.assertRaises(device_errors.CommandFailedError): |
| 179 alwaysRaisesCommandFailedError() | 223 alwaysRaisesCommandFailedError() |
| 180 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 224 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
| 181 | 225 |
| 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 |
| 182 def testExplicitDecoratorTranslatesReraiserExceptions(self): | 244 def testExplicitDecoratorTranslatesReraiserExceptions(self): |
| 183 """Tests that the explicit decorator translates reraiser exceptions.""" | 245 """Tests that the explicit decorator translates reraiser exceptions.""" |
| 184 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 246 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
| 185 def alwaysRaisesProvidedException(exception): | 247 def alwaysRaisesProvidedException(exception): |
| 186 raise exception | 248 raise exception |
| 187 | 249 |
| 188 exception_desc = 'Reraiser thread timeout error' | 250 exception_desc = 'Reraiser thread timeout error' |
| 189 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 251 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 190 alwaysRaisesProvidedException( | 252 alwaysRaisesProvidedException( |
| 191 reraiser_thread.TimeoutError(exception_desc)) | 253 reraiser_thread.TimeoutError(exception_desc)) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 329 |
| 268 def testMethodDecoratorPassesValues(self): | 330 def testMethodDecoratorPassesValues(self): |
| 269 """Tests that the method decorator passes timeout and retries kwargs.""" | 331 """Tests that the method decorator passes timeout and retries kwargs.""" |
| 270 test_obj = self._MethodDecoratorTestObject( | 332 test_obj = self._MethodDecoratorTestObject( |
| 271 self, default_timeout=42, default_retries=31) | 333 self, default_timeout=42, default_retries=31) |
| 272 self.assertEquals(42, test_obj.alwaysReturnsTimeout()) | 334 self.assertEquals(42, test_obj.alwaysReturnsTimeout()) |
| 273 self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) | 335 self.assertEquals(41, test_obj.alwaysReturnsTimeout(timeout=41)) |
| 274 self.assertEquals(31, test_obj.alwaysReturnsRetries()) | 336 self.assertEquals(31, test_obj.alwaysReturnsRetries()) |
| 275 self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) | 337 self.assertEquals(32, test_obj.alwaysReturnsRetries(retries=32)) |
| 276 | 338 |
| 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 |
| 277 def testMethodDecoratorTranslatesReraiserExceptions(self): | 354 def testMethodDecoratorTranslatesReraiserExceptions(self): |
| 278 test_obj = self._MethodDecoratorTestObject(self) | 355 test_obj = self._MethodDecoratorTestObject(self) |
| 279 | 356 |
| 280 exception_desc = 'Reraiser thread timeout error' | 357 exception_desc = 'Reraiser thread timeout error' |
| 281 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 358 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
| 282 test_obj.alwaysRaisesProvidedException( | 359 test_obj.alwaysRaisesProvidedException( |
| 283 reraiser_thread.TimeoutError(exception_desc)) | 360 reraiser_thread.TimeoutError(exception_desc)) |
| 284 self.assertEquals(exception_desc, str(e.exception)) | 361 self.assertEquals(exception_desc, str(e.exception)) |
| 285 | 362 |
| 286 if __name__ == '__main__': | 363 if __name__ == '__main__': |
| 287 unittest.main(verbosity=2) | 364 unittest.main(verbosity=2) |
| 288 | 365 |
| OLD | NEW |