Skip to content

Commit 911315b

Browse files
author
Alexander Furer
committed
Consul use random port
1 parent 1b7ab1d commit 911315b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

grpc-spring-boot2-starter-demo/src/test/java/org/lognet/springboot/grpc/ConsulRegistrationTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.grpc.ManagedChannelBuilder;
66
import io.grpc.examples.GreeterGrpc;
77
import io.grpc.examples.GreeterOuterClass;
8+
import org.junit.AfterClass;
89
import org.junit.ClassRule;
910
import org.junit.Test;
1011
import org.junit.runner.RunWith;
@@ -14,6 +15,7 @@
1415
import org.springframework.cloud.client.ServiceInstance;
1516
import org.springframework.cloud.client.discovery.DiscoveryClient;
1617
import org.springframework.test.context.junit4.SpringRunner;
18+
import org.springframework.util.SocketUtils;
1719

1820
import java.util.List;
1921
import java.util.concurrent.ExecutionException;
@@ -27,7 +29,16 @@
2729
public class ConsulRegistrationTest {
2830

2931
@ClassRule
30-
public static final ConsulResource consul = new ConsulResource(8500);
32+
public static final ConsulResource consul(){
33+
int port = SocketUtils.findAvailableTcpPort();
34+
ConsulResource consulResource = new ConsulResource(port);
35+
System.setProperty("spring.cloud.consul.port",String.valueOf(port));
36+
return consulResource;
37+
}
38+
@AfterClass
39+
public static void clear(){
40+
System.clearProperty("spring.cloud.consul.port");
41+
}
3142

3243
@Autowired
3344
private DiscoveryClient discoveryClient;
@@ -46,7 +57,5 @@ public void contextLoads() throws ExecutionException, InterruptedException {
4657
final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
4758
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
4859
assertNotNull("Replay should not be null",reply);
49-
50-
5160
}
5261
}

0 commit comments

Comments
 (0)