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 cmd_helper |
15 from pylib import constants | 15 from pylib import constants |
16 from pylib.device import device_errors | 16 from pylib.device import device_errors |
17 from pylib.utils import reraiser_thread | 17 from pylib.utils import reraiser_thread |
18 from pylib.utils import timeout_retry | 18 from pylib.utils import timeout_retry |
19 | 19 |
| 20 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
| 21 # backed by AndroidCommands / android_testrunner. |
| 22 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
| 23 'android_testrunner')) |
| 24 import errors as old_errors |
| 25 |
20 DEFAULT_TIMEOUT_ATTR = '_default_timeout' | 26 DEFAULT_TIMEOUT_ATTR = '_default_timeout' |
21 DEFAULT_RETRIES_ATTR = '_default_retries' | 27 DEFAULT_RETRIES_ATTR = '_default_retries' |
22 | 28 |
23 | 29 |
24 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): | 30 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): |
25 """ Wraps a funcion with timeout and retry handling logic. | 31 """ Wraps a funcion with timeout and retry handling logic. |
26 | 32 |
27 Args: | 33 Args: |
28 f: The function to wrap. | 34 f: The function to wrap. |
29 timeout_func: A callable that returns the timeout value. | 35 timeout_func: A callable that returns the timeout value. |
(...skipping 12 matching lines...) Expand all Loading... |
42 kwargs['timeout'] = timeout | 48 kwargs['timeout'] = timeout |
43 kwargs['retries'] = retries | 49 kwargs['retries'] = retries |
44 def impl(): | 50 def impl(): |
45 return f(*args, **kwargs) | 51 return f(*args, **kwargs) |
46 try: | 52 try: |
47 if isinstance(threading.current_thread(), | 53 if isinstance(threading.current_thread(), |
48 timeout_retry.TimeoutRetryThread): | 54 timeout_retry.TimeoutRetryThread): |
49 return impl() | 55 return impl() |
50 else: | 56 else: |
51 return timeout_retry.Run(impl, timeout, retries) | 57 return timeout_retry.Run(impl, timeout, retries) |
| 58 except old_errors.WaitForResponseTimedOutError as e: |
| 59 raise device_errors.CommandTimeoutError(str(e)), None, ( |
| 60 sys.exc_info()[2]) |
| 61 except old_errors.DeviceUnresponsiveError as e: |
| 62 raise device_errors.DeviceUnreachableError(str(e)), None, ( |
| 63 sys.exc_info()[2]) |
52 except reraiser_thread.TimeoutError as e: | 64 except reraiser_thread.TimeoutError as e: |
53 raise device_errors.CommandTimeoutError(str(e)), None, ( | 65 raise device_errors.CommandTimeoutError(str(e)), None, ( |
54 sys.exc_info()[2]) | 66 sys.exc_info()[2]) |
55 except cmd_helper.TimeoutError as e: | 67 except cmd_helper.TimeoutError as e: |
56 raise device_errors.CommandTimeoutError(str(e)), None, ( | 68 raise device_errors.CommandTimeoutError(str(e)), None, ( |
57 sys.exc_info()[2]) | 69 sys.exc_info()[2]) |
58 return TimeoutRetryWrapper | 70 return TimeoutRetryWrapper |
59 | 71 |
60 | 72 |
61 def WithTimeoutAndRetries(f): | 73 def WithTimeoutAndRetries(f): |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 The actual decorator. | 148 The actual decorator. |
137 """ | 149 """ |
138 def decorator(f): | 150 def decorator(f): |
139 def get_timeout(inst, *_args, **kwargs): | 151 def get_timeout(inst, *_args, **kwargs): |
140 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 152 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
141 def get_retries(inst, *_args, **kwargs): | 153 def get_retries(inst, *_args, **kwargs): |
142 return kwargs.get('retries', getattr(inst, default_retries_name)) | 154 return kwargs.get('retries', getattr(inst, default_retries_name)) |
143 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 155 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
144 return decorator | 156 return decorator |
145 | 157 |
OLD | NEW |