diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java index 5392bd080..79b66ad68 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java @@ -49,8 +49,8 @@ public class GrpcClientTimeoutAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call using a - * {@link ClientInterceptor}. + * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call + * using a {@link ClientInterceptor}. * * @param props The properties for timeout configuration. * @return The GrpcChannelConfigurer bean with interceptor if timeout is configured. diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java index 85f186433..cfbec9300 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java @@ -16,6 +16,8 @@ package net.devh.boot.grpc.client.interceptor; +import static java.util.Objects.requireNonNull; + import java.time.Duration; import java.util.concurrent.TimeUnit; @@ -24,7 +26,6 @@ import io.grpc.ClientCall; import io.grpc.ClientInterceptor; import io.grpc.MethodDescriptor; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; /** @@ -33,18 +34,21 @@ * @author Sergei Batsura (batsura.sa@gmail.com) */ @Slf4j -@RequiredArgsConstructor public class TimeoutSetupClientInterceptor implements ClientInterceptor { private final Duration timeout; + public TimeoutSetupClientInterceptor(Duration timeout) { + this.timeout = requireNonNull(timeout, "timeout"); + } + @Override public ClientCall interceptCall( final MethodDescriptor method, final CallOptions callOptions, final Channel next) { - if (timeout != null && callOptions.getDeadline() == null) { + if (callOptions.getDeadline() == null) { return next.newCall(method, callOptions.withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS)); } else { diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java index c25089c1d..b93c94d6b 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java @@ -50,6 +50,7 @@ public class TimeoutSetupTests { }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) static class TimeoutSetupTest extends AbstractSimpleServerClientTest { + @Test @SneakyThrows @DirtiesContext @@ -66,6 +67,21 @@ void testServiceStubTimeoutEnabledAndSuccessful() { assertNotNull(streamRecorder2.firstValue().get().getVersion()); log.info("--- Test completed --- "); } + + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { + log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); + final StreamRecorder streamRecorder = StreamRecorder.create(); + StreamObserver echo = + this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder); + TimeUnit.SECONDS.sleep(2); + echo.onNext(SomeType.getDefaultInstance()); + assertNull(streamRecorder.getError()); + assertNotNull(streamRecorder.firstValue().get().getVersion()); + log.info("--- Test completed --- "); + } } @Slf4j @@ -76,18 +92,6 @@ void testServiceStubTimeoutEnabledAndSuccessful() { }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) static class ZeroTimeoutSetupTest extends AbstractSimpleServerClientTest { - - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { - log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); - final StreamRecorder streamRecorder1 = StreamRecorder.create(); - this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1); - assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); - log.info("--- Test completed --- "); - } - } }