Use channel factory API w/o customizer in tests

This commit is contained in:
Chris Bono
2025-01-03 14:31:44 -06:00
committed by Dave Syer
parent b08e11ad39
commit 81990f0fd6
8 changed files with 26 additions and 47 deletions

View File

@@ -12,7 +12,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
@@ -53,8 +52,7 @@ public class DemoApplicationTests {
@Bean
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
}
}

View File

@@ -12,7 +12,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +51,7 @@ public class GrpcServerApplicationTests {
@Bean
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
}
}

View File

@@ -32,7 +32,6 @@ import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -63,7 +62,7 @@ class GrpcServerHealthIntegrationTests {
@Test
void loadBalancerRespectsServerHealth(@Autowired GrpcChannelFactory channels,
@Autowired HealthStatusManager healthStatusManager) {
ManagedChannel channel = channels.createChannel("health-test", ChannelBuilderOptions.defaults());
ManagedChannel channel = channels.createChannel("health-test");
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channel);
// put the service up (SERVING) and give load balancer time to update
@@ -118,7 +117,7 @@ class GrpcServerHealthIntegrationTests {
@Test
void healthIndicatorsAdaptedToGrpcHealthStatus(@Autowired GrpcChannelFactory channels) {
var channel = channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults());
var channel = channels.createChannel("0.0.0.0:0");
var healthStub = HealthGrpc.newBlockingStub(channel);
var serviceName = "custom";

View File

@@ -54,7 +54,7 @@ class GrpcServerIntegrationTests {
@Test
void servesResponseToClient(@Autowired GrpcChannelFactory channels) {
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0"));
}
}
@@ -65,8 +65,7 @@ class GrpcServerIntegrationTests {
@Test
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
.getStatus()
@@ -75,8 +74,7 @@ class GrpcServerIntegrationTests {
@Test
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
.getStatus()
@@ -91,8 +89,7 @@ class GrpcServerIntegrationTests {
@Test
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
.getStatus()
@@ -101,8 +98,7 @@ class GrpcServerIntegrationTests {
@Test
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
assertThat(assertThrows(StatusRuntimeException.class,
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
.getStatus()
@@ -119,8 +115,7 @@ class GrpcServerIntegrationTests {
@Test
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
@LocalGrpcPort int port) {
assertThatResponseIsServedToChannel(
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
}
}
@@ -133,8 +128,7 @@ class GrpcServerIntegrationTests {
@Test
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
@LocalGrpcPort int port) {
assertThatResponseIsServedToChannel(
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
}
}
@@ -147,8 +141,7 @@ class GrpcServerIntegrationTests {
@Test
void servesResponseToClientWithLocalhostAndRandomPort(@Autowired GrpcChannelFactory channels,
@LocalGrpcPort int port) {
assertThatResponseIsServedToChannel(
channels.createChannel("127.0.0.1:" + port, ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("127.0.0.1:" + port));
}
}
@@ -162,8 +155,7 @@ class GrpcServerIntegrationTests {
@Test
void servesResponseToClientWithConfiguredChannel(@Autowired GrpcChannelFactory channels) {
assertThatResponseIsServedToChannel(
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
}
}
@@ -193,8 +185,7 @@ class GrpcServerIntegrationTests {
@Test
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
assertThatResponseIsServedToChannel(
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
}
}
@@ -212,8 +203,7 @@ class GrpcServerIntegrationTests {
@Test
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
assertThatResponseIsServedToChannel(
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
}
}

View File

@@ -13,7 +13,6 @@ import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -51,8 +50,7 @@ public class GrpcServerApplicationTests {
@Bean
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalServerPort int port) {
return SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
}
}

View File

@@ -5,13 +5,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +52,7 @@ public class ListenOnTwoPortsTests {
@Bean
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
}
}

View File

@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
@@ -12,7 +13,6 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +52,7 @@ public class GrpcServerApplicationTests {
@Bean
@Lazy
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return SimpleGrpc
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
}
}

View File

@@ -50,7 +50,7 @@ class GrpcChannelFactoryTests {
var customizer2 = mock(GrpcChannelBuilderCustomizer.class);
var channelFactory = new DefaultGrpcChannelFactory(List.of(customizer1, customizer2), mock());
channelFactory.setVirtualTargets(path -> path);
var channel = channelFactory.createChannel(channelName, ChannelBuilderOptions.defaults());
var channel = channelFactory.createChannel(channelName);
assertThat(channel).isNotNull();
var inOrder = inOrder(customizer1, customizer2);
inOrder.verify(customizer1).customize(anyString(), any(ManagedChannelBuilder.class));
@@ -95,7 +95,7 @@ class GrpcChannelFactoryTests {
var channelName = "localhost";
var channelFactory = new DefaultGrpcChannelFactory(List.of(), configurer);
channelFactory.setVirtualTargets(path -> path);
var channel = channelFactory.createChannel(channelName, ChannelBuilderOptions.defaults());
var channel = channelFactory.createChannel(channelName);
assertThat(channel).isNotNull();
verify(configurer).configureInterceptors(any(ManagedChannelBuilder.class),
assertArg((interceptors) -> assertThat(interceptors).isEmpty()), eq(false));
@@ -131,9 +131,8 @@ class GrpcChannelFactoryTests {
var channel = channelFactory.createChannel(channelName,
ChannelBuilderOptions.defaults().withCustomizer(customizer1));
assertThat(channel).isNotNull();
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> {
assertThat(builder).isInstanceOf(NettyChannelBuilder.class);
}));
verify(customizer1).customize(anyString(), ArgumentMatchers
.assertArg((builder) -> assertThat(builder).isInstanceOf(NettyChannelBuilder.class)));
}
@Test
@@ -145,9 +144,8 @@ class GrpcChannelFactoryTests {
var channel = channelFactory.createChannel(channelName,
ChannelBuilderOptions.defaults().withCustomizer(customizer1));
assertThat(channel).isNotNull();
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> {
assertThat(builder).isInstanceOf(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.class);
}));
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> assertThat(builder)
.isInstanceOf(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.class)));
}
}