Skip to content

Commit

Permalink
feat: introduced retry rules for flaky android push tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Oct 3, 2024
1 parent 0718012 commit f8a5578
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
49 changes: 49 additions & 0 deletions android/src/androidTest/java/io/ably/lib/test/RetryTestRule.java
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 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;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.ably.lib.rest.Auth;
import io.ably.lib.rest.Channel;
import io.ably.lib.rest.DeviceDetails;
import io.ably.lib.test.RetryTestRule;
import io.ably.lib.test.common.Helpers;
import io.ably.lib.test.common.Helpers.AsyncWaiter;
import io.ably.lib.test.common.Helpers.CompletionWaiter;
Expand All @@ -60,6 +61,7 @@

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

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

@Rule
public RetryTestRule retryRule = new RetryTestRule(2);

private class TestActivation {
private Helpers.RawHttpTracker httpTracker;
private AblyRest rest;
Expand Down

0 comments on commit f8a5578

Please sign in to comment.