-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduced retry rules for flaky android push tests
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
android/src/androidTest/java/io/ably/lib/test/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,48 @@ | ||
package io.ably.lib.test; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
|
||
public class RetryTestRule implements TestRule { | ||
/** | ||
* If `times` is 0, then we should run the test once. | ||
*/ | ||
private final int timesToRunTestCount; | ||
|
||
public RetryTestRule(int times) { | ||
this.timesToRunTestCount = times + 1; | ||
} | ||
|
||
@Override | ||
public Statement apply(Statement base, Description description) { | ||
return statement(base, description); | ||
} | ||
|
||
private Statement statement(Statement base, Description description) { | ||
return new Statement() { | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
Throwable latestException = null; | ||
|
||
for (int i = 0; i < timesToRunTestCount; i++) { | ||
try { | ||
base.evaluate(); | ||
return; | ||
} catch (Throwable t) { | ||
latestException = t; | ||
System.err.println("${description.methodName}: test failed on run: `$runCount`. Will run a maximum of `$timesToRunTestCount` times."); | ||
t.printStackTrace(); | ||
} | ||
} | ||
|
||
if (latestException != null) { | ||
System.err.println("${description.displayName}: giving up after `$timesToRunTestCount` failures"); | ||
throw latestException; | ||
} | ||
} | ||
}; | ||
} | ||
} |
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