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