OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import Queue |
| 6 import glob |
| 7 import multiprocessing |
| 8 import re |
| 9 import signal |
| 10 |
| 11 from .types import Test, UnknownError, TestError, Result, ResultStageAbort |
| 12 |
| 13 |
| 14 def gen_loop_process(gen, test_queue, result_queue, opts, kill_switch, |
| 15 cover_ctx): |
| 16 """Generate `Test`'s from |gen|, and feed them into |test_queue|. |
| 17 |
| 18 Non-Test instances will be translated into `UnknownError` objects. |
| 19 |
| 20 On completion, feed |opts.jobs| None objects into |test_queue|. |
| 21 |
| 22 @param gen: generator yielding Test() instances. |
| 23 @type test_queue: multiprocessing.Queue() |
| 24 @type result_queue: multiprocessing.Queue() |
| 25 @type opts: argparse.Namespace |
| 26 @type kill_switch: multiprocessing.Event() |
| 27 @type cover_ctx: cover.CoverageContext().create_subprocess_context() |
| 28 """ |
| 29 matcher = re.compile( |
| 30 '^%s$' % '|'.join('(?:%s)' % glob.fnmatch.translate(g) |
| 31 for g in opts.test_glob if g[0] != '-')) |
| 32 if matcher.pattern == '^$': |
| 33 matcher = re.compile('^.*$') |
| 34 |
| 35 neg_matcher = re.compile( |
| 36 '^%s$' % '|'.join('(?:%s)' % glob.fnmatch.translate(g[1:]) |
| 37 for g in opts.test_glob if g[0] == '-')) |
| 38 |
| 39 def generate_tests(): |
| 40 try: |
| 41 for test in gen(): |
| 42 if kill_switch.is_set(): |
| 43 break |
| 44 |
| 45 if not isinstance(test, Test): |
| 46 result_queue.put_nowait( |
| 47 UnknownError( |
| 48 'Got non-Test isinstance from generator: %r' % test)) |
| 49 continue |
| 50 |
| 51 if not neg_matcher.match(test.name) and matcher.match(test.name): |
| 52 yield test |
| 53 except KeyboardInterrupt: |
| 54 pass |
| 55 finally: |
| 56 for _ in xrange(opts.jobs): |
| 57 test_queue.put_nowait(None) |
| 58 |
| 59 |
| 60 next_stage = (result_queue if opts.handler.SKIP_RUNLOOP else test_queue) |
| 61 with cover_ctx: |
| 62 opts.handler.gen_stage_loop(opts, generate_tests(), next_stage.put_nowait, |
| 63 result_queue.put_nowait) |
| 64 |
| 65 |
| 66 def run_loop_process(test_queue, result_queue, opts, kill_switch, cover_ctx): |
| 67 """Consume `Test` instances from |test_queue|, run them, and yield the results |
| 68 into opts.run_stage_loop(). |
| 69 |
| 70 Generates coverage data as a side-effect. |
| 71 @type test_queue: multiprocessing.Queue() |
| 72 @type result_queue: multiprocessing.Queue() |
| 73 @type opts: argparse.Namespace |
| 74 @type kill_switch: multiprocessing.Event() |
| 75 @type cover_ctx: cover.CoverageContext().create_subprocess_context() |
| 76 """ |
| 77 def generate_tests_results(): |
| 78 try: |
| 79 while not kill_switch.is_set(): |
| 80 try: |
| 81 test = test_queue.get(timeout=0.1) |
| 82 if test is None: |
| 83 break |
| 84 except Queue.Empty: |
| 85 continue |
| 86 |
| 87 try: |
| 88 result = test.run() |
| 89 if not isinstance(result, Result): |
| 90 result_queue.put_nowait( |
| 91 TestError(test, 'Got non-Result instance from test: %r' |
| 92 % result)) |
| 93 continue |
| 94 |
| 95 yield test, result |
| 96 except Exception as e: |
| 97 # TODO(iannucci): include stacktrace |
| 98 result_queue.put_nowait(TestError(test, str(e))) |
| 99 except KeyboardInterrupt: |
| 100 pass |
| 101 |
| 102 with cover_ctx: |
| 103 opts.handler.run_stage_loop(opts, generate_tests_results(), |
| 104 result_queue.put_nowait) |
| 105 |
| 106 |
| 107 def result_loop(test_gen, cover_ctx, opts): |
| 108 kill_switch = multiprocessing.Event() |
| 109 def handle_killswitch(*_): |
| 110 kill_switch.set() |
| 111 # Reset the signal to DFL so that double ctrl-C kills us for sure. |
| 112 signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 113 signal.signal(signal.SIGTERM, signal.SIG_DFL) |
| 114 signal.signal(signal.SIGINT, handle_killswitch) |
| 115 signal.signal(signal.SIGTERM, handle_killswitch) |
| 116 |
| 117 test_queue = multiprocessing.Queue() |
| 118 result_queue = multiprocessing.Queue() |
| 119 |
| 120 test_gen_args = ( |
| 121 test_gen, test_queue, result_queue, opts, kill_switch, cover_ctx) |
| 122 |
| 123 procs = [] |
| 124 if opts.handler.SKIP_RUNLOOP: |
| 125 gen_loop_process(*test_gen_args) |
| 126 else: |
| 127 procs = [multiprocessing.Process( |
| 128 target=gen_loop_process, args=test_gen_args)] |
| 129 |
| 130 procs += [ |
| 131 multiprocessing.Process( |
| 132 target=run_loop_process, args=( |
| 133 test_queue, result_queue, opts, kill_switch, cover_ctx)) |
| 134 for _ in xrange(opts.jobs) |
| 135 ] |
| 136 |
| 137 for p in procs: |
| 138 p.daemon = True |
| 139 p.start() |
| 140 |
| 141 error = False |
| 142 try: |
| 143 def generate_objects(): |
| 144 while not kill_switch.is_set(): |
| 145 while not kill_switch.is_set(): |
| 146 try: |
| 147 yield result_queue.get(timeout=0.1) |
| 148 except Queue.Empty: |
| 149 break |
| 150 |
| 151 if not any(p.is_alive() for p in procs): |
| 152 break |
| 153 |
| 154 # Get everything still in the queue. Still need timeout, but since nothing |
| 155 # is going to be adding stuff to the queue, use a very short timeout. |
| 156 while not kill_switch.is_set(): |
| 157 try: |
| 158 yield result_queue.get(timeout=0.00001) |
| 159 except Queue.Empty: |
| 160 break |
| 161 |
| 162 if kill_switch.is_set(): |
| 163 raise ResultStageAbort() |
| 164 error = opts.handler.result_stage_loop(opts, generate_objects()) |
| 165 except ResultStageAbort: |
| 166 pass |
| 167 |
| 168 for p in procs: |
| 169 p.join() |
| 170 |
| 171 if not kill_switch.is_set() and not result_queue.empty(): |
| 172 error = True |
| 173 |
| 174 return error, kill_switch.is_set() |
OLD | NEW |