Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ext {
guavaVersion = "33.6.0-jre"
jupiterVersion = "6.1.1"
jupiterLauncherVersion = "6.1.1"
junitPioneerVersion = "2.3.0"
}

def releaseTag = System.getenv("BUILD_TAG")
Expand Down Expand Up @@ -55,8 +54,6 @@ dependencies {
testImplementation "org.junit.platform:junit-platform-launcher:$jupiterLauncherVersion"

testRuntimeOnly "org.junit.platform:junit-platform-launcher:$jupiterLauncherVersion" // already have this

testImplementation "org.junit-pioneer:junit-pioneer:$junitPioneerVersion"
}

// === Experimental JDK handling for Outreach Program ===
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/io/reactivex/rxjava4/core/RxJavaSelfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava4.core;

import static org.junit.jupiter.api.Assertions.*;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.Test;

import io.reactivex.rxjava4.exceptions.TestException;

public final class RxJavaSelfTest extends RxJavaTest {

@Test
public void withRetrySuccess() {
var counter = new AtomicInteger();
withRetry(3, () -> {
counter.incrementAndGet();
});

assertEquals(1, counter.get());
}

@Test
public void withRetryFailOnce() {
var counter = new AtomicInteger();
withRetry(3, () -> {
if (counter.getAndIncrement() == 0) {
throw new TestException("Failed index " + counter.get());
}
});

assertEquals(2, counter.get());
}

@Test
public void withRetryFailTwice() {
var counter = new AtomicInteger();
withRetry(3, () -> {
if (counter.getAndIncrement() < 2) {
throw new TestException("Failed index " + counter.get());
}
});

assertEquals(3, counter.get());
}

@Test
public void withRetryTruce() {
var counter = new AtomicInteger();

assertThrows(AssertionError.class, () -> {
withRetry(3, () -> {
counter.getAndIncrement();
throw new TestException("Failed index " + counter.get());
});
});

assertEquals(3, counter.get());
}
}
26 changes: 25 additions & 1 deletion src/test/java/io/reactivex/rxjava4/core/RxJavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import org.junit.jupiter.api.*;

import io.reactivex.rxjava4.exceptions.UndeliverableException;
import io.reactivex.rxjava4.functions.Action;
import io.reactivex.rxjava4.plugins.RxJavaPlugins;
import io.reactivex.rxjava4.testsupport.*;
import io.reactivex.rxjava4.testsupport.SuppressUndeliverable;

@Timeout(value = 5, unit = TimeUnit.MINUTES)
public abstract class RxJavaTest {
Expand Down Expand Up @@ -53,4 +54,27 @@ public void afterEach(TestInfo info) {
RxJavaPlugins.setErrorHandler(null);
}

/**
* Wrap your test body into this retry lambda-based callback to retry flaky tests
* that usually depend on Thread.sleep consistency.
* @param count the number of times to retry
* @param code the code to run
*/
public static void withRetry(int count, Action code) {
AssertionError error = null;
while (count-- > 0) {
try {
code.run();
return;
} catch (Throwable ex) {
if (error == null) {
error = new AssertionError("withRetry failures");
}
error.addSuppressed(ex);
}
}
if (error != null) {
throw error;
}
}
}
Loading
Loading