-
Notifications
You must be signed in to change notification settings - Fork 64
#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 1 commit
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,101 @@ | ||
| /* | ||
| * 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 io.grpc.ManagedChannel; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
| import org.springframework.grpc.test.InProcessApplicationContextInitializer; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
|
||
| /* | ||
| * @author Andrei Lisa | ||
| */ | ||
|
|
||
| @SpringBootTest | ||
| class InProcessApplicationContextInitializerTests { | ||
|
|
||
| private AnnotationConfigApplicationContext context; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| System.clearProperty("spring.grpc.inprocess"); | ||
| context = new AnnotationConfigApplicationContext(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void cleanUp() { | ||
| InProcessApplicationContextInitializer.shutdown(); | ||
| context.close(); | ||
| } | ||
|
|
||
| @Nested | ||
| class WhenDefaultEnabled { | ||
|
|
||
| @Test | ||
| void shouldInitializeInProcessServer() { | ||
| new InProcessApplicationContextInitializer().initialize(context); | ||
| context.refresh(); | ||
|
|
||
| ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class); | ||
| assertThat(channel).isNotNull().isInstanceOf(ManagedChannel.class); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Nested | ||
| class WhenDisabledByProperty { | ||
|
|
||
| @Test | ||
| void shouldNotInitializeInProcessServer() { | ||
| System.setProperty("spring.grpc.inprocess", "false"); | ||
| new InProcessApplicationContextInitializer().initialize(context); | ||
| context.refresh(); | ||
| assertThatThrownBy(() -> { | ||
| context.getBean("grpcInProcessChannel", ManagedChannel.class); | ||
| }).isInstanceOf(NoSuchBeanDefinitionException.class); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Nested | ||
| class WhenShutdownIsCalled { | ||
|
|
||
| @Test | ||
| void shouldShutdownInProcessServer() { | ||
| new InProcessApplicationContextInitializer().initialize(context); | ||
| context.refresh(); | ||
|
|
||
| ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class); | ||
| assertThat(channel).isNotNull(); | ||
|
|
||
| InProcessApplicationContextInitializer.shutdown(); | ||
|
|
||
| assertThat(channel).isNotNull(); | ||
| assertThat(channel.isShutdown()).isTrue(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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 io.grpc.ManagedChannel; | ||
| import io.grpc.Server; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
|
|
||
| /* | ||
| * @author Andrei Lisa | ||
| */ | ||
| public class InProcessApplicationContextInitializer | ||
|
||
| implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
|
||
| private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess"; | ||
dsyer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dsyer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private static final String CHANNEL_NAME = "grpcInProcessChannel"; | ||
|
|
||
| private static Server grpcServer; | ||
|
|
||
| private static ManagedChannel grpcChannel; | ||
|
|
||
| @Override | ||
| public void initialize(ConfigurableApplicationContext applicationContext) { | ||
| String inProcessEnabled = System.getProperty(PROPERTY_SOURCE_NAME, "true"); | ||
|
|
||
| if ("true".equalsIgnoreCase(inProcessEnabled) && isJarOnClasspath()) { | ||
dsyer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| String serverName = InProcessServerBuilder.generateName(); | ||
|
|
||
| grpcServer = InProcessServerBuilder.forName(serverName).directExecutor().build().start(); | ||
|
|
||
| grpcChannel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); | ||
| applicationContext.getBeanFactory().registerSingleton(CHANNEL_NAME, 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; | ||
| } | ||
| } | ||
|
|
||
| public static void shutdown() { | ||
dsyer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 |
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?).