Skip to content

Commit 171f255

Browse files
onobcdsyer
authored andcommitted
Fixes to allow non-test inprocess channels
- Fix @LocalGrpcPort to ignore the inprocess factory port - Don't set load balancer policy for inprocess channel factory Signed-off-by: Chris Bono <[email protected]>
1 parent a087eac commit 171f255

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
338338

339339
}
340340

341+
@Nested
342+
@SpringBootTest(properties = { "spring.grpc.server.inprocess.name=foo", "spring.grpc.server.host=0.0.0.0",
343+
"spring.grpc.server.port=0" })
344+
class ServerWithRegularAndInProcessChannelsAndFactories {
345+
346+
@Test
347+
void servesResponseToNonInProcessClient(@Autowired GrpcChannelFactory channels, @LocalGrpcPort int port) {
348+
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
349+
}
350+
351+
@Test
352+
void servesResponseToInProcessClient(@Autowired GrpcChannelFactory channels) {
353+
assertThatResponseIsServedToChannel(channels.createChannel("in-process:foo"));
354+
}
355+
356+
}
357+
341358
private void assertThatResponseIsServedToChannel(ManagedChannel clientChannel) {
342359
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(clientChannel);
343360
HelloReply response = client.sayHello(HelloRequest.newBuilder().setName("Alien").build());

spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public int getPort() {
107107
return this.server == null ? 0 : this.server.getPort();
108108
}
109109

110+
/**
111+
* Gets the server factory used to create the server.
112+
* @return the server factory to create the server
113+
*/
114+
public GrpcServerFactory getFactory() {
115+
return this.factory;
116+
}
117+
110118
/**
111119
* Creates and starts the grpc server.
112120
* @throws IOException If the server is unable to bind the port.

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientPropertiesChannelBuilderCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void customize(String authority, T builder) {
4949
ChannelConfig channel = this.properties.getChannel(authority);
5050
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
5151
mapper.from(channel.getUserAgent()).to(builder::userAgent);
52-
if (!authority.startsWith("unix:")) {
52+
if (!authority.startsWith("unix:") && !authority.startsWith("in-process:")) {
5353
mapper.from(channel.getDefaultLoadBalancingPolicy()).to(builder::defaultLoadBalancingPolicy);
5454
}
5555
mapper.from(channel.getMaxInboundMessageSize()).asInt(DataSize::toBytes).to(builder::maxInboundMessageSize);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.env.MapPropertySource;
2727
import org.springframework.core.env.MutablePropertySources;
2828
import org.springframework.core.env.PropertySource;
29+
import org.springframework.grpc.server.InProcessGrpcServerFactory;
2930
import org.springframework.grpc.server.lifecycle.GrpcServerStartedEvent;
3031

3132
public class ServerPortInfoApplicationContextInitializer implements
@@ -43,6 +44,9 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
4344

4445
@Override
4546
public void onApplicationEvent(GrpcServerStartedEvent event) {
47+
if (event.getSource().getFactory() instanceof InProcessGrpcServerFactory) {
48+
return;
49+
}
4650
String propertyName = "local.grpc.port";
4751
setPortProperty(applicationContext, propertyName, event.getPort());
4852
}

0 commit comments

Comments
 (0)