Skip to content

Commit f645b98

Browse files
committed
#7: fix comments
Signed-off-by: andreilisa <[email protected]>
1 parent f7f7979 commit f645b98

File tree

4 files changed

+89
-29
lines changed

4 files changed

+89
-29
lines changed

samples/grpc-server/src/test/java/org/springframework/grpc/sample/InProcessApplicationContextInitializerTests.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@
2424
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.context.support.GenericApplicationContext;
2728
import org.springframework.grpc.test.InProcessApplicationContextInitializer;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
3132

32-
/*
33-
* @author Andrei Lisa
34-
*/
35-
3633
@SpringBootTest
3734
class InProcessApplicationContextInitializerTests {
3835

39-
private AnnotationConfigApplicationContext context;
36+
private GenericApplicationContext context;
4037

4138
@BeforeEach
4239
public void setUp() {
@@ -46,7 +43,6 @@ public void setUp() {
4643

4744
@AfterEach
4845
public void cleanUp() {
49-
InProcessApplicationContextInitializer.shutdown();
5046
context.close();
5147
}
5248

@@ -55,6 +51,7 @@ class WhenDefaultEnabled {
5551

5652
@Test
5753
void shouldInitializeInProcessServer() {
54+
System.setProperty("spring.grpc.inprocess", "true");
5855
new InProcessApplicationContextInitializer().initialize(context);
5956
context.refresh();
6057

@@ -72,9 +69,9 @@ void shouldNotInitializeInProcessServer() {
7269
System.setProperty("spring.grpc.inprocess", "false");
7370
new InProcessApplicationContextInitializer().initialize(context);
7471
context.refresh();
75-
assertThatThrownBy(() -> {
76-
context.getBean("grpcInProcessChannel", ManagedChannel.class);
77-
}).isInstanceOf(NoSuchBeanDefinitionException.class);
72+
73+
assertThatThrownBy(() -> context.getBean("grpcInProcessChannel", ManagedChannel.class))
74+
.isInstanceOf(NoSuchBeanDefinitionException.class);
7875
}
7976

8077
}
@@ -84,18 +81,19 @@ class WhenShutdownIsCalled {
8481

8582
@Test
8683
void shouldShutdownInProcessServer() {
84+
System.setProperty("spring.grpc.inprocess", "true");
8785
new InProcessApplicationContextInitializer().initialize(context);
86+
context.registerShutdownHook();
8887
context.refresh();
8988

9089
ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class);
9190
assertThat(channel).isNotNull();
9291

93-
InProcessApplicationContextInitializer.shutdown();
92+
context.close();
9493

95-
assertThat(channel).isNotNull();
9694
assertThat(channel.isShutdown()).isTrue();
9795
}
9896

9997
}
10098

101-
}
99+
}

spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessApplicationContextInitializer.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
package org.springframework.grpc.test;
1818

19+
import org.springframework.context.ApplicationContextInitializer;
20+
import org.springframework.context.ConfigurableApplicationContext;
21+
1922
import io.grpc.ManagedChannel;
2023
import io.grpc.Server;
2124
import io.grpc.inprocess.InProcessChannelBuilder;
2225
import io.grpc.inprocess.InProcessServerBuilder;
23-
import org.springframework.context.ApplicationContextInitializer;
24-
import org.springframework.context.ConfigurableApplicationContext;
2526

26-
/*
27+
/**
28+
* An {@link ApplicationContextInitializer} that configures and registers an in-process
29+
* gRPC {@link Server} and {@link ManagedChannel} within a Spring
30+
* {@link ConfigurableApplicationContext}. This initializer is intended for testing
31+
* environments, allowing gRPC communication within the same process without network
32+
* overhead.
33+
*
2734
* @author Andrei Lisa
2835
*/
2936
public class InProcessApplicationContextInitializer
@@ -33,23 +40,21 @@ public class InProcessApplicationContextInitializer
3340

3441
private static final String CHANNEL_NAME = "grpcInProcessChannel";
3542

36-
private static Server grpcServer;
37-
38-
private static ManagedChannel grpcChannel;
39-
4043
@Override
4144
public void initialize(ConfigurableApplicationContext applicationContext) {
42-
String inProcessEnabled = System.getProperty(PROPERTY_SOURCE_NAME, "true");
45+
String inProcessEnabled = applicationContext.getEnvironment().getProperty(PROPERTY_SOURCE_NAME);
4346

4447
if ("true".equalsIgnoreCase(inProcessEnabled) && isJarOnClasspath()) {
4548
try {
4649
String serverName = InProcessServerBuilder.generateName();
4750

48-
grpcServer = InProcessServerBuilder.forName(serverName).directExecutor().build().start();
51+
Server grpcServer = InProcessServerBuilder.forName(serverName).directExecutor().build().start();
4952

50-
grpcChannel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
53+
ManagedChannel grpcChannel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
5154
applicationContext.getBeanFactory().registerSingleton(CHANNEL_NAME, grpcChannel);
5255

56+
applicationContext.addApplicationListener(new InProcessServerShutdownListener(grpcServer, grpcChannel));
57+
5358
}
5459
catch (Exception e) {
5560
throw new RuntimeException("Failed to initialize in-process gRPC server", e);
@@ -67,11 +72,4 @@ private boolean isJarOnClasspath() {
6772
}
6873
}
6974

70-
public static void shutdown() {
71-
if (grpcChannel != null)
72-
grpcChannel.shutdownNow();
73-
if (grpcServer != null)
74-
grpcServer.shutdownNow();
75-
}
76-
7775
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.grpc.test;
18+
19+
import org.springframework.context.ApplicationListener;
20+
import org.springframework.context.event.ContextClosedEvent;
21+
22+
import io.grpc.ManagedChannel;
23+
import io.grpc.Server;
24+
25+
/**
26+
* Listener that automatically shuts down the in-process gRPC {@link Server} and
27+
* {@link ManagedChannel} when the Spring
28+
* {@link org.springframework.context.ApplicationContext} is closed. This helps to ensure
29+
* proper resource cleanup after the application context is no longer active, avoiding the
30+
* need for manual shutdown calls in tests or other classes.
31+
*
32+
* @author Andrei Lisa
33+
*/
34+
final class InProcessServerShutdownListener implements ApplicationListener<ContextClosedEvent> {
35+
36+
private final Server grpcServer;
37+
38+
private final ManagedChannel grpcChannel;
39+
40+
InProcessServerShutdownListener(Server grpcServer, ManagedChannel grpcChannel) {
41+
this.grpcServer = grpcServer;
42+
this.grpcChannel = grpcChannel;
43+
}
44+
45+
@Override
46+
public void onApplicationEvent(ContextClosedEvent event) {
47+
if (grpcChannel != null) {
48+
grpcChannel.shutdownNow();
49+
}
50+
if (grpcServer != null) {
51+
grpcServer.shutdownNow();
52+
}
53+
}
54+
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"groups": [],
3+
"properties": [
4+
{
5+
"name": "spring.grpc.inprocess",
6+
"defaultValue": "true"
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)