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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 elapsed_time = time.time() - start_time | 45 elapsed_time = time.time() - start_time |
46 self.assertTrue(elapsed_time >= 1) | 46 self.assertTrue(elapsed_time >= 1) |
47 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) | 47 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) |
48 | 48 |
49 def testFunctionDecoratorDoesRetries(self): | 49 def testFunctionDecoratorDoesRetries(self): |
50 """Tests that the base decorator handles the retries logic.""" | 50 """Tests that the base decorator handles the retries logic.""" |
51 DecoratorsTest._decorated_function_called_count = 0 | 51 DecoratorsTest._decorated_function_called_count = 0 |
52 @decorators.WithTimeoutAndRetries | 52 @decorators.WithTimeoutAndRetries |
53 def alwaysRaisesCommandFailedError(timeout=None, retries=None): | 53 def alwaysRaisesCommandFailedError(timeout=None, retries=None): |
54 DecoratorsTest._decorated_function_called_count += 1 | 54 DecoratorsTest._decorated_function_called_count += 1 |
55 raise device_errors.CommandFailedError(['testCommand'], | 55 raise device_errors.CommandFailedError('testCommand failed') |
56 'testCommand failed') | |
57 | 56 |
58 with self.assertRaises(device_errors.CommandFailedError): | 57 with self.assertRaises(device_errors.CommandFailedError): |
59 alwaysRaisesCommandFailedError(timeout=30, retries=10) | 58 alwaysRaisesCommandFailedError(timeout=30, retries=10) |
60 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 59 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
61 | 60 |
62 def testFunctionDecoratorRequiresParams(self): | 61 def testFunctionDecoratorRequiresParams(self): |
63 """Tests that the base decorator requires timeout and retries params.""" | 62 """Tests that the base decorator requires timeout and retries params.""" |
64 @decorators.WithTimeoutAndRetries | 63 @decorators.WithTimeoutAndRetries |
65 def requiresExplicitTimeoutAndRetries(timeout=None, retries=None): | 64 def requiresExplicitTimeoutAndRetries(timeout=None, retries=None): |
66 return (timeout, retries) | 65 return (timeout, retries) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 elapsed_time = time.time() - start_time | 132 elapsed_time = time.time() - start_time |
134 self.assertTrue(elapsed_time >= 2) | 133 self.assertTrue(elapsed_time >= 2) |
135 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) | 134 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) |
136 | 135 |
137 def testDefaultsFunctionDecoratorDoesRetries(self): | 136 def testDefaultsFunctionDecoratorDoesRetries(self): |
138 """Tests that the defaults decorator handles retries logic.""" | 137 """Tests that the defaults decorator handles retries logic.""" |
139 DecoratorsTest._decorated_function_called_count = 0 | 138 DecoratorsTest._decorated_function_called_count = 0 |
140 @decorators.WithTimeoutAndRetriesDefaults(30, 10) | 139 @decorators.WithTimeoutAndRetriesDefaults(30, 10) |
141 def alwaysRaisesCommandFailedError(timeout=None, retries=None): | 140 def alwaysRaisesCommandFailedError(timeout=None, retries=None): |
142 DecoratorsTest._decorated_function_called_count += 1 | 141 DecoratorsTest._decorated_function_called_count += 1 |
143 raise device_errors.CommandFailedError(['testCommand'], | 142 raise device_errors.CommandFailedError('testCommand failed') |
144 'testCommand failed') | |
145 | 143 |
146 with self.assertRaises(device_errors.CommandFailedError): | 144 with self.assertRaises(device_errors.CommandFailedError): |
147 alwaysRaisesCommandFailedError() | 145 alwaysRaisesCommandFailedError() |
148 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 146 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
149 | 147 |
150 DecoratorsTest._decorated_function_called_count = 0 | 148 DecoratorsTest._decorated_function_called_count = 0 |
151 with self.assertRaises(device_errors.CommandFailedError): | 149 with self.assertRaises(device_errors.CommandFailedError): |
152 alwaysRaisesCommandFailedError(retries=5) | 150 alwaysRaisesCommandFailedError(retries=5) |
153 self.assertEquals(6, DecoratorsTest._decorated_function_called_count) | 151 self.assertEquals(6, DecoratorsTest._decorated_function_called_count) |
154 | 152 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 elapsed_time = time.time() - start_time | 210 elapsed_time = time.time() - start_time |
213 self.assertTrue(elapsed_time >= 1) | 211 self.assertTrue(elapsed_time >= 1) |
214 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) | 212 self.assertEquals(1, DecoratorsTest._decorated_function_called_count) |
215 | 213 |
216 def testExplicitFunctionDecoratorDoesRetries(self): | 214 def testExplicitFunctionDecoratorDoesRetries(self): |
217 """Tests that the explicit decorator handles retries logic.""" | 215 """Tests that the explicit decorator handles retries logic.""" |
218 DecoratorsTest._decorated_function_called_count = 0 | 216 DecoratorsTest._decorated_function_called_count = 0 |
219 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 217 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
220 def alwaysRaisesCommandFailedError(): | 218 def alwaysRaisesCommandFailedError(): |
221 DecoratorsTest._decorated_function_called_count += 1 | 219 DecoratorsTest._decorated_function_called_count += 1 |
222 raise device_errors.CommandFailedError(['testCommand'], | 220 raise device_errors.CommandFailedError('testCommand failed') |
223 'testCommand failed') | |
224 | 221 |
225 with self.assertRaises(device_errors.CommandFailedError): | 222 with self.assertRaises(device_errors.CommandFailedError): |
226 alwaysRaisesCommandFailedError() | 223 alwaysRaisesCommandFailedError() |
227 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) | 224 self.assertEquals(11, DecoratorsTest._decorated_function_called_count) |
228 | 225 |
229 def testExplicitDecoratorTranslatesOldExceptions(self): | 226 def testExplicitDecoratorTranslatesOldExceptions(self): |
230 """Tests that the explicit decorator translates old exceptions.""" | 227 """Tests that the explicit decorator translates old exceptions.""" |
231 @decorators.WithExplicitTimeoutAndRetries(30, 10) | 228 @decorators.WithExplicitTimeoutAndRetries(30, 10) |
232 def alwaysRaisesProvidedException(exception): | 229 def alwaysRaisesProvidedException(exception): |
233 raise exception | 230 raise exception |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 'default_timeout', 'default_retries') | 271 'default_timeout', 'default_retries') |
275 def alwaysTimesOut(self, timeout=None, retries=None): | 272 def alwaysTimesOut(self, timeout=None, retries=None): |
276 self.function_call_counters['alwaysTimesOut'] += 1 | 273 self.function_call_counters['alwaysTimesOut'] += 1 |
277 time.sleep(100) | 274 time.sleep(100) |
278 self._test_case.assertFalse(True, msg='Failed to time out?') | 275 self._test_case.assertFalse(True, msg='Failed to time out?') |
279 | 276 |
280 @decorators.WithTimeoutAndRetriesFromInstance( | 277 @decorators.WithTimeoutAndRetriesFromInstance( |
281 'default_timeout', 'default_retries') | 278 'default_timeout', 'default_retries') |
282 def alwaysRaisesCommandFailedError(self, timeout=None, retries=None): | 279 def alwaysRaisesCommandFailedError(self, timeout=None, retries=None): |
283 self.function_call_counters['alwaysRaisesCommandFailedError'] += 1 | 280 self.function_call_counters['alwaysRaisesCommandFailedError'] += 1 |
284 raise device_errors.CommandFailedError(['testCommand'], | 281 raise device_errors.CommandFailedError('testCommand failed') |
285 'testCommand failed') | |
286 | 282 |
287 # pylint: disable=R0201 | 283 # pylint: disable=R0201 |
288 | 284 |
289 @decorators.WithTimeoutAndRetriesFromInstance( | 285 @decorators.WithTimeoutAndRetriesFromInstance( |
290 'default_timeout', 'default_retries') | 286 'default_timeout', 'default_retries') |
291 def alwaysReturnsTimeout(self, timeout=None, retries=None): | 287 def alwaysReturnsTimeout(self, timeout=None, retries=None): |
292 return timeout | 288 return timeout |
293 | 289 |
294 @decorators.WithTimeoutAndRetriesFromInstance( | 290 @decorators.WithTimeoutAndRetriesFromInstance( |
295 'default_timeout', 'default_retries') | 291 'default_timeout', 'default_retries') |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 356 |
361 exception_desc = 'Reraiser thread timeout error' | 357 exception_desc = 'Reraiser thread timeout error' |
362 with self.assertRaises(device_errors.CommandTimeoutError) as e: | 358 with self.assertRaises(device_errors.CommandTimeoutError) as e: |
363 test_obj.alwaysRaisesProvidedException( | 359 test_obj.alwaysRaisesProvidedException( |
364 reraiser_thread.TimeoutError(exception_desc)) | 360 reraiser_thread.TimeoutError(exception_desc)) |
365 self.assertEquals(exception_desc, str(e.exception)) | 361 self.assertEquals(exception_desc, str(e.exception)) |
366 | 362 |
367 if __name__ == '__main__': | 363 if __name__ == '__main__': |
368 unittest.main(verbosity=2) | 364 unittest.main(verbosity=2) |
369 | 365 |
OLD | NEW |