use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ConnectionBase method close.
@Override
public Future<Void> close() {
PromiseInternal<Void> promise = context.promise();
ChannelPromise pr = chctx.newPromise();
ChannelPromise channelPromise = pr.addListener(promise);
handlerContext.writeAndFlush(Unpooled.EMPTY_BUFFER, pr);
channelPromise.addListener((ChannelFutureListener) future -> shutdown(0L));
return promise.future();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class FaultToleranceVerticle method start.
@Override
public void start() throws Exception {
JsonObject config = config();
id = config.getInteger("id");
numAddresses = config.getInteger("addressesCount");
List<Future> registrationFutures = new ArrayList<>(numAddresses);
for (int i = 0; i < numAddresses; i++) {
Promise<Void> registrationFuture = Promise.promise();
registrationFutures.add(registrationFuture.future());
vertx.eventBus().consumer(createAddress(id, i), msg -> msg.reply("pong")).completionHandler(registrationFuture);
}
Promise<Void> registrationFuture = Promise.promise();
registrationFutures.add(registrationFuture.future());
vertx.eventBus().consumer("ping", this::ping).completionHandler(registrationFuture);
CompositeFuture.all(registrationFutures).onSuccess(ar -> vertx.eventBus().send("control", "start"));
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ServerTest method testUpgradeToClearText.
private void testUpgradeToClearText(HttpMethod method, Buffer expected, Handler<HttpServerOptions> optionsConfig) throws Exception {
server.close();
AtomicInteger serverConnectionCount = new AtomicInteger();
optionsConfig.handle(serverOptions);
server = vertx.createHttpServer(serverOptions.setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false).setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(20000))).connectionHandler(conn -> serverConnectionCount.incrementAndGet());
server.requestHandler(req -> {
assertEquals("http", req.scheme());
assertEquals(method, req.method());
assertEquals(HttpVersion.HTTP_2, req.version());
assertEquals(10000, req.connection().remoteSettings().getMaxConcurrentStreams());
assertFalse(req.isSSL());
req.bodyHandler(body -> {
assertEquals(expected, body);
vertx.setTimer(10, id -> {
req.response().end();
});
});
}).connectionHandler(conn -> {
assertNotNull(conn);
serverConnectionCount.incrementAndGet();
});
startServer(testAddress);
AtomicInteger clientConnectionCount = new AtomicInteger();
client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false).setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(10000)));
Promise<HttpClientResponse> p1 = Promise.promise();
p1.future().onComplete(onSuccess(resp -> {
assertEquals(HttpVersion.HTTP_2, resp.version());
// assertEquals(20000, req.connection().remoteSettings().getMaxConcurrentStreams());
assertEquals(1, serverConnectionCount.get());
assertEquals(1, clientConnectionCount.get());
Promise<HttpClientResponse> p2 = Promise.promise();
p2.future().onComplete(onSuccess(resp2 -> {
testComplete();
}));
doRequest(method, expected, null, p2);
}));
doRequest(method, expected, conn -> clientConnectionCount.incrementAndGet(), p1);
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class WebSocketTest method testAsyncAccept.
@Test
public void testAsyncAccept() {
AtomicBoolean resolved = new AtomicBoolean();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
Promise<Integer> promise = Promise.promise();
ws.setHandshake(promise.future());
try {
ws.accept();
fail();
} catch (IllegalStateException ignore) {
// Expected
}
try {
ws.writeTextMessage("hello");
fail();
} catch (IllegalStateException ignore) {
// Expected
}
vertx.setTimer(500, id -> {
resolved.set(true);
promise.complete(101);
});
});
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", onSuccess(ws -> {
assertTrue(resolved.get());
testComplete();
}));
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2Test method testClientStreamPausedWhenConnectionIsPaused.
@Test
public void testClientStreamPausedWhenConnectionIsPaused() throws Exception {
waitFor(2);
Buffer buffer = TestUtils.randomBuffer(512);
CompletableFuture<Void> resumeLatch = new CompletableFuture<>();
server.requestHandler(req -> {
switch(req.path()) {
case "/0":
{
req.pause();
resumeLatch.thenAccept(v -> {
req.resume();
});
req.endHandler(v -> {
req.response().end();
});
break;
}
case "/1":
{
req.bodyHandler(v -> {
assertEquals(v, buffer);
req.response().end();
});
break;
}
}
});
startServer(testAddress);
Promise<Void> l = Promise.promise();
client.request(new RequestOptions(requestOptions).setURI("/0")).onComplete(onSuccess(req1 -> {
req1.response(resp -> {
complete();
}).setChunked(true).sendHead(onSuccess(v -> {
vertx.setPeriodic(1, id -> {
req1.write(Buffer.buffer(TestUtils.randomAlphaString(512)));
if (req1.writeQueueFull()) {
req1.writeQueueFull();
vertx.cancelTimer(id);
l.complete();
}
});
}));
l.future().onComplete(onSuccess(v -> {
client.request(new RequestOptions(requestOptions).setURI("/1")).onComplete(onSuccess(req2 -> {
req2.response(onSuccess(resp -> {
complete();
})).setChunked(true);
assertFalse(req2.writeQueueFull());
req2.sendHead();
waitUntil(req2::writeQueueFull);
resumeLatch.complete(null);
waitUntil(() -> !req2.writeQueueFull(), () -> {
req1.end();
req2.end(buffer);
});
}));
}));
}));
await();
}
Aggregations