Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one retry count when using exponential backoff #678

Merged
merged 1 commit into from Sep 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -39,7 +39,7 @@ public static Function<Multi<Throwable>, Publisher<Long>> randomExponentialBacko
AtomicInteger index = new AtomicInteger();
return t -> t
.onItem().transformToUni(failure -> {
int iteration = index.incrementAndGet();
int iteration = index.getAndIncrement();
if (iteration >= numRetries) {
return Uni.createFrom().failure(
new IllegalStateException("Retries exhausted: " + iteration + "/" + numRetries,
Expand Down
Expand Up @@ -11,7 +11,6 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.smallrye.mutiny.Multi;
Expand Down Expand Up @@ -87,7 +86,6 @@ public void testWithOtherStreamFailing() {
}

@Test
@Disabled("To be investigated - the switch to the strict serializer broken the cancellation check")
public void testWhatTheWhenStreamFailsTheUpstreamIsCancelled() {
AtomicBoolean subscribed = new AtomicBoolean();
AtomicBoolean cancelled = new AtomicBoolean();
Expand Down Expand Up @@ -318,9 +316,9 @@ public void testRetryWithRandomBackoff() {
.onFailure().retry().withBackOff(Duration.ofMillis(10), Duration.ofHours(1)).withJitter(0.1)
.atMost(4)
.subscribe().withSubscriber(AssertSubscriber.create(100));
await().until(() -> subscriber.getItems().size() >= 8);
await().until(() -> subscriber.getItems().size() >= 10);
subscriber
.assertItems(0, 1, 0, 1, 0, 1, 0, 1)
.assertItems(0, 1, 0, 1, 0, 1, 0, 1, 0, 1) // Initial subscription + 4 retries
.assertFailedWith(IllegalStateException.class, "Retries exhausted");

}
Expand All @@ -335,9 +333,9 @@ public void testRetryWithRandomBackoffAndDefaultJitter() {
.atMost(4)
.subscribe().withSubscriber(AssertSubscriber.create(100));

await().until(() -> subscriber.getItems().size() >= 8);
await().until(() -> subscriber.getItems().size() >= 10);
subscriber
.assertItems(0, 1, 0, 1, 0, 1, 0, 1)
.assertItems(0, 1, 0, 1, 0, 1, 0, 1, 0, 1) // Initial subscription + 4 retries
.assertFailedWith(IllegalStateException.class, "Retries exhausted: 4/4");
}

Expand All @@ -352,9 +350,9 @@ public void testRetryWithDefaultMax() {

await()
.atMost(1, TimeUnit.MINUTES)
.until(() -> subscriber.getItems().size() >= 8);
.until(() -> subscriber.getItems().size() >= 10);
subscriber
.assertItems(0, 1, 0, 1, 0, 1, 0, 1)
.assertItems(0, 1, 0, 1, 0, 1, 0, 1, 0, 1) // Initial subscription + 4 retries
.assertFailedWith(IllegalStateException.class, "Retries exhausted: 4/4");
}

Expand Down
@@ -1,5 +1,6 @@
package io.smallrye.mutiny.operators;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -84,4 +85,19 @@ public void testWithMapperFailure() {
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertItem(1);
}

@Test
public void testThatNumberOfRetryIfCorrect() {
AtomicInteger calls = new AtomicInteger();

UniAssertSubscriber<Integer> subscriber = Uni.createFrom().<Integer> item(() -> {
calls.incrementAndGet();
throw new RuntimeException("boom");
})
.onFailure().retry().atMost(3)
.subscribe().withSubscriber(UniAssertSubscriber.create());

subscriber.awaitFailure();
assertThat(calls).hasValue(4); // initial subscription + 3 retries
}
}