| OLD | NEW |
| 1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 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 import glob | 4 import glob |
| 5 import imp | 5 import imp |
| 6 import inspect | 6 import inspect |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import socket | 9 import socket |
| 10 import sys | 10 import sys |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 _counter = [0] | 44 _counter = [0] |
| 45 def _GetUniqueModuleName(): | 45 def _GetUniqueModuleName(): |
| 46 _counter[0] += 1 | 46 _counter[0] += 1 |
| 47 return "page_set_module_" + str(_counter[0]) | 47 return "page_set_module_" + str(_counter[0]) |
| 48 | 48 |
| 49 def GetPythonPageSetModule(file_path): | 49 def GetPythonPageSetModule(file_path): |
| 50 return imp.load_source(_GetUniqueModuleName(), file_path) | 50 return imp.load_source(_GetUniqueModuleName(), file_path) |
| 51 | 51 |
| 52 | 52 |
| 53 def WaitFor(condition, timeout): | 53 def WaitFor(condition, timeout, fixed_poll_interval=None): |
| 54 """Waits for up to |timeout| secs for the function |condition| to return True. | 54 """Waits for up to |timeout| secs for the function |condition| to return True. |
| 55 | 55 |
| 56 Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s. | 56 Polling interval is (elapsed_time / 10), with a min of .1s and max of 5s, |
| 57 unless |fixed_poll_interval| is specified. |
| 57 | 58 |
| 58 Returns: | 59 Returns: |
| 59 Result of |condition| function (if present). | 60 Result of |condition| function (if present). |
| 60 """ | 61 """ |
| 61 min_poll_interval = 0.1 | 62 min_poll_interval = 0.1 |
| 62 max_poll_interval = 5 | 63 max_poll_interval = 5 |
| 63 output_interval = 300 | 64 output_interval = 300 |
| 64 | 65 |
| 65 def GetConditionString(): | 66 def GetConditionString(): |
| 66 if condition.__name__ == '<lambda>': | 67 if condition.__name__ == '<lambda>': |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 now = time.time() | 80 now = time.time() |
| 80 elapsed_time = now - start_time | 81 elapsed_time = now - start_time |
| 81 last_output_elapsed_time = now - last_output_time | 82 last_output_elapsed_time = now - last_output_time |
| 82 if elapsed_time > timeout: | 83 if elapsed_time > timeout: |
| 83 raise TimeoutException('Timed out while waiting %ds for %s.' % | 84 raise TimeoutException('Timed out while waiting %ds for %s.' % |
| 84 (timeout, GetConditionString())) | 85 (timeout, GetConditionString())) |
| 85 if last_output_elapsed_time > output_interval: | 86 if last_output_elapsed_time > output_interval: |
| 86 logging.info('Continuing to wait %ds for %s. Elapsed: %ds.', | 87 logging.info('Continuing to wait %ds for %s. Elapsed: %ds.', |
| 87 timeout, GetConditionString(), elapsed_time) | 88 timeout, GetConditionString(), elapsed_time) |
| 88 last_output_time = time.time() | 89 last_output_time = time.time() |
| 89 poll_interval = min(max(elapsed_time / 10., min_poll_interval), | 90 poll_interval = fixed_poll_interval or \ |
| 91 min(max(elapsed_time / 10., min_poll_interval), |
| 90 max_poll_interval) | 92 max_poll_interval) |
| 91 time.sleep(poll_interval) | 93 time.sleep(poll_interval) |
| 92 | 94 |
| 93 | 95 |
| 94 def GetUnreservedAvailableLocalPort(): | 96 def GetUnreservedAvailableLocalPort(): |
| 95 """Returns an available port on the system. | 97 """Returns an available port on the system. |
| 96 | 98 |
| 97 WARNING: This method does not reserve the port it returns, so it may be used | 99 WARNING: This method does not reserve the port it returns, so it may be used |
| 98 by something else before you get to use it. This can lead to flake. | 100 by something else before you get to use it. This can lead to flake. |
| 99 """ | 101 """ |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 """ | 137 """ |
| 136 name, ext = os.path.splitext(base_name) | 138 name, ext = os.path.splitext(base_name) |
| 137 assert ext == '', 'base_name cannot contain file extension.' | 139 assert ext == '', 'base_name cannot contain file extension.' |
| 138 index = 0 | 140 index = 0 |
| 139 while True: | 141 while True: |
| 140 output_name = '%s_%03d' % (name, index) | 142 output_name = '%s_%03d' % (name, index) |
| 141 if not glob.glob(output_name + '.*'): | 143 if not glob.glob(output_name + '.*'): |
| 142 break | 144 break |
| 143 index = index + 1 | 145 index = index + 1 |
| 144 return output_name | 146 return output_name |
| OLD | NEW |