Skip to content

Commit f8a5578

Browse files
committed
feat: introduced retry rules for flaky android push tests
1 parent 0718012 commit f8a5578

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.ably.lib.test;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
8+
public class RetryTestRule implements TestRule {
9+
10+
private final int timesToRunTestCount;
11+
12+
/**
13+
* If `times` is 0, then we should run the test once.
14+
*/
15+
public RetryTestRule(int times) {
16+
this.timesToRunTestCount = times + 1;
17+
}
18+
19+
@Override
20+
public Statement apply(Statement base, Description description) {
21+
return statement(base, description);
22+
}
23+
24+
private Statement statement(Statement base, Description description) {
25+
return new Statement() {
26+
27+
@Override
28+
public void evaluate() throws Throwable {
29+
Throwable latestException = null;
30+
31+
for (int i = 0; i < timesToRunTestCount; i++) {
32+
try {
33+
base.evaluate();
34+
return;
35+
} catch (Throwable t) {
36+
latestException = t;
37+
System.err.println("${description.methodName}: test failed on run: `$runCount`. Will run a maximum of `$timesToRunTestCount` times.");
38+
t.printStackTrace();
39+
}
40+
}
41+
42+
if (latestException != null) {
43+
System.err.println("${description.displayName}: giving up after `$timesToRunTestCount` failures");
44+
throw latestException;
45+
}
46+
}
47+
};
48+
}
49+
}

android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.ably.lib.rest.Auth;
4242
import io.ably.lib.rest.Channel;
4343
import io.ably.lib.rest.DeviceDetails;
44+
import io.ably.lib.test.RetryTestRule;
4445
import io.ably.lib.test.common.Helpers;
4546
import io.ably.lib.test.common.Helpers.AsyncWaiter;
4647
import io.ably.lib.test.common.Helpers.CompletionWaiter;
@@ -60,6 +61,7 @@
6061

6162
import org.junit.AfterClass;
6263
import org.junit.BeforeClass;
64+
import org.junit.Rule;
6365
import org.junit.Test;
6466
import org.junit.runner.RunWith;
6567

@@ -86,6 +88,9 @@
8688
public class AndroidPushTest {
8789
private static final int TIMEOUT_SECONDS = 30;
8890

91+
@Rule
92+
public RetryTestRule retryRule = new RetryTestRule(2);
93+
8994
private class TestActivation {
9095
private Helpers.RawHttpTracker httpTracker;
9196
private AblyRest rest;

0 commit comments

Comments
 (0)