Skip to content

Commit

Permalink
Feat(timeout) constructor and test changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Бацура Сергей Александрович authored and Бацура Сергей Александрович committed Sep 6, 2024
1 parent 20dc197 commit 9db6998
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,7 +26,6 @@
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.MethodDescriptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -33,18 +34,21 @@
* @author Sergei Batsura ([email protected])
*/
@Slf4j
@RequiredArgsConstructor
public class TimeoutSetupClientInterceptor implements ClientInterceptor {

private final Duration timeout;

public TimeoutSetupClientInterceptor(Duration timeout) {
this.timeout = requireNonNull(timeout, "timeout");
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TimeoutSetupTests {
})
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
static class TimeoutSetupTest extends AbstractSimpleServerClientTest {

@Test
@SneakyThrows
@DirtiesContext
Expand All @@ -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<SomeType> streamRecorder = StreamRecorder.create();
StreamObserver<SomeType> 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
Expand All @@ -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<SomeType> streamRecorder1 = StreamRecorder.create();
this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1);
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());
log.info("--- Test completed --- ");
}

}

}

0 comments on commit 9db6998

Please sign in to comment.