Skip to content

Commit

Permalink
HTTPDownloaderTest is flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Mar 8, 2024
1 parent a3bf5a0 commit 1cd72c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
9 changes: 6 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1092,9 +1092,11 @@ lazy val testkit = project
.settings(
frgaalJavaCompilerSetting,
libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
"commons-io" % "commons-io" % commonsIoVersion,
"org.scalatest" %% "scalatest" % scalatestVersion
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
"commons-io" % "commons-io" % commonsIoVersion,
"org.scalatest" %% "scalatest" % scalatestVersion,
"junit" % "junit" % junitVersion,
"com.github.sbt" % "junit-interface" % junitIfVersion
)
)

Expand Down Expand Up @@ -2552,6 +2554,7 @@ lazy val downloader = (project in file("lib/scala/downloader"))
)
.dependsOn(cli)
.dependsOn(`http-test-helper`)
.dependsOn(testkit % Test)

lazy val `edition-updater` = project
.in(file("lib/scala/edition-updater"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import org.enso.shttp.HTTPTestHelperServer;
import org.enso.shttp.HybridHTTPServer;
import org.enso.shttp.SimpleHttpHandler;
import org.enso.testkit.RetryTestRule;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import scala.Option;
import scala.util.Try;
Expand All @@ -41,6 +43,8 @@ public class HttpDownloaderTest {
private static HybridHTTPServer server;
private static ExecutorService serverExecutor;

@Rule public RetryTestRule retry = new RetryTestRule(3);

@BeforeClass
public static void initServer() throws URISyntaxException, IOException {
serverExecutor = Executors.newSingleThreadExecutor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.enso.testkit;

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() throws Throwable {
Throwable caughtThrowable = null;

for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
}
}
System.err.println(
description.getDisplayName() + ": giving up after " + retryCount + " failures");
throw caughtThrowable;
}
};
}
}

0 comments on commit 1cd72c3

Please sign in to comment.