Skip to content

Commit 668f41d

Browse files
committed
feat: introduced retry rules for flaky android push tests
1 parent 1d04fc4 commit 668f41d

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

.github/workflows/emulate.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ jobs:
3535
api-level: ${{ matrix.android-api-level }}
3636
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
3737
disable-animations: true
38-
script: ./gradlew :android:connectedAndroidTest
38+
script: |
39+
./gradlew installDebugAndroidTest
40+
adb shell am instrument -w io.ably.lib.test.android/android.support.test.runner.AndroidJUnitRunner
3941
4042
- uses: actions/upload-artifact@v3
4143
if: always()

android/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535
abortOnError = false
3636
}
3737

38-
testOptions.targetSdk = 30
38+
testOptions.targetSdk = 34
3939

4040
sourceSets {
4141
getByName("main") {
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 runCount = 0; runCount < timesToRunTestCount; runCount++) {
32+
try {
33+
base.evaluate();
34+
return;
35+
} catch (Throwable t) {
36+
latestException = t;
37+
System.err.printf("%s: test failed on run: `%d`. Will run a maximum of `%d` times.%n", description.getDisplayName(), runCount, timesToRunTestCount);
38+
t.printStackTrace();
39+
}
40+
}
41+
42+
if (latestException != null) {
43+
System.err.printf("%s: giving up after `%d` failures%n", description.getDisplayName(), timesToRunTestCount);
44+
throw latestException;
45+
}
46+
}
47+
};
48+
}
49+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.IntentFilter;
77
import android.os.Build;
88
import android.preference.PreferenceManager;
9+
import android.support.test.InstrumentationRegistry;
910
import android.support.test.runner.AndroidJUnit4;
1011
import android.util.Log;
1112
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@@ -41,6 +42,7 @@
4142
import io.ably.lib.rest.Auth;
4243
import io.ably.lib.rest.Channel;
4344
import io.ably.lib.rest.DeviceDetails;
45+
import io.ably.lib.test.RetryTestRule;
4446
import io.ably.lib.test.common.Helpers;
4547
import io.ably.lib.test.common.Helpers.AsyncWaiter;
4648
import io.ably.lib.test.common.Helpers.CompletionWaiter;
@@ -59,7 +61,9 @@
5961
import java9.util.stream.StreamSupport;
6062

6163
import org.junit.AfterClass;
64+
import org.junit.Before;
6265
import org.junit.BeforeClass;
66+
import org.junit.Rule;
6367
import org.junit.Test;
6468
import org.junit.runner.RunWith;
6569

@@ -86,6 +90,9 @@
8690
public class AndroidPushTest {
8791
private static final int TIMEOUT_SECONDS = 30;
8892

93+
@Rule
94+
public RetryTestRule retryRule = new RetryTestRule(2);
95+
8996
private class TestActivation {
9097
private Helpers.RawHttpTracker httpTracker;
9198
private AblyRest rest;
@@ -181,6 +188,11 @@ private void moveToAfterRegistrationUpdateFailed() throws AblyException {
181188
}
182189
}
183190

191+
@Before
192+
public void cleanUp() {
193+
PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext()).edit().clear().commit();
194+
}
195+
184196
// RSH2a
185197
@Test
186198
public void push_activate() throws InterruptedException, AblyException {

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ concurrentunit = "0.4.2"
1212
slf4j = "1.7.30"
1313
build-config = "5.4.0"
1414
firebase-messaging = "22.0.0"
15-
android-test = "0.5"
15+
android-test = "1.0.2"
1616
dexmaker = "1.4"
1717
android-retrostreams = "1.7.4"
1818
maven-publish = "0.29.0"

0 commit comments

Comments
 (0)