Skip to content

Commit e14b9fe

Browse files
Бацура Сергей АлександровичБацура Сергей Александрович
Бацура Сергей Александрович
authored and
Бацура Сергей Александрович
committed
Feat(deadline) changed zero deadline behavior, javadoc corrections
1 parent 3b295c7 commit e14b9fe

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import org.springframework.context.annotation.Configuration;
2626

2727
import io.grpc.CallOptions;
28-
import io.grpc.stub.AbstractStub;
28+
import io.grpc.Channel;
29+
import io.grpc.MethodDescriptor;
2930
import lombok.extern.slf4j.Slf4j;
3031
import net.devh.boot.grpc.client.config.GrpcChannelProperties;
3132
import net.devh.boot.grpc.client.config.GrpcChannelsProperties;
@@ -55,19 +56,21 @@ public class GrpcClientDeadlineAutoConfiguration {
5556
CallOptions.Key.createWithDefault("deadlineDuration", null);
5657

5758
/**
58-
* Creates a {@link StubTransformer} bean that will add the call credentials to the created stubs.
59+
* Creates a {@link StubTransformer} bean that will add the deadlineDuration to the callOptions for using in
60+
* DeadlineSetupClientInterceptor.
5961
*
6062
* @param props The properties for deadline configuration.
61-
* @return The StubTransformer bean that will add the deadline from properties.
62-
* @see AbstractStub#withDeadline(io.grpc.Deadline)
63+
* @return The StubTransformer bean that will add the deadlineDuration from properties to the callOptions.
64+
* @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel)
6365
*/
6466
@Bean
6567
StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) {
6668
requireNonNull(props, "properties");
6769

6870
return (name, stub) -> {
6971
GrpcChannelProperties channelProps = props.getChannel(name);
70-
if (channelProps != null && channelProps.getDeadline() != null) {
72+
if (channelProps != null && channelProps.getDeadline() != null
73+
&& channelProps.getDeadline().toMillis() > 0L) {
7174
return stub.withOption(deadlineDuration, channelProps.getDeadline());
7275
} else {
7376
return stub;

grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.util.unit.DataSize;
3333
import org.springframework.util.unit.DataUnit;
3434

35+
import io.grpc.CallOptions;
3536
import io.grpc.LoadBalancerRegistry;
3637
import io.grpc.ManagedChannelBuilder;
3738
import io.grpc.NameResolverProvider;
@@ -135,13 +136,12 @@ public Duration getDeadline() {
135136
}
136137

137138
/**
138-
* Set the default deadline duration for new calls (on a per call basis).
139-
* If nothing is configured then the deadline will not be used by default.
140-
* If zero value is configured, then the call will timeout immediately.
139+
* Set the default deadline duration for new calls (on a per call basis). If nothing is configured then the deadline
140+
* will not be used by default. If zero value is configured, the deadline will not be used.
141141
*
142142
* @param deadline The connection deadline or null.
143143
*
144-
* @see #setDeadline(Duration)
144+
* @see CallOptions#withDeadlineAfter(long, TimeUnit)
145145
*/
146146
public void setDeadline(Duration deadline) {
147147
this.deadline = deadline;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import lombok.extern.slf4j.Slf4j;
2929

3030
/**
31-
* Deadline setup client interceptor that create new deadline object.
31+
* Deadline setup client interceptor that create new deadline instance from deadlineDuration.
3232
*
3333
* @author Sergei Batsura ([email protected])
3434
*/

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

+32-38
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package net.devh.boot.grpc.test.setup;
1818

19-
import static io.grpc.Status.DEADLINE_EXCEEDED;
20-
import static net.devh.boot.grpc.test.util.GrpcAssertions.assertStatus;
21-
import static org.junit.jupiter.api.Assertions.assertEquals;
2219
import static org.junit.jupiter.api.Assertions.assertNotNull;
2320
import static org.junit.jupiter.api.Assertions.assertNull;
2421
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -30,7 +27,6 @@
3027
import org.springframework.test.annotation.DirtiesContext;
3128
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3229

33-
import io.grpc.StatusRuntimeException;
3430
import io.grpc.internal.testing.StreamRecorder;
3531
import io.grpc.stub.StreamObserver;
3632
import lombok.SneakyThrows;
@@ -43,44 +39,42 @@
4339
/**
4440
* These tests check the property {@link GrpcChannelProperties#getDeadline()}.
4541
*/
46-
@Slf4j
47-
@SpringBootTest(properties = {
48-
"grpc.client.GLOBAL.address=localhost:9090",
49-
"grpc.client.GLOBAL.deadline=1s",
50-
"grpc.client.GLOBAL.negotiationType=PLAINTEXT",
51-
})
52-
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
53-
public class DeadlineTests extends AbstractSimpleServerClientTest {
42+
public class DeadlineTests {
5443

55-
@Test
56-
@SneakyThrows
57-
@DirtiesContext
58-
void testServiceStubDeadlineEnabledAndSuccessful() {
59-
log.info("--- Starting test with unsuccessful and than successful call ---");
60-
final StreamRecorder<SomeType> streamRecorder1 = StreamRecorder.create();
61-
StreamObserver<SomeType> echo1 = this.testServiceStub.echo(streamRecorder1);
62-
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());
44+
@Slf4j
45+
@SpringBootTest(properties = {
46+
"grpc.client.GLOBAL.address=localhost:9090",
47+
"grpc.client.GLOBAL.deadline=1s",
48+
"grpc.client.GLOBAL.negotiationType=PLAINTEXT",
49+
})
50+
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
51+
static class DeadlineSetupTest extends AbstractSimpleServerClientTest {
52+
@Test
53+
@SneakyThrows
54+
@DirtiesContext
55+
void testServiceStubDeadlineEnabledAndSuccessful() {
56+
log.info("--- Starting test with unsuccessful and than successful call ---");
57+
final StreamRecorder<SomeType> streamRecorder1 = StreamRecorder.create();
58+
StreamObserver<SomeType> echo1 = this.testServiceStub.echo(streamRecorder1);
59+
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());
6360

64-
final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create();
65-
StreamObserver<SomeType> echo2 = testServiceStub.echo(streamRecorder2);
66-
echo2.onNext(SomeType.getDefaultInstance());
67-
assertNull(streamRecorder2.getError());
68-
assertNotNull(streamRecorder2.firstValue().get().getVersion());
69-
log.info("--- Test completed --- ");
61+
final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create();
62+
StreamObserver<SomeType> echo2 = testServiceStub.echo(streamRecorder2);
63+
echo2.onNext(SomeType.getDefaultInstance());
64+
assertNull(streamRecorder2.getError());
65+
assertNotNull(streamRecorder2.firstValue().get().getVersion());
66+
log.info("--- Test completed --- ");
67+
}
7068
}
7169

72-
@Test
73-
@SneakyThrows
74-
@DirtiesContext
75-
void testServiceStubDeadlineEnabledAndUnsuccessful() {
76-
log.info("--- Starting test with unsuccessful call ---");
77-
final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create();
78-
this.testServiceStub.echo(streamRecorder);
79-
assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get());
80-
assertNotNull(streamRecorder.getError());
81-
assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass());
82-
assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError());
83-
log.info("--- Test completed --- ");
70+
@Slf4j
71+
@SpringBootTest(properties = {
72+
"grpc.client.GLOBAL.address=localhost:9090",
73+
"grpc.client.GLOBAL.deadline=0s",
74+
"grpc.client.GLOBAL.negotiationType=PLAINTEXT",
75+
})
76+
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class})
77+
static class ZeroDeadlineSetupTest extends AbstractSimpleServerClientTest {
8478
}
8579

8680
}

0 commit comments

Comments
 (0)