Skip to content

Commit 26a3548

Browse files
committed
fixes tests
Signed-off-by: Oleh Dokuka <[email protected]>
1 parent 6957a43 commit 26a3548

File tree

4 files changed

+44
-256
lines changed

4 files changed

+44
-256
lines changed

rsocket-core/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ dependencies {
4343
// TODO: Remove after JUnit5 migration
4444
testCompileOnly 'junit:junit'
4545
testImplementation 'org.hamcrest:hamcrest-library'
46-
testImplementation 'io.projectreactor.addons:reactor-extra'
4746
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
4847
}
4948

rsocket-core/src/test/java/io/rsocket/RSocketReconnectTest.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
import org.assertj.core.api.Assertions;
3030
import org.junit.jupiter.api.Test;
3131
import org.mockito.Mockito;
32+
import reactor.core.Exceptions;
3233
import reactor.core.publisher.Mono;
33-
import reactor.retry.Retry;
34-
import reactor.retry.RetryContext;
35-
import reactor.retry.RetryExhaustedException;
34+
import reactor.util.retry.Retry;
3635

3736
public class RSocketReconnectTest {
3837

39-
private Queue<RetryContext<?>> retries = new ConcurrentLinkedQueue<>();
38+
private Queue<Retry.RetrySignal> retries = new ConcurrentLinkedQueue<>();
4039

4140
@Test
4241
public void shouldBeASharedReconnectableInstanceOfRSocketMono() {
@@ -45,7 +44,7 @@ public void shouldBeASharedReconnectableInstanceOfRSocketMono() {
4544
Mono<RSocket> rSocketMono =
4645
RSocketFactory.connect()
4746
.singleSubscriberRequester()
48-
.reconnect()
47+
.reconnect(Retry.indefinitely())
4948
.transport(
5049
() -> {
5150
return testClientTransport[0];
@@ -80,10 +79,9 @@ public void shouldBeRetrieableConnectionSharedReconnectableInstanceOfRSocketMono
8079
RSocketFactory.connect()
8180
.singleSubscriberRequester()
8281
.reconnect(
83-
Retry.any()
84-
.exponentialBackoff(Duration.ofMillis(100), Duration.ofMillis(500))
85-
.retryMax(4)
86-
.doOnRetry(onRetry()))
82+
Retry.backoff(4, Duration.ofMillis(100))
83+
.maxBackoff(Duration.ofMillis(500))
84+
.doAfterRetry(onRetry()))
8785
.transport(mockTransportSupplier)
8886
.start();
8987

@@ -96,7 +94,6 @@ public void shouldBeRetrieableConnectionSharedReconnectableInstanceOfRSocketMono
9694
UncheckedIOException.class,
9795
UncheckedIOException.class,
9896
UncheckedIOException.class);
99-
RetryTestUtils.assertDelays(retries, 100L, 200L, 400L, 500L);
10097
}
10198

10299
@Test
@@ -114,27 +111,25 @@ public void shouldBeExaustedRetrieableConnectionSharedReconnectableInstanceOfRSo
114111
RSocketFactory.connect()
115112
.singleSubscriberRequester()
116113
.reconnect(
117-
Retry.any()
118-
.exponentialBackoff(Duration.ofMillis(100), Duration.ofMillis(500))
119-
.retryMax(4)
120-
.doOnRetry(onRetry()))
114+
Retry.backoff(4, Duration.ofMillis(100))
115+
.maxBackoff(Duration.ofMillis(500))
116+
.doAfterRetry(onRetry()))
121117
.transport(mockTransportSupplier)
122118
.start();
123119

124120
Assertions.assertThatThrownBy(rSocketMono::block)
125-
.isInstanceOf(RetryExhaustedException.class)
121+
.matches(Exceptions::isRetryExhausted)
126122
.hasCauseInstanceOf(UncheckedIOException.class);
127123

128124
Assertions.assertThatThrownBy(rSocketMono::block)
129-
.isInstanceOf(RetryExhaustedException.class)
125+
.matches(Exceptions::isRetryExhausted)
130126
.hasCauseInstanceOf(UncheckedIOException.class);
131127

132128
assertRetries(
133129
UncheckedIOException.class,
134130
UncheckedIOException.class,
135131
UncheckedIOException.class,
136132
UncheckedIOException.class);
137-
RetryTestUtils.assertDelays(retries, 100L, 200L, 400L, 500L);
138133
}
139134

