Skip to content

Commit

Permalink
Issue ReactiveX#235: TimeLimiter as a Reactor operator (ReactiveX#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexmind authored and RobWin committed Sep 18, 2019
1 parent d6a1833 commit 3267aed
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resilience4j-reactor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
dependencies {
compileOnly project(':resilience4j-circuitbreaker')
compileOnly project(':resilience4j-ratelimiter')
compileOnly project(':resilience4j-timelimiter')
compileOnly project(':resilience4j-bulkhead')
compileOnly project(':resilience4j-retry')
compileOnly (libraries.reactor)
testCompile project(':resilience4j-test')
testCompile project(':resilience4j-circuitbreaker')
testCompile project(':resilience4j-ratelimiter')
testCompile project(':resilience4j-timelimiter')
testCompile project(':resilience4j-bulkhead')
testCompile project(':resilience4j-retry')
testCompile (libraries.reactor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2019 authors
*
* 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.github.resilience4j.reactor.timelimiter;

import io.github.resilience4j.reactor.IllegalPublisherException;
import io.github.resilience4j.timelimiter.TimeLimiter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.function.UnaryOperator;

import org.reactivestreams.Publisher;

/**
* A Reactor TimeLimiter operator which wraps a reactive type in a TimeLimiter.
*
* @param <T> the value type of the upstream and downstream
*/
public class TimeLimiterOperator<T> implements UnaryOperator<Publisher<T>> {

private final TimeLimiter timeLimiter;

private TimeLimiterOperator(TimeLimiter timeLimiter) {
this.timeLimiter = timeLimiter;
}

/**
* Creates a timeLimiter.
*
* @param <T> the value type of the upstream and downstream
* @param timeLimiter the timeLimiter
* @return a TimeLimiterOperator
*/
public static <T> TimeLimiterOperator<T> of(TimeLimiter timeLimiter) {
return new TimeLimiterOperator<>(timeLimiter);
}

@Override
public Publisher<T> apply(Publisher<T> publisher) {
if (publisher instanceof Mono) {
return withTimeout((Mono<T>) publisher);
} else if (publisher instanceof Flux) {
return withTimeout((Flux<T>) publisher);
} else {
throw new IllegalPublisherException(publisher);
}
}

private Publisher<T> withTimeout(Mono<T> upstream) {
return upstream.timeout(getTimeout())
.doOnNext(t -> timeLimiter.onSuccess())
.doOnSuccess(t -> timeLimiter.onSuccess())
.doOnError(timeLimiter::onError);
}

private Publisher<T> withTimeout(Flux<T> upstream) {
return upstream.timeout(getTimeout())
.doOnNext(t -> timeLimiter.onSuccess())
.doOnComplete(timeLimiter::onSuccess)
.doOnError(timeLimiter::onError);
}

private Duration getTimeout() {
return timeLimiter.getTimeLimiterConfig().getTimeoutDuration();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright 2019 authors
*
* 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.github.resilience4j.reactor.timelimiter;

import io.github.resilience4j.test.HelloWorldService;
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.concurrent.TimeoutException;

import org.junit.Test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;


public class TimeLimiterOperatorTest {

private final TimeLimiter timeLimiter = mock(TimeLimiter.class);
private final HelloWorldService helloWorldService = mock(HelloWorldService.class);

@Test
public void doNotTimeoutUsingMono() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMinutes(1)));
given(helloWorldService.returnHelloWorld())
.willReturn("Hello world");

Mono<?> mono = Mono.fromCallable(helloWorldService::returnHelloWorld)
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(mono)
.expectNextCount(1)
.verifyComplete();
then(timeLimiter).should(times(2))
.onSuccess();
}

@Test
public void timeoutUsingMono() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMillis(1)));

Mono<?> mono = Mono.delay(Duration.ofMinutes(1))
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(mono)
.expectError(TimeoutException.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(TimeoutException.class));
}

@Test
public void timeoutNeverUsingMono() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMillis(1)));

Mono<?> flux = Mono.never()
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(flux)
.expectError(TimeoutException.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(TimeoutException.class));
}

@Test
public void otherErrorUsingMono() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMinutes(1)));
given(helloWorldService.returnHelloWorld())
.willThrow(new Error("BAM!"));

Mono<?> mono = Mono.fromCallable(helloWorldService::returnHelloWorld)
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(mono)
.expectError(Error.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(Error.class));
}

@Test
public void doNotTimeoutUsingFlux() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMinutes(1)));

Flux<?> flux = Flux.interval(Duration.ofMillis(1))
.take(2)
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(flux)
.expectNextCount(2)
.verifyComplete();
then(timeLimiter).should(times(3))
.onSuccess();
}

@Test
public void timeoutUsingFlux() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMillis(1)));

Flux<?> flux = Flux.interval(Duration.ofSeconds(1))
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(flux)
.expectError(TimeoutException.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(TimeoutException.class));
}

@Test
public void timeoutNeverUsingFlux() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMillis(1)));

Flux<?> flux = Flux.never()
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(flux)
.expectError(TimeoutException.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(TimeoutException.class));
}

@Test
public void otherErrorUsingFlux() {
given(timeLimiter.getTimeLimiterConfig())
.willReturn(toConfig(Duration.ofMinutes(1)));

Flux<?> flux = Flux.error(new Error("BAM!"))
.compose(TimeLimiterOperator.of(timeLimiter));

StepVerifier.create(flux)
.expectError(Error.class)
.verify(Duration.ofMinutes(1));
then(timeLimiter).should()
.onError(any(Error.class));
}

private TimeLimiterConfig toConfig(Duration timeout) {
return TimeLimiterConfig.custom()
.timeoutDuration(timeout)
.build();
}
}

0 comments on commit 3267aed

Please sign in to comment.