-
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
61 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
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 { | ||
|
||
private final int timesToRunTestCount; | ||
|
||
/** | ||
* If `times` is 0, then we should run the test once. | ||
*/ | ||
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 runCount = 0; runCount < timesToRunTestCount; runCount++) { | ||
try { | ||
base.evaluate(); | ||
return; | ||
} catch (Throwable t) { | ||
latestException = t; | ||
System.err.printf("%s: test failed on run: `%d`. Will run a maximum of `%d` times.%n", description.getDisplayName(), runCount, timesToRunTestCount); | ||
t.printStackTrace(); | ||
} | ||
} | ||
|
||
if (latestException != null) { | ||
System.err.printf("%s: giving up after `%d` failures%n", description.getDisplayName(), timesToRunTestCount); | ||
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