Problem/Motivation

#3037436: [random test failure] Make QuickEditIntegrationTest more robust and fail proof and #3066447: [random test failure] Random failures building media library form after uploading image (WidgetUploadTest) amongst others are wasting contributor time as they knock RTBC patches back to Needs Work, when in fact it was just a random test failure. These failures are false positives and always go away on a retest.

Steps to reproduce

Mark a patch RTBC and wait a few days/weeks for a random test failure.

Proposed resolution

Provide a way of marking a test as "flaky". If a flaky test fails, automatically repeat it in the test run and only actually fail if it fails the second time round.

Remaining tasks

Decide if this is a good idea.
Figure out how to implement it.

User interface changes

API changes

Data model changes

Release notes snippet

Comments

longwave created an issue. See original summary.

jhodgdon’s picture

If a test is that flaky (and I personally have seen multiple failures on RTBC patches for that QuickEditIntegrationTest), I think the best thing to do would be to remove it from the code base as soon as it's identified as a major annoyance, and then have an issue that adds it back in once it has been fixed.

jonathanshaw’s picture

This is what I have in my project:

<?php

namespace Drupal\Tests\my_project_tests\Traits;

trait RetryTrait {

  public function runBare() {
    $e = NULL;
    $numberOfRetries = $this->getNumberOfRetries();
    if (FALSE == is_numeric($numberOfRetries)) {
      throw new \LogicException(sprintf('The $numberOfRetries must be a number but got "%s"', var_export($numberOfRetries, TRUE)));
    }
    $numberOfRetries = (int) $numberOfRetries;
    if ($numberOfRetries <= 0) {
      throw new \LogicException(sprintf('The $numberOfRetries must be a positive number greater than 0 but got "%s".', $numberOfRetries));
    }
    for ($i = 0; $i < $numberOfRetries; ++$i) {
      try {
        parent::runBare();
        return;
      } catch (\PHPUnit_Framework_IncompleteTestError $e) {
        throw $e;
      } catch (\PHPUnit_Framework_SkippedTestError $e) {
        throw $e;
      } catch (\Throwable $e) {
        // last one thrown below
      } catch (\Exception $e) {
        // last one thrown below
      }
    }
    if ($e) {
      throw $e;
    }
  }

  /**
   * @return int
   */
  private function getNumberOfRetries() {
    $annotations = $this->getAnnotations();
    if (isset($annotations['method']['retry'][0])) {
      return $annotations['method']['retry'][0];
    }
    if (isset($annotations['class']['retry'][0])) {
      return $annotations['class']['retry'][0];
    }
    if (isset($this->retry) && (!empty($this->retry))) {
      return $this->retry;
    }
    return 1;
  }
}
mondrake’s picture

Steps to reproduce

Mark a patch RTBC and wait a few days/weeks for a random test failure.

Delightfully lovely :) and so true.

+1 on the idea of this patch.

alexpott’s picture

This seems pragmatic. I think there should always be an issue open to remove it and it should be a matter of last resort. But the random fails in the JS tests have prove intractable for some reason.

catch’s picture

With issues like #3037436: [random test failure] Make QuickEditIntegrationTest more robust and fail proof I think we should have been marking those as skipped (leaving the code there) with issues to diagnose and re-enable them, but there's a question as to why we haven't done that, so we should definitely at least discuss this option instead.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

In PHPUnit upstream, this is being proposed in #[Retry] attribute to support retrying flaky test #6182

mondrake’s picture

Issue tags: +run-tests.sh
mondrake’s picture

Status: Active » Closed (outdated)

I suggest to continue in #3564900: Let run-tests.sh re-run flaky test classes on failure as now the overall context changed, and in fact the situation deteriorated quite a lot.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.