From 3aa8e11a213be98b9edef699a9b7d766393f8609 Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 3 Oct 2024 12:25:18 +0100 Subject: [PATCH] feat: introduced retry rules for flaky android push tests --- .../java/io/ably/lib/test/RetryTestRule.java | 48 +++++++++++++++++++ .../lib/test/android/AndroidPushTest.java | 5 ++ 2 files changed, 53 insertions(+) create mode 100644 android/src/androidTest/java/io/ably/lib/test/RetryTestRule.java diff --git a/android/src/androidTest/java/io/ably/lib/test/RetryTestRule.java b/android/src/androidTest/java/io/ably/lib/test/RetryTestRule.java new file mode 100644 index 000000000..e0b037140 --- /dev/null +++ b/android/src/androidTest/java/io/ably/lib/test/RetryTestRule.java @@ -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; + } + } + }; + } +} diff --git a/android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java b/android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java index 3ecd0407c..3cb5df5fe 100644 --- a/android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java +++ b/android/src/androidTest/java/io/ably/lib/test/android/AndroidPushTest.java @@ -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; @@ -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; @@ -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;