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 |
| 11 import sys |
10 | 12 |
| 13 from pylib import constants |
11 from pylib.device import device_errors | 14 from pylib.device import device_errors |
12 from pylib.utils import reraiser_thread | 15 from pylib.utils import reraiser_thread |
13 from pylib.utils import timeout_retry | 16 from pylib.utils import timeout_retry |
14 | 17 |
| 18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
| 19 # backed by AndroidCommands / android_testrunner. |
| 20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
| 21 'android_testrunner')) |
| 22 import errors as old_errors |
| 23 |
15 | 24 |
16 def _TimeoutRetryWrapper(f, timeout_func, retries_func): | 25 def _TimeoutRetryWrapper(f, timeout_func, retries_func): |
17 @functools.wraps(f) | 26 @functools.wraps(f) |
18 def TimeoutRetryWrapper(*args, **kwargs): | 27 def TimeoutRetryWrapper(*args, **kwargs): |
19 timeout = timeout_func(*args, **kwargs) | 28 timeout = timeout_func(*args, **kwargs) |
20 retries = retries_func(*args, **kwargs) | 29 retries = retries_func(*args, **kwargs) |
21 def impl(): | 30 def impl(): |
22 return f(*args, **kwargs) | 31 return f(*args, **kwargs) |
23 try: | 32 try: |
24 return timeout_retry.Run(impl, timeout, retries) | 33 return timeout_retry.Run(impl, timeout, retries) |
| 34 except old_errors.WaitForResponseTimedOutError as e: |
| 35 raise device_errors.CommandTimeoutError(str(e)) |
| 36 except old_errors.DeviceUnresponsiveError as e: |
| 37 raise device_errors.DeviceUnreachableError(str(e)) |
25 except reraiser_thread.TimeoutError as e: | 38 except reraiser_thread.TimeoutError as e: |
26 raise device_errors.CommandTimeoutError(str(e)) | 39 raise device_errors.CommandTimeoutError(str(e)) |
27 return TimeoutRetryWrapper | 40 return TimeoutRetryWrapper |
28 | 41 |
29 | 42 |
30 def WithTimeoutAndRetries(f): | 43 def WithTimeoutAndRetries(f): |
31 """A decorator that handles timeouts and retries.""" | 44 """A decorator that handles timeouts and retries.""" |
32 get_timeout = lambda *a, **kw: kw['timeout'] | 45 get_timeout = lambda *a, **kw: kw['timeout'] |
33 get_retries = lambda *a, **kw: kw['retries'] | 46 get_retries = lambda *a, **kw: kw['retries'] |
34 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 47 return _TimeoutRetryWrapper(f, get_timeout, get_retries) |
(...skipping 27 matching lines...) Expand all Loading... |
62 A decorator that handles timeouts and retries using instance defaults. | 75 A decorator that handles timeouts and retries using instance defaults. |
63 """ | 76 """ |
64 def decorator(f): | 77 def decorator(f): |
65 def get_timeout(inst, *_args, **kwargs): | 78 def get_timeout(inst, *_args, **kwargs): |
66 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 79 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
67 def get_retries(inst, *_args, **kwargs): | 80 def get_retries(inst, *_args, **kwargs): |
68 return kwargs.get('retries', getattr(inst, default_retries_name)) | 81 return kwargs.get('retries', getattr(inst, default_retries_name)) |
69 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 82 return _TimeoutRetryWrapper(f, get_timeout, get_retries) |
70 return decorator | 83 return decorator |
71 | 84 |
OLD | NEW |