Disable in-process transport by default and add @AutoConfigureInProcessTransport annotation

This commit is contained in:
Andrey Litvitski
2025-01-01 18:28:21 +03:00
committed by Dave Syer
parent f8c6070eaa
commit ddef9007af
11 changed files with 94 additions and 19 deletions

View File

@@ -20,8 +20,7 @@ import com.example.demo.proto.HelloReply;
import com.example.demo.proto.HelloRequest;
import com.example.demo.proto.SimpleGrpc;
@SpringBootTest(properties = { "spring.grpc.client.channels.test.address=static://localhost:0",
"spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.client.channels.test.address=static://localhost:0" })
public class DemoApplicationTests {
private static Log log = LogFactory.getLog(DemoApplicationTests.class);

View File

@@ -19,14 +19,13 @@ import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest(properties = { "spring.grpc.server.port=0", "spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.server.port=0" })
public class GrpcServerApplicationTests {
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);
public static void main(String[] args) {
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class)
.run("--spring.grpc.inprocess.enabled=false");
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).run();
}
@Autowired

View File

@@ -36,6 +36,7 @@ import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import org.springframework.test.annotation.DirtiesContext;
import io.grpc.ManagedChannel;
@@ -52,7 +53,7 @@ import io.grpc.protobuf.services.HealthStatusManager;
class GrpcServerHealthIntegrationTests {
@Nested
@SpringBootTest(properties = { "spring.grpc.inprocess.enabled=false", "spring.grpc.server.port=0",
@SpringBootTest(properties = { "spring.grpc.server.port=0",
"spring.grpc.client.channels.health-test.address=static://0.0.0.0:${local.grpc.port}",
"spring.grpc.client.channels.health-test.health.enabled=true",
"spring.grpc.client.channels.health-test.health.service-name=my-service" })
@@ -112,6 +113,7 @@ class GrpcServerHealthIntegrationTests {
@SpringBootTest(properties = { "spring.grpc.server.health.actuator.health-indicator-paths=custom",
"spring.grpc.server.health.actuator.update-initial-delay=3s",
"spring.grpc.server.health.actuator.update-rate=3s", "management.health.defaults.enabled=true" })
@AutoConfigureInProcessTransport
@DirtiesContext
class WithActuatorHealthAdapter {

View File

@@ -33,6 +33,7 @@ import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.server.GrpcServerFactory;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
@@ -50,6 +51,7 @@ class GrpcServerIntegrationTests {
@Nested
@SpringBootTest
@AutoConfigureInProcessTransport
class ServerWithInProcessChannel {
@Test
@@ -61,6 +63,7 @@ class GrpcServerIntegrationTests {
@Nested
@SpringBootTest
@AutoConfigureInProcessTransport
class ServerWithException {
@Test
@@ -85,6 +88,7 @@ class GrpcServerIntegrationTests {
@Nested
@SpringBootTest("spring.grpc.server.exception-handler.enabled=false")
@AutoConfigureInProcessTransport
class ServerWithUnhandledException {
@Test
@@ -108,8 +112,7 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0",
"spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.server.host=0.0.0.0", "spring.grpc.server.port=0" })
class ServerWithAnyIPv4AddressAndRandomPort {
@Test
@@ -121,8 +124,7 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest(properties = { "spring.grpc.server.host=::", "spring.grpc.server.port=0",
"spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.server.host=::", "spring.grpc.server.port=0" })
class ServerWithAnyIPv6AddressAndRandomPort {
@Test
@@ -134,8 +136,7 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest(properties = { "spring.grpc.server.host=127.0.0.1", "spring.grpc.server.port=0",
"spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.server.host=127.0.0.1", "spring.grpc.server.port=0" })
class ServerWithLocalhostAndRandomPort {
@Test
@@ -148,8 +149,7 @@ class GrpcServerIntegrationTests {
@Nested
@SpringBootTest(properties = { "spring.grpc.server.port=0",
"spring.grpc.client.channels.test-channel.address=static://0.0.0.0:${local.grpc.port}",
"spring.grpc.inprocess.enabled=false" })
"spring.grpc.client.channels.test-channel.address=static://0.0.0.0:${local.grpc.port}" })
@DirtiesContext
class ServerConfiguredWithStaticClientChannel {
@@ -161,8 +161,7 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest(
properties = { "spring.grpc.server.address=unix:unix-test-channel", "spring.grpc.inprocess.enabled=false" })
@SpringBootTest(properties = { "spring.grpc.server.address=unix:unix-test-channel" })
@EnabledOnOs(OS.LINUX)
class ServerWithUnixDomain {
@@ -178,7 +177,7 @@ class GrpcServerIntegrationTests {
@SpringBootTest(properties = { "spring.grpc.server.port=0",
"spring.grpc.client.channels.test-channel.address=static://0.0.0.0:${local.grpc.port}",
"spring.grpc.client.channels.test-channel.negotiation-type=TLS",
"spring.grpc.client.channels.test-channel.secure=false", "spring.grpc.inprocess.enabled=false" })
"spring.grpc.client.channels.test-channel.secure=false" })
@ActiveProfiles("ssl")
@DirtiesContext
class ServerWithSsl {
@@ -196,7 +195,7 @@ class GrpcServerIntegrationTests {
"spring.grpc.client.channels.test-channel.address=static://0.0.0.0:${local.grpc.port}",
"spring.grpc.client.channels.test-channel.ssl.bundle=ssltest",
"spring.grpc.client.channels.test-channel.negotiation-type=TLS",
"spring.grpc.client.channels.test-channel.secure=false", "spring.grpc.inprocess.enabled=false" })
"spring.grpc.client.channels.test-channel.secure=false" })
@ActiveProfiles("ssl")
@DirtiesContext
class ServerWithClientAuth {

View File

@@ -17,9 +17,11 @@ import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureInProcessTransport
public class GrpcServerApplicationTests {
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);

View File

@@ -16,11 +16,13 @@ import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "spring.grpc.server.servlet.enabled=false", "spring.grpc.server.port=0" })
@AutoConfigureInProcessTransport
public class ListenOnTwoPortsTests {
private static Log log = LogFactory.getLog(ListenOnTwoPortsTests.class);

View File

@@ -17,10 +17,12 @@ import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.grpc.server.reactive.enabled=false")
@AutoConfigureInProcessTransport
public class GrpcServerApplicationTests {
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);

View File

@@ -0,0 +1,27 @@
/*
* 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 java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoConfigureInProcessTransport {
}

View File

@@ -35,7 +35,7 @@ import io.grpc.BindableService;
import io.grpc.inprocess.InProcessServerBuilder;
@AutoConfiguration(before = { GrpcServerFactoryAutoConfiguration.class, GrpcClientAutoConfiguration.class })
@ConditionalOnProperty(prefix = "spring.grpc.inprocess", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = "spring.grpc.inprocess", name = "enabled", havingValue = "true")
@ConditionalOnClass(BindableService.class)
@ConditionalOnNotWebApplication
@Import(ClientInterceptorsConfiguration.class)

View File

@@ -0,0 +1,39 @@
/*
* 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 java.util.Collections;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
public class InProcessTransportEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
if (AnnotationUtils.findAnnotation(application.getMainApplicationClass(),
AutoConfigureInProcessTransport.class) == null) {
return;
}
MapPropertySource inProcessTransportPropertySource = new MapPropertySource("inProcessTransportPropertySource",
Collections.singletonMap("spring.grpc.inprocess.enabled", "true"));
environment.getPropertySources().addFirst(inProcessTransportPropertySource);
}
}

View File

@@ -1,3 +1,7 @@
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.grpc.test.ServerPortInfoApplicationContextInitializer
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.grpc.test.InProcessTransportEnvironmentPostProcessor