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

Unified Diff: common/py_utils/py_utils/__init__.py

Issue 2451553006: [Common] Move WaitFor from telemetry to common/py_utils (Closed)
Patch Set: nix logging Created 4 years, 2 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | common/py_utils/py_utils/py_utils_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/py_utils/py_utils/__init__.py
diff --git a/common/py_utils/py_utils/__init__.py b/common/py_utils/py_utils/__init__.py
index e5081706e51bee1d95a49104f20b80e63dd421e1..fb662bc4bb8b3473256d499767fe821435f65bd8 100644
--- a/common/py_utils/py_utils/__init__.py
+++ b/common/py_utils/py_utils/__init__.py
@@ -5,8 +5,10 @@
# found in the LICENSE file.
import functools
+import inspect
import os
import sys
+import time
def GetCatapultDir():
@@ -85,3 +87,50 @@ def TimeoutDeco(func, default_timeout):
print '%s timed out.' % func.__name__
return False
return RunWithTimeout
+
+
+MIN_POLL_INTERVAL_IN_SECONDS = 0.1
+MAX_POLL_INTERVAL_IN_SECONDS = 5
+OUTPUT_INTERVAL_IN_SECONDS = 300
+
+def WaitFor(condition, timeout):
+ """Waits for up to |timeout| secs for the function |condition| to return True.
+
+ Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s.
+
+ Returns:
+ Result of |condition| function (if present).
+ """
+ def GetConditionString():
+ if condition.__name__ == '<lambda>':
+ try:
+ return inspect.getsource(condition).strip()
+ except IOError:
+ pass
+ return condition.__name__
+
+ start_time = time.time()
+ last_output_time = start_time
+ elapsed_time = time.time() - start_time
+ while elapsed_time < timeout:
+ res = condition()
+ if res:
+ return res
+ now = time.time()
+ elapsed_time = now - start_time
+ last_output_elapsed_time = now - last_output_time
+ if last_output_elapsed_time > OUTPUT_INTERVAL_IN_SECONDS:
+ last_output_time = time.time()
+ poll_interval = min(max(elapsed_time / 10., MIN_POLL_INTERVAL_IN_SECONDS),
+ MAX_POLL_INTERVAL_IN_SECONDS)
+ time.sleep(poll_interval)
+ raise TimeoutException('Timed out while waiting %ds for %s.' %
+ (timeout, GetConditionString()))
+
+class TimeoutException(Exception):
+ """The operation failed to complete because of a timeout.
+
+ It is possible that waiting for a longer period of time would result in a
+ successful operation.
+ """
+ pass
« no previous file with comments | « no previous file | common/py_utils/py_utils/py_utils_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698