use of io.vertx.core.Promise in project vert.x by eclipse.
the class CustomNodeSelectorTest method test.
@Test
public void test() throws Exception {
CompositeFuture startFuture = IntStream.range(0, 4).mapToObj(i -> {
VertxOptions vertxOptions = getOptions();
vertxOptions.getEventBusOptions().setClusterNodeMetadata(new JsonObject().put("rack", i % 2 == 0 ? "foo" : "bar"));
return vertxOptions;
}).map(options -> {
VertxBuilder factory = new VertxBuilder(options).init().clusterNodeSelector(new CustomNodeSelector());
Promise promise = Promise.promise();
factory.clusteredVertx(promise);
return promise.future();
}).collect(collectingAndThen(toList(), CompositeFuture::all));
CountDownLatch startLatch = new CountDownLatch(1);
startFuture.onComplete(onSuccess(cf -> startLatch.countDown()));
awaitLatch(startLatch);
vertices = startFuture.list();
ConcurrentMap<Integer, Set<String>> received = new ConcurrentHashMap<>();
CountDownLatch latch = new CountDownLatch(8);
CompositeFuture cf = IntStream.range(0, 4).mapToObj(i -> vertices.get(i).eventBus().<String>consumer("test", msg -> {
received.merge(i, Collections.singleton(msg.body()), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
latch.countDown();
})).map(consumer -> {
Promise promise = Promise.promise();
consumer.completionHandler(promise);
return promise.future();
}).collect(collectingAndThen(toList(), CompositeFuture::all));
Map<Integer, Set<String>> expected = new HashMap<>();
cf.onComplete(onSuccess(v -> {
for (int i = 0; i < 4; i++) {
String s = String.valueOf((char) ('a' + i));
vertices.get(i).eventBus().publish("test", s);
expected.merge(i, Collections.singleton(s), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
expected.merge((i + 2) % 4, Collections.singleton(s), (s1, s2) -> Stream.concat(s1.stream(), s2.stream()).collect(toSet()));
}
}));
awaitLatch(latch);
assertEquals(expected, received);
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class ClusteredEventBusTest method testSubsRemovedForKilledNode.
@Test
public void testSubsRemovedForKilledNode() throws Exception {
testSubsRemoved(latch -> {
VertxInternal vi = (VertxInternal) vertices[1];
Promise<Void> promise = vi.getOrCreateContext().promise();
vi.getClusterManager().leave(promise);
promise.future().onComplete(onSuccess(v -> {
latch.countDown();
}));
});
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayInternalError.
@Test
public void testClientSendGoAwayInternalError() throws Exception {
// On windows the client will close the channel immediately (since it's an error)
// and the server might see the channel inactive without receiving the close frame before
Assume.assumeFalse(Utils.isWindows());
Promise<Void> abc = Promise.promise();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger status = new AtomicInteger();
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, status.getAndIncrement());
req.response().end();
});
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
assertEquals(1, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(2, status.getAndIncrement());
testComplete();
});
abc.complete();
};
server.requestHandler(requestHandler);
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
abc.future().onComplete(ar -> {
encoder.writeGoAway(request.context, id, 3, Unpooled.EMPTY_BUFFER, request.context.newPromise());
request.context.flush();
});
});
fut.sync();
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ServerTest method testStreamWritability.
private void testStreamWritability(Function<HttpServerRequest, Future<WriteStream<Buffer>>> streamProvider) throws Exception {
Context ctx = vertx.getOrCreateContext();
String content = TestUtils.randomAlphaString(1024);
StringBuilder expected = new StringBuilder();
Promise<Void> whenFull = Promise.promise();
AtomicBoolean drain = new AtomicBoolean();
server.requestHandler(req -> {
Future<WriteStream<Buffer>> fut = streamProvider.apply(req);
fut.onComplete(onSuccess(stream -> {
vertx.setPeriodic(1, timerID -> {
if (stream.writeQueueFull()) {
stream.drainHandler(v -> {
assertOnIOContext(ctx);
expected.append("last");
stream.end(Buffer.buffer("last"));
});
vertx.cancelTimer(timerID);
drain.set(true);
whenFull.complete();
} else {
expected.append(content);
Buffer buf = Buffer.buffer(content);
stream.write(buf);
}
});
}));
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
AtomicInteger toAck = new AtomicInteger();
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.decoder.frameListener(new Http2FrameAdapter() {
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.append(data.toString(StandardCharsets.UTF_8));
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(expected.toString(), received.toString());
testComplete();
});
return delta;
} else {
if (drain.get()) {
return delta;
} else {
toAck.getAndAdd(delta);
return 0;
}
}
}
});
whenFull.future().onComplete(ar -> {
request.context.executor().execute(() -> {
try {
request.decoder.flowController().consumeBytes(request.connection.stream(id), toAck.intValue());
request.context.flush();
} catch (Http2Exception e) {
e.printStackTrace();
fail(e);
}
});
});
});
fut.sync();
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ServerTest method testPromiseStreamError.
@Test
public void testPromiseStreamError() throws Exception {
Context ctx = vertx.getOrCreateContext();
waitFor(2);
Promise<Void> when = Promise.promise();
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/wibble", ar -> {
assertTrue(ar.succeeded());
assertOnIOContext(ctx);
when.complete();
HttpServerResponse resp = ar.result();
AtomicInteger erros = new AtomicInteger();
resp.exceptionHandler(err -> {
assertOnIOContext(ctx);
erros.incrementAndGet();
});
resp.closeHandler(v -> {
assertOnIOContext(ctx);
assertTrue("Was expecting errors to be > 0", erros.get() > 0);
complete();
});
resp.endHandler(v -> {
assertOnIOContext(ctx);
complete();
});
// Transition to half-closed remote
resp.setChunked(true).write("whatever");
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
when.future().onComplete(ar -> {
Http2ConnectionEncoder encoder = request.encoder;
encoder.frameWriter().writeHeaders(request.context, promisedStreamId, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
}
});
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
Aggregations