Skip to content

Commit 1cd72c3

Browse files
committed
HTTPDownloaderTest is flaky
1 parent a3bf5a0 commit 1cd72c3

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

build.sbt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,11 @@ lazy val testkit = project
10921092
.settings(
10931093
frgaalJavaCompilerSetting,
10941094
libraryDependencies ++= Seq(
1095-
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
1096-
"commons-io" % "commons-io" % commonsIoVersion,
1097-
"org.scalatest" %% "scalatest" % scalatestVersion
1095+
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
1096+
"commons-io" % "commons-io" % commonsIoVersion,
1097+
"org.scalatest" %% "scalatest" % scalatestVersion,
1098+
"junit" % "junit" % junitVersion,
1099+
"com.github.sbt" % "junit-interface" % junitIfVersion
10981100
)
10991101
)
11001102

@@ -2552,6 +2554,7 @@ lazy val downloader = (project in file("lib/scala/downloader"))
25522554
)
25532555
.dependsOn(cli)
25542556
.dependsOn(`http-test-helper`)
2557+
.dependsOn(testkit % Test)
25552558

25562559
lazy val `edition-updater` = project
25572560
.in(file("lib/scala/edition-updater"))

lib/scala/downloader/src/test/java/org/enso/downloader/http/HttpDownloaderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import org.enso.shttp.HTTPTestHelperServer;
2828
import org.enso.shttp.HybridHTTPServer;
2929
import org.enso.shttp.SimpleHttpHandler;
30+
import org.enso.testkit.RetryTestRule;
3031
import org.junit.AfterClass;
3132
import org.junit.BeforeClass;
33+
import org.junit.Rule;
3234
import org.junit.Test;
3335
import scala.Option;
3436
import scala.util.Try;
@@ -41,6 +43,8 @@ public class HttpDownloaderTest {
4143
private static HybridHTTPServer server;
4244
private static ExecutorService serverExecutor;
4345

46+
@Rule public RetryTestRule retry = new RetryTestRule(3);
47+
4448
@BeforeClass
4549
public static void initServer() throws URISyntaxException, IOException {
4650
serverExecutor = Executors.newSingleThreadExecutor();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.enso.testkit;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
/**
8+
* Flaky test specification for JUnit.
9+
*
10+
* <p>Inspired by <a href="https://stackoverflow.com/a/8301639/4816269">this SO answer</a>.
11+
*
12+
* <p>Use it like this:
13+
*
14+
* <pre>
15+
* public class MyTest {
16+
* &#64;Rule
17+
* public RetryTestRule retry = new RetryTestRule(3);
18+
* &#64;Test
19+
* public void myTest() {...}
20+
* }
21+
* </pre>
22+
*/
23+
public class RetryTestRule implements TestRule {
24+
private int retryCount;
25+
26+
public RetryTestRule(int retryCount) {
27+
this.retryCount = retryCount;
28+
}
29+
30+
@Override
31+
public Statement apply(Statement base, Description description) {
32+
return statement(base, description);
33+
}
34+
35+
private Statement statement(final Statement base, final Description description) {
36+
return new Statement() {
37+
@Override
38+
public void evaluate() throws Throwable {
39+
Throwable caughtThrowable = null;
40+
41+
for (int i = 0; i < retryCount; i++) {
42+
try {
43+
base.evaluate();
44+
return;
45+
} catch (Throwable t) {
46+
caughtThrowable = t;
47+
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
48+
}
49+
}
50+
System.err.println(
51+
description.getDisplayName() + ": giving up after " + retryCount + " failures");
52+
throw caughtThrowable;
53+
}
54+
};
55+
}
56+
}

0 commit comments

Comments
 (0)