run_tests.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/python
  2. """Run tests in parallel."""
  3. import argparse
  4. import glob
  5. import itertools
  6. import multiprocessing
  7. import sys
  8. import time
  9. import jobset
  10. import watch_dirs
  11. # flags required for make for each configuration
  12. _CONFIGS = ['dbg', 'opt', 'tsan', 'msan', 'asan']
  13. _MAKE_TEST_TARGETS = ['buildtests_c', 'buildtests_cxx']
  14. # parse command line
  15. argp = argparse.ArgumentParser(description='Run grpc tests.')
  16. argp.add_argument('-c', '--config',
  17. choices=['all'] + _CONFIGS,
  18. nargs='+',
  19. default=['all'])
  20. argp.add_argument('-t', '--test-filter', nargs='*', default=['*'])
  21. argp.add_argument('-n', '--runs_per_test', default=1, type=int)
  22. argp.add_argument('-f', '--forever',
  23. default=False,
  24. action='store_const',
  25. const=True)
  26. args = argp.parse_args()
  27. # grab config
  28. configs = [cfg
  29. for cfg in itertools.chain.from_iterable(
  30. _CONFIGS if x == 'all' else [x]
  31. for x in args.config)]
  32. filters = args.test_filter
  33. runs_per_test = args.runs_per_test
  34. forever = args.forever
  35. def _build_and_run(check_cancelled):
  36. """Do one pass of building & running tests."""
  37. # build latest, sharing cpu between the various makes
  38. if not jobset.run(
  39. (['make',
  40. '-j', '%d' % (multiprocessing.cpu_count() + 1),
  41. target,
  42. 'CONFIG=%s' % cfg]
  43. for cfg in configs
  44. for target in _MAKE_TEST_TARGETS),
  45. check_cancelled, maxjobs=1):
  46. sys.exit(1)
  47. # run all the tests
  48. jobset.run(([x]
  49. for x in itertools.chain.from_iterable(
  50. itertools.chain.from_iterable(itertools.repeat(
  51. glob.glob('bins/%s/%s_test' % (config, filt)),
  52. runs_per_test))
  53. for config in configs
  54. for filt in filters)), check_cancelled)
  55. if forever:
  56. while True:
  57. dw = watch_dirs.DirWatcher(['src', 'include', 'test'])
  58. initial_time = dw.most_recent_change()
  59. have_files_changed = lambda: dw.most_recent_change() != initial_time
  60. _build_and_run(have_files_changed)
  61. while not have_files_changed():
  62. time.sleep(1)
  63. else:
  64. _build_and_run(lambda: False)