-
Notifications
You must be signed in to change notification settings - Fork 63
#7: Support for in-process transport for testing #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
296653e
f7f7979
f645b98
dded6bd
4493635
91debdb
957c320
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or 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 | ||
| * | ||
| * https://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 org.springframework.grpc.sample; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
|
|
||
| @SpringBootTest | ||
| class InProcessApplicationContextInitializerTests { | ||
|
|
||
| @Nested | ||
| @SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0", | ||
| "spring.grpc.inprocess.enabled=true" }) | ||
| class WithInProcessEnabled { | ||
|
|
||
| @Autowired | ||
| private ConfigurableApplicationContext context; | ||
|
|
||
| @Test | ||
| void testInitialize_WithEnabledProperty_ShouldRegisterServerAndChannel() { | ||
| ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class); | ||
| assertThat(channel).isNotNull(); | ||
| assertThat(channel.isShutdown()).isFalse(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Nested | ||
| @SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0", | ||
| "spring.grpc.inprocess.enabled=false" }) | ||
| class WithInProcessDisabled { | ||
|
|
||
| @Autowired | ||
| private ConfigurableApplicationContext context; | ||
|
|
||
| @Test | ||
| void testInitialize_WithDisabledProperty_ShouldNotRegisterServerAndChannel() { | ||
| assertThat(context.containsBean("grpcInProcessChannel")).isFalse(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Nested | ||
| @SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0", | ||
| "spring.grpc.inprocess.enabled=true" }) | ||
|
||
| class WithDefaultInProcessEnabled { | ||
|
|
||
| @Autowired | ||
| private ConfigurableApplicationContext context; | ||
|
|
||
| @Test | ||
| void testDefaultEnabledProperty_ShouldRegisterServerAndChannel() { | ||
| ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class); | ||
| assertThat(channel).isNotNull(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or 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 | ||
| * | ||
| * https://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 org.springframework.grpc.test; | ||
|
|
||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Server; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
|
|
||
| /** | ||
| * An {@link ApplicationContextInitializer} that configures and registers an in-process | ||
| * gRPC {@link Server} and {@link ManagedChannel} within a Spring | ||
| * {@link ConfigurableApplicationContext}. This initializer is intended for testing | ||
| * environments, allowing gRPC communication within the same process without network | ||
| * overhead. | ||
| * | ||
| * @author Andrei Lisa | ||
| */ | ||
| public class InProcessApplicationContextInitializer | ||
|
||
| implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
|
||
| private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess.enabled"; | ||
|
|
||
| private static final String CHANNEL_NAME = "grpcInProcessChannel"; | ||
|
|
||
| @Override | ||
| public void initialize(ConfigurableApplicationContext applicationContext) { | ||
| String inProcessEnabled = applicationContext.getEnvironment().getProperty(PROPERTY_SOURCE_NAME, "true"); | ||
|
|
||
| if ("true".equalsIgnoreCase(inProcessEnabled) && isJarOnClasspath()) { | ||
| try { | ||
| String serverName = InProcessServerBuilder.generateName(); | ||
|
|
||
| Server grpcServer = InProcessServerBuilder.forName(serverName).directExecutor().build().start(); | ||
|
||
|
|
||
| ManagedChannel grpcChannel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); | ||
| applicationContext.getBeanFactory().registerSingleton(CHANNEL_NAME, grpcChannel); | ||
|
|
||
| applicationContext.addApplicationListener(new InProcessServerShutdownListener(grpcServer, grpcChannel)); | ||
|
|
||
| } | ||
| catch (Exception e) { | ||
| throw new RuntimeException("Failed to initialize in-process gRPC server", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean isJarOnClasspath() { | ||
|
||
| try { | ||
| Class.forName("io.grpc.inprocess.InProcessChannelBuilder"); | ||
| return true; | ||
| } | ||
| catch (ClassNotFoundException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or 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 | ||
| * | ||
| * https://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 org.springframework.grpc.test; | ||
|
|
||
| import org.springframework.context.ApplicationListener; | ||
| import org.springframework.context.event.ContextClosedEvent; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Server; | ||
|
|
||
| /** | ||
| * Listener that automatically shuts down the in-process gRPC {@link Server} and | ||
| * {@link ManagedChannel} when the Spring | ||
| * {@link org.springframework.context.ApplicationContext} is closed. This helps to ensure | ||
| * proper resource cleanup after the application context is no longer active, avoiding the | ||
| * need for manual shutdown calls in tests or other classes. | ||
| * | ||
| * @author Andrei Lisa | ||
| */ | ||
| final class InProcessServerShutdownListener implements ApplicationListener<ContextClosedEvent> { | ||
|
|
||
| private final Server grpcServer; | ||
|
|
||
| private final ManagedChannel grpcChannel; | ||
|
|
||
| InProcessServerShutdownListener(Server grpcServer, ManagedChannel grpcChannel) { | ||
| this.grpcServer = grpcServer; | ||
| this.grpcChannel = grpcChannel; | ||
| } | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(ContextClosedEvent event) { | ||
| if (grpcChannel != null) { | ||
| grpcChannel.shutdownNow(); | ||
| } | ||
| if (grpcServer != null) { | ||
| grpcServer.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Application Context Initializers | ||
| org.springframework.context.ApplicationContextInitializer=\ | ||
| org.springframework.grpc.test.ServerPortInfoApplicationContextInitializer | ||
| org.springframework.grpc.test.ServerPortInfoApplicationContextInitializer,\ | ||
| org.springframework.grpc.test.InProcessApplicationContextInitializer |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "groups": [], | ||
| "properties": [ | ||
| { | ||
| "name": "spring.grpc.inprocess.enabled", | ||
| "defaultValue": "true" | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this test class be in
spring-grpc-testmodule instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It depends if the test jar depends on Spring Boot or not. I'm happy for it to be here for now anyway -it could also be a nested class inside the existing
GrpcServerIntegrationTests(which I see below you already have, so maybe we don't need this one at all?).