Skip to content

Commit 403c610

Browse files
author
Alexander Furer
committed
closes #251
1 parent 96928fa commit 403c610

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.lognet.springboot.grpc.consul;
2+
3+
import com.ecwid.consul.v1.health.model.HealthService;
4+
import org.hamcrest.Matchers;
5+
import org.junit.runner.RunWith;
6+
import org.lognet.springboot.grpc.demo.DemoApp;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.event.RecordApplicationEvents;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.util.List;
12+
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
15+
16+
@SpringBootTest(classes = DemoApp.class,properties = "grpc.consul.registration-mode=NOOP")
17+
@RunWith(SpringRunner.class)
18+
@RecordApplicationEvents
19+
public class ConsulNoopHealthCheckTest extends ConsulRegistrationBaseTest{
20+
21+
@Override
22+
void doTest( List<HealthService> healthServices) {
23+
assertThat(healthServices,Matchers.hasSize(0));
24+
}
25+
}

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/consul/ConsulRegistrationBaseTest.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,54 @@ public void setUp() throws Exception {
8080

8181

8282
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
83-
assertThat(instances, Matchers.not(Matchers.empty()));
8483

85-
ServiceInstance serviceInstance = instances.get(0);
84+
final ServiceRegistrationMode registrationMode = applicationContext.getBean(GRpcServerProperties.class)
85+
.getConsul().getRegistrationMode();
8686

87-
channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
88-
.usePlaintext()
89-
.build();
87+
if(!ServiceRegistrationMode.NOOP.equals(registrationMode)) {
9088

91-
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
92-
final GreeterOuterClass.HelloRequest helloRequest = GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
93-
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
94-
assertThat("Reply should not be null", reply, Matchers.notNullValue(String.class));
89+
90+
assertThat(instances, Matchers.not(Matchers.empty()));
91+
92+
ServiceInstance serviceInstance = instances.get(0);
93+
94+
channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
95+
.usePlaintext()
96+
.build();
97+
98+
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
99+
final GreeterOuterClass.HelloRequest helloRequest = GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
100+
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
101+
assertThat("Reply should not be null", reply, Matchers.notNullValue(String.class));
102+
}
95103
}
96104

97105
@After
98106
public void tearDown() throws Exception {
99-
channel.shutdownNow();
100-
channel.awaitTermination(1, TimeUnit.SECONDS);
107+
if(null!=channel) {
108+
channel.shutdownNow();
109+
channel.awaitTermination(1, TimeUnit.SECONDS);
110+
}
101111
applicationContext.stop();
102112
}
103113

104114
@Test
105115
public void contextLoads() {
106116

107-
108-
int expectedRegistrations = applicationContext.getBean(GRpcServerProperties.class)
117+
int minExpectedRegistrations;
118+
switch (applicationContext.getBean(GRpcServerProperties.class)
109119
.getConsul()
110-
.getRegistrationMode().equals(ServiceRegistrationMode.STANDALONE_SERVICES) ?
111-
getServicesDefinitions().size() : 1;
120+
.getRegistrationMode()) {
121+
case STANDALONE_SERVICES:
122+
minExpectedRegistrations = getServicesDefinitions().size();
123+
break;
124+
case NOOP:
125+
minExpectedRegistrations = 0;
126+
break;
127+
default:
128+
minExpectedRegistrations = 1;
129+
}
130+
112131

113132
final List<HealthService> healthServices = Awaitility.await()
114133
.atMost(Duration.ofSeconds(20))
@@ -119,7 +138,7 @@ public void contextLoads() {
119138
.build())
120139
.getValue()
121140

122-
, Matchers.hasSize(Matchers.greaterThanOrEqualTo(expectedRegistrations)));
141+
, Matchers.hasSize(Matchers.greaterThanOrEqualTo(minExpectedRegistrations)));
123142

124143
doTest(healthServices);
125144

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/consul/ServiceRegistrationMode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
import java.util.stream.Collectors;
1919

2020
public enum ServiceRegistrationMode implements ServiceRegistrationStrategy {
21+
NOOP{
22+
@Override
23+
public Collection<NewService> createServices(Server grpcServer, ApplicationContext applicationContext) {
24+
return Collections.emptyList();
25+
}
26+
},
2127
SINGLE_SERVER_WITH_GLOBAL_CHECK {
2228
@Override
2329
public Collection<NewService> createServices(Server grpcServer, ApplicationContext applicationContext) {

0 commit comments

Comments
 (0)