Chromium Code Reviews| 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 Function/method decorators that provide timeout and retry logic. | 6 Function/method decorators that provide timeout and retry logic. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import functools | 9 import functools |
| 10 import os | 10 import os |
| 11 import sys | 11 import sys |
| 12 | 12 |
| 13 from pylib import constants | 13 from pylib import constants |
| 14 from pylib.device import device_errors | 14 from pylib.device import device_errors |
| 15 from pylib.utils import reraiser_thread | 15 from pylib.utils import reraiser_thread |
| 16 from pylib.utils import timeout_retry | 16 from pylib.utils import timeout_retry |
| 17 | 17 |
| 18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer | 18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
| 19 # backed by AndroidCommands / android_testrunner. | 19 # backed by AndroidCommands / android_testrunner. |
| 20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | 20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
| 21 'android_testrunner')) | 21 'android_testrunner')) |
| 22 import errors as old_errors | 22 import errors as old_errors |
| 23 | 23 |
| 24 | 24 |
| 25 def _TimeoutRetryWrapper(f, timeout_func, retries_func): | 25 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): |
|
craigdh
2014/05/21 22:25:00
docstring with description of parameters
jbudorick
2014/05/22 04:49:58
Done.
| |
| 26 @functools.wraps(f) | 26 @functools.wraps(f) |
| 27 def TimeoutRetryWrapper(*args, **kwargs): | 27 def TimeoutRetryWrapper(*args, **kwargs): |
| 28 timeout = timeout_func(*args, **kwargs) | 28 timeout = timeout_func(*args, **kwargs) |
| 29 retries = retries_func(*args, **kwargs) | 29 retries = retries_func(*args, **kwargs) |
| 30 if pass_values: | |
| 31 kwargs['timeout'] = timeout | |
| 32 kwargs['retries'] = retries | |
| 30 def impl(): | 33 def impl(): |
| 31 return f(*args, **kwargs) | 34 return f(*args, **kwargs) |
| 32 try: | 35 try: |
| 33 return timeout_retry.Run(impl, timeout, retries) | 36 return timeout_retry.Run(impl, timeout, retries) |
| 34 except old_errors.WaitForResponseTimedOutError as e: | 37 except old_errors.WaitForResponseTimedOutError as e: |
| 35 raise device_errors.CommandTimeoutError(str(e)) | 38 raise device_errors.CommandTimeoutError(str(e)) |
| 36 except old_errors.DeviceUnresponsiveError as e: | 39 except old_errors.DeviceUnresponsiveError as e: |
| 37 raise device_errors.DeviceUnreachableError(str(e)) | 40 raise device_errors.DeviceUnreachableError(str(e)) |
| 38 except reraiser_thread.TimeoutError as e: | 41 except reraiser_thread.TimeoutError as e: |
| 39 raise device_errors.CommandTimeoutError(str(e)) | 42 raise device_errors.CommandTimeoutError(str(e)) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 58 return decorator | 61 return decorator |
| 59 | 62 |
| 60 | 63 |
| 61 def WithTimeoutAndRetriesDefaults(default_timeout, default_retries): | 64 def WithTimeoutAndRetriesDefaults(default_timeout, default_retries): |
| 62 """ | 65 """ |
| 63 A decorator that handles timeouts and retries using the provided defaults. | 66 A decorator that handles timeouts and retries using the provided defaults. |
| 64 """ | 67 """ |
| 65 def decorator(f): | 68 def decorator(f): |
| 66 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) | 69 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) |
| 67 get_retries = lambda *a, **kw: kw.get('retries', default_retries) | 70 get_retries = lambda *a, **kw: kw.get('retries', default_retries) |
| 68 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 71 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 69 return decorator | 72 return decorator |
| 70 | 73 |
| 71 | 74 |
| 72 def WithTimeoutAndRetriesFromInstance( | 75 def WithTimeoutAndRetriesFromInstance( |
| 73 default_timeout_name, default_retries_name): | 76 default_timeout_name, default_retries_name): |
| 74 """ | 77 """ |
| 75 A decorator that handles timeouts and retries using instance defaults. | 78 A decorator that handles timeouts and retries using instance defaults. |
| 76 """ | 79 """ |
| 77 def decorator(f): | 80 def decorator(f): |
| 78 def get_timeout(inst, *_args, **kwargs): | 81 def get_timeout(inst, *_args, **kwargs): |
| 79 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 82 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
| 80 def get_retries(inst, *_args, **kwargs): | 83 def get_retries(inst, *_args, **kwargs): |
| 81 return kwargs.get('retries', getattr(inst, default_retries_name)) | 84 return kwargs.get('retries', getattr(inst, default_retries_name)) |
| 82 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 85 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 83 return decorator | 86 return decorator |
| 84 | 87 |
| OLD | NEW |