use of io.vertx.core.Future in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketPingPongCheck.
private void testServerWebSocketPingPongCheck(int maxFrameSize, Function<ServerWebSocket, Future<Void>> check) {
Pattern pattern = Pattern.compile("^P[io]ng cannot exceed maxWebSocketFrameSize or 125 bytes$");
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
ws.pongHandler(buff -> fail());
check.apply(ws).onComplete(onFailure(err -> {
Matcher matcher = pattern.matcher(err.getMessage());
if (matcher.matches()) {
ws.close();
} else {
fail("Unexpected error message" + err.getMessage());
}
}));
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.closeHandler(v2 -> {
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.Future 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.Future in project vert.x by eclipse.
the class DatagramSocketImpl method listen.
private Future<DatagramSocket> listen(SocketAddress local) {
AddressResolver resolver = context.owner().addressResolver();
PromiseInternal<Void> promise = context.promise();
io.netty.util.concurrent.Future<InetSocketAddress> f1 = resolver.resolveHostname(context.nettyEventLoop(), local.host());
f1.addListener((GenericFutureListener<io.netty.util.concurrent.Future<InetSocketAddress>>) res1 -> {
if (res1.isSuccess()) {
ChannelFuture f2 = channel.bind(new InetSocketAddress(res1.getNow().getAddress(), local.port()));
if (metrics != null) {
f2.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Void>>) res2 -> {
if (res2.isSuccess()) {
metrics.listening(local.host(), localAddress());
}
});
}
f2.addListener(promise);
} else {
promise.fail(res1.cause());
}
});
return promise.future().map(this);
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class SharedDataImpl method getLocalAsyncMap.
@SuppressWarnings("unchecked")
@Override
public <K, V> Future<AsyncMap<K, V>> getLocalAsyncMap(String name) {
LocalAsyncMapImpl<K, V> asyncMap = (LocalAsyncMapImpl<K, V>) localAsyncMaps.computeIfAbsent(name, n -> new LocalAsyncMapImpl<>(vertx));
ContextInternal context = vertx.getOrCreateContext();
return context.succeededFuture(new WrappedAsyncMap<>(asyncMap));
}
use of io.vertx.core.Future in project vert.x by eclipse.
the class Http1xTest method testPipelinedPostRequestStartedByResponseSent.
@Test
public void testPipelinedPostRequestStartedByResponseSent() throws Exception {
String chunk1 = TestUtils.randomAlphaString(1024);
String chunk2 = TestUtils.randomAlphaString(1024);
Promise<Void> latch2 = Promise.promise();
AtomicInteger count = new AtomicInteger();
server.requestHandler(req -> {
switch(count.getAndIncrement()) {
case 0:
req.pause();
vertx.setTimer(500, id -> {
req.resume();
});
req.endHandler(v -> {
req.response().end();
});
break;
case 1:
latch2.complete();
req.bodyHandler(body -> {
assertEquals(chunk1 + chunk2, body.toString());
req.response().end();
});
break;
}
});
startServer(testAddress);
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true));
CountDownLatch latch1 = new CountDownLatch(1);
client.request(new RequestOptions(requestOptions).setMethod(PUT)).onComplete(onSuccess(req -> {
req.end(TestUtils.randomAlphaString(1024), onSuccess(v -> {
latch1.countDown();
}));
}));
awaitLatch(latch1);
client.request(new RequestOptions(requestOptions).setMethod(PUT)).onComplete(onSuccess(req -> {
req.response(resp -> {
testComplete();
}).setChunked(true);
req.write(chunk1);
latch2.future().onComplete(onSuccess(v -> {
req.end(chunk2);
}));
}));
await();
}
Aggregations