| 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 | 13 |
| 13 from pylib import constants | 14 from pylib import constants |
| 14 from pylib.device import device_errors | 15 from pylib.device import device_errors |
| 15 from pylib.utils import reraiser_thread | 16 from pylib.utils import reraiser_thread |
| 16 from pylib.utils import timeout_retry | 17 from pylib.utils import timeout_retry |
| 17 | 18 |
| 18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer | 19 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
| 19 # backed by AndroidCommands / android_testrunner. | 20 # backed by AndroidCommands / android_testrunner. |
| 20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | 21 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
| 21 'android_testrunner')) | 22 'android_testrunner')) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 @functools.wraps(f) | 42 @functools.wraps(f) |
| 42 def TimeoutRetryWrapper(*args, **kwargs): | 43 def TimeoutRetryWrapper(*args, **kwargs): |
| 43 timeout = timeout_func(*args, **kwargs) | 44 timeout = timeout_func(*args, **kwargs) |
| 44 retries = retries_func(*args, **kwargs) | 45 retries = retries_func(*args, **kwargs) |
| 45 if pass_values: | 46 if pass_values: |
| 46 kwargs['timeout'] = timeout | 47 kwargs['timeout'] = timeout |
| 47 kwargs['retries'] = retries | 48 kwargs['retries'] = retries |
| 48 def impl(): | 49 def impl(): |
| 49 return f(*args, **kwargs) | 50 return f(*args, **kwargs) |
| 50 try: | 51 try: |
| 51 return timeout_retry.Run(impl, timeout, retries) | 52 if isinstance(threading.current_thread(), |
| 53 timeout_retry.TimeoutRetryThread): |
| 54 return impl() |
| 55 else: |
| 56 return timeout_retry.Run(impl, timeout, retries) |
| 52 except old_errors.WaitForResponseTimedOutError as e: | 57 except old_errors.WaitForResponseTimedOutError as e: |
| 53 raise device_errors.CommandTimeoutError(str(e)), None, ( | 58 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 54 sys.exc_info()[2]) | 59 sys.exc_info()[2]) |
| 55 except old_errors.DeviceUnresponsiveError as e: | 60 except old_errors.DeviceUnresponsiveError as e: |
| 56 raise device_errors.DeviceUnreachableError(str(e)), None, ( | 61 raise device_errors.DeviceUnreachableError(str(e)), None, ( |
| 57 sys.exc_info()[2]) | 62 sys.exc_info()[2]) |
| 58 except reraiser_thread.TimeoutError as e: | 63 except reraiser_thread.TimeoutError as e: |
| 59 raise device_errors.CommandTimeoutError(str(e)), None, ( | 64 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 60 sys.exc_info()[2]) | 65 sys.exc_info()[2]) |
| 61 return TimeoutRetryWrapper | 66 return TimeoutRetryWrapper |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 The actual decorator. | 144 The actual decorator. |
| 140 """ | 145 """ |
| 141 def decorator(f): | 146 def decorator(f): |
| 142 def get_timeout(inst, *_args, **kwargs): | 147 def get_timeout(inst, *_args, **kwargs): |
| 143 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 148 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
| 144 def get_retries(inst, *_args, **kwargs): | 149 def get_retries(inst, *_args, **kwargs): |
| 145 return kwargs.get('retries', getattr(inst, default_retries_name)) | 150 return kwargs.get('retries', getattr(inst, default_retries_name)) |
| 146 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 151 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 147 return decorator | 152 return decorator |
| 148 | 153 |
| OLD | NEW |