| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import functools | 7 import functools |
| 8 import inspect | 8 import inspect |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 Result of |condition| function (if present). | 102 Result of |condition| function (if present). |
| 103 """ | 103 """ |
| 104 def GetConditionString(): | 104 def GetConditionString(): |
| 105 if condition.__name__ == '<lambda>': | 105 if condition.__name__ == '<lambda>': |
| 106 try: | 106 try: |
| 107 return inspect.getsource(condition).strip() | 107 return inspect.getsource(condition).strip() |
| 108 except IOError: | 108 except IOError: |
| 109 pass | 109 pass |
| 110 return condition.__name__ | 110 return condition.__name__ |
| 111 | 111 |
| 112 # Do an initial check to see if its true. |
| 113 res = condition() |
| 114 if res: |
| 115 return res |
| 112 start_time = time.time() | 116 start_time = time.time() |
| 113 last_output_time = start_time | 117 last_output_time = start_time |
| 114 elapsed_time = time.time() - start_time | 118 elapsed_time = time.time() - start_time |
| 115 while elapsed_time < timeout: | 119 while elapsed_time < timeout: |
| 116 res = condition() | 120 res = condition() |
| 117 if res: | 121 if res: |
| 118 return res | 122 return res |
| 119 now = time.time() | 123 now = time.time() |
| 120 elapsed_time = now - start_time | 124 elapsed_time = now - start_time |
| 121 last_output_elapsed_time = now - last_output_time | 125 last_output_elapsed_time = now - last_output_time |
| 122 if last_output_elapsed_time > OUTPUT_INTERVAL_IN_SECONDS: | 126 if last_output_elapsed_time > OUTPUT_INTERVAL_IN_SECONDS: |
| 123 last_output_time = time.time() | 127 last_output_time = time.time() |
| 124 poll_interval = min(max(elapsed_time / 10., MIN_POLL_INTERVAL_IN_SECONDS), | 128 poll_interval = min(max(elapsed_time / 10., MIN_POLL_INTERVAL_IN_SECONDS), |
| 125 MAX_POLL_INTERVAL_IN_SECONDS) | 129 MAX_POLL_INTERVAL_IN_SECONDS) |
| 126 time.sleep(poll_interval) | 130 time.sleep(poll_interval) |
| 127 raise TimeoutException('Timed out while waiting %ds for %s.' % | 131 raise TimeoutException('Timed out while waiting %ds for %s.' % |
| 128 (timeout, GetConditionString())) | 132 (timeout, GetConditionString())) |
| 129 | 133 |
| 130 class TimeoutException(Exception): | 134 class TimeoutException(Exception): |
| 131 """The operation failed to complete because of a timeout. | 135 """The operation failed to complete because of a timeout. |
| 132 | 136 |
| 133 It is possible that waiting for a longer period of time would result in a | 137 It is possible that waiting for a longer period of time would result in a |
| 134 successful operation. | 138 successful operation. |
| 135 """ | 139 """ |
| 136 pass | 140 pass |
| OLD | NEW |