| 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 import threading | 12 import threading |
| 13 | 13 |
| 14 from pylib import cmd_helper |
| 14 from pylib import constants | 15 from pylib import constants |
| 15 from pylib.device import device_errors | 16 from pylib.device import device_errors |
| 16 from pylib.utils import reraiser_thread | 17 from pylib.utils import reraiser_thread |
| 17 from pylib.utils import timeout_retry | 18 from pylib.utils import timeout_retry |
| 18 | 19 |
| 19 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer | 20 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
| 20 # backed by AndroidCommands / android_testrunner. | 21 # backed by AndroidCommands / android_testrunner. |
| 21 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | 22 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
| 22 'android_testrunner')) | 23 'android_testrunner')) |
| 23 import errors as old_errors | 24 import errors as old_errors |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 def impl(): | 50 def impl(): |
| 50 return f(*args, **kwargs) | 51 return f(*args, **kwargs) |
| 51 try: | 52 try: |
| 52 if isinstance(threading.current_thread(), | 53 if isinstance(threading.current_thread(), |
| 53 timeout_retry.TimeoutRetryThread): | 54 timeout_retry.TimeoutRetryThread): |
| 54 return impl() | 55 return impl() |
| 55 else: | 56 else: |
| 56 return timeout_retry.Run(impl, timeout, retries) | 57 return timeout_retry.Run(impl, timeout, retries) |
| 57 except old_errors.WaitForResponseTimedOutError as e: | 58 except old_errors.WaitForResponseTimedOutError as e: |
| 58 raise device_errors.CommandTimeoutError(str(e)), None, ( | 59 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 59 sys.exc_info()[2]) | 60 sys.exc_info()[2]) |
| 60 except old_errors.DeviceUnresponsiveError as e: | 61 except old_errors.DeviceUnresponsiveError as e: |
| 61 raise device_errors.DeviceUnreachableError(str(e)), None, ( | 62 raise device_errors.DeviceUnreachableError(str(e)), None, ( |
| 62 sys.exc_info()[2]) | 63 sys.exc_info()[2]) |
| 63 except reraiser_thread.TimeoutError as e: | 64 except reraiser_thread.TimeoutError as e: |
| 64 raise device_errors.CommandTimeoutError(str(e)), None, ( | 65 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 65 sys.exc_info()[2]) | 66 sys.exc_info()[2]) |
| 67 except cmd_helper.TimeoutError as e: |
| 68 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 69 sys.exc_info()[2]) |
| 66 return TimeoutRetryWrapper | 70 return TimeoutRetryWrapper |
| 67 | 71 |
| 68 | 72 |
| 69 def WithTimeoutAndRetries(f): | 73 def WithTimeoutAndRetries(f): |
| 70 """A decorator that handles timeouts and retries. | 74 """A decorator that handles timeouts and retries. |
| 71 | 75 |
| 72 'timeout' and 'retries' kwargs must be passed to the function. | 76 'timeout' and 'retries' kwargs must be passed to the function. |
| 73 | 77 |
| 74 Args: | 78 Args: |
| 75 f: The function to decorate. | 79 f: The function to decorate. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 The actual decorator. | 148 The actual decorator. |
| 145 """ | 149 """ |
| 146 def decorator(f): | 150 def decorator(f): |
| 147 def get_timeout(inst, *_args, **kwargs): | 151 def get_timeout(inst, *_args, **kwargs): |
| 148 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 152 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
| 149 def get_retries(inst, *_args, **kwargs): | 153 def get_retries(inst, *_args, **kwargs): |
| 150 return kwargs.get('retries', getattr(inst, default_retries_name)) | 154 return kwargs.get('retries', getattr(inst, default_retries_name)) |
| 151 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 155 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 152 return decorator | 156 return decorator |
| 153 | 157 |
| OLD | NEW |