-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(deadline) add deadline property for client
- Loading branch information
Бацура Сергей Александрович
authored and
Бацура Сергей Александрович
committed
Aug 3, 2024
1 parent
a18aeeb
commit 72842c9
Showing
8 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...ain/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2016-2023 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.devh.boot.grpc.client.autoconfigure; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.grpc.stub.AbstractStub; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.devh.boot.grpc.client.config.GrpcChannelsProperties; | ||
import net.devh.boot.grpc.client.deadline.DeadlineHelper; | ||
import net.devh.boot.grpc.client.inject.StubTransformer; | ||
|
||
/** | ||
* The deadline auto configuration for the client. | ||
* | ||
* <p> | ||
* You can disable this config by using: | ||
* </p> | ||
* | ||
* <pre> | ||
* <code>@ImportAutoConfiguration(exclude = GrpcClientDeadlineAutoConfiguration.class)</code> | ||
* </pre> | ||
* | ||
* @author Sergei Batsura ([email protected]) | ||
*/ | ||
@Slf4j | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureBefore(GrpcClientAutoConfiguration.class) | ||
public class GrpcClientDeadlineAutoConfiguration { | ||
|
||
/** | ||
* Creates a {@link StubTransformer} bean that will add the call credentials to the created stubs. | ||
* | ||
* @param props The properties for deadline configuration. | ||
* @return The StubTransformer bean that will add the deadline from properties. | ||
* @see DeadlineHelper#deadlineStubTransformer(GrpcChannelsProperties) | ||
* @see AbstractStub#withDeadline(io.grpc.Deadline) | ||
*/ | ||
@Bean | ||
StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { | ||
return DeadlineHelper.deadlineStubTransformer(props); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/deadline/DeadlineHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2016-2023 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.devh.boot.grpc.client.deadline; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.grpc.Deadline; | ||
import io.grpc.stub.AbstractStub; | ||
import net.devh.boot.grpc.client.config.GrpcChannelProperties; | ||
import net.devh.boot.grpc.client.config.GrpcChannelsProperties; | ||
import net.devh.boot.grpc.client.inject.StubTransformer; | ||
|
||
public class DeadlineHelper { | ||
|
||
/** | ||
* Creates a new {@link StubTransformer} that will assign the given deadline to the given {@link AbstractStub}. | ||
* | ||
* @param props The properties to assign the deadline. | ||
* @return The transformed stub. | ||
* @see AbstractStub#withDeadline(Deadline) | ||
*/ | ||
public static StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { | ||
requireNonNull(props, "properties"); | ||
|
||
return (name, stub) -> { | ||
GrpcChannelProperties channelProps = props.getChannel(name); | ||
if (channelProps != null && channelProps.getDeadline() != null) { | ||
return stub.withDeadline(Deadline.after(channelProps.getDeadline().toMillis(), TimeUnit.MILLISECONDS)); | ||
} else { | ||
return stub; | ||
} | ||
}; | ||
} | ||
|
||
private DeadlineHelper() {} | ||
} |
5 changes: 5 additions & 0 deletions
5
...nt-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/deadline/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Contains classes and utilities that help with the deadline. | ||
*/ | ||
|
||
package net.devh.boot.grpc.client.deadline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2016-2023 The gRPC-Spring Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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.assertThrows; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import com.google.protobuf.Empty; | ||
|
||
import io.grpc.Channel; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.internal.testing.StreamRecorder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.devh.boot.grpc.client.config.GrpcChannelProperties; | ||
import net.devh.boot.grpc.client.inject.GrpcClient; | ||
import net.devh.boot.grpc.test.config.BaseAutoConfiguration; | ||
import net.devh.boot.grpc.test.config.ServiceConfiguration; | ||
import net.devh.boot.grpc.test.proto.SomeType; | ||
import net.devh.boot.grpc.test.proto.TestServiceGrpc; | ||
|
||
/** | ||
* These tests check the property {@link GrpcChannelProperties#getDeadline()}. | ||
*/ | ||
public class DeadlineTests { | ||
|
||
@Slf4j | ||
@SpringBootTest(properties = { | ||
"grpc.client.GLOBAL.address=localhost:9090", | ||
"grpc.client.GLOBAL.deadline=10s", | ||
"grpc.client.GLOBAL.negotiationType=PLAINTEXT", | ||
}) | ||
@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) | ||
@DirtiesContext | ||
static class SuccessfullyDeadlineTests extends AbstractSimpleServerClientTest { | ||
} | ||
|
||
@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}) | ||
@DirtiesContext | ||
static class UnsuccessfullyDeadlineTests { | ||
|
||
@GrpcClient("test") | ||
protected Channel channel; | ||
@GrpcClient("test") | ||
protected TestServiceGrpc.TestServiceStub testServiceStub; | ||
@GrpcClient("test") | ||
protected TestServiceGrpc.TestServiceBlockingStub testServiceBlockingStub; | ||
@GrpcClient("test") | ||
protected TestServiceGrpc.TestServiceFutureStub testServiceFutureStub; | ||
|
||
UnsuccessfullyDeadlineTests() { | ||
log.info("--- UnsuccessfullyDeadlineTests ---"); | ||
} | ||
|
||
@Test | ||
@DirtiesContext | ||
void testServiceStubDeadlineEnabledAndUnsuccessful() { | ||
log.info("--- Starting tests with successful call ---"); | ||
final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create(); | ||
this.testServiceStub.normal(Empty.getDefaultInstance(), streamRecorder); | ||
|
||
assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get().getVersion()); | ||
assertNotNull(streamRecorder.getError()); | ||
assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass()); | ||
assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError()); | ||
assertStatus(DEADLINE_EXCEEDED.getCode(), | ||
assertThrows(StatusRuntimeException.class, | ||
() -> testServiceBlockingStub.normal(Empty.getDefaultInstance()))); | ||
assertThrows(ExecutionException.class, | ||
() -> testServiceFutureStub.normal(Empty.getDefaultInstance()).get()); | ||
log.info("--- Test completed ---"); | ||
} | ||
} | ||
} |