-
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
Sep 3, 2024
1 parent
a18aeeb
commit 1094f3f
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...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,72 @@ | ||
/* | ||
* Copyright (c) 2016-2024 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 static java.util.Objects.requireNonNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
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.GrpcChannelProperties; | ||
import net.devh.boot.grpc.client.config.GrpcChannelsProperties; | ||
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 AbstractStub#withDeadline(io.grpc.Deadline) | ||
*/ | ||
@Bean | ||
StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { | ||
requireNonNull(props, "properties"); | ||
|
||
return (name, stub) -> { | ||
GrpcChannelProperties channelProps = props.getChannel(name); | ||
if (channelProps != null && channelProps.getDeadline() != null) { | ||
return stub.withDeadlineAfter(channelProps.getDeadline().toMillis(), TimeUnit.MILLISECONDS); | ||
} else { | ||
return stub; | ||
} | ||
}; | ||
} | ||
|
||
} |
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
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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
/* | ||
* Copyright (c) 2016-2024 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 io.grpc.StatusRuntimeException; | ||
import io.grpc.internal.testing.StreamRecorder; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.devh.boot.grpc.client.config.GrpcChannelProperties; | ||
import net.devh.boot.grpc.test.config.BaseAutoConfiguration; | ||
import net.devh.boot.grpc.test.config.ServiceConfiguration; | ||
import net.devh.boot.grpc.test.proto.SomeType; | ||
|
||
/** | ||
* 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}) | ||
@DirtiesContext | ||
public class DeadlineTests extends AbstractSimpleServerClientTest { | ||
|
||
@Test | ||
@DirtiesContext | ||
@SneakyThrows | ||
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 --- "); | ||
} | ||
|
||
} |