use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2Test method testConnectionCloseEvictsConnectionFromThePoolBeforeStreamsAreClosed.
/**
* Test that socket close (without an HTTP/2 go away frame) removes the connection from the pool
* before the streams are notified. Otherwise a notified stream might reuse a stale connection from
* the pool.
*/
@Test
public void testConnectionCloseEvictsConnectionFromThePoolBeforeStreamsAreClosed() throws Exception {
Set<HttpConnection> serverConnections = new HashSet<>();
server.requestHandler(req -> {
serverConnections.add(req.connection());
switch(req.path()) {
case "/1":
req.response().end();
break;
case "/2":
assertEquals(1, serverConnections.size());
// Socket close without HTTP/2 go away
Channel ch = ((ConnectionBase) req.connection()).channel();
ChannelPromise promise = ch.newPromise();
ch.unsafe().close(promise);
break;
case "/3":
assertEquals(2, serverConnections.size());
req.response().end();
break;
}
});
startServer(testAddress);
Future<Buffer> f1 = client.request(new RequestOptions(requestOptions).setURI("/1")).compose(req -> req.send().compose(HttpClientResponse::body));
f1.onComplete(onSuccess(v -> {
Future<Buffer> f2 = client.request(new RequestOptions(requestOptions).setURI("/2")).compose(req -> {
System.out.println(req.connection());
return req.send().compose(HttpClientResponse::body);
});
f2.onComplete(onFailure(v2 -> {
Future<Buffer> f3 = client.request(new RequestOptions(requestOptions).setURI("/3")).compose(req -> {
System.out.println(req.connection());
return req.send().compose(HttpClientResponse::body);
});
f3.onComplete(onSuccess(vvv -> {
testComplete();
}));
}));
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class ClusteredEventBus method start.
@Override
public void start(Promise<Void> promise) {
NetServerOptions serverOptions = getServerOptions();
server = vertx.createNetServer(serverOptions);
server.connectHandler(getServerHandler());
int port = getClusterPort();
String host = getClusterHost();
server.listen(port, host).flatMap(v -> {
int publicPort = getClusterPublicPort(server.actualPort());
String publicHost = getClusterPublicHost(host);
nodeInfo = new NodeInfo(publicHost, publicPort, options.getClusterNodeMetadata());
nodeId = clusterManager.getNodeId();
Promise<Void> setPromise = Promise.promise();
clusterManager.setNodeInfo(nodeInfo, setPromise);
return setPromise.future();
}).onSuccess(v -> {
started = true;
nodeSelector.eventBusStarted();
}).onComplete(promise);
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayNoError.
@Test
public void testClientSendGoAwayNoError() throws Exception {
Promise<Void> abc = Promise.promise();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger numShutdown = new AtomicInteger();
AtomicBoolean completed = new AtomicBoolean();
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
numShutdown.getAndIncrement();
vertx.setTimer(100, timerID -> {
// Delay so we can check the connection is not closed
completed.set(true);
testComplete();
});
});
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, numShutdown.get());
req.response().end();
});
conn.closeHandler(v -> {
assertTrue(completed.get());
});
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, 0, 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 Http2ClientTest method testServerResetClientStreamDuringResponse.
@Test
public void testServerResetClientStreamDuringResponse() throws Exception {
waitFor(2);
String chunk = TestUtils.randomAlphaString(1024);
Promise<Void> doReset = Promise.promise();
server.requestHandler(req -> {
doReset.future().onComplete(onSuccess(v -> {
req.response().reset(8);
}));
req.response().setChunked(true).write(Buffer.buffer(chunk));
});
startServer(testAddress);
Context ctx = vertx.getOrCreateContext();
Handler<Throwable> resetHandler = err -> {
assertOnIOContext(ctx);
if (err instanceof StreamResetException) {
StreamResetException reset = (StreamResetException) err;
assertEquals(8, reset.getCode());
complete();
}
};
client.close();
ctx.runOnContext(v -> {
client = vertx.createHttpClient(createBaseClientOptions());
client.request(new RequestOptions(requestOptions).setMethod(HttpMethod.POST)).onComplete(onSuccess(req -> {
req.response(onSuccess(resp -> {
resp.exceptionHandler(resetHandler);
resp.handler(buff -> {
doReset.complete();
});
})).exceptionHandler(resetHandler).setChunked(true).write(chunk);
}));
});
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class DatagramTest method testWorker.
@Test
public void testWorker() {
waitFor(2);
Buffer expected = TestUtils.randomBuffer(128);
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start(Promise<Void> startPromise) {
peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
peer2.exceptionHandler(t -> fail(t.getMessage()));
peer2.handler(packet -> {
assertTrue(Context.isOnWorkerThread());
assertSame(context, Vertx.currentContext());
complete();
});
peer2.listen(1234, "127.0.0.1").<Void>mapEmpty().onComplete(startPromise);
}
}, new DeploymentOptions().setWorker(true), onSuccess(id -> {
peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
peer1.send(expected, 1234, "127.0.0.1", onSuccess(s -> {
complete();
}));
}));
await();
}
Aggregations