Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: common/py_utils/py_utils/__init__.py

Issue 2451553006: [Common] Move WaitFor from telemetry to common/py_utils (Closed)
Patch Set: change times in unittetss Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
9 import logging
8 import os 10 import os
9 import sys 11 import sys
12 import time
10 13
11 14
12 def GetCatapultDir(): 15 def GetCatapultDir():
13 return os.path.normpath( 16 return os.path.normpath(
14 os.path.join(os.path.dirname(__file__), '..', '..', '..')) 17 os.path.join(os.path.dirname(__file__), '..', '..', '..'))
15 18
16 19
17 def IsRunningOnCrosDevice(): 20 def IsRunningOnCrosDevice():
18 """Returns True if we're on a ChromeOS device.""" 21 """Returns True if we're on a ChromeOS device."""
19 lsb_release = '/etc/lsb-release' 22 lsb_release = '/etc/lsb-release'
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if 'timeout' in kwargs: 81 if 'timeout' in kwargs:
79 timeout = kwargs['timeout'] 82 timeout = kwargs['timeout']
80 else: 83 else:
81 timeout = default_timeout 84 timeout = default_timeout
82 try: 85 try:
83 return timeout_retry.Run(func, timeout, 0, args=args) 86 return timeout_retry.Run(func, timeout, 0, args=args)
84 except reraiser_thread.TimeoutError: 87 except reraiser_thread.TimeoutError:
85 print '%s timed out.' % func.__name__ 88 print '%s timed out.' % func.__name__
86 return False 89 return False
87 return RunWithTimeout 90 return RunWithTimeout
91
92
93 MIN_POLL_INTERVAL_IN_SECONDS = 0.1
94 MAX_POLL_INTERVAL_IN_SECONDS = 5
95 OUTPUT_INTERVAL_IN_SECONDS = 300
96
97 def WaitFor(condition, timeout):
98 """Waits for up to |timeout| secs for the function |condition| to return True.
99
100 Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s.
101
102 Returns:
103 Result of |condition| function (if present).
104 """
105 def GetConditionString():
106 if condition.__name__ == '<lambda>':
107 try:
108 return inspect.getsource(condition).strip()
109 except IOError:
110 pass
111 return condition.__name__
112
113 start_time = time.time()
114 last_output_time = start_time
115 elapsed_time = time.time() - start_time
116 while elapsed_time < timeout:
117 res = condition()
118 if res:
119 return res
120 now = time.time()
121 elapsed_time = now - start_time
122 last_output_elapsed_time = now - last_output_time
123 if last_output_elapsed_time > OUTPUT_INTERVAL_IN_SECONDS:
124 logging.info('Continuing to wait %ds for %s. Elapsed: %ds.', timeout,
125 GetConditionString(), elapsed_time)
nednguyen 2016/10/26 18:59:40 pings about remove this log
126 last_output_time = time.time()
127 poll_interval = min(max(elapsed_time / 10., MIN_POLL_INTERVAL_IN_SECONDS),
128 MAX_POLL_INTERVAL_IN_SECONDS)
129 time.sleep(poll_interval)
130 raise TimeoutException('Timed out while waiting %ds for %s.' %
131 (timeout, GetConditionString()))
132
133 class TimeoutException(Exception):
134 """The operation failed to complete because of a timeout.
135
136 It is possible that waiting for a longer period of time would result in a
137 successful operation.
138 """
139 pass
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698