diff --git a/samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java b/samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java index b6460db..58c4a48 100644 --- a/samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java +++ b/samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java @@ -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); diff --git a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 266c4e2..bbd61cf 100644 --- a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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 diff --git a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerHealthIntegrationTests.java b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerHealthIntegrationTests.java index f6640d7..6024c94 100644 --- a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerHealthIntegrationTests.java +++ b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerHealthIntegrationTests.java @@ -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 { diff --git a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java index 7a7f723..e23edde 100644 --- a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java +++ b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java @@ -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 { diff --git a/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 209caf1..de8aec9 100644 --- a/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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); diff --git a/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java b/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java index d6e689e..3b42cb7 100644 --- a/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java +++ b/samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java @@ -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); diff --git a/samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 6c58324..396da38 100644 --- a/samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -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); diff --git a/spring-grpc-test/src/main/java/org/springframework/grpc/test/AutoConfigureInProcessTransport.java b/spring-grpc-test/src/main/java/org/springframework/grpc/test/AutoConfigureInProcessTransport.java new file mode 100644 index 0000000..9d4a881 --- /dev/null +++ b/spring-grpc-test/src/main/java/org/springframework/grpc/test/AutoConfigureInProcessTransport.java @@ -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 { + +} diff --git a/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessGrpcServerFactoryAutoConfiguration.java b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessGrpcServerFactoryAutoConfiguration.java index f8e83df..9f345ca 100644 --- a/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessGrpcServerFactoryAutoConfiguration.java +++ b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessGrpcServerFactoryAutoConfiguration.java @@ -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) diff --git a/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTransportEnvironmentPostProcessor.java b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTransportEnvironmentPostProcessor.java new file mode 100644 index 0000000..3837473 --- /dev/null +++ b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTransportEnvironmentPostProcessor.java @@ -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); + } + +} diff --git a/spring-grpc-test/src/main/resources/META-INF/spring.factories b/spring-grpc-test/src/main/resources/META-INF/spring.factories index 93d33e3..a606d21 100644 --- a/spring-grpc-test/src/main/resources/META-INF/spring.factories +++ b/spring-grpc-test/src/main/resources/META-INF/spring.factories @@ -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 \ No newline at end of file