use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2Test method testNonUpgradedH2CConnectionIsEvictedFromThePool.
@Test
public void testNonUpgradedH2CConnectionIsEvictedFromThePool() throws Exception {
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2));
server.close();
System.setProperty("vertx.disableH2c", "true");
server = vertx.createHttpServer();
try {
Promise<Void> p = Promise.promise();
AtomicBoolean first = new AtomicBoolean(true);
server.requestHandler(req -> {
if (first.compareAndSet(true, false)) {
HttpConnection conn = req.connection();
p.future().onComplete(ar -> {
conn.close();
});
}
req.response().end();
});
startServer(testAddress);
client.request(requestOptions).compose(req1 -> {
req1.connection().closeHandler(v1 -> {
vertx.runOnContext(v2 -> {
client.request(requestOptions).compose(req2 -> req2.send().compose(HttpClientResponse::body)).onComplete(onSuccess(b2 -> {
testComplete();
}));
});
});
return req1.send().compose(resp -> {
assertEquals(HttpVersion.HTTP_1_1, resp.version());
return resp.body();
});
}).onComplete(onSuccess(b -> {
p.complete();
}));
await();
} finally {
System.clearProperty("vertx.disableH2c");
}
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class NetClientImpl method close.
@Override
public void close(Promise<Void> completion) {
ChannelGroupFuture fut = channelGroup.close();
if (metrics != null) {
PromiseInternal<Void> p = (PromiseInternal) Promise.promise();
fut.addListener(p);
p.future().<Void>compose(v -> {
metrics.close();
return Future.succeededFuture();
}).onComplete(completion);
} else {
fut.addListener((PromiseInternal) completion);
}
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class NetClientImpl method connectInternal.
public void connectInternal(ProxyOptions proxyOptions, SocketAddress remoteAddress, SocketAddress peerAddress, String serverName, boolean ssl, boolean useAlpn, boolean registerWriteHandlers, Promise<NetSocket> connectHandler, ContextInternal context, int remainingAttempts) {
checkClosed();
EventLoop eventLoop = context.nettyEventLoop();
if (eventLoop.inEventLoop()) {
Objects.requireNonNull(connectHandler, "No null connectHandler accepted");
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoop);
bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
vertx.transport().configure(options, remoteAddress.isDomainSocket(), bootstrap);
ChannelProvider channelProvider = new ChannelProvider(bootstrap, sslHelper, context).proxyOptions(proxyOptions);
channelProvider.handler(ch -> connected(context, ch, connectHandler, remoteAddress, channelProvider.applicationProtocol(), registerWriteHandlers));
io.netty.util.concurrent.Future<Channel> fut = channelProvider.connect(remoteAddress, peerAddress, serverName, ssl, useAlpn);
fut.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Channel>>) future -> {
if (!future.isSuccess()) {
Throwable cause = future.cause();
boolean connectError = cause instanceof ConnectException || cause instanceof FileNotFoundException;
if (connectError && (remainingAttempts > 0 || remainingAttempts == -1)) {
context.emit(v -> {
log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds");
vertx.setTimer(options.getReconnectInterval(), tid -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1));
});
} else {
failed(context, null, cause, connectHandler);
}
}
});
} else {
eventLoop.execute(() -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts));
}
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class WebSocketTest method testWorker.
@Test
public void testWorker() {
waitFor(2);
DeploymentOptions deploymentOptions = new DeploymentOptions().setWorker(true);
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start(Promise<Void> startPromise) {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.webSocketHandler(ws -> {
assertTrue(Context.isOnWorkerThread());
ws.handler(msg -> {
assertTrue(Context.isOnWorkerThread());
ws.write(Buffer.buffer("pong"));
});
ws.endHandler(v -> {
assertTrue(Context.isOnWorkerThread());
complete();
});
});
server.listen().<Void>mapEmpty().onComplete(startPromise);
}
}, deploymentOptions, onSuccess(serverID -> {
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start() {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
assertTrue(Context.isOnWorkerThread());
ws.write(Buffer.buffer("ping"));
ws.handler(buf -> {
assertTrue(Context.isOnWorkerThread());
ws.end();
});
ws.endHandler(v -> {
assertTrue(Context.isOnWorkerThread());
complete();
});
}));
}
}, deploymentOptions, onSuccess(id -> {
}));
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class ConnectionPoolTest method testRecycleRemovedConnection.
@Test
public void testRecycleRemovedConnection() throws Exception {
EventLoopContext context = vertx.createEventLoopContext();
ConnectionManager mgr = new ConnectionManager();
ConnectionPool<Connection> pool = ConnectionPool.pool(mgr, new int[] { 10 }, 10);
Connection expected1 = new Connection();
Promise<Lease<Connection>> promise = Promise.promise();
pool.acquire(context, 0, promise);
ConnectionRequest request1 = mgr.assertRequest();
request1.connect(expected1, 0);
CountDownLatch latch = new CountDownLatch(1);
promise.future().onComplete(onSuccess(lease -> {
request1.listener.onRemove();
lease.recycle();
latch.countDown();
}));
awaitLatch(latch);
Connection expected2 = new Connection();
pool.acquire(context, 0, onSuccess(lease -> {
assertSame(expected2, lease.get());
assertSame(context, Vertx.currentContext());
testComplete();
}));
ConnectionRequest request2 = mgr.assertRequest();
request2.connect(expected2, 0);
await();
}
Aggregations