blob: 0536ed68511327d11b2476302dc6b5f0fb895700 [file] [log] [blame]
Nicolas Nobleddef2462015-01-06 18:08:25 -08001#!/usr/bin/python
2"""Run tests in parallel."""
3
4import argparse
5import glob
6import itertools
7import multiprocessing
8import sys
ctiller3040cb72015-01-07 12:13:17 -08009import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080010
11import jobset
ctiller3040cb72015-01-07 12:13:17 -080012import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080013
Craig Tiller738c3342015-01-12 14:28:33 -080014# SimpleConfig: just compile with CONFIG=config, and run the binary to test
15class SimpleConfig(object):
16 def __init__(self, config):
17 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080018 self.maxjobs = 32 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080019
20 def run_command(self, binary):
21 return [binary]
22
23
24# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
25class ValgrindConfig(object):
Craig Tiller2aa4d642015-01-14 15:59:44 -080026 def __init__(self, config, tool):
Craig Tiller738c3342015-01-12 14:28:33 -080027 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080028 self.tool = tool
29 self.maxjobs = 4 * multiprocessing.cpu_count()
Craig Tiller738c3342015-01-12 14:28:33 -080030
31 def run_command(self, binary):
Craig Tiller2aa4d642015-01-14 15:59:44 -080032 return ['valgrind', binary, '--tool=%s' % self.tool]
Craig Tiller738c3342015-01-12 14:28:33 -080033
34
35# different configurations we can run under
36_CONFIGS = {
37 'dbg': SimpleConfig('dbg'),
38 'opt': SimpleConfig('opt'),
39 'tsan': SimpleConfig('tsan'),
40 'msan': SimpleConfig('msan'),
41 'asan': SimpleConfig('asan'),
Craig Tiller934baa32015-01-12 18:19:45 -080042 'gcov': SimpleConfig('gcov'),
Craig Tiller2aa4d642015-01-14 15:59:44 -080043 'memcheck': ValgrindConfig('dbg', 'memcheck'),
44 'helgrind': ValgrindConfig('dbg', 'helgrind')
Craig Tiller738c3342015-01-12 14:28:33 -080045 }
46
47
Craig Tillerb29797b2015-01-12 13:51:54 -080048_DEFAULT = ['dbg', 'opt']
Craig Tiller7c1d7f72015-01-12 17:08:33 -080049_MAKE_TEST_TARGETS = ['buildtests_c', 'buildtests_cxx']
Nicolas Nobleddef2462015-01-06 18:08:25 -080050
51# parse command line
52argp = argparse.ArgumentParser(description='Run grpc tests.')
53argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -080054 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -080055 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -080056 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -080057argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
58argp.add_argument('-n', '--runs_per_test', default=1, type=int)
ctiller3040cb72015-01-07 12:13:17 -080059argp.add_argument('-f', '--forever',
60 default=False,
61 action='store_const',
62 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -080063argp.add_argument('--newline_on_success',
64 default=False,
65 action='store_const',
66 const=True)
Nicolas Nobleddef2462015-01-06 18:08:25 -080067args = argp.parse_args()
68
69# grab config
Craig Tiller738c3342015-01-12 14:28:33 -080070run_configs = set(_CONFIGS[cfg]
71 for cfg in itertools.chain.from_iterable(
72 _CONFIGS.iterkeys() if x == 'all' else [x]
73 for x in args.config))
74build_configs = set(cfg.build_config for cfg in run_configs)
Nicolas Nobleddef2462015-01-06 18:08:25 -080075filters = args.test_filter
76runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -080077forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -080078
Nicolas Nobleddef2462015-01-06 18:08:25 -080079
Nicolas Noble044db742015-01-14 16:57:24 -080080def _build_and_run(check_cancelled, newline_on_success, forever=False):
ctiller3040cb72015-01-07 12:13:17 -080081 """Do one pass of building & running tests."""
82 # build latest, sharing cpu between the various makes
83 if not jobset.run(
84 (['make',
Craig Tiller738c3342015-01-12 14:28:33 -080085 '-j', '%d' % (multiprocessing.cpu_count() + 1),
Craig Tillerbe56a962015-01-14 11:33:03 -080086 'CONFIG=%s' % cfg] + _MAKE_TEST_TARGETS
87 for cfg in build_configs),
Craig Tiller7c1d7f72015-01-12 17:08:33 -080088 check_cancelled, maxjobs=1):
Craig Tillerd86a3942015-01-14 12:48:54 -080089 return 1
ctiller3040cb72015-01-07 12:13:17 -080090
91 # run all the tests
Craig Tiller2aa4d642015-01-14 15:59:44 -080092 if not jobset.run(
93 itertools.ifilter(
94 lambda x: x is not None, (
95 config.run_command(x)
96 for config in run_configs
97 for filt in filters
98 for x in itertools.chain.from_iterable(itertools.repeat(
99 glob.glob('bins/%s/%s_test' % (
100 config.build_config, filt)),
101 runs_per_test)))),
102 check_cancelled,
Nicolas Noble594ef6c2015-01-14 18:02:04 -0800103 newline_on_success=newline_on_success,
Craig Tiller2aa4d642015-01-14 15:59:44 -0800104 maxjobs=min(c.maxjobs for c in run_configs)):
Craig Tillerd86a3942015-01-14 12:48:54 -0800105 return 2
106
107 return 0
ctiller3040cb72015-01-07 12:13:17 -0800108
109
110if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800111 success = True
ctiller3040cb72015-01-07 12:13:17 -0800112 while True:
113 dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
114 initial_time = dw.most_recent_change()
115 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800116 previous_success = success
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800117 success = _build_and_run(have_files_changed,
118 newline_on_success=False,
119 forever=True) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800120 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800121 jobset.message('SUCCESS',
122 'All tests are now passing properly',
123 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800124 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800125 while not have_files_changed():
126 time.sleep(1)
127else:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800128 result = _build_and_run(lambda: False,
129 newline_on_success=args.newline_on_success)
130 if result == 0:
131 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
132 else:
133 jobset.message('FAILED', 'Some tests failed', do_newline=True)
134 sys.exit(result)