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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Runs each test cases as a single shard, single process execution. 6 """Runs each test cases as a single shard, single process execution.
7 7
8 Similar to sharding_supervisor.py but finer grained. It runs each test case 8 Similar to sharding_supervisor.py but finer grained. It runs each test case
9 individually instead of running per shard. Runs multiple instances in parallel. 9 individually instead of running per shard. Runs multiple instances in parallel.
10 """ 10 """
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 # communicate() uses wait() internally. 192 # communicate() uses wait() internally.
193 self.end = time.time() 193 self.end = time.time()
194 return ret 194 return ret
195 195
196 def poll(self): 196 def poll(self):
197 ret = super(Popen, self).poll() 197 ret = super(Popen, self).poll()
198 if ret is not None and not self.end: 198 if ret is not None and not self.end:
199 self.end = time.time() 199 self.end = time.time()
200 return ret 200 return ret
201 201
202 def yield_any(self, timeout=None):
203 """Yields output until the process terminates or is killed by a timeout.
204
205 Yielded values are in the form (pipename, data).
206 """
207 remaining = 0
csharp 2013/03/21 19:14:16 nit: remaining -> remaining_time
208 while self.poll() is None:
209 if timeout:
210 remaining = max(timeout - self.duration(), 0.001)
211 t, data = self.recv_any(timeout=remaining)
212 if data:
213 yield (t, data)
214 if timeout and self.duration() >= timeout:
215 break
216 if self.poll() is None and timeout and self.duration() >= timeout:
217 logging.debug('Kill %s %s', self.duration(), timeout)
218 self.kill()
219 self.wait()
220 # Try reading a last time.
csharp 2013/03/21 19:14:16 nit: Try reading a last time -> Read all remaining
221 while True:
222 t, data = self.recv_any()
223 if not data:
224 break
225 yield (t, data)
226
202 def recv_any(self, maxsize=None, timeout=None): 227 def recv_any(self, maxsize=None, timeout=None):
203 """Reads from stderr and if empty, from stdout.""" 228 """Reads from stderr and if empty, from stdout."""
204 pipes = [ 229 pipes = [
205 x for x in ((self.stderr, 'stderr'), (self.stdout, 'stdout')) if x[0] 230 x for x in ((self.stderr, 'stderr'), (self.stdout, 'stdout')) if x[0]
206 ] 231 ]
207 if len(pipes) == 2 and self.stderr.fileno() == self.stdout.fileno(): 232 if len(pipes) == 2 and self.stderr.fileno() == self.stdout.fileno():
208 pipes.pop(0) 233 pipes.pop(0)
209 if not pipes: 234 if not pipes:
210 return None, None 235 return None, None
211 conns, names = zip(*pipes) 236 conns, names = zip(*pipes)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 def call_with_timeout(cmd, timeout, **kwargs): 271 def call_with_timeout(cmd, timeout, **kwargs):
247 """Runs an executable with an optional timeout.""" 272 """Runs an executable with an optional timeout."""
248 proc = Popen( 273 proc = Popen(
249 cmd, 274 cmd,
250 stdin=subprocess.PIPE, 275 stdin=subprocess.PIPE,
251 stdout=subprocess.PIPE, 276 stdout=subprocess.PIPE,
252 **kwargs) 277 **kwargs)
253 if timeout: 278 if timeout:
254 out = '' 279 out = ''
255 err = '' 280 err = ''
256 while proc.poll() is None: 281 for t, data in proc.yield_any(timeout):
257 remaining = max(timeout - proc.duration(), 0.001)
258 t, data = proc.recv_any(timeout=remaining)
259 if data:
260 if t == 'stdout':
261 out += data
262 else:
263 err += data
264 if proc.duration() >= timeout:
265 break
266 if proc.poll() is None and proc.duration() >= timeout:
267 logging.debug('Kill %s %s', proc.duration(), timeout)
268 proc.kill()
269 proc.wait()
270 # Try reading a last time.
271 while True:
272 t, data = proc.recv_any()
273 if not data:
274 break
275 if t == 'stdout': 282 if t == 'stdout':
276 out += data 283 out += data
277 else: 284 else:
278 err += data 285 err += data
279 else: 286 else:
280 # This code path is much faster. 287 # This code path is much faster.
281 out, err = proc.communicate() 288 out, err = proc.communicate()
282 return out, err, proc.returncode, proc.duration() 289 return out, err, proc.returncode, proc.duration()
283 290
284 291
(...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1548 options.gtest_output, 1555 options.gtest_output,
1549 result_file, 1556 result_file,
1550 options.verbose) 1557 options.verbose)
1551 except Failure as e: 1558 except Failure as e:
1552 print >> sys.stderr, e.args[0] 1559 print >> sys.stderr, e.args[0]
1553 return 1 1560 return 1
1554 1561
1555 1562
1556 if __name__ == '__main__': 1563 if __name__ == '__main__':
1557 sys.exit(main(sys.argv[1:])) 1564 sys.exit(main(sys.argv[1:]))
OLDNEW
« 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