blob: 3a344693c1fdf702eb63cfe70adabd1bd3b358c7 [file] [log] [blame]
Nicolas Noblef3585732015-03-15 20:05:24 -07001#!/usr/bin/env python
Craig Tillerc2c79212015-02-16 12:00:01 -08002# Copyright 2015, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Nicolas Nobleddef2462015-01-06 18:08:25 -080031"""Run tests in parallel."""
32
33import argparse
34import glob
35import itertools
Craig Tiller261dd982015-01-16 16:41:45 -080036import json
Nicolas Nobleddef2462015-01-06 18:08:25 -080037import multiprocessing
Craig Tiller1cc11db2015-01-15 22:50:50 -080038import os
Craig Tillerfe406ec2015-02-24 13:55:12 -080039import re
Nicolas Nobleddef2462015-01-06 18:08:25 -080040import sys
ctiller3040cb72015-01-07 12:13:17 -080041import time
Craig Tiller5058c692015-04-08 09:42:04 -070042import platform
Craig Tiller234b6e72015-05-23 10:12:40 -070043import subprocess
Nicolas Nobleddef2462015-01-06 18:08:25 -080044
45import jobset
ctiller3040cb72015-01-07 12:13:17 -080046import watch_dirs
Nicolas Nobleddef2462015-01-06 18:08:25 -080047
Craig Tillerb50d1662015-01-15 17:28:21 -080048
Craig Tiller2cc2b842015-02-27 11:38:31 -080049ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
50os.chdir(ROOT)
51
52
Craig Tiller738c3342015-01-12 14:28:33 -080053# SimpleConfig: just compile with CONFIG=config, and run the binary to test
54class SimpleConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080055
murgatroid99132ce6a2015-03-04 17:29:14 -080056 def __init__(self, config, environ=None):
57 if environ is None:
58 environ = {}
Craig Tiller738c3342015-01-12 14:28:33 -080059 self.build_config = config
Craig Tillere68de0e2015-01-26 08:44:00 -080060 self.maxjobs = 2 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080061 self.allow_hashing = (config != 'gcov')
Craig Tiller547db2b2015-01-30 14:08:39 -080062 self.environ = environ
murgatroid99132ce6a2015-03-04 17:29:14 -080063 self.environ['CONFIG'] = config
Craig Tiller738c3342015-01-12 14:28:33 -080064
Craig Tiller4fc90032015-05-21 10:39:52 -070065 def job_spec(self, cmdline, hash_targets, shortname=None, environ={}):
Craig Tiller49f61322015-03-03 13:02:11 -080066 """Construct a jobset.JobSpec for a test under this config
67
68 Args:
69 cmdline: a list of strings specifying the command line the test
70 would like to run
71 hash_targets: either None (don't do caching of test results), or
72 a list of strings specifying files to include in a
73 binary hash to check if a test has changed
74 -- if used, all artifacts needed to run the test must
75 be listed
76 """
Craig Tiller4fc90032015-05-21 10:39:52 -070077 actual_environ = self.environ.copy()
78 for k, v in environ.iteritems():
79 actual_environ[k] = v
Craig Tiller49f61322015-03-03 13:02:11 -080080 return jobset.JobSpec(cmdline=cmdline,
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -070081 shortname=shortname,
Craig Tiller4fc90032015-05-21 10:39:52 -070082 environ=actual_environ,
Craig Tiller547db2b2015-01-30 14:08:39 -080083 hash_targets=hash_targets
84 if self.allow_hashing else None)
Craig Tiller738c3342015-01-12 14:28:33 -080085
86
87# ValgrindConfig: compile with some CONFIG=config, but use valgrind to run
88class ValgrindConfig(object):
Craig Tillerb50d1662015-01-15 17:28:21 -080089
murgatroid99132ce6a2015-03-04 17:29:14 -080090 def __init__(self, config, tool, args=None):
91 if args is None:
92 args = []
Craig Tiller738c3342015-01-12 14:28:33 -080093 self.build_config = config
Craig Tiller2aa4d642015-01-14 15:59:44 -080094 self.tool = tool
Craig Tiller1a305b12015-02-18 13:37:06 -080095 self.args = args
Craig Tillere68de0e2015-01-26 08:44:00 -080096 self.maxjobs = 2 * multiprocessing.cpu_count()
Craig Tillerc7449162015-01-16 14:42:10 -080097 self.allow_hashing = False
Craig Tiller738c3342015-01-12 14:28:33 -080098
Craig Tiller49f61322015-03-03 13:02:11 -080099 def job_spec(self, cmdline, hash_targets):
Craig Tiller1a305b12015-02-18 13:37:06 -0800100 return jobset.JobSpec(cmdline=['valgrind', '--tool=%s' % self.tool] +
Craig Tiller49f61322015-03-03 13:02:11 -0800101 self.args + cmdline,
Craig Tiller1a305b12015-02-18 13:37:06 -0800102 shortname='valgrind %s' % binary,
103 hash_targets=None)
Craig Tiller738c3342015-01-12 14:28:33 -0800104
105
Craig Tillerc7449162015-01-16 14:42:10 -0800106class CLanguage(object):
107
Craig Tillere9c959d2015-01-18 10:23:26 -0800108 def __init__(self, make_target, test_lang):
Craig Tillerc7449162015-01-16 14:42:10 -0800109 self.make_target = make_target
Craig Tillerd625d812015-04-08 15:52:35 -0700110 if platform.system() == 'Windows':
111 plat = 'windows'
112 else:
113 plat = 'posix'
Nicolas Noblee1445362015-05-11 17:40:26 -0700114 self.platform = plat
Craig Tillere9c959d2015-01-18 10:23:26 -0800115 with open('tools/run_tests/tests.json') as f:
Craig Tiller06b4ff22015-01-18 11:01:25 -0800116 js = json.load(f)
Craig Tillerd625d812015-04-08 15:52:35 -0700117 self.binaries = [tgt
118 for tgt in js
119 if tgt['language'] == test_lang and
120 plat in tgt['platforms']]
Craig Tillerc7449162015-01-16 14:42:10 -0800121
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100122 def test_specs(self, config, travis):
Craig Tiller547db2b2015-01-30 14:08:39 -0800123 out = []
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100124 for target in self.binaries:
125 if travis and target['flaky']:
126 continue
Nicolas Noblee1445362015-05-11 17:40:26 -0700127 if self.platform == 'windows':
128 binary = 'vsprojects\\test_bin\\%s.exe' % (target['name'])
129 else:
130 binary = 'bins/%s/%s' % (config.build_config, target['name'])
Craig Tiller49f61322015-03-03 13:02:11 -0800131 out.append(config.job_spec([binary], [binary]))
Nicolas Noblee1445362015-05-11 17:40:26 -0700132 return sorted(out)
Craig Tillerc7449162015-01-16 14:42:10 -0800133
134 def make_targets(self):
135 return ['buildtests_%s' % self.make_target]
136
137 def build_steps(self):
138 return []
139
murgatroid99132ce6a2015-03-04 17:29:14 -0800140 def supports_multi_config(self):
141 return True
142
143 def __str__(self):
144 return self.make_target
145
Craig Tiller99775822015-01-30 13:07:16 -0800146
murgatroid992c8d5162015-01-26 10:41:21 -0800147class NodeLanguage(object):
148
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100149 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700150 return [config.job_spec(['tools/run_tests/run_node.sh'], None,
151 environ={'GRPC_TRACE': 'surface,batch'})]
murgatroid992c8d5162015-01-26 10:41:21 -0800152
153 def make_targets(self):
murgatroid99c2791652015-01-26 11:33:39 -0800154 return ['static_c']
murgatroid992c8d5162015-01-26 10:41:21 -0800155
156 def build_steps(self):
157 return [['tools/run_tests/build_node.sh']]
Craig Tillerc7449162015-01-16 14:42:10 -0800158
murgatroid99132ce6a2015-03-04 17:29:14 -0800159 def supports_multi_config(self):
160 return False
161
162 def __str__(self):
163 return 'node'
164
Craig Tiller99775822015-01-30 13:07:16 -0800165
Craig Tillerc7449162015-01-16 14:42:10 -0800166class PhpLanguage(object):
167
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100168 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700169 return [config.job_spec(['src/php/bin/run_tests.sh'], None,
170 environ={'GRPC_TRACE': 'surface,batch'})]
Craig Tillerc7449162015-01-16 14:42:10 -0800171
172 def make_targets(self):
murgatroid99564b9442015-01-26 11:07:59 -0800173 return ['static_c']
Craig Tillerc7449162015-01-16 14:42:10 -0800174
175 def build_steps(self):
176 return [['tools/run_tests/build_php.sh']]
177
murgatroid99132ce6a2015-03-04 17:29:14 -0800178 def supports_multi_config(self):
179 return False
180
181 def __str__(self):
182 return 'php'
183
Craig Tillerc7449162015-01-16 14:42:10 -0800184
Nathaniel Manista840615e2015-01-22 20:31:47 +0000185class PythonLanguage(object):
186
Craig Tiller49f61322015-03-03 13:02:11 -0800187 def __init__(self):
188 with open('tools/run_tests/python_tests.json') as f:
189 self._tests = json.load(f)
190
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100191 def test_specs(self, config, travis):
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800192 modules = [config.job_spec(['tools/run_tests/run_python.sh', '-m',
Craig Tiller4fc90032015-05-21 10:39:52 -0700193 test['module']],
194 None,
195 environ={'GRPC_TRACE': 'surface,batch'},
Craig Tiller83020252015-05-13 14:46:45 -0700196 shortname=test['module'])
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800197 for test in self._tests if 'module' in test]
198 files = [config.job_spec(['tools/run_tests/run_python.sh',
Craig Tiller4fc90032015-05-21 10:39:52 -0700199 test['file']],
200 None,
201 environ={'GRPC_TRACE': 'surface,batch'},
Craig Tiller83020252015-05-13 14:46:45 -0700202 shortname=test['file'])
Craig Tiller4fc90032015-05-21 10:39:52 -0700203 for test in self._tests if 'file' in test]
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800204 return files + modules
Nathaniel Manista840615e2015-01-22 20:31:47 +0000205
206 def make_targets(self):
Masood Malekghassemib2d4a8d2015-03-05 17:04:18 -0800207 return ['static_c', 'grpc_python_plugin']
Nathaniel Manista840615e2015-01-22 20:31:47 +0000208
209 def build_steps(self):
210 return [['tools/run_tests/build_python.sh']]
211
murgatroid99132ce6a2015-03-04 17:29:14 -0800212 def supports_multi_config(self):
213 return False
214
215 def __str__(self):
216 return 'python'
217
Craig Tillerd625d812015-04-08 15:52:35 -0700218
murgatroid996a4c4fa2015-02-27 12:08:57 -0800219class RubyLanguage(object):
220
221 def test_specs(self, config, travis):
Craig Tiller4fc90032015-05-21 10:39:52 -0700222 return [config.job_spec(['tools/run_tests/run_ruby.sh'], None,
223 environ={'GRPC_TRACE': 'surface,batch'})]
murgatroid996a4c4fa2015-02-27 12:08:57 -0800224
225 def make_targets(self):
Nicolas "Pixel" Noblecbd9c8b2015-05-14 06:22:26 +0200226 return ['run_dep_checks']
murgatroid996a4c4fa2015-02-27 12:08:57 -0800227
228 def build_steps(self):
229 return [['tools/run_tests/build_ruby.sh']]
230
murgatroid99132ce6a2015-03-04 17:29:14 -0800231 def supports_multi_config(self):
232 return False
233
234 def __str__(self):
235 return 'ruby'
236
Craig Tillerd625d812015-04-08 15:52:35 -0700237
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800238class CSharpLanguage(object):
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800239 def test_specs(self, config, travis):
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700240 assemblies = ['Grpc.Core.Tests',
241 'Grpc.Examples.Tests',
242 'Grpc.IntegrationTesting']
243 return [config.job_spec(['tools/run_tests/run_csharp.sh', assembly],
Craig Tiller4fc90032015-05-21 10:39:52 -0700244 None, shortname=assembly,
245 environ={'GRPC_TRACE': 'surface,batch'})
Jan Tattermusch9a7d30c2015-04-23 16:12:55 -0700246 for assembly in assemblies ]
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800247
248 def make_targets(self):
249 return ['grpc_csharp_ext']
250
251 def build_steps(self):
252 return [['tools/run_tests/build_csharp.sh']]
Nathaniel Manista840615e2015-01-22 20:31:47 +0000253
murgatroid99132ce6a2015-03-04 17:29:14 -0800254 def supports_multi_config(self):
255 return False
256
257 def __str__(self):
258 return 'csharp'
259
Craig Tillerd625d812015-04-08 15:52:35 -0700260
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100261class Sanity(object):
262
263 def test_specs(self, config, travis):
264 return [config.job_spec('tools/run_tests/run_sanity.sh', None)]
265
266 def make_targets(self):
267 return ['run_dep_checks']
268
269 def build_steps(self):
270 return []
271
272 def supports_multi_config(self):
273 return False
274
275 def __str__(self):
276 return 'sanity'
277
Nicolas "Pixel" Noblee55cd7f2015-04-14 17:59:13 +0200278
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100279class Build(object):
280
281 def test_specs(self, config, travis):
282 return []
283
284 def make_targets(self):
Nicolas "Pixel" Noblec23827b2015-04-23 06:17:55 +0200285 return ['static']
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100286
287 def build_steps(self):
288 return []
289
290 def supports_multi_config(self):
291 return True
292
293 def __str__(self):
294 return self.make_target
295
296
Craig Tiller738c3342015-01-12 14:28:33 -0800297# different configurations we can run under
298_CONFIGS = {
Craig Tillerb50d1662015-01-15 17:28:21 -0800299 'dbg': SimpleConfig('dbg'),
300 'opt': SimpleConfig('opt'),
David Klempner1d0302d2015-02-04 16:08:01 -0800301 'tsan': SimpleConfig('tsan', environ={
302 'TSAN_OPTIONS': 'suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800303 'msan': SimpleConfig('msan'),
Craig Tiller96bd5f62015-02-13 09:04:13 -0800304 'ubsan': SimpleConfig('ubsan'),
Craig Tiller547db2b2015-01-30 14:08:39 -0800305 'asan': SimpleConfig('asan', environ={
David Klempner1d0302d2015-02-04 16:08:01 -0800306 'ASAN_OPTIONS': 'detect_leaks=1:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tiller6efa6eb2015-05-12 09:44:41 -0700307 'asan-noleaks': SimpleConfig('asan', environ={
308 'ASAN_OPTIONS': 'detect_leaks=0:color=always:suppressions=tools/tsan_suppressions.txt'}),
Craig Tillerb50d1662015-01-15 17:28:21 -0800309 'gcov': SimpleConfig('gcov'),
Craig Tiller1a305b12015-02-18 13:37:06 -0800310 'memcheck': ValgrindConfig('valgrind', 'memcheck', ['--leak-check=full']),
Craig Tillerb50d1662015-01-15 17:28:21 -0800311 'helgrind': ValgrindConfig('dbg', 'helgrind')
312 }
Craig Tiller738c3342015-01-12 14:28:33 -0800313
314
Nicolas "Pixel" Noble1fb5e822015-03-16 06:20:37 +0100315_DEFAULT = ['opt']
Craig Tillerc7449162015-01-16 14:42:10 -0800316_LANGUAGES = {
Craig Tillere9c959d2015-01-18 10:23:26 -0800317 'c++': CLanguage('cxx', 'c++'),
318 'c': CLanguage('c', 'c'),
murgatroid992c8d5162015-01-26 10:41:21 -0800319 'node': NodeLanguage(),
Nathaniel Manista840615e2015-01-22 20:31:47 +0000320 'php': PhpLanguage(),
321 'python': PythonLanguage(),
Jan Tattermusch1970a5b2015-03-03 15:17:25 -0800322 'ruby': RubyLanguage(),
Nicolas "Pixel" Noble9f728642015-03-24 18:50:30 +0100323 'csharp': CSharpLanguage(),
324 'sanity': Sanity(),
Nicolas "Pixel" Noblefd2b0932015-03-26 00:26:29 +0100325 'build': Build(),
Craig Tillereb272bc2015-01-30 13:13:14 -0800326 }
Nicolas Nobleddef2462015-01-06 18:08:25 -0800327
328# parse command line
329argp = argparse.ArgumentParser(description='Run grpc tests.')
330argp.add_argument('-c', '--config',
Craig Tiller738c3342015-01-12 14:28:33 -0800331 choices=['all'] + sorted(_CONFIGS.keys()),
Nicolas Nobleddef2462015-01-06 18:08:25 -0800332 nargs='+',
Craig Tillerb29797b2015-01-12 13:51:54 -0800333 default=_DEFAULT)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800334argp.add_argument('-n', '--runs_per_test', default=1, type=int)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800335argp.add_argument('-r', '--regex', default='.*', type=str)
Craig Tillerc2c79212015-02-16 12:00:01 -0800336argp.add_argument('-j', '--jobs', default=1000, type=int)
Craig Tiller8451e872015-02-27 09:25:51 -0800337argp.add_argument('-s', '--slowdown', default=1.0, type=float)
ctiller3040cb72015-01-07 12:13:17 -0800338argp.add_argument('-f', '--forever',
339 default=False,
340 action='store_const',
341 const=True)
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100342argp.add_argument('-t', '--travis',
343 default=False,
344 action='store_const',
345 const=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800346argp.add_argument('--newline_on_success',
347 default=False,
348 action='store_const',
349 const=True)
Craig Tiller686fb262015-01-15 07:39:09 -0800350argp.add_argument('-l', '--language',
Craig Tillerc7449162015-01-16 14:42:10 -0800351 choices=sorted(_LANGUAGES.keys()),
Craig Tiller686fb262015-01-15 07:39:09 -0800352 nargs='+',
Craig Tillerc7449162015-01-16 14:42:10 -0800353 default=sorted(_LANGUAGES.keys()))
Craig Tiller234b6e72015-05-23 10:12:40 -0700354argp.add_argument('-a', '--antagonists', default=0, type=int)
Nicolas Nobleddef2462015-01-06 18:08:25 -0800355args = argp.parse_args()
356
357# grab config
Craig Tiller738c3342015-01-12 14:28:33 -0800358run_configs = set(_CONFIGS[cfg]
359 for cfg in itertools.chain.from_iterable(
360 _CONFIGS.iterkeys() if x == 'all' else [x]
361 for x in args.config))
362build_configs = set(cfg.build_config for cfg in run_configs)
Craig Tillerf1973b02015-01-16 12:32:13 -0800363
Craig Tillerc7449162015-01-16 14:42:10 -0800364make_targets = []
365languages = set(_LANGUAGES[l] for l in args.language)
murgatroid99132ce6a2015-03-04 17:29:14 -0800366
367if len(build_configs) > 1:
368 for language in languages:
369 if not language.supports_multi_config():
370 print language, 'does not support multiple build configurations'
371 sys.exit(1)
372
Craig Tiller5058c692015-04-08 09:42:04 -0700373if platform.system() == 'Windows':
374 def make_jobspec(cfg, targets):
Jan Tattermusche8243592015-04-17 14:14:01 -0700375 return jobset.JobSpec(['make.bat', 'CONFIG=%s' % cfg] + targets,
376 cwd='vsprojects', shell=True)
Craig Tiller5058c692015-04-08 09:42:04 -0700377else:
378 def make_jobspec(cfg, targets):
379 return jobset.JobSpec(['make',
380 '-j', '%d' % (multiprocessing.cpu_count() + 1),
381 'EXTRA_DEFINES=GRPC_TEST_SLOWDOWN_MACHINE_FACTOR=%f' %
382 args.slowdown,
383 'CONFIG=%s' % cfg] + targets)
384
385build_steps = [make_jobspec(cfg,
386 list(set(itertools.chain.from_iterable(
387 l.make_targets() for l in languages))))
388 for cfg in build_configs]
389build_steps.extend(set(
murgatroid99132ce6a2015-03-04 17:29:14 -0800390 jobset.JobSpec(cmdline, environ={'CONFIG': cfg})
391 for cfg in build_configs
Craig Tiller547db2b2015-01-30 14:08:39 -0800392 for l in languages
Craig Tiller5058c692015-04-08 09:42:04 -0700393 for cmdline in l.build_steps()))
Craig Tiller547db2b2015-01-30 14:08:39 -0800394one_run = set(
395 spec
396 for config in run_configs
397 for language in args.language
Nicolas "Pixel" Noble9db7c3b2015-02-27 06:03:00 +0100398 for spec in _LANGUAGES[language].test_specs(config, args.travis)
Craig Tillerfe406ec2015-02-24 13:55:12 -0800399 if re.search(args.regex, spec.shortname))
Craig Tillerf1973b02015-01-16 12:32:13 -0800400
Nicolas Nobleddef2462015-01-06 18:08:25 -0800401runs_per_test = args.runs_per_test
ctiller3040cb72015-01-07 12:13:17 -0800402forever = args.forever
Nicolas Nobleddef2462015-01-06 18:08:25 -0800403
Nicolas Nobleddef2462015-01-06 18:08:25 -0800404
Craig Tiller71735182015-01-15 17:07:13 -0800405class TestCache(object):
Craig Tillerb50d1662015-01-15 17:28:21 -0800406 """Cache for running tests."""
407
David Klempner25739582015-02-11 15:57:32 -0800408 def __init__(self, use_cache_results):
Craig Tiller71735182015-01-15 17:07:13 -0800409 self._last_successful_run = {}
David Klempner25739582015-02-11 15:57:32 -0800410 self._use_cache_results = use_cache_results
Craig Tiller71735182015-01-15 17:07:13 -0800411
412 def should_run(self, cmdline, bin_hash):
Craig Tiller71735182015-01-15 17:07:13 -0800413 if cmdline not in self._last_successful_run:
414 return True
415 if self._last_successful_run[cmdline] != bin_hash:
416 return True
David Klempner25739582015-02-11 15:57:32 -0800417 if not self._use_cache_results:
418 return True
Craig Tiller71735182015-01-15 17:07:13 -0800419 return False
420
421 def finished(self, cmdline, bin_hash):
Craig Tiller547db2b2015-01-30 14:08:39 -0800422 self._last_successful_run[cmdline] = bin_hash
Craig Tillerc1f11622015-02-25 09:09:59 -0800423 self.save()
Craig Tiller71735182015-01-15 17:07:13 -0800424
425 def dump(self):
Craig Tillerb50d1662015-01-15 17:28:21 -0800426 return [{'cmdline': k, 'hash': v}
427 for k, v in self._last_successful_run.iteritems()]
Craig Tiller71735182015-01-15 17:07:13 -0800428
429 def parse(self, exdump):
430 self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump)
431
432 def save(self):
433 with open('.run_tests_cache', 'w') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800434 f.write(json.dumps(self.dump()))
Craig Tiller71735182015-01-15 17:07:13 -0800435
Craig Tiller1cc11db2015-01-15 22:50:50 -0800436 def maybe_load(self):
437 if os.path.exists('.run_tests_cache'):
438 with open('.run_tests_cache') as f:
Craig Tiller261dd982015-01-16 16:41:45 -0800439 self.parse(json.loads(f.read()))
Craig Tiller71735182015-01-15 17:07:13 -0800440
441
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100442def _build_and_run(check_cancelled, newline_on_success, travis, cache):
ctiller3040cb72015-01-07 12:13:17 -0800443 """Do one pass of building & running tests."""
murgatroid99666450e2015-01-26 13:03:31 -0800444 # build latest sequentially
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100445 if not jobset.run(build_steps, maxjobs=1,
446 newline_on_success=newline_on_success, travis=travis):
Craig Tillerd86a3942015-01-14 12:48:54 -0800447 return 1
ctiller3040cb72015-01-07 12:13:17 -0800448
Craig Tiller234b6e72015-05-23 10:12:40 -0700449 # start antagonists
450 antagonists = [subprocess.Popen(['tools/run_tests/antagonist.py'])
451 for _ in range(0, args.antagonists)]
452 try:
453 # run all the tests
454 all_runs = itertools.chain.from_iterable(
455 itertools.repeat(one_run, runs_per_test))
456 if not jobset.run(all_runs, check_cancelled,
457 newline_on_success=newline_on_success, travis=travis,
458 maxjobs=min(args.jobs, min(c.maxjobs for c in run_configs)),
459 cache=cache):
460 return 2
461 finally:
462 for antagonist in antagonists:
463 antagonist.kill()
Craig Tillerd86a3942015-01-14 12:48:54 -0800464
465 return 0
ctiller3040cb72015-01-07 12:13:17 -0800466
467
David Klempner25739582015-02-11 15:57:32 -0800468test_cache = TestCache(runs_per_test == 1)
Craig Tiller547db2b2015-01-30 14:08:39 -0800469test_cache.maybe_load()
Craig Tiller71735182015-01-15 17:07:13 -0800470
ctiller3040cb72015-01-07 12:13:17 -0800471if forever:
Nicolas Noble044db742015-01-14 16:57:24 -0800472 success = True
ctiller3040cb72015-01-07 12:13:17 -0800473 while True:
Craig Tiller42bc87c2015-02-23 08:50:19 -0800474 dw = watch_dirs.DirWatcher(['src', 'include', 'test', 'examples'])
ctiller3040cb72015-01-07 12:13:17 -0800475 initial_time = dw.most_recent_change()
476 have_files_changed = lambda: dw.most_recent_change() != initial_time
Nicolas Noble044db742015-01-14 16:57:24 -0800477 previous_success = success
Craig Tiller71735182015-01-15 17:07:13 -0800478 success = _build_and_run(check_cancelled=have_files_changed,
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800479 newline_on_success=False,
Craig Tiller9a5a9402015-04-16 10:39:50 -0700480 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800481 cache=test_cache) == 0
Nicolas Noble044db742015-01-14 16:57:24 -0800482 if not previous_success and success:
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800483 jobset.message('SUCCESS',
484 'All tests are now passing properly',
485 do_newline=True)
Nicolas Noble044db742015-01-14 16:57:24 -0800486 jobset.message('IDLE', 'No change detected')
ctiller3040cb72015-01-07 12:13:17 -0800487 while not have_files_changed():
488 time.sleep(1)
489else:
Craig Tiller71735182015-01-15 17:07:13 -0800490 result = _build_and_run(check_cancelled=lambda: False,
491 newline_on_success=args.newline_on_success,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100492 travis=args.travis,
Craig Tiller71735182015-01-15 17:07:13 -0800493 cache=test_cache)
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800494 if result == 0:
495 jobset.message('SUCCESS', 'All tests passed', do_newline=True)
496 else:
497 jobset.message('FAILED', 'Some tests failed', do_newline=True)
498 sys.exit(result)