use of io.vertx.core.Promise in project vert.x by eclipse.
the class AsyncMapTest method loadData.
protected void loadData(Map<JsonObject, Buffer> map, BiConsumer<Vertx, AsyncMap<JsonObject, Buffer>> test) {
List<Future> futures = new ArrayList<>(map.size());
map.forEach((key, value) -> {
Promise future = Promise.promise();
getVertx().sharedData().getAsyncMap("foo", onSuccess(asyncMap -> {
asyncMap.put(key, value, future);
}));
futures.add(future.future());
});
CompositeFuture.all(futures).onComplete(onSuccess(cf -> {
Vertx v = getVertx();
v.sharedData().<JsonObject, Buffer>getAsyncMap("foo", onSuccess(asyncMap -> {
test.accept(v, asyncMap);
}));
}));
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ClientTest method testClientSettings.
@Test
public void testClientSettings() throws Exception {
waitFor(2);
io.vertx.core.http.Http2Settings initialSettings = TestUtils.randomHttp2Settings();
io.vertx.core.http.Http2Settings updatedSettings = TestUtils.randomHttp2Settings();
// Otherwise it raise "invalid max dynamic table size" in Netty
updatedSettings.setHeaderTableSize(initialSettings.getHeaderTableSize());
AtomicInteger count = new AtomicInteger();
Promise<Void> end = Promise.promise();
server.requestHandler(req -> {
end.future().onComplete(v -> {
req.response().end();
});
}).connectionHandler(conn -> {
io.vertx.core.http.Http2Settings initialRemoteSettings = conn.remoteSettings();
assertEquals(initialSettings.isPushEnabled(), initialRemoteSettings.isPushEnabled());
assertEquals(initialSettings.getMaxHeaderListSize(), initialRemoteSettings.getMaxHeaderListSize());
assertEquals(initialSettings.getMaxFrameSize(), initialRemoteSettings.getMaxFrameSize());
assertEquals(initialSettings.getInitialWindowSize(), initialRemoteSettings.getInitialWindowSize());
// assertEquals(Math.min(initialSettings.getMaxConcurrentStreams(), Integer.MAX_VALUE), settings.getMaxConcurrentStreams());
assertEquals(initialSettings.getHeaderTableSize(), initialRemoteSettings.getHeaderTableSize());
assertEquals(initialSettings.get('\u0007'), initialRemoteSettings.get(7));
Context ctx = Vertx.currentContext();
conn.remoteSettingsHandler(settings -> {
assertOnIOContext(ctx);
switch(count.getAndIncrement()) {
case 0:
// find out why it fails sometimes ...
// assertEquals(updatedSettings.pushEnabled(), settings.getEnablePush());
assertEquals(updatedSettings.getMaxHeaderListSize(), settings.getMaxHeaderListSize());
assertEquals(updatedSettings.getMaxFrameSize(), settings.getMaxFrameSize());
assertEquals(updatedSettings.getInitialWindowSize(), settings.getInitialWindowSize());
// find out why it fails sometimes ...
// assertEquals(Math.min(updatedSettings.maxConcurrentStreams(), Integer.MAX_VALUE), settings.getMaxConcurrentStreams());
assertEquals(updatedSettings.getHeaderTableSize(), settings.getHeaderTableSize());
assertEquals(updatedSettings.get('\u0007'), settings.get(7));
complete();
break;
default:
fail();
}
});
});
startServer();
client.close();
client = vertx.createHttpClient(clientOptions.setInitialSettings(initialSettings));
client.connectionHandler(conn -> {
vertx.runOnContext(v -> {
conn.updateSettings(updatedSettings, ar -> {
end.complete();
});
});
});
client.request(requestOptions).onComplete(onSuccess(req -> {
req.send(onSuccess(resp -> complete()));
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ClientTest method testClientResetServerStreamDuringRequest.
@Test
public void testClientResetServerStreamDuringRequest() throws Exception {
waitFor(2);
Promise<Void> bufReceived = Promise.promise();
server.requestHandler(req -> {
req.handler(buf -> {
bufReceived.complete();
});
AtomicBoolean errored = new AtomicBoolean();
req.exceptionHandler(err -> {
if (errored.compareAndSet(false, true)) {
assertEquals(err.getClass(), StreamResetException.class);
}
});
AtomicLong reset = new AtomicLong();
req.response().exceptionHandler(err -> {
if (err instanceof StreamResetException) {
reset.set(((StreamResetException) err).getCode());
}
});
req.response().closeHandler(v -> {
assertEquals(10L, reset.get());
complete();
});
});
startServer(testAddress);
client.request(requestOptions).onComplete(onSuccess(req -> {
req.response(onFailure(err -> {
complete();
})).setChunked(true);
req.write(Buffer.buffer("hello"));
bufReceived.future().onComplete(ar -> {
req.reset(10);
});
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ClientTest method testClientResponsePauseResume.
@Test
public void testClientResponsePauseResume() throws Exception {
String content = TestUtils.randomAlphaString(1024);
Buffer expected = Buffer.buffer();
Promise<Void> whenFull = Promise.promise();
AtomicBoolean drain = new AtomicBoolean();
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.putHeader("content-type", "text/plain");
resp.setChunked(true);
vertx.setPeriodic(1, timerID -> {
if (resp.writeQueueFull()) {
resp.drainHandler(v -> {
Buffer last = Buffer.buffer("last");
expected.appendBuffer(last);
resp.end(last);
assertEquals(expected.toString().getBytes().length, resp.bytesWritten());
});
vertx.cancelTimer(timerID);
drain.set(true);
whenFull.complete();
} else {
Buffer chunk = Buffer.buffer(content);
expected.appendBuffer(chunk);
resp.write(chunk);
}
});
});
startServer();
client.request(requestOptions).onComplete(onSuccess(req -> {
req.send(onSuccess(resp -> {
Context ctx = vertx.getOrCreateContext();
Buffer received = Buffer.buffer();
resp.pause();
resp.handler(buff -> {
if (whenFull.future().isComplete()) {
assertSame(ctx, Vertx.currentContext());
} else {
assertOnIOContext(ctx);
}
received.appendBuffer(buff);
});
resp.endHandler(v -> {
assertEquals(expected.toString().length(), received.toString().length());
testComplete();
});
whenFull.future().onComplete(v -> {
resp.resume();
});
}));
}));
await();
}
use of io.vertx.core.Promise in project vert.x by eclipse.
the class Http2ClientTest method testClientStreamPriorityNoChange.
@Ignore("Cannot pass reliably for now (https://github.com/netty/netty/issues/9842)")
@Test
public void testClientStreamPriorityNoChange() throws Exception {
StreamPriority streamPriority = new StreamPriority().setDependency(123).setWeight((short) 45).setExclusive(true);
waitFor(2);
Promise<Void> latch = Promise.promise();
ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> new Http2EventAdapter() {
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(streamPriority.getDependency(), streamDependency);
assertEquals(streamPriority.getWeight(), weight);
assertEquals(streamPriority.isExclusive(), exclusive);
assertFalse(endStream);
latch.complete();
});
encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise());
ctx.flush();
}
@Override
public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception {
fail("Priority frame should not be sent");
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
if (endOfStream) {
vertx.runOnContext(v -> {
complete();
});
}
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
try {
client.request(new RequestOptions().setHost(DEFAULT_HTTPS_HOST).setPort(DEFAULT_HTTPS_PORT).setURI("/somepath")).onComplete(onSuccess(req -> {
req.response(onSuccess(resp -> {
resp.endHandler(v -> {
complete();
});
})).setStreamPriority(streamPriority);
req.sendHead();
latch.future().onComplete(onSuccess(v -> {
req.setStreamPriority(streamPriority);
req.end();
}));
}));
await();
} finally {
s.channel().close().sync();
}
}
Aggregations