Skip to content

Commit 81990f0

Browse files
onobcdsyer
authored andcommitted
Use channel factory API w/o customizer in tests
1 parent b08e11a commit 81990f0

File tree

8 files changed

+26
-47
lines changed

8 files changed

+26
-47
lines changed

samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.springframework.boot.test.context.TestConfiguration;
1313
import org.springframework.context.annotation.Bean;
1414
import org.springframework.context.annotation.Lazy;
15-
import org.springframework.grpc.client.ChannelBuilderOptions;
1615
import org.springframework.grpc.client.GrpcChannelFactory;
1716
import org.springframework.grpc.test.LocalGrpcPort;
1817
import org.springframework.test.annotation.DirtiesContext;
@@ -53,8 +52,7 @@ static class TestListener {
5352
@Bean
5453
@Lazy
5554
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
56-
return SimpleGrpc
57-
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
55+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
5856
}
5957

6058
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.springframework.boot.test.context.TestConfiguration;
1313
import org.springframework.context.annotation.Bean;
1414
import org.springframework.context.annotation.Lazy;
15-
import org.springframework.grpc.client.ChannelBuilderOptions;
1615
import org.springframework.grpc.client.GrpcChannelFactory;
1716
import org.springframework.grpc.sample.proto.HelloReply;
1817
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +51,7 @@ static class ExtraConfiguration {
5251
@Bean
5352
@Lazy
5453
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
55-
return SimpleGrpc
56-
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
54+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
5755
}
5856

5957
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.context.TestConfiguration;
3434
import org.springframework.context.annotation.Bean;
35-
import org.springframework.grpc.client.ChannelBuilderOptions;
3635
import org.springframework.grpc.client.GrpcChannelFactory;
3736
import org.springframework.grpc.sample.proto.HelloReply;
3837
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -63,7 +62,7 @@ class WithClientHealthEnabled {
6362
@Test
6463
void loadBalancerRespectsServerHealth(@Autowired GrpcChannelFactory channels,
6564
@Autowired HealthStatusManager healthStatusManager) {
66-
ManagedChannel channel = channels.createChannel("health-test", ChannelBuilderOptions.defaults());
65+
ManagedChannel channel = channels.createChannel("health-test");
6766
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channel);
6867

6968
// put the service up (SERVING) and give load balancer time to update
@@ -118,7 +117,7 @@ class WithActuatorHealthAdapter {
118117

119118
@Test
120119
void healthIndicatorsAdaptedToGrpcHealthStatus(@Autowired GrpcChannelFactory channels) {
121-
var channel = channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults());
120+
var channel = channels.createChannel("0.0.0.0:0");
122121
var healthStub = HealthGrpc.newBlockingStub(channel);
123122
var serviceName = "custom";
124123

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ServerWithInProcessChannel {
5454

5555
@Test
5656
void servesResponseToClient(@Autowired GrpcChannelFactory channels) {
57-
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
57+
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0"));
5858
}
5959

6060
}
@@ -65,8 +65,7 @@ class ServerWithException {
6565

6666
@Test
6767
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
68-
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
69-
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
68+
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
7069
assertThat(assertThrows(StatusRuntimeException.class,
7170
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
7271
.getStatus()
@@ -75,8 +74,7 @@ void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
7574

7675
@Test
7776
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
78-
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
79-
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
77+
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
8078
assertThat(assertThrows(StatusRuntimeException.class,
8179
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
8280
.getStatus()
@@ -91,8 +89,7 @@ class ServerWithUnhandledException {
9189

9290
@Test
9391
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
94-
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
95-
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
92+
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
9693
assertThat(assertThrows(StatusRuntimeException.class,
9794
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
9895
.getStatus()
@@ -101,8 +98,7 @@ void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
10198

10299
@Test
103100
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
104-
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
105-
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
101+
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:0"));
106102
assertThat(assertThrows(StatusRuntimeException.class,
107103
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
108104
.getStatus()
@@ -119,8 +115,7 @@ class ServerWithAnyIPv4AddressAndRandomPort {
119115
@Test
120116
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
121117
@LocalGrpcPort int port) {
122-
assertThatResponseIsServedToChannel(
123-
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
118+
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
124119
}
125120

126121
}
@@ -133,8 +128,7 @@ class ServerWithAnyIPv6AddressAndRandomPort {
133128
@Test
134129
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
135130
@LocalGrpcPort int port) {
136-
assertThatResponseIsServedToChannel(
137-
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
131+
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
138132
}
139133

140134
}
@@ -147,8 +141,7 @@ class ServerWithLocalhostAndRandomPort {
147141
@Test
148142
void servesResponseToClientWithLocalhostAndRandomPort(@Autowired GrpcChannelFactory channels,
149143
@LocalGrpcPort int port) {
150-
assertThatResponseIsServedToChannel(
151-
channels.createChannel("127.0.0.1:" + port, ChannelBuilderOptions.defaults()));
144+
assertThatResponseIsServedToChannel(channels.createChannel("127.0.0.1:" + port));
152145
}
153146

154147
}
@@ -162,8 +155,7 @@ class ServerConfiguredWithStaticClientChannel {
162155

163156
@Test
164157
void servesResponseToClientWithConfiguredChannel(@Autowired GrpcChannelFactory channels) {
165-
assertThatResponseIsServedToChannel(
166-
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
158+
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
167159
}
168160

169161
}
@@ -193,8 +185,7 @@ class ServerWithSsl {
193185

194186
@Test
195187
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
196-
assertThatResponseIsServedToChannel(
197-
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
188+
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
198189
}
199190

200191
}
@@ -212,8 +203,7 @@ class ServerWithClientAuth {
212203

213204
@Test
214205
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
215-
assertThatResponseIsServedToChannel(
216-
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
206+
assertThatResponseIsServedToChannel(channels.createChannel("test-channel"));
217207
}
218208

219209
}

samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.springframework.boot.test.web.server.LocalServerPort;
1414
import org.springframework.context.annotation.Bean;
1515
import org.springframework.context.annotation.Lazy;
16-
import org.springframework.grpc.client.ChannelBuilderOptions;
1716
import org.springframework.grpc.client.GrpcChannelFactory;
1817
import org.springframework.grpc.sample.proto.HelloReply;
1918
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -51,8 +50,7 @@ static class ExtraConfiguration {
5150
@Bean
5251
@Lazy
5352
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalServerPort int port) {
54-
return SimpleGrpc
55-
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
53+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
5654
}
5755

5856
}

samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
77
import org.junit.jupiter.api.Test;
8+
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.builder.SpringApplicationBuilder;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.context.TestConfiguration;
1213
import org.springframework.context.annotation.Bean;
1314
import org.springframework.context.annotation.Lazy;
14-
import org.springframework.grpc.client.ChannelBuilderOptions;
1515
import org.springframework.grpc.client.GrpcChannelFactory;
1616
import org.springframework.grpc.sample.proto.HelloReply;
1717
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +52,7 @@ static class ExtraConfiguration {
5252
@Bean
5353
@Lazy
5454
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
55-
return SimpleGrpc
56-
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
55+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
5756
}
5857

5958
}

samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
77
import org.junit.jupiter.api.Test;
8+
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.builder.SpringApplicationBuilder;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1213
import org.springframework.boot.test.context.TestConfiguration;
1314
import org.springframework.context.annotation.Bean;
1415
import org.springframework.context.annotation.Lazy;
15-
import org.springframework.grpc.client.ChannelBuilderOptions;
1616
import org.springframework.grpc.client.GrpcChannelFactory;
1717
import org.springframework.grpc.sample.proto.HelloReply;
1818
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -52,8 +52,7 @@ static class ExtraConfiguration {
5252
@Bean
5353
@Lazy
5454
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
55-
return SimpleGrpc
56-
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
55+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port));
5756
}
5857

5958
}

spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcChannelFactoryTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void globalCustomizersInvokedInOrder() {
5050
var customizer2 = mock(GrpcChannelBuilderCustomizer.class);
5151
var channelFactory = new DefaultGrpcChannelFactory(List.of(customizer1, customizer2), mock());
5252
channelFactory.setVirtualTargets(path -> path);
53-
var channel = channelFactory.createChannel(channelName, ChannelBuilderOptions.defaults());
53+
var channel = channelFactory.createChannel(channelName);
5454
assertThat(channel).isNotNull();
5555
var inOrder = inOrder(customizer1, customizer2);
5656
inOrder.verify(customizer1).customize(anyString(), any(ManagedChannelBuilder.class));
@@ -95,7 +95,7 @@ void whenOptionsContainNoInterceptorThenConfigurerInvokedWithNoInterceptor() {
9595
var channelName = "localhost";
9696
var channelFactory = new DefaultGrpcChannelFactory(List.of(), configurer);
9797
channelFactory.setVirtualTargets(path -> path);
98-
var channel = channelFactory.createChannel(channelName, ChannelBuilderOptions.defaults());
98+
var channel = channelFactory.createChannel(channelName);
9999
assertThat(channel).isNotNull();
100100
verify(configurer).configureInterceptors(any(ManagedChannelBuilder.class),
101101
assertArg((interceptors) -> assertThat(interceptors).isEmpty()), eq(false));
@@ -131,9 +131,8 @@ void nettyChannelFactoryUsesNettyChannelBuilder() {
131131
var channel = channelFactory.createChannel(channelName,
132132
ChannelBuilderOptions.defaults().withCustomizer(customizer1));
133133
assertThat(channel).isNotNull();
134-
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> {
135-
assertThat(builder).isInstanceOf(NettyChannelBuilder.class);
136-
}));
134+
verify(customizer1).customize(anyString(), ArgumentMatchers
135+
.assertArg((builder) -> assertThat(builder).isInstanceOf(NettyChannelBuilder.class)));
137136
}
138137

139138
@Test
@@ -145,9 +144,8 @@ void shadedNettyChannelFactoryUsesShadedNettyChannelBuilder() {
145144
var channel = channelFactory.createChannel(channelName,
146145
ChannelBuilderOptions.defaults().withCustomizer(customizer1));
147146
assertThat(channel).isNotNull();
148-
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> {
149-
assertThat(builder).isInstanceOf(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.class);
150-
}));
147+
verify(customizer1).customize(anyString(), ArgumentMatchers.assertArg((builder) -> assertThat(builder)
148+
.isInstanceOf(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder.class)));
151149
}
152150

153151
}

0 commit comments

Comments
 (0)