Skip to content

Commit

Permalink
#7: clean up
Browse files Browse the repository at this point in the history
Signed-off-by: andreilisa <[email protected]>
  • Loading branch information
andreilisa committed Nov 9, 2024
1 parent f645b98 commit 4e99be9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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;
Expand All @@ -30,14 +14,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

@SpringBootTest
class InProcessApplicationContextInitializerTests {

private GenericApplicationContext context;

@BeforeEach
public void setUp() {
System.clearProperty("spring.grpc.inprocess");
context = new AnnotationConfigApplicationContext();
}

Expand All @@ -51,7 +33,6 @@ class WhenDefaultEnabled {

@Test
void shouldInitializeInProcessServer() {
System.setProperty("spring.grpc.inprocess", "true");
new InProcessApplicationContextInitializer().initialize(context);
context.refresh();

Expand All @@ -62,26 +43,38 @@ void shouldInitializeInProcessServer() {
}

@Nested
@SpringBootTest(properties = { "spring.grpc.server.host=127.0.0.1", "spring.grpc.server.port=0",
"spring.grpc.inprocess.enabled=false" })
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 WhenNoPropertyIsSet {

@Test
void shouldUseDefaultTrueAndInitializeInProcessServer() {
new InProcessApplicationContextInitializer().initialize(context);
context.refresh();

ManagedChannel channel = context.getBean("grpcInProcessChannel", ManagedChannel.class);
assertThat(channel).isNotNull().isInstanceOf(ManagedChannel.class);
}

}

@Nested
class WhenShutdownIsCalled {

@Test
void shouldShutdownInProcessServer() {
System.setProperty("spring.grpc.inprocess", "true");
new InProcessApplicationContextInitializer().initialize(context);
context.registerShutdownHook();
context.refresh();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.core.env.Environment;

/**
* A configuration class that holds the properties related to in-process gRPC
* communication. It reads the `spring.grpc.inprocess.enabled` property from the Spring
* environment and provides access to it via a getter method.
* <p>
* This class is typically used to determine whether the in-process gRPC server should be
* enabled in the application. It supports reading the property from external sources such
* as `application.properties` or `application.yml`.
* <p>
* The default value for the `spring.grpc.inprocess.enabled` property is `true` if not
* explicitly defined.
*
* @author Andrei Lisa
*/

class GrpcProperties {

private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess.enabled";

private final boolean inProcessEnabled;

GrpcProperties(Environment environment) {
this.inProcessEnabled = Boolean.parseBoolean(environment.getProperty(PROPERTY_SOURCE_NAME, "true"));
}

public boolean isInProcessEnabled() {
return inProcessEnabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@
public class InProcessApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private static final String PROPERTY_SOURCE_NAME = "spring.grpc.inprocess";

private static final String CHANNEL_NAME = "grpcInProcessChannel";

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
String inProcessEnabled = applicationContext.getEnvironment().getProperty(PROPERTY_SOURCE_NAME);

if ("true".equalsIgnoreCase(inProcessEnabled) && isJarOnClasspath()) {
GrpcProperties grpcProperties = new GrpcProperties(applicationContext.getEnvironment());
if (grpcProperties.isInProcessEnabled() && isJarOnClasspath()) {
try {
String serverName = InProcessServerBuilder.generateName();

Expand Down

This file was deleted.

0 comments on commit 4e99be9

Please sign in to comment.