diff --git a/resilience4j-reactor/build.gradle b/resilience4j-reactor/build.gradle index 5adaff1c4c..7a72894f6d 100644 --- a/resilience4j-reactor/build.gradle +++ b/resilience4j-reactor/build.gradle @@ -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) diff --git a/resilience4j-reactor/src/main/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperator.java b/resilience4j-reactor/src/main/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperator.java new file mode 100644 index 0000000000..70ce3e0e92 --- /dev/null +++ b/resilience4j-reactor/src/main/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperator.java @@ -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 the value type of the upstream and downstream + */ +public class TimeLimiterOperator implements UnaryOperator> { + + private final TimeLimiter timeLimiter; + + private TimeLimiterOperator(TimeLimiter timeLimiter) { + this.timeLimiter = timeLimiter; + } + + /** + * Creates a timeLimiter. + * + * @param the value type of the upstream and downstream + * @param timeLimiter the timeLimiter + * @return a TimeLimiterOperator + */ + public static TimeLimiterOperator of(TimeLimiter timeLimiter) { + return new TimeLimiterOperator<>(timeLimiter); + } + + @Override + public Publisher apply(Publisher publisher) { + if (publisher instanceof Mono) { + return withTimeout((Mono) publisher); + } else if (publisher instanceof Flux) { + return withTimeout((Flux) publisher); + } else { + throw new IllegalPublisherException(publisher); + } + } + + private Publisher withTimeout(Mono upstream) { + return upstream.timeout(getTimeout()) + .doOnNext(t -> timeLimiter.onSuccess()) + .doOnSuccess(t -> timeLimiter.onSuccess()) + .doOnError(timeLimiter::onError); + } + + private Publisher withTimeout(Flux upstream) { + return upstream.timeout(getTimeout()) + .doOnNext(t -> timeLimiter.onSuccess()) + .doOnComplete(timeLimiter::onSuccess) + .doOnError(timeLimiter::onError); + } + + private Duration getTimeout() { + return timeLimiter.getTimeLimiterConfig().getTimeoutDuration(); + } + +} diff --git a/resilience4j-reactor/src/test/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperatorTest.java b/resilience4j-reactor/src/test/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperatorTest.java new file mode 100644 index 0000000000..9a1c988e44 --- /dev/null +++ b/resilience4j-reactor/src/test/java/io/github/resilience4j/reactor/timelimiter/TimeLimiterOperatorTest.java @@ -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(); + } +}