Skip to content

Commit 4e6c9f6

Browse files
committed
Merge remote-tracking branch 'vertx/master' into 3.5.0-SNAPSHOT
2 parents 5ff674f + 730e098 commit 4e6c9f6

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

grpc-examples/src/main/java/io/vertx/example/grpc/consumer/Client.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.vertx.example.grpc.Messages;
77
import io.vertx.example.util.Runner;
88
import io.vertx.grpc.VertxChannelBuilder;
9+
import java.nio.charset.Charset;
910

1011
/*
1112
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
@@ -30,12 +31,16 @@ public void start() throws Exception {
3031
ConsumerServiceGrpc.ConsumerServiceVertxStub stub = ConsumerServiceGrpc.newVertxStub(channel);
3132

3233
// Make a request
33-
Messages.StreamingOutputCallRequest request = Messages.StreamingOutputCallRequest.newBuilder().build();
34+
Messages.StreamingOutputCallRequest request = Messages
35+
.StreamingOutputCallRequest
36+
.newBuilder()
37+
.build();
3438

3539
// Call the remote service
3640
stub.streamingOutputCall(request, stream -> {
3741
stream.handler(response -> {
38-
System.out.println(response.getPayload().getType().getNumber());
42+
System.out
43+
.println(new String(response.getPayload().toByteArray(), Charset.forName("UTF-8")));
3944
}).endHandler(v -> {
4045
System.out.println("Response has ended.");
4146
});

grpc-examples/src/main/java/io/vertx/example/grpc/consumer/Server.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.vertx.example.grpc.consumer;
22

3+
import com.google.protobuf.ByteString;
34
import io.vertx.core.AbstractVerticle;
4-
import io.vertx.example.grpc.*;
5+
import io.vertx.example.grpc.ConsumerServiceGrpc;
6+
import io.vertx.example.grpc.Messages;
7+
import io.vertx.example.grpc.Messages.PayloadType;
58
import io.vertx.example.util.Runner;
69
import io.vertx.grpc.GrpcWriteStream;
710
import io.vertx.grpc.VertxServer;
811
import io.vertx.grpc.VertxServerBuilder;
9-
12+
import java.nio.charset.Charset;
1013
import java.util.concurrent.atomic.AtomicInteger;
1114

1215
/*
@@ -23,15 +26,24 @@ public static void main(String[] args) {
2326
public void start() throws Exception {
2427

2528
// The rcp service
26-
ConsumerServiceGrpc.ConsumerServiceVertxImplBase service = new ConsumerServiceGrpc.ConsumerServiceVertxImplBase() {
27-
@Override
28-
public void streamingOutputCall(Messages.StreamingOutputCallRequest request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response) {
29-
final AtomicInteger counter = new AtomicInteger();
30-
vertx.setPeriodic(1000L, t -> {
31-
response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(Messages.Payload.newBuilder().setTypeValue(counter.incrementAndGet())).build());
32-
});
33-
}
34-
};
29+
ConsumerServiceGrpc.ConsumerServiceVertxImplBase service =
30+
new ConsumerServiceGrpc.ConsumerServiceVertxImplBase() {
31+
@Override
32+
public void streamingOutputCall(
33+
Messages.StreamingOutputCallRequest request,
34+
GrpcWriteStream<Messages.StreamingOutputCallResponse> response
35+
) {
36+
final AtomicInteger counter = new AtomicInteger();
37+
vertx.setPeriodic(1000L, t -> {
38+
response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload(
39+
Messages.Payload.newBuilder()
40+
.setTypeValue(PayloadType.COMPRESSABLE.getNumber())
41+
.setBody(ByteString.copyFrom(
42+
String.valueOf(counter.incrementAndGet()), Charset.forName("UTF-8")))
43+
).build());
44+
});
45+
}
46+
};
3547

3648
// Create the server
3749
VertxServer rpcServer = VertxServerBuilder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.vertx.example.unit;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.core.Future;
5+
6+
public class HelloVerticle extends AbstractVerticle {
7+
8+
@Override
9+
public void start(Future<Void> startFuture) throws Exception {
10+
int port = config().getInteger("http.port", 8080);
11+
vertx
12+
.createHttpServer()
13+
.requestHandler(request -> request.response().end("Hello!"))
14+
.listen(port, result -> startFuture.complete());
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.vertx.example.unit.test;
2+
3+
import io.vertx.core.DeploymentOptions;
4+
import io.vertx.core.Vertx;
5+
import io.vertx.core.http.HttpClient;
6+
import io.vertx.core.json.JsonObject;
7+
import io.vertx.example.unit.HelloVerticle;
8+
import io.vertx.ext.unit.Async;
9+
import io.vertx.ext.unit.TestContext;
10+
import io.vertx.ext.unit.junit.VertxUnitRunner;
11+
12+
import java.io.IOException;
13+
import java.net.ServerSocket;
14+
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
20+
/*
21+
* Example of an asynchronous JUnit test for a Verticle.
22+
*/
23+
@RunWith(VertxUnitRunner.class)
24+
public class JUnitVerticleTest {
25+
26+
Vertx vertx;
27+
int port;
28+
29+
@Before
30+
public void before(TestContext context) throws IOException {
31+
ServerSocket socket = new ServerSocket(0);
32+
port = socket.getLocalPort();
33+
socket.close();
34+
35+
DeploymentOptions options = new DeploymentOptions()
36+
.setConfig(new JsonObject().put("http.port", port));
37+
38+
vertx = Vertx.vertx();
39+
vertx.deployVerticle(HelloVerticle.class.getName(), options, context.asyncAssertSuccess());
40+
}
41+
42+
@After
43+
public void after(TestContext context) {
44+
vertx.close(context.asyncAssertSuccess());
45+
}
46+
47+
@Test
48+
public void canGetHello(TestContext context) {
49+
Async async = context.async();
50+
HttpClient client = vertx.createHttpClient();
51+
client.getNow(port, "localhost", "/", response -> {
52+
response.bodyHandler(body -> {
53+
context.assertEquals("Hello!", body.toString());
54+
client.close();
55+
async.complete();
56+
});
57+
});
58+
}
59+
}

0 commit comments

Comments
 (0)