-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/scala/testkit/src/main/java/org/enso/testkit/RetryTestRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
* @Rule | ||
* public RetryTestRule retry = new RetryTestRule(3); | ||
* @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; | ||
} | ||
}; | ||
} | ||
} |