diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 74000992e..9782cc404 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -25,7 +25,8 @@ import org.springframework.context.annotation.Configuration; import io.grpc.CallOptions; -import io.grpc.stub.AbstractStub; +import io.grpc.Channel; +import io.grpc.MethodDescriptor; import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.config.GrpcChannelProperties; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; @@ -55,11 +56,12 @@ public class GrpcClientDeadlineAutoConfiguration { CallOptions.Key.createWithDefault("deadlineDuration", null); /** - * Creates a {@link StubTransformer} bean that will add the call credentials to the created stubs. + * Creates a {@link StubTransformer} bean that will add the deadlineDuration to the callOptions for using in + * DeadlineSetupClientInterceptor. * * @param props The properties for deadline configuration. - * @return The StubTransformer bean that will add the deadline from properties. - * @see AbstractStub#withDeadline(io.grpc.Deadline) + * @return The StubTransformer bean that will add the deadlineDuration from properties to the callOptions. + * @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel) */ @Bean StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { @@ -67,7 +69,8 @@ StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { return (name, stub) -> { GrpcChannelProperties channelProps = props.getChannel(name); - if (channelProps != null && channelProps.getDeadline() != null) { + if (channelProps != null && channelProps.getDeadline() != null + && channelProps.getDeadline().toMillis() > 0L) { return stub.withOption(deadlineDuration, channelProps.getDeadline()); } else { return stub; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 69c166ba0..c77216735 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -32,6 +32,7 @@ import org.springframework.util.unit.DataSize; import org.springframework.util.unit.DataUnit; +import io.grpc.CallOptions; import io.grpc.LoadBalancerRegistry; import io.grpc.ManagedChannelBuilder; import io.grpc.NameResolverProvider; @@ -135,13 +136,12 @@ public Duration getDeadline() { } /** - * Set the default deadline duration for new calls (on a per call basis). - * If nothing is configured then the deadline will not be used by default. - * If zero value is configured, then the call will timeout immediately. + * Set the default deadline duration for new calls (on a per call basis). If nothing is configured then the deadline + * will not be used by default. If zero value is configured, the deadline will not be used. * * @param deadline The connection deadline or null. * - * @see #setDeadline(Duration) + * @see CallOptions#withDeadlineAfter(long, TimeUnit) */ public void setDeadline(Duration deadline) { this.deadline = deadline; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java index c1e7aa74a..4ff2329c5 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -28,7 +28,7 @@ import lombok.extern.slf4j.Slf4j; /** - * Deadline setup client interceptor that create new deadline object. + * Deadline setup client interceptor that create new deadline instance from deadlineDuration. * * @author Sergei Batsura (batsura.sa@gmail.com) */ diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java index e172e1742..b3054e6d9 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java @@ -16,9 +16,6 @@ package net.devh.boot.grpc.test.setup; -import static io.grpc.Status.DEADLINE_EXCEEDED; -import static net.devh.boot.grpc.test.util.GrpcAssertions.assertStatus; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -30,7 +27,6 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import io.grpc.StatusRuntimeException; import io.grpc.internal.testing.StreamRecorder; import io.grpc.stub.StreamObserver; import lombok.SneakyThrows; @@ -43,44 +39,42 @@ /** * These tests check the property {@link GrpcChannelProperties#getDeadline()}. */ -@Slf4j -@SpringBootTest(properties = { - "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.deadline=1s", - "grpc.client.GLOBAL.negotiationType=PLAINTEXT", -}) -@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) -public class DeadlineTests extends AbstractSimpleServerClientTest { +public class DeadlineTests { - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubDeadlineEnabledAndSuccessful() { - log.info("--- Starting test with unsuccessful and than successful call ---"); - final StreamRecorder streamRecorder1 = StreamRecorder.create(); - StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); - assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); + @Slf4j + @SpringBootTest(properties = { + "grpc.client.GLOBAL.address=localhost:9090", + "grpc.client.GLOBAL.deadline=1s", + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", + }) + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) + static class DeadlineSetupTest extends AbstractSimpleServerClientTest { + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubDeadlineEnabledAndSuccessful() { + log.info("--- Starting test with unsuccessful and than successful call ---"); + final StreamRecorder streamRecorder1 = StreamRecorder.create(); + StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); - final StreamRecorder streamRecorder2 = StreamRecorder.create(); - StreamObserver echo2 = testServiceStub.echo(streamRecorder2); - echo2.onNext(SomeType.getDefaultInstance()); - assertNull(streamRecorder2.getError()); - assertNotNull(streamRecorder2.firstValue().get().getVersion()); - log.info("--- Test completed --- "); + final StreamRecorder streamRecorder2 = StreamRecorder.create(); + StreamObserver echo2 = testServiceStub.echo(streamRecorder2); + echo2.onNext(SomeType.getDefaultInstance()); + assertNull(streamRecorder2.getError()); + assertNotNull(streamRecorder2.firstValue().get().getVersion()); + log.info("--- Test completed --- "); + } } - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubDeadlineEnabledAndUnsuccessful() { - log.info("--- Starting test with unsuccessful call ---"); - final StreamRecorder streamRecorder = StreamRecorder.create(); - this.testServiceStub.echo(streamRecorder); - assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get()); - assertNotNull(streamRecorder.getError()); - assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass()); - assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError()); - log.info("--- Test completed --- "); + @Slf4j + @SpringBootTest(properties = { + "grpc.client.GLOBAL.address=localhost:9090", + "grpc.client.GLOBAL.deadline=0s", + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", + }) + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) + static class ZeroDeadlineSetupTest extends AbstractSimpleServerClientTest { } }