File tree Expand file tree Collapse file tree 3 files changed +66
-3
lines changed
downloader/src/test/java/org/enso/downloader/http
testkit/src/main/java/org/enso/testkit Expand file tree Collapse file tree 3 files changed +66
-3
lines changed Original file line number Diff line number Diff 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
25562559lazy val `edition-updater` = project
25572560 .in(file(" lib/scala/edition-updater" ))
Original file line number Diff line number Diff line change 2727import org .enso .shttp .HTTPTestHelperServer ;
2828import org .enso .shttp .HybridHTTPServer ;
2929import org .enso .shttp .SimpleHttpHandler ;
30+ import org .enso .testkit .RetryTestRule ;
3031import org .junit .AfterClass ;
3132import org .junit .BeforeClass ;
33+ import org .junit .Rule ;
3234import org .junit .Test ;
3335import scala .Option ;
3436import 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 ();
Original file line number Diff line number Diff line change 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+ * @Rule
17+ * public RetryTestRule retry = new RetryTestRule(3);
18+ * @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+ }
You can’t perform that action at this time.
0 commit comments