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

Unified Diff: run_test_cases.py

Issue 12737010: Move call_with_timeout() guts into Proc.yield_any() generator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/swarm_client
Patch Set: update comments Created 7 years, 9 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: run_test_cases.py
diff --git a/run_test_cases.py b/run_test_cases.py
index 18eabfa901c0771b156572d44acb370cb216dcea..f06a2d3553f53270c67ceeac90b0659a97365daa 100755
--- a/run_test_cases.py
+++ b/run_test_cases.py
@@ -199,6 +199,31 @@ class Popen(subprocess.Popen):
self.end = time.time()
return ret
+ def yield_any(self, timeout=None):
+ """Yields output until the process terminates or is killed by a timeout.
+
+ Yielded values are in the form (pipename, data).
+ """
+ remaining = 0
+ while self.poll() is None:
+ if timeout:
+ remaining = max(timeout - self.duration(), 0.001)
+ t, data = self.recv_any(timeout=remaining)
+ if data:
+ yield (t, data)
+ if timeout and self.duration() >= timeout:
+ break
+ if self.poll() is None and timeout and self.duration() >= timeout:
+ logging.debug('Kill %s %s', self.duration(), timeout)
+ self.kill()
+ self.wait()
+ # Read all remaining output in the pipes.
+ while True:
+ t, data = self.recv_any()
+ if not data:
+ break
+ yield (t, data)
+
def recv_any(self, maxsize=None, timeout=None):
"""Reads from stderr and if empty, from stdout."""
pipes = [
@@ -253,25 +278,7 @@ def call_with_timeout(cmd, timeout, **kwargs):
if timeout:
out = ''
err = ''
- while proc.poll() is None:
- remaining = max(timeout - proc.duration(), 0.001)
- t, data = proc.recv_any(timeout=remaining)
- if data:
- if t == 'stdout':
- out += data
- else:
- err += data
- if proc.duration() >= timeout:
- break
- if proc.poll() is None and proc.duration() >= timeout:
- logging.debug('Kill %s %s', proc.duration(), timeout)
- proc.kill()
- proc.wait()
- # Try reading a last time.
- while True:
- t, data = proc.recv_any()
- if not data:
- break
+ for t, data in proc.yield_any(timeout):
if t == 'stdout':
out += data
else:
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698