Skip to content

Commit 7cf859b

Browse files
Бацура Сергей АлександровичБацура Сергей Александрович
Бацура Сергей Александрович
authored and
Бацура Сергей Александрович
committed
Feat(timeout) constructor and test changed.
1 parent 20dc197 commit 7cf859b

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
public class GrpcClientTimeoutAutoConfiguration {
5050

5151
/**
52-
* Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call using a
53-
* {@link ClientInterceptor}.
52+
* Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call
53+
* using a {@link ClientInterceptor}.
5454
*
5555
* @param props The properties for timeout configuration.
5656
* @return The GrpcChannelConfigurer bean with interceptor if timeout is configured.

grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package net.devh.boot.grpc.client.interceptor;
1818

19+
import static java.util.Objects.requireNonNull;
20+
1921
import java.time.Duration;
2022
import java.util.concurrent.TimeUnit;
2123

@@ -24,7 +26,6 @@
2426
import io.grpc.ClientCall;
2527
import io.grpc.ClientInterceptor;
2628
import io.grpc.MethodDescriptor;
27-
import lombok.RequiredArgsConstructor;
2829
import lombok.extern.slf4j.Slf4j;
2930

3031
/**
@@ -33,11 +34,14 @@
3334
* @author Sergei Batsura ([email protected])
3435
*/
3536
@Slf4j
36-
@RequiredArgsConstructor
3737
public class TimeoutSetupClientInterceptor implements ClientInterceptor {
3838

3939
private final Duration timeout;
4040

41+
public TimeoutSetupClientInterceptor(Duration timeout) {
42+
this.timeout = requireNonNull(timeout, "timeout");
43+
}
44+
4145
@Override
4246
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
4347
final MethodDescriptor<ReqT, RespT> method,

tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class TimeoutSetupTests {
5050
})
5151
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
5252
static class TimeoutSetupTest extends AbstractSimpleServerClientTest {
53+
5354
@Test
5455
@SneakyThrows
5556
@DirtiesContext
@@ -66,6 +67,21 @@ void testServiceStubTimeoutEnabledAndSuccessful() {
6667
assertNotNull(streamRecorder2.firstValue().get().getVersion());
6768
log.info("--- Test completed --- ");
6869
}
70+
71+
@Test
72+
@SneakyThrows
73+
@DirtiesContext
74+
void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() {
75+
log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---");
76+
final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
77+
StreamObserver<SomeType> echo =
78+
this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder);
79+
TimeUnit.SECONDS.sleep(2);
80+
echo.onNext(SomeType.getDefaultInstance());
81+
assertNull(streamRecorder.getError());
82+
assertNotNull(streamRecorder.firstValue().get().getVersion());
83+
log.info("--- Test completed --- ");
84+
}
6985
}
7086

7187
@Slf4j
@@ -76,18 +92,6 @@ void testServiceStubTimeoutEnabledAndSuccessful() {
7692
})
7793
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
7894
static class ZeroTimeoutSetupTest extends AbstractSimpleServerClientTest {
79-
80-
@Test
81-
@SneakyThrows
82-
@DirtiesContext
83-
void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() {
84-
log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---");
85-
final StreamRecorder<SomeType> streamRecorder1 = StreamRecorder.create();
86-
this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1);
87-
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());
88-
log.info("--- Test completed --- ");
89-
}
90-
9195
}
9296

9397
}

0 commit comments

Comments
 (0)