11cb0ef41Sopenharmony_ci# Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci# found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciimport collections
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifrom . import base
81cb0ef41Sopenharmony_cifrom .result import RerunResult
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciclass RerunProc(base.TestProcProducer):
121cb0ef41Sopenharmony_ci  def __init__(self, rerun_max, rerun_max_total=None):
131cb0ef41Sopenharmony_ci    super(RerunProc, self).__init__('Rerun')
141cb0ef41Sopenharmony_ci    self._requirement = base.DROP_OUTPUT
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    self._rerun = {}
171cb0ef41Sopenharmony_ci    self._results = collections.defaultdict(list)
181cb0ef41Sopenharmony_ci    self._rerun_max = rerun_max
191cb0ef41Sopenharmony_ci    self._rerun_total_left = rerun_max_total
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  def _next_test(self, test):
221cb0ef41Sopenharmony_ci    return self._send_next_subtest(test)
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  def _result_for(self, test, subtest, result):
251cb0ef41Sopenharmony_ci    # First result
261cb0ef41Sopenharmony_ci    if subtest.procid[-2:] == '-1':
271cb0ef41Sopenharmony_ci      # Passed, no reruns
281cb0ef41Sopenharmony_ci      if not result.has_unexpected_output:
291cb0ef41Sopenharmony_ci        self._send_result(test, result)
301cb0ef41Sopenharmony_ci        return
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci      self._rerun[test.procid] = 0
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    results = self._results[test.procid]
351cb0ef41Sopenharmony_ci    results.append(result)
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    if not self.is_stopped and self._needs_rerun(test, result):
381cb0ef41Sopenharmony_ci      self._rerun[test.procid] += 1
391cb0ef41Sopenharmony_ci      if self._rerun_total_left is not None:
401cb0ef41Sopenharmony_ci        self._rerun_total_left -= 1
411cb0ef41Sopenharmony_ci      self._send_next_subtest(test, self._rerun[test.procid])
421cb0ef41Sopenharmony_ci    else:
431cb0ef41Sopenharmony_ci      result = RerunResult.create(results)
441cb0ef41Sopenharmony_ci      self._finalize_test(test)
451cb0ef41Sopenharmony_ci      self._send_result(test, result)
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  def _needs_rerun(self, test, result):
481cb0ef41Sopenharmony_ci    # TODO(majeski): Limit reruns count for slow tests.
491cb0ef41Sopenharmony_ci    return ((self._rerun_total_left is None or self._rerun_total_left > 0) and
501cb0ef41Sopenharmony_ci            self._rerun[test.procid] < self._rerun_max and
511cb0ef41Sopenharmony_ci            result.has_unexpected_output)
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  def _send_next_subtest(self, test, run=0):
541cb0ef41Sopenharmony_ci    subtest = self._create_subtest(test, str(run + 1), keep_output=(run != 0))
551cb0ef41Sopenharmony_ci    return self._send_test(subtest)
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  def _finalize_test(self, test):
581cb0ef41Sopenharmony_ci    del self._rerun[test.procid]
591cb0ef41Sopenharmony_ci    del self._results[test.procid]
60