140135
@Test
@@ -156,15 +151,15 @@ public void shouldBeNotBeASharedReconnectableInstanceOfRSocketMono() {
156151
private final void assertRetries(Class<? extends Throwable>... exceptions) {
157152
assertEquals(exceptions.length, retries.size());
158153
int index = 0;
159-
for (Iterator<RetryContext<?>> it = retries.iterator(); it.hasNext(); ) {
160-
RetryContext<?> retryContext = it.next();
161-
assertEquals(index + 1, retryContext.iteration());
162-
assertEquals(exceptions[index], retryContext.exception().getClass());
154+
for (Iterator<Retry.RetrySignal> it = retries.iterator(); it.hasNext(); ) {
155+
Retry.RetrySignal retryContext = it.next();
156+
assertEquals(index, retryContext.totalRetries());
157+
assertEquals(exceptions[index], retryContext.failure().getClass());
163158
index++;
164159
}
165160
}
166161

167-
Consumer<? super RetryContext<?>> onRetry() {
162+
Consumer<Retry.RetrySignal> onRetry() {
168163
return context -> retries.add(context);
169164
}
170165
}

rsocket-core/src/test/java/io/rsocket/ReconnectMonoTests.java

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
import static org.junit.Assert.assertEquals;
2020

2121
import java.io.IOException;
22-
import java.net.SocketException;
2322
import java.time.Duration;
2423
import java.util.ArrayList;
2524
import java.util.Iterator;
2625
import java.util.List;
2726
import java.util.Queue;
2827
import java.util.concurrent.CancellationException;
2928
import java.util.concurrent.ConcurrentLinkedQueue;
30-
import java.util.concurrent.Semaphore;
29+
import java.util.concurrent.TimeoutException;
3130
import java.util.function.BiConsumer;
3231
import java.util.function.Consumer;
3332
import java.util.stream.Collectors;
@@ -36,25 +35,23 @@
3635
import org.mockito.Mockito;
3736
import org.reactivestreams.Subscription;
3837
import reactor.core.CoreSubscriber;
38+
import reactor.core.Exceptions;
3939
import reactor.core.Scannable;
4040
import reactor.core.publisher.Hooks;
4141
import reactor.core.publisher.Mono;
4242
import reactor.core.publisher.MonoProcessor;
4343
import reactor.core.scheduler.Schedulers;
44-
import reactor.retry.Retry;
45-
import reactor.retry.RetryContext;
46-
import reactor.retry.RetryExhaustedException;
4744
import reactor.test.StepVerifier;
4845
import reactor.test.publisher.TestPublisher;
4946
import reactor.test.util.RaceTestUtils;
5047
import reactor.util.function.Tuple2;
5148
import reactor.util.function.Tuples;
49+
import reactor.util.retry.Retry;
5250

5351
public class ReconnectMonoTests {
5452

55-
private Queue<RetryContext<?>> retries = new ConcurrentLinkedQueue<>();
56-
private Queue<Tuple2<Object, ReconnectMono.Invalidatable>> received =
57-
new ConcurrentLinkedQueue<>();
53+
private Queue<Retry.RetrySignal> retries = new ConcurrentLinkedQueue<>();
54+
private Queue<Tuple2<Object, Invalidatable>> received = new ConcurrentLinkedQueue<>();
5855
private Queue<Object> expired = new ConcurrentLinkedQueue<>();
5956

6057
@Test
@@ -407,11 +404,7 @@ public void shouldExpireValueOnRacingDisposeAndErrorWithNoBackoff() {
407404

408405
final ReconnectMono<String> reconnectMono =
409406
cold.mono()
410-
.retryWhen(
411-
Retry.anyOf(Exception.class)
412-
.retryMax(1)
413-
.withBackoffScheduler(Schedulers.immediate())
414-
.noBackoff())
407+
.retryWhen(Retry.max(1).filter(t -> t instanceof Exception))
415408
.as(source -> new ReconnectMono<>(source, onExpire(), onValue()));
416409

417410
final MonoProcessor<String> processor = reconnectMono.subscribeWith(MonoProcessor.create());
@@ -433,7 +426,7 @@ public void shouldExpireValueOnRacingDisposeAndErrorWithNoBackoff() {
433426
.hasMessage("ReconnectMono has already been disposed");
434427
} else {
435428
Assertions.assertThat(processor.getError())
436-
.isInstanceOf(RetryExhaustedException.class)
429+
.matches(t -> Exceptions.isRetryExhausted(t))
437430
.hasCause(runtimeException);
438431
}
439432

@@ -772,15 +765,15 @@ public void shouldTimeoutRetryWithVirtualTime() {
772765
() ->
773766
Mono.<String>error(new RuntimeException("Something went wrong"))
774767
.retryWhen(
775-
Retry.anyOf(Exception.class)
776-
.exponentialBackoffWithJitter(
777-
Duration.ofSeconds(minBackoff), Duration.ofSeconds(maxBackoff))
778-
.timeout(Duration.ofSeconds(timeout)))
768+
Retry.backoff(Long.MAX_VALUE, Duration.ofSeconds(minBackoff))
769+
.doAfterRetry(onRetry())
770+
.maxBackoff(Duration.ofSeconds(maxBackoff)))
771+
.timeout(Duration.ofSeconds(timeout))
779772
.as(m -> new ReconnectMono<>(m, onExpire(), onValue()))
780773
.subscribeOn(Schedulers.elastic()))
781774
.expectSubscription()
782775
.thenAwait(Duration.ofSeconds(timeout))
783-
.expectError(RetryExhaustedException.class)
776+
.expectError(TimeoutException.class)
784777
.verify(Duration.ofSeconds(timeout));
785778

786779
Assertions.assertThat(received).isEmpty();
@@ -791,12 +784,11 @@ public void shouldTimeoutRetryWithVirtualTime() {
791784
public void monoRetryNoBackoff() {
792785
Mono<?> mono =
793786
Mono.error(new IOException())
794-
.retryWhen(Retry.any().noBackoff().retryMax(2).doOnRetry(onRetry()))
787+
.retryWhen(Retry.max(2).doAfterRetry(onRetry()))
795788
.as(m -> new ReconnectMono<>(m, onExpire(), onValue()));
796789

797-
StepVerifier.create(mono).verifyError(RetryExhaustedException.class);
790+
StepVerifier.create(mono).verifyErrorMatches(Exceptions::isRetryExhausted);
798791
assertRetries(IOException.class, IOException.class);
799-
RetryTestUtils.assertDelays(retries, 0L, 0L);
800792

801793
Assertions.assertThat(received).isEmpty();
802794
Assertions.assertThat(expired).isEmpty();
@@ -806,18 +798,16 @@ public void monoRetryNoBackoff() {
806798
public void monoRetryFixedBackoff() {
807799
Mono<?> mono =
808800
Mono.error(new IOException())
809-
.retryWhen(
810-
Retry.any().fixedBackoff(Duration.ofMillis(500)).retryOnce().doOnRetry(onRetry()))
801+
.retryWhen(Retry.fixedDelay(1, Duration.ofMillis(500)).doAfterRetry(onRetry()))
811802
.as(m -> new ReconnectMono<>(m, onExpire(), onValue()));
812803

813804
StepVerifier.withVirtualTime(() -> mono)
814805
.expectSubscription()
815806
.expectNoEvent(Duration.ofMillis(300))
816807
.thenAwait(Duration.ofMillis(300))
817-
.verifyError(RetryExhaustedException.class);
808+
.verifyErrorMatches(Exceptions::isRetryExhausted);
818809

819810
assertRetries(IOException.class);
820-
RetryTestUtils.assertDelays(retries, 500L);
821811

822812
Assertions.assertThat(received).isEmpty();
823813
Assertions.assertThat(expired).isEmpty();
@@ -828,10 +818,9 @@ public void monoRetryExponentialBackoff() {
828818
Mono<?> mono =
829819
Mono.error(new IOException())
830820
.retryWhen(
831-
Retry.any()
832-
.exponentialBackoff(Duration.ofMillis(100), Duration.ofMillis(500))
833-
.retryMax(4)
834-
.doOnRetry(onRetry()))
821+
Retry.backoff(4, Duration.ofMillis(100))
822+
.maxBackoff(Duration.ofMillis(500))
823+
.doAfterRetry(onRetry()))
835824
.as(m -> new ReconnectMono<>(m, onExpire(), onValue()));
836825

837826
StepVerifier.withVirtualTime(() -> mono)
@@ -840,74 +829,19 @@ public void monoRetryExponentialBackoff() {
840829
.thenAwait(Duration.ofMillis(200))
841830
.thenAwait(Duration.ofMillis(400))
842831
.thenAwait(Duration.ofMillis(500))
843-
.verifyError(RetryExhaustedException.class);
832+
.verifyErrorMatches(Exceptions::isRetryExhausted);
844833

845834
assertRetries(IOException.class, IOException.class, IOException.class, IOException.class);
846-
RetryTestUtils.assertDelays(retries, 100L, 200L, 400L, 500L);
847-
848-
Assertions.assertThat(received).isEmpty();
849-
Assertions.assertThat(expired).isEmpty();
850-
}
851-
852-
@Test
853-
public void monoRetryRandomBackoff() {
854-
Mono<?> mono =
855-
Mono.error(new IOException())
856-
.retryWhen(
857-
Retry.any()
858-
.randomBackoff(Duration.ofMillis(100), Duration.ofMillis(2000))
859-
.retryMax(4)
860-
.doOnRetry(onRetry()))
861-
.as(m -> new ReconnectMono<>(m, onExpire(), onValue()));
862-
863-
StepVerifier.withVirtualTime(() -> mono)
864-
.expectSubscription()
865-
.thenAwait(Duration.ofMillis(100))
866-
.thenAwait(Duration.ofMillis(2000))
867-
.thenAwait(Duration.ofMillis(2000))
868-
.thenAwait(Duration.ofMillis(2000))
869-
.verifyError(RetryExhaustedException.class);
870-
871-
assertRetries(IOException.class, IOException.class, IOException.class, IOException.class);
872-
RetryTestUtils.assertRandomDelays(retries, 100, 2000);
873-
874-
Assertions.assertThat(received).isEmpty();
875-
Assertions.assertThat(expired).isEmpty();
876-
}
877-
878-
@Test
879-
public void doOnRetry() {
880-
Semaphore semaphore = new Semaphore(0);
881-
Retry<?> retry =
882-
Retry.any()
883-
.retryOnce()
884-
.fixedBackoff(Duration.ofMillis(500))
885-
.doOnRetry(context -> semaphore.release());
886-
887-
StepVerifier.withVirtualTime(
888-
() ->
889-
Mono.<Integer>error(new SocketException())
890-
.retryWhen(retry)
891-
.as(m -> new ReconnectMono<>(m, onExpire(), onValue())))
892-
.then(() -> semaphore.acquireUninterruptibly())
893-
.expectNoEvent(Duration.ofMillis(400))
894-
.thenAwait(Duration.ofMillis(200))
895-
.verifyErrorMatches(e -> isRetryExhausted(e, SocketException.class));
896-
897-
StepVerifier.withVirtualTime(
898-
() -> Mono.error(new SocketException()).retryWhen(retry.noBackoff()))
899-
.then(() -> semaphore.acquireUninterruptibly())
900-
.verifyErrorMatches(e -> isRetryExhausted(e, SocketException.class));
901835

902836
Assertions.assertThat(received).isEmpty();
903837
Assertions.assertThat(expired).isEmpty();
904838
}
905839

906-
Consumer<? super RetryContext<?>> onRetry() {
840+
Consumer<Retry.RetrySignal> onRetry() {
907841
return context -> retries.add(context);
908842
}
909843

910-
<T> BiConsumer<T, ReconnectMono.Invalidatable> onValue() {
844+
<T> BiConsumer<T, Invalidatable> onValue() {
911845
return (v, __) -> received.add(Tuples.of(v, __));
912846
}
913847

@@ -919,15 +853,15 @@ <T> Consumer<T> onExpire() {
919853
private final void assertRetries(Class<? extends Throwable>... exceptions) {
920854
assertEquals(exceptions.length, retries.size());
921855
int index = 0;
922-
for (Iterator<RetryContext<?>> it = retries.iterator(); it.hasNext(); ) {
923-
RetryContext<?> retryContext = it.next();
924-
assertEquals(index + 1, retryContext.iteration());
925-
assertEquals(exceptions[index], retryContext.exception().getClass());
856+
for (Iterator<Retry.RetrySignal> it = retries.iterator(); it.hasNext(); ) {
857+
Retry.RetrySignal retryContext = it.next();
858+
assertEquals(index, retryContext.totalRetries());
859+
assertEquals(exceptions[index], retryContext.failure().getClass());
926860
index++;
927861
}
928862
}
929863

930864
static boolean isRetryExhausted(Throwable e, Class<? extends Throwable> cause) {
931-
return e instanceof RetryExhaustedException && cause.isInstance(e.getCause());
865+
return Exceptions.isRetryExhausted(e) && cause.isInstance(e.getCause());
932866
}
933867
}

0 commit comments

Comments
 (0)