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