Skip to content

Commit bd2531b

Browse files
committed
Refactor RefreshAppTest to use dynamic port allocation and improve readability
1 parent bd5e14f commit bd2531b

File tree

1 file changed

+83
-59
lines changed

1 file changed

+83
-59
lines changed

examples/refresh/src/test/java/grpcstarter/example/RefreshAppTest.java

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,108 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatCode;
5+
import static org.springframework.test.util.TestSocketUtils.findAvailableTcpPort;
56

67
import io.grpc.StatusRuntimeException;
78
import io.grpc.testing.protobuf.SimpleRequest;
89
import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub;
910
import org.junit.jupiter.api.Disabled;
1011
import org.junit.jupiter.api.Test;
11-
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.builder.SpringApplicationBuilder;
1313
import org.springframework.cloud.endpoint.event.RefreshEvent;
14-
import org.springframework.context.ApplicationContext;
15-
16-
@SpringBootTest(
17-
properties = {
18-
"grpc.test.enabled=false",
19-
"grpc.server.port=52330",
20-
"grpc.client.authority=localhost:${grpc.server.port}",
21-
})
22-
class RefreshAppTest {
23-
24-
@Autowired
25-
SimpleServiceBlockingStub simpleStub;
26-
27-
@Autowired
28-
ApplicationContext ctx;
2914

30-
static SimpleRequest request =
31-
SimpleRequest.newBuilder().setRequestMessage("Hello").build();
15+
class RefreshAppTest {
3216

3317
@Test
3418
void testDeadline() {
35-
normalRequest();
36-
37-
// change deadline to 500ms
38-
System.setProperty("grpc.client.deadline", "500");
39-
40-
ctx.publishEvent(new RefreshEvent(ctx, null, null));
41-
42-
assertThatCode(() -> simpleStub.unaryRpc(request))
43-
.isInstanceOf(StatusRuntimeException.class)
44-
.hasMessageContaining("DEADLINE_EXCEEDED: CallOptions deadline exceeded after");
45-
46-
System.clearProperty("grpc.client.deadline");
19+
var port = findAvailableTcpPort();
20+
try (var ctx = new SpringApplicationBuilder(RefreshApp.class)
21+
.properties("grpc.server.port= " + port)
22+
.properties("grpc.client.authority=localhost:" + port)
23+
.run()) {
24+
25+
var simpleStub = ctx.getBean(SimpleServiceBlockingStub.class);
26+
var responseMessage = simpleStub
27+
.unaryRpc(SimpleRequest.newBuilder()
28+
.setRequestMessage("Hello")
29+
.build())
30+
.getResponseMessage();
31+
32+
assertThat(responseMessage).isEqualTo("Hello");
33+
34+
// change deadline to 500ms
35+
System.setProperty("grpc.client.deadline", "500");
36+
ctx.publishEvent(new RefreshEvent(ctx, null, null));
37+
38+
assertThatCode(() -> simpleStub.unaryRpc(SimpleRequest.newBuilder()
39+
.setRequestMessage("Hello")
40+
.build()))
41+
.isInstanceOf(StatusRuntimeException.class)
42+
.hasMessageContaining("DEADLINE_EXCEEDED: CallOptions deadline exceeded after");
43+
} finally {
44+
System.clearProperty("grpc.client.deadline");
45+
}
4746
}
4847

4948
@Test
5049
@Disabled("Can't gracefully exit in this test case")
5150
void testMaxOutboundMessageSize() {
52-
normalRequest();
53-
54-
// change max outbound message size to 1B
55-
System.setProperty("grpc.client.max-outbound-message-size", "3B");
56-
57-
ctx.publishEvent(new RefreshEvent(ctx, null, null));
58-
59-
assertThatCode(() -> simpleStub.unaryRpc(request))
60-
.isInstanceOf(StatusRuntimeException.class)
61-
.hasMessageContaining("INTERNAL: Failed to frame message");
62-
63-
System.clearProperty("grpc.client.max-outbound-message-size");
51+
var port = findAvailableTcpPort();
52+
try (var ctx = new SpringApplicationBuilder(RefreshApp.class)
53+
.properties("grpc.server.port= " + port)
54+
.properties("grpc.client.authority=localhost:" + port)
55+
.run()) {
56+
57+
var simpleStub = ctx.getBean(SimpleServiceBlockingStub.class);
58+
var responseMessage = simpleStub
59+
.unaryRpc(SimpleRequest.newBuilder()
60+
.setRequestMessage("Hello")
61+
.build())
62+
.getResponseMessage();
63+
64+
assertThat(responseMessage).isEqualTo("Hello");
65+
66+
// change max outbound message size to 1B
67+
System.setProperty("grpc.client.max-outbound-message-size", "1B");
68+
ctx.publishEvent(new RefreshEvent(ctx, null, null));
69+
70+
assertThatCode(() -> simpleStub.unaryRpc(SimpleRequest.newBuilder()
71+
.setRequestMessage("Hello")
72+
.build()))
73+
.isInstanceOf(StatusRuntimeException.class)
74+
.hasMessageContaining("RESOURCE_EXHAUSTED: message too large");
75+
} finally {
76+
System.clearProperty("grpc.client.max-outbound-message-size");
77+
}
6478
}
6579

6680
@Test
6781
void testCompression() {
68-
normalRequest();
69-
70-
// change compression to identity
71-
System.setProperty("grpc.client.compression", "identity");
72-
73-
ctx.publishEvent(new RefreshEvent(ctx, null, null));
74-
75-
assertThatCode(() -> simpleStub.unaryRpc(request)).doesNotThrowAnyException();
76-
77-
System.clearProperty("grpc.client.compression");
78-
}
79-
80-
private void normalRequest() {
81-
String responseMessage = simpleStub.unaryRpc(request).getResponseMessage();
82-
83-
assertThat(responseMessage).isEqualTo("Hello");
82+
var port = findAvailableTcpPort();
83+
try (var ctx = new SpringApplicationBuilder(RefreshApp.class)
84+
.properties("grpc.server.port= " + port)
85+
.properties("grpc.client.authority=localhost:" + port)
86+
.run()) {
87+
88+
var simpleStub = ctx.getBean(SimpleServiceBlockingStub.class);
89+
var responseMessage = simpleStub
90+
.unaryRpc(SimpleRequest.newBuilder()
91+
.setRequestMessage("Hello")
92+
.build())
93+
.getResponseMessage();
94+
95+
assertThat(responseMessage).isEqualTo("Hello");
96+
97+
// change compression to gzip
98+
System.setProperty("grpc.client.compression", "gzip");
99+
ctx.publishEvent(new RefreshEvent(ctx, null, null));
100+
101+
assertThatCode(() -> simpleStub.unaryRpc(SimpleRequest.newBuilder()
102+
.setRequestMessage("Hello")
103+
.build()))
104+
.doesNotThrowAnyException();
105+
} finally {
106+
System.clearProperty("grpc.client.max-outbound-message-size");
107+
}
84108
}
85109
}

0 commit comments

Comments
 (0)