| 9 | |
| 10 | |
| 11 | class RerunProc(base.TestProcProducer): |
| 12 | @staticmethod |
| 13 | def create(options): |
| 14 | if not options.rerun_failures_count: |
| 15 | return None |
| 16 | return RerunProc(options.rerun_failures_count, |
| 17 | options.rerun_failures_max) |
| 18 | |
| 19 | def __init__(self, rerun_max, rerun_max_total=None): |
| 20 | super(RerunProc, self).__init__('Rerun') |
| 21 | self._requirement = base.DROP_OUTPUT |
| 22 | |
| 23 | self._rerun = {} |
| 24 | self._results = collections.defaultdict(list) |
| 25 | self._rerun_max = rerun_max |
| 26 | self._rerun_total_left = rerun_max_total |
| 27 | |
| 28 | def _next_test(self, test): |
| 29 | return self._send_next_subtest(test) |
| 30 | |
| 31 | def _result_for(self, test, subtest, result): |
| 32 | # First result |
| 33 | if subtest.procid[-2:] == '-1': |
| 34 | # Passed, no reruns |
| 35 | if not result.has_unexpected_output: |
| 36 | self._send_result(test, result) |
| 37 | return |
| 38 | |
| 39 | self._rerun[test.procid] = 0 |
| 40 | |
| 41 | results = self._results[test.procid] |
| 42 | results.append(result) |
| 43 | |
| 44 | if not self.is_stopped and self._needs_rerun(test, result): |
| 45 | self._rerun[test.procid] += 1 |
| 46 | if self._rerun_total_left is not None: |
| 47 | self._rerun_total_left -= 1 |
| 48 | self._send_next_subtest(test, self._rerun[test.procid]) |
| 49 | else: |
| 50 | result = RerunResult.create(results) |
| 51 | self._finalize_test(test) |
| 52 | self._send_result(test, result) |
| 53 | |
| 54 | def _needs_rerun(self, test, result): |
| 55 | # TODO(majeski): Limit reruns count for slow tests. |
| 56 | return ((self._rerun_total_left is None or self._rerun_total_left > 0) and |
| 57 | self._rerun[test.procid] < self._rerun_max and |
| 58 | result.has_unexpected_output) |
| 59 | |
| 60 | def _send_next_subtest(self, test, run=0): |
| 61 | subtest = test.create_subtest(self, str(run + 1), keep_output=(run != 0)) |
| 62 | return self._send_test(subtest) |
| 63 | |
| 64 | def _finalize_test(self, test): |
| 65 | del self._rerun[test.procid] |
| 66 | del self._results[test.procid] |