Declare HTTPDownloaderTest as flaky (#9339)

`HTTPDownloaderTest` failed recently transiently. Let's declare it as flaky.
This commit is contained in:
Pavel Marek 2024-03-12 10:53:55 +01:00 committed by GitHub
parent 6bf37b3d17
commit af73768d14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 3 deletions

View File

@ -1094,7 +1094,9 @@ lazy val testkit = project
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % commonsLangVersion, "org.apache.commons" % "commons-lang3" % commonsLangVersion,
"commons-io" % "commons-io" % commonsIoVersion, "commons-io" % "commons-io" % commonsIoVersion,
"org.scalatest" %% "scalatest" % scalatestVersion "org.scalatest" %% "scalatest" % scalatestVersion,
"junit" % "junit" % junitVersion,
"com.github.sbt" % "junit-interface" % junitIfVersion
) )
) )
@ -2551,6 +2553,7 @@ lazy val downloader = (project in file("lib/scala/downloader"))
) )
.dependsOn(cli) .dependsOn(cli)
.dependsOn(`http-test-helper`) .dependsOn(`http-test-helper`)
.dependsOn(testkit % Test)
lazy val `edition-updater` = project lazy val `edition-updater` = project
.in(file("lib/scala/edition-updater")) .in(file("lib/scala/edition-updater"))

View File

@ -27,8 +27,10 @@ import org.enso.cli.task.TaskProgress;
import org.enso.shttp.HTTPTestHelperServer; import org.enso.shttp.HTTPTestHelperServer;
import org.enso.shttp.HybridHTTPServer; import org.enso.shttp.HybridHTTPServer;
import org.enso.shttp.SimpleHttpHandler; import org.enso.shttp.SimpleHttpHandler;
import org.enso.testkit.RetryTestRule;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import scala.Option; import scala.Option;
import scala.util.Try; import scala.util.Try;
@ -41,6 +43,8 @@ public class HttpDownloaderTest {
private static HybridHTTPServer server; private static HybridHTTPServer server;
private static ExecutorService serverExecutor; private static ExecutorService serverExecutor;
@Rule public RetryTestRule retry = new RetryTestRule(3);
@BeforeClass @BeforeClass
public static void initServer() throws URISyntaxException, IOException { public static void initServer() throws URISyntaxException, IOException {
serverExecutor = Executors.newSingleThreadExecutor(); serverExecutor = Executors.newSingleThreadExecutor();

View File

@ -0,0 +1,74 @@
package org.enso.testkit;
import java.util.ArrayList;
import java.util.List;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* Flaky test specification for JUnit.
*
* <p>Inspired by <a href="https://stackoverflow.com/a/8301639/4816269">this SO answer</a>.
*
* <p>Use it like this:
*
* <pre>
* public class MyTest {
* &#64;Rule
* public RetryTestRule retry = new RetryTestRule(3);
* &#64;Test
* public void myTest() {...}
* }
* </pre>
*/
public class RetryTestRule implements TestRule {
private int retryCount;
public RetryTestRule(int retryCount) {
this.retryCount = retryCount;
}
@Override
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() {
List<Throwable> caughtThrowables = new ArrayList<>();
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowables.add(t);
System.err.println(
description.getClassName()
+ "."
+ description.getDisplayName()
+ ": run "
+ (i + 1)
+ " failed");
}
}
var err =
new AssertionError(
description.getDisplayName() + ": giving up after " + retryCount + " failures");
for (var t : caughtThrowables) {
Throwable n = err;
while (n.getCause() != null) {
n = n.getCause();
}
n.initCause(t);
}
System.err.println(
description.getDisplayName() + ": giving up after " + retryCount + " failures");
throw err;
}
};
}
}