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 | 12 |
13 from pylib import constants | 13 from pylib import constants |
14 from pylib.device import device_errors | 14 from pylib.device import device_errors |
15 from pylib.utils import reraiser_thread | 15 from pylib.utils import reraiser_thread |
16 from pylib.utils import timeout_retry | 16 from pylib.utils import timeout_retry |
17 | 17 |
18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer | 18 # TODO(jbudorick) Remove once the DeviceUtils implementations are no longer |
19 # backed by AndroidCommands / android_testrunner. | 19 # backed by AndroidCommands / android_testrunner. |
20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', | 20 sys.path.append(os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', |
21 'android_testrunner')) | 21 'android_testrunner')) |
22 import errors as old_errors | 22 import errors as old_errors |
23 | 23 |
| 24 DEFAULT_TIMEOUT_ATTR = '_default_timeout' |
| 25 DEFAULT_RETRIES_ATTR = '_default_retries' |
24 | 26 |
25 def _TimeoutRetryWrapper(f, timeout_func, retries_func): | 27 |
| 28 def _TimeoutRetryWrapper(f, timeout_func, retries_func, pass_values=False): |
| 29 """ Wraps a funcion with timeout and retry handling logic. |
| 30 |
| 31 Args: |
| 32 f: The function to wrap. |
| 33 timeout_func: A callable that returns the timeout value. |
| 34 retries_func: A callable that returns the retries value. |
| 35 pass_values: If True, passes the values returned by |timeout_func| and |
| 36 |retries_func| to the wrapped function as 'timeout' and |
| 37 'retries' kwargs, respectively. |
| 38 Returns: |
| 39 The wrapped function. |
| 40 """ |
26 @functools.wraps(f) | 41 @functools.wraps(f) |
27 def TimeoutRetryWrapper(*args, **kwargs): | 42 def TimeoutRetryWrapper(*args, **kwargs): |
28 timeout = timeout_func(*args, **kwargs) | 43 timeout = timeout_func(*args, **kwargs) |
29 retries = retries_func(*args, **kwargs) | 44 retries = retries_func(*args, **kwargs) |
| 45 if pass_values: |
| 46 kwargs['timeout'] = timeout |
| 47 kwargs['retries'] = retries |
30 def impl(): | 48 def impl(): |
31 return f(*args, **kwargs) | 49 return f(*args, **kwargs) |
32 try: | 50 try: |
33 return timeout_retry.Run(impl, timeout, retries) | 51 return timeout_retry.Run(impl, timeout, retries) |
34 except old_errors.WaitForResponseTimedOutError as e: | 52 except old_errors.WaitForResponseTimedOutError as e: |
35 raise device_errors.CommandTimeoutError(str(e)) | 53 raise device_errors.CommandTimeoutError(str(e)) |
36 except old_errors.DeviceUnresponsiveError as e: | 54 except old_errors.DeviceUnresponsiveError as e: |
37 raise device_errors.DeviceUnreachableError(str(e)) | 55 raise device_errors.DeviceUnreachableError(str(e)) |
38 except reraiser_thread.TimeoutError as e: | 56 except reraiser_thread.TimeoutError as e: |
39 raise device_errors.CommandTimeoutError(str(e)) | 57 raise device_errors.CommandTimeoutError(str(e)) |
40 return TimeoutRetryWrapper | 58 return TimeoutRetryWrapper |
41 | 59 |
42 | 60 |
43 def WithTimeoutAndRetries(f): | 61 def WithTimeoutAndRetries(f): |
44 """A decorator that handles timeouts and retries.""" | 62 """A decorator that handles timeouts and retries. |
| 63 |
| 64 'timeout' and 'retries' kwargs must be passed to the function. |
| 65 |
| 66 Args: |
| 67 f: The function to decorate. |
| 68 Returns: |
| 69 The decorated function. |
| 70 """ |
45 get_timeout = lambda *a, **kw: kw['timeout'] | 71 get_timeout = lambda *a, **kw: kw['timeout'] |
46 get_retries = lambda *a, **kw: kw['retries'] | 72 get_retries = lambda *a, **kw: kw['retries'] |
47 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 73 return _TimeoutRetryWrapper(f, get_timeout, get_retries) |
48 | 74 |
49 | 75 |
50 def WithExplicitTimeoutAndRetries(timeout, retries): | 76 def WithExplicitTimeoutAndRetries(timeout, retries): |
51 """ | 77 """Returns a decorator that handles timeouts and retries. |
52 A decorator that handles timeouts and retries using the provided values. | 78 |
| 79 The provided |timeout| and |retries| values are always used. |
| 80 |
| 81 Args: |
| 82 timeout: The number of seconds to wait for the decorated function to |
| 83 return. Always used. |
| 84 retries: The number of times the decorated function should be retried on |
| 85 failure. Always used. |
| 86 Returns: |
| 87 The actual decorator. |
53 """ | 88 """ |
54 def decorator(f): | 89 def decorator(f): |
55 get_timeout = lambda *a, **kw: timeout | 90 get_timeout = lambda *a, **kw: timeout |
56 get_retries = lambda *a, **kw: retries | 91 get_retries = lambda *a, **kw: retries |
57 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 92 return _TimeoutRetryWrapper(f, get_timeout, get_retries) |
58 return decorator | 93 return decorator |
59 | 94 |
60 | 95 |
61 def WithTimeoutAndRetriesDefaults(default_timeout, default_retries): | 96 def WithTimeoutAndRetriesDefaults(default_timeout, default_retries): |
62 """ | 97 """Returns a decorator that handles timeouts and retries. |
63 A decorator that handles timeouts and retries using the provided defaults. | 98 |
| 99 The provided |default_timeout| and |default_retries| values are used only |
| 100 if timeout and retries values are not provided. |
| 101 |
| 102 Args: |
| 103 default_timeout: The number of seconds to wait for the decorated function |
| 104 to return. Only used if a 'timeout' kwarg is not passed |
| 105 to the decorated function. |
| 106 default_retries: The number of times the decorated function should be |
| 107 retried on failure. Only used if a 'retries' kwarg is not |
| 108 passed to the decorated function. |
| 109 Returns: |
| 110 The actual decorator. |
64 """ | 111 """ |
65 def decorator(f): | 112 def decorator(f): |
66 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) | 113 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) |
67 get_retries = lambda *a, **kw: kw.get('retries', default_retries) | 114 get_retries = lambda *a, **kw: kw.get('retries', default_retries) |
68 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 115 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
69 return decorator | 116 return decorator |
70 | 117 |
71 | 118 |
72 def WithTimeoutAndRetriesFromInstance( | 119 def WithTimeoutAndRetriesFromInstance( |
73 default_timeout_name, default_retries_name): | 120 default_timeout_name=DEFAULT_TIMEOUT_ATTR, |
74 """ | 121 default_retries_name=DEFAULT_RETRIES_ATTR): |
75 A decorator that handles timeouts and retries using instance defaults. | 122 """Returns a decorator that handles timeouts and retries. |
| 123 |
| 124 The provided |default_timeout_name| and |default_retries_name| are used to |
| 125 get the default timeout value and the default retries value from the object |
| 126 instance if timeout and retries values are not provided. |
| 127 |
| 128 Note that this should only be used to decorate methods, not functions. |
| 129 |
| 130 Args: |
| 131 default_timeout_name: The name of the default timeout attribute of the |
| 132 instance. |
| 133 default_retries_name: The name of the default retries attribute of the |
| 134 instance. |
| 135 Returns: |
| 136 The actual decorator. |
76 """ | 137 """ |
77 def decorator(f): | 138 def decorator(f): |
78 def get_timeout(inst, *_args, **kwargs): | 139 def get_timeout(inst, *_args, **kwargs): |
79 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 140 return kwargs.get('timeout', getattr(inst, default_timeout_name)) |
80 def get_retries(inst, *_args, **kwargs): | 141 def get_retries(inst, *_args, **kwargs): |
81 return kwargs.get('retries', getattr(inst, default_retries_name)) | 142 return kwargs.get('retries', getattr(inst, default_retries_name)) |
82 return _TimeoutRetryWrapper(f, get_timeout, get_retries) | 143 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
83 return decorator | 144 return decorator |
84 | 145 |
OLD | NEW |