Skip to content

Commit

Permalink
Feat(deadline) changed zero deadline behavior, javadoc corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Бацура Сергей Александрович authored and Бацура Сергей Александрович committed Sep 4, 2024
1 parent 3b295c7 commit e14b9fe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,19 +56,21 @@ 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) {
requireNonNull(props, "properties");

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<SomeType> streamRecorder1 = StreamRecorder.create();
StreamObserver<SomeType> 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<SomeType> streamRecorder1 = StreamRecorder.create();
StreamObserver<SomeType> echo1 = this.testServiceStub.echo(streamRecorder1);
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());

final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create();
StreamObserver<SomeType> echo2 = testServiceStub.echo(streamRecorder2);
echo2.onNext(SomeType.getDefaultInstance());
assertNull(streamRecorder2.getError());
assertNotNull(streamRecorder2.firstValue().get().getVersion());
log.info("--- Test completed --- ");
final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create();
StreamObserver<SomeType> 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<SomeType> 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 {
}

}

0 comments on commit e14b9fe

Please sign in to comment.