|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 | import static org.assertj.core.api.Assertions.assertThatCode; |
| 5 | +import static org.springframework.test.util.TestSocketUtils.findAvailableTcpPort; |
5 | 6 |
|
6 | 7 | import io.grpc.StatusRuntimeException; |
7 | 8 | import io.grpc.testing.protobuf.SimpleRequest; |
8 | 9 | import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub; |
9 | 10 | import org.junit.jupiter.api.Disabled; |
10 | 11 | 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; |
13 | 13 | 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; |
29 | 14 |
|
30 | | - static SimpleRequest request = |
31 | | - SimpleRequest.newBuilder().setRequestMessage("Hello").build(); |
| 15 | +class RefreshAppTest { |
32 | 16 |
|
33 | 17 | @Test |
34 | 18 | 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 | + } |
47 | 46 | } |
48 | 47 |
|
49 | 48 | @Test |
50 | 49 | @Disabled("Can't gracefully exit in this test case") |
51 | 50 | 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 | + } |
64 | 78 | } |
65 | 79 |
|
66 | 80 | @Test |
67 | 81 | 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 | + } |
84 | 108 | } |
85 | 109 | } |
0 commit comments