| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """A utility to run functions with timeouts and retries.""" | 5 """A utility to run functions with timeouts and retries.""" |
| 6 # pylint: disable=W0702 | 6 # pylint: disable=W0702 |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import threading | 9 import threading |
| 10 import time | 10 import time |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 and the timeout expires. | 106 and the timeout expires. |
| 107 """ | 107 """ |
| 108 condition_name = condition.__name__ | 108 condition_name = condition.__name__ |
| 109 timeout_thread = CurrentTimeoutThread() | 109 timeout_thread = CurrentTimeoutThread() |
| 110 while max_tries is None or max_tries > 0: | 110 while max_tries is None or max_tries > 0: |
| 111 result = condition() | 111 result = condition() |
| 112 if max_tries is not None: | 112 if max_tries is not None: |
| 113 max_tries -= 1 | 113 max_tries -= 1 |
| 114 msg = ['condition', repr(condition_name), 'met' if result else 'not met'] | 114 msg = ['condition', repr(condition_name), 'met' if result else 'not met'] |
| 115 if timeout_thread: | 115 if timeout_thread: |
| 116 # pylint: disable=no-member |
| 116 msg.append('(%.1fs)' % timeout_thread.GetElapsedTime()) | 117 msg.append('(%.1fs)' % timeout_thread.GetElapsedTime()) |
| 117 logging.info(' '.join(msg)) | 118 logging.info(' '.join(msg)) |
| 118 if result: | 119 if result: |
| 119 return result | 120 return result |
| 120 if timeout_thread: | 121 if timeout_thread: |
| 122 # pylint: disable=no-member |
| 121 timeout_thread.GetRemainingTime(wait_period, | 123 timeout_thread.GetRemainingTime(wait_period, |
| 122 msg='Timed out waiting for %r' % condition_name) | 124 msg='Timed out waiting for %r' % condition_name) |
| 123 time.sleep(wait_period) | 125 time.sleep(wait_period) |
| 124 return None | 126 return None |
| 125 | 127 |
| 126 | 128 |
| 127 def Run(func, timeout, retries, args=None, kwargs=None): | 129 def Run(func, timeout, retries, args=None, kwargs=None): |
| 128 """Runs the passed function in a separate thread with timeouts and retries. | 130 """Runs the passed function in a separate thread with timeouts and retries. |
| 129 | 131 |
| 130 Args: | 132 Args: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 158 try: | 160 try: |
| 159 thread_group = reraiser_thread.ReraiserThreadGroup([child_thread]) | 161 thread_group = reraiser_thread.ReraiserThreadGroup([child_thread]) |
| 160 thread_group.StartAll() | 162 thread_group.StartAll() |
| 161 thread_group.JoinAll(child_thread.GetWatcher()) | 163 thread_group.JoinAll(child_thread.GetWatcher()) |
| 162 return ret[0] | 164 return ret[0] |
| 163 except: | 165 except: |
| 164 child_thread.LogTimeoutException() | 166 child_thread.LogTimeoutException() |
| 165 if num_try > retries: | 167 if num_try > retries: |
| 166 raise | 168 raise |
| 167 num_try += 1 | 169 num_try += 1 |
| OLD | NEW |