Skip to content

Commit dd0970b

Browse files
author
Alexander Furer
committed
pure netty dependencies test
1 parent b11fb87 commit dd0970b

File tree

8 files changed

+171
-13
lines changed

8 files changed

+171
-13
lines changed

grpc-spring-boot-starter-demo/build.gradle

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,39 @@ buildscript {
44
}
55
dependencies {
66
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
7+
classpath "com.netflix.nebula:nebula-project-plugin:7.0.7"
78
}
89
}
910
apply plugin: 'java'
1011
apply plugin: 'com.google.protobuf'
1112
apply plugin: 'org.springframework.boot'
1213
apply plugin: 'io.spring.dependency-management'
14+
apply plugin: "nebula.facet"
1315

14-
16+
facets {
17+
pureNettyTest
18+
}
1519

1620
dependencyManagement {
1721
imports {
1822
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
1923
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
2024
}
2125
}
26+
configurations.findAll{ cfg ->
27+
if (cfg.name.startsWith("pureNetty")) {
28+
cfg.exclude group: 'io.grpc', module: 'grpc-netty-shaded'
29+
dependencies.add(cfg.name,"io.grpc:grpc-netty")
30+
}
31+
if(cfg.name.equals("pureNettyTestCompileClasspath")){
32+
cfg.extendsFrom(configurations.getByName("testCompileClasspath"))
33+
}
34+
if(cfg.name.equals("pureNettyTestRuntimeClasspath")){
35+
cfg.extendsFrom(configurations.getByName("testRuntimeClasspath"))
36+
}
37+
38+
}
39+
2240

2341
dependencies {
2442

@@ -42,7 +60,6 @@ dependencies {
4260
testCompile 'org.awaitility:awaitility:4.0.3'
4361

4462

45-
4663
testCompile "org.springframework.cloud:spring-cloud-config-server"
4764
testCompile "org.springframework.cloud:spring-cloud-config-client"
4865
testCompile "com.playtika.testcontainers:embedded-keycloak:2.0.6"
@@ -61,9 +78,9 @@ dependencies {
6178

6279
configurations.all {
6380
resolutionStrategy.eachDependency { details ->
64-
if ("io.grpc".equalsIgnoreCase(details.requested.group)) {
65-
details.useVersion grpcVersion
66-
}
81+
if ("io.grpc".equalsIgnoreCase(details.requested.group)) {
82+
details.useVersion grpcVersion
83+
}
6784
}
6885
}
6986
sourceSets {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import io.grpc.ManagedChannel;
4+
import io.grpc.ManagedChannelBuilder;
5+
import io.grpc.Server;
6+
import io.grpc.ServerBuilder;
7+
import io.grpc.examples.GreeterGrpc;
8+
import io.grpc.examples.GreeterOuterClass;
9+
import org.hamcrest.Matchers;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
13+
import org.lognet.springboot.grpc.context.LocalRunningGrpcPort;
14+
import org.lognet.springboot.grpc.demo.DemoApp;
15+
import org.mockito.ArgumentCaptor;
16+
import org.mockito.Mockito;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
22+
import org.springframework.context.event.EventListener;
23+
import org.springframework.test.context.junit4.SpringRunner;
24+
25+
import java.net.InetSocketAddress;
26+
import java.net.SocketAddress;
27+
import java.util.List;
28+
import java.util.concurrent.ExecutionException;
29+
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.junit.Assert.assertNotNull;
32+
33+
34+
import static org.junit.Assert.assertThrows;
35+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
36+
37+
@RunWith(SpringRunner.class)
38+
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE,
39+
properties = {
40+
"grpc.netty-server.additional-listen-addresses[0]=localhost:0",
41+
"grpc.netty-server.primary-listen-address=localhost:0"
42+
43+
}
44+
)
45+
@Import(PureNettyTransportConfigTest.TestConfig.class)
46+
public class PureNettyTransportConfigTest {
47+
48+
@LocalRunningGrpcPort
49+
protected int runningPort;
50+
51+
@Configuration
52+
static class TestConfig {
53+
54+
@EventListener
55+
public void onServer(GRpcServerInitializedEvent e){
56+
final List<? extends SocketAddress> listenSockets = e.getServer().getListenSockets();
57+
assertThat(listenSockets.size(), Matchers.is(2));
58+
}
59+
60+
@Bean
61+
public GRpcServerBuilderConfigurer customGrpcServerBuilderConfigurer() {
62+
return Mockito.mock(GRpcServerBuilderConfigurer.class);
63+
}
64+
65+
}
66+
@Autowired
67+
private GRpcServerBuilderConfigurer configurer;
68+
69+
70+
@Test
71+
public void simpleGreeting() throws ExecutionException, InterruptedException {
72+
73+
assertThrows(ClassNotFoundException.class,()->Class.forName("io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder"));
74+
final ArgumentCaptor<ServerBuilder> captor = ArgumentCaptor.forClass(ServerBuilder.class);
75+
Mockito.verify(configurer,Mockito.times(1)).configure(captor.capture());
76+
assertThat("Should be pure NettyServerBuilder, not repackaged",io.grpc.netty.NettyServerBuilder.class.isInstance(captor.getValue()));
77+
78+
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", runningPort)
79+
.usePlaintext()
80+
.build();
81+
82+
83+
String name = "John";
84+
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
85+
final GreeterOuterClass.HelloRequest helloRequest = GreeterOuterClass.HelloRequest.newBuilder().setName(name).build();
86+
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
87+
assertNotNull("Reply should not be null", reply);
88+
assertThat(String.format("Reply should contain name '%s'", name), reply.contains(name));
89+
90+
91+
}
92+
93+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
management:
2+
metrics:
3+
export:
4+
simple:
5+
enabled: false
6+
endpoints:
7+
web:
8+
base-path: "/"
9+
exposure:
10+
include: "*"
11+
12+
13+
logging:
14+
level:
15+
root: INFO
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
spring:
2+
application:
3+
name: grpc-demo
4+
cloud:
5+
config:
6+
enabled: false
7+
consul:
8+
discovery:
9+
enabled: false
10+
enabled: false
11+
service-registry:
12+
auto-registration:
13+
enabled: false
14+
embedded:
15+
keycloak:
16+
enabled: false
17+
containers:
18+
enabled: false
19+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<configuration>
2+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
3+
<logger name="org.springframework" level="INFO"/>
4+
5+
6+
</configuration>

grpc-spring-boot-starter-demo/src/test/resources/application.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ management:
1313
spring:
1414
profiles:
1515
active: disable-security
16-
cloud:
17-
consul:
18-
discovery:
19-
enabled: false
20-
enabled: false
21-
service-registry:
22-
auto-registration:
23-
enabled: false
16+
logging:
17+
level:
18+
root: INFO

grpc-spring-boot-starter-demo/src/test/resources/bootstrap.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ spring:
22
application:
33
name: grpc-demo
44
cloud:
5+
consul:
6+
discovery:
7+
enabled: false
8+
enabled: false
9+
service-registry:
10+
auto-registration:
11+
enabled: false
512
config:
613
uri: http://localhost:${config.port:8888}
714
embedded:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<configuration>
2+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
3+
<logger name="org.springframework" level="INFO"/>
4+
5+
6+
</configuration>

0 commit comments

Comments
 (0)