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 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | |
craigdh
2014/05/14 20:33:08
if this is temporary please add a TODO saying when
jbudorick
2014/05/14 21:02:01
Done.
| |
19 'android_testrunner')) | |
20 import errors as old_errors | |
21 | |
15 | 22 |
16 def _TimeoutRetryWrapper(f, timeout_func, retries_func): | 23 def _TimeoutRetryWrapper(f, timeout_func, retries_func): |
17 @functools.wraps(f) | 24 @functools.wraps(f) |
18 def TimeoutRetryWrapper(*args, **kwargs): | 25 def TimeoutRetryWrapper(*args, **kwargs): |
19 timeout = timeout_func(*args, **kwargs) | 26 timeout = timeout_func(*args, **kwargs) |
20 retries = retries_func(*args, **kwargs) | 27 retries = retries_func(*args, **kwargs) |
21 def impl(): | 28 def impl(): |
22 return f(*args, **kwargs) | 29 return f(*args, **kwargs) |
23 try: | 30 try: |
24 return timeout_retry.Run(impl, timeout, retries) | 31 return timeout_retry.Run(impl, timeout, retries) |
32 except old_errors.WaitForResponseTimedOutError as e: | |
33 raise device_errors.CommandTimeoutError(str(e)) | |
34 except old_errors.DeviceUnresponsiveError as e: | |
35 raise device_errors.DeviceUnreachableError(str(e)) | |
25 except reraiser_thread.TimeoutError as e: | 36 except reraiser_thread.TimeoutError as e: |
26 raise device_errors.CommandTimeoutError(str(e)) | 37 raise device_errors.CommandTimeoutError(str(e)) |
27 return TimeoutRetryWrapper | 38 return TimeoutRetryWrapper |
28 | 39 |
29 | 40 |
30 def WithTimeoutAndRetries(f): | 41 def WithTimeoutAndRetries(f): |
31 """A decorator that handles timeouts and retries.""" | 42 """A decorator that handles timeouts and retries.""" |
32 get_timeout = lambda *a, **kw: kw['timeout'] | 43 get_timeout = lambda *a, **kw: kw['timeout'] |
33 get_retries = lambda *a, **kw: kw['retries'] | 44 get_retries = lambda *a, **kw: kw['retries'] |
34 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 45 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. | 73 A decorator that handles timeouts and retries using instance defaults. |
63 """ | 74 """ |
64 def decorator(f): | 75 def decorator(f): |
65 def get_timeout(inst, *_args, **kwargs): | 76 def get_timeout(inst, *_args, **kwargs): |
66 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 77 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
67 def get_retries(inst, *_args, **kwargs): | 78 def get_retries(inst, *_args, **kwargs): |
68 return kwargs.get('retries', getattr(inst, default_retries_name)) | 79 return kwargs.get('retries', getattr(inst, default_retries_name)) |
69 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 80 return _TimeoutRetryWrapper(f, get_timeout, get_retries) |
70 return decorator | 81 return decorator |
71 | 82 |
OLD | NEW |