Skip to content

Commit ab377a3

Browse files
author
Alexander Furer
committed
random grpc port usage documentation
1 parent 9b02a10 commit ab377a3

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

README.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ dependencies {
2828
* Start by https://github.com/google/protobuf-gradle-plugin[generating] stub and server interface(s) from your `.proto` file(s).
2929
* Annotate your server interface implementation(s) with `@org.lognet.springboot.grpc.GRpcService`
3030
* Optionally configure the server port in your `application.yml/properties`. Default port is `6565`.
31-
* A random port can be defined by setting the port to `0`. The actual port being used can then be retrieved from the property `local.grpc.port`.
31+
* A random port can be defined by setting the port to `0`. The actual port being used can then be retrieved from the property `local.grpc.port`, or
32+
using `@LocalRunningGrpcPort` annotation which will inject the running port (explicitly configured or randomly selected)
3233
3334
[source,yaml]
3435
----

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class GrpcServerTestBase {
4343
@Before
4444
public final void setupChannels() {
4545
if(gRpcServerProperties.isEnabled()) {
46-
channel = onChannelBuild(ManagedChannelBuilder.forAddress("localhost", gRpcServerProperties.getPort())
46+
channel = onChannelBuild(ManagedChannelBuilder.forAddress("localhost",getPort() )
4747
.usePlaintext(true)
4848
).build();
4949
}
@@ -55,6 +55,9 @@ public final void setupChannels() {
5555

5656
}
5757
}
58+
protected int getPort(){
59+
return gRpcServerProperties.getPort();
60+
}
5861

5962
protected ManagedChannelBuilder<?> onChannelBuild(ManagedChannelBuilder<?> channelBuilder){
6063
return channelBuilder;
@@ -71,9 +74,9 @@ public final void shutdownChannels() {
7174
}
7275

7376
@Test
74-
public void simpleGreeting() throws ExecutionException, InterruptedException {
75-
77+
final public void simpleGreeting() throws ExecutionException, InterruptedException {
7678

79+
beforeGreeting();
7780
String name ="John";
7881
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(Optional.ofNullable(channel).orElse(inProcChannel));
7982
final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName(name).build();
@@ -83,6 +86,11 @@ public void simpleGreeting() throws ExecutionException, InterruptedException {
8386
afterGreeting();
8487

8588
}
89+
90+
protected void beforeGreeting() {
91+
92+
}
93+
8694
protected void afterGreeting(){
8795

8896
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.Assert;
4+
import org.junit.runner.RunWith;
5+
import org.lognet.springboot.grpc.context.LocalRunningGrpcPort;
6+
import org.lognet.springboot.grpc.demo.DemoApp;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.util.concurrent.ExecutionException;
12+
13+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(classes = {DemoApp.class},webEnvironment = NONE,
17+
properties = "grpc.port=0"
18+
)
19+
public class RandomGrpcPortTest extends GrpcServerTestBase {
20+
21+
@Value("${local.grpc.port}")
22+
int port;
23+
24+
@LocalRunningGrpcPort
25+
int runningPort;
26+
27+
@Override
28+
protected int getPort() {
29+
return port;
30+
}
31+
32+
@Override
33+
protected void beforeGreeting() {
34+
Assert.assertEquals(0,gRpcServerProperties.getPort());
35+
Assert.assertEquals(port,runningPort);
36+
}
37+
}

0 commit comments

Comments
 (0)