Skip to content

Commit 1b20c87

Browse files
committed
Refactor request handling to improve Future chaining
1 parent e48df54 commit 1b20c87

2 files changed

Lines changed: 140 additions & 1 deletion

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2011-2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
package io.vertx.grpc.it;
12+
13+
import io.grpc.examples.helloworld.*;
14+
import io.grpc.testing.integration.*;
15+
import io.vertx.core.Future;
16+
import io.vertx.core.http.HttpServer;
17+
import io.vertx.core.net.SocketAddress;
18+
import io.vertx.ext.unit.Async;
19+
import io.vertx.ext.unit.TestContext;
20+
import io.vertx.grpc.client.GrpcClient;
21+
import io.vertx.grpc.client.GrpcClientOptions;
22+
import io.vertx.grpc.server.GrpcServer;
23+
import io.vertx.grpc.server.GrpcServerOptions;
24+
import io.vertx.grpc.server.Service;
25+
import org.junit.Test;
26+
27+
public class CompressionTest extends ProtocPluginTestBase {
28+
29+
@Test
30+
public void testIdentityCompression(TestContext should) throws Exception {
31+
testCompression(should, "identity");
32+
}
33+
34+
@Test
35+
public void testGzipCompression(TestContext should) throws Exception {
36+
testCompression(should, "gzip");
37+
}
38+
39+
@Test
40+
public void testSnappyCompression(TestContext should) throws Exception {
41+
testCompression(should, "snappy");
42+
}
43+
44+
@Test
45+
public void testInvalidCompression(TestContext should) throws Exception {
46+
// Create gRPC Server with invalid compression
47+
GrpcServer grpcServer = GrpcServer.server(vertx, new GrpcServerOptions());
48+
49+
grpcServer.addService(greeterService(new GreeterService() {
50+
@Override
51+
public Future<HelloReply> sayHello(HelloRequest request) {
52+
return Future.succeededFuture(HelloReply.newBuilder()
53+
.setMessage("Hello " + request.getName())
54+
.build());
55+
}
56+
}));
57+
58+
HttpServer httpServer = vertx.createHttpServer();
59+
httpServer.requestHandler(grpcServer).listen(8080).toCompletionStage().toCompletableFuture().get(20, java.util.concurrent.TimeUnit.SECONDS);
60+
61+
// Create gRPC Client with invalid compression
62+
GrpcClient grpcClient = GrpcClient.client(vertx, new GrpcClientOptions().setCompressionEncoding("invalid"));
63+
GreeterClient client = greeterClient(grpcClient, SocketAddress.inetSocketAddress(port, "localhost"));
64+
65+
Async test = should.async();
66+
client.sayHello(HelloRequest.newBuilder().setName("World").build())
67+
.onFailure(exception -> test.complete())
68+
.onSuccess(reply -> should.fail("Expected failure due to invalid compression"));
69+
70+
test.awaitSuccess(20_000);
71+
72+
// Close the server
73+
httpServer.close();
74+
}
75+
76+
private void testCompression(TestContext should, String compressionType) throws Exception {
77+
// Create gRPC Server with the specified compression
78+
GrpcServer grpcServer = GrpcServer.server(vertx);
79+
80+
grpcServer.addService(greeterService(new GreeterService() {
81+
@Override
82+
public Future<HelloReply> sayHello(HelloRequest request) {
83+
return Future.succeededFuture(HelloReply.newBuilder()
84+
.setMessage("Hello " + request.getName())
85+
.build());
86+
}
87+
}));
88+
89+
HttpServer httpServer = vertx.createHttpServer();
90+
httpServer.requestHandler(grpcServer).listen(8080).toCompletionStage().toCompletableFuture().get(20, java.util.concurrent.TimeUnit.SECONDS);
91+
92+
// Create gRPC Client with the specified compression
93+
GrpcClient grpcClient = GrpcClient.client(vertx, new GrpcClientOptions().setCompressionEncoding(compressionType));
94+
GreeterClient client = greeterClient(grpcClient, SocketAddress.inetSocketAddress(port, "localhost"));
95+
96+
Async test = should.async();
97+
client.sayHello(HelloRequest.newBuilder()
98+
.setName("World")
99+
.build())
100+
.onComplete(should.asyncAssertSuccess(reply -> {
101+
should.assertEquals("Hello World", reply.getMessage());
102+
test.complete();
103+
}));
104+
test.awaitSuccess(20_000);
105+
106+
// Close the server
107+
httpServer.close();
108+
}
109+
110+
@Override
111+
protected GrpcServer grpcServer() {
112+
return GrpcServer.server(vertx);
113+
}
114+
115+
@Override
116+
protected GrpcClient grpcClient() {
117+
return GrpcClient.client(vertx);
118+
}
119+
120+
@Override
121+
protected Service greeterService(GreeterService service) {
122+
return GreeterGrpcService.of(service);
123+
}
124+
125+
@Override
126+
protected GreeterClient greeterClient(GrpcClient grpcClient, SocketAddress socketAddress) {
127+
return GreeterGrpcClient.create(grpcClient, socketAddress);
128+
}
129+
130+
@Override
131+
protected Service testService(TestServiceService service) {
132+
return TestServiceGrpcService.of(service);
133+
}
134+
135+
@Override
136+
protected TestServiceClient testClient(GrpcClient client, SocketAddress socketAddress) {
137+
return TestServiceGrpcClient.create(client, socketAddress);
138+
}
139+
}

vertx-grpc-it/src/test/java/io/vertx/grpc/it/ProtocPluginTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected GrpcServer grpcServer() {
2727

2828
@Override
2929
protected GrpcClient grpcClient() {
30-
return GrpcClient.client(vertx, new GrpcClientOptions().setCompressionEncoding("snappy"));
30+
return GrpcClient.client(vertx, new GrpcClientOptions());
3131
}
3232

3333
@Override

0 commit comments

Comments
 (0)