forked from LogNet/grpc-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsulRegistrationTest.java
103 lines (81 loc) · 3.76 KB
/
ConsulRegistrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.lognet.springboot.grpc;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.health.model.HealthService;
import com.pszymczyk.consul.ConsulProcess;
import com.pszymczyk.consul.ConsulStarterBuilder;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.examples.GreeterGrpc;
import io.grpc.examples.GreeterOuterClass;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.*;
@SpringBootTest(classes = DemoApp.class, properties = {"spring.cloud.config.enabled:false",
"spring.cloud.consul.discovery.enabled=true",
"spring.cloud.consul.enabled=true",
"spring.cloud.service-registry.auto-registration.enabled=true",
"grpc.shutdownGrace=1"})
@RunWith(SpringRunner.class)
public class ConsulRegistrationTest {
private static ConsulProcess consul;
@BeforeClass
public static void startConsul(){
int port = SocketUtils.findAvailableTcpPort();
consul = ConsulStarterBuilder.consulStarter().withHttpPort(port).build().start();
System.setProperty("spring.cloud.consul.port",String.valueOf(port));
}
@AfterClass
public static void clear(){
System.clearProperty("spring.cloud.consul.port");
consul.close();
}
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
ConfigurableApplicationContext applicationContext;
@Test
public void contextLoads() throws ExecutionException, InterruptedException {
final String serviceId = "grpc-grpc-demo";
final ConsulClient consulClient = new ConsulClient("localhost", Integer.parseInt(System.getProperty("spring.cloud.consul.port")));
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertFalse(instances.isEmpty());
ServiceInstance serviceInstance = instances.get(0);
ManagedChannel channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
.usePlaintext()
.build();
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(channel);
final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
assertNotNull("Replay should not be null",reply);
boolean isHealthy = false;
for(int i=0;i<5; ++i){
final List<HealthService> healthServices = consulClient.getHealthServices(serviceId, true, QueryParams.DEFAULT).getValue();
isHealthy =healthServices
.stream()
.flatMap(h->h.getChecks().stream())
.anyMatch(c-> Check.CheckStatus.PASSING.equals(c.getStatus())&& c.getCheckId().contains(serviceId));
if(isHealthy){
break;
}else{
Thread.sleep(Duration.ofSeconds(10).toMillis());
}
}
assertTrue(isHealthy);
applicationContext.stop();
}
}