use of io.vertx.core.streams.WriteStream in project vert.x by eclipse.
the class Http2ServerTest method testStreamWritability.
private void testStreamWritability(Function<HttpServerRequest, WriteStream<Buffer>> streamProvider) throws Exception {
Context ctx = vertx.getOrCreateContext();
String content = TestUtils.randomAlphaString(1024);
StringBuilder expected = new StringBuilder();
Future<Void> whenFull = Future.future();
AtomicBoolean drain = new AtomicBoolean();
server.requestHandler(req -> {
WriteStream<Buffer> stream = streamProvider.apply(req);
vertx.setPeriodic(1, timerID -> {
if (stream.writeQueueFull()) {
stream.drainHandler(v -> {
assertOnIOContext(ctx);
expected.append("last");
stream.end(Buffer.buffer("last"));
});
vertx.cancelTimer(timerID);
drain.set(true);
whenFull.complete();
} else {
expected.append(content);
Buffer buf = Buffer.buffer(content);
stream.write(buf);
}
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
AtomicInteger toAck = new AtomicInteger();
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.decoder.frameListener(new Http2FrameAdapter() {
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.append(data.toString(StandardCharsets.UTF_8));
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(expected.toString(), received.toString());
testComplete();
});
return delta;
} else {
if (drain.get()) {
return delta;
} else {
toAck.getAndAdd(delta);
return 0;
}
}
}
});
whenFull.setHandler(ar -> {
request.context.executor().execute(() -> {
try {
request.decoder.flowController().consumeBytes(request.connection.stream(id), toAck.intValue());
request.context.flush();
} catch (Http2Exception e) {
e.printStackTrace();
fail(e);
}
});
});
});
fut.sync();
await();
}
use of io.vertx.core.streams.WriteStream in project vert.x by eclipse.
the class FileSystemTest method testPumpFileStreams.
@Test
@SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {
String fileName1 = "some-file.dat";
String fileName2 = "some-other-file.dat";
//Non integer multiple of buffer size
int fileSize = (int) (AsyncFileImpl.DEFAULT_READ_BUFFER_SIZE * 1000.3);
byte[] content = TestUtils.randomByteArray(fileSize);
createFile(fileName1, content);
vertx.fileSystem().open(testDir + pathSep + fileName1, new OpenOptions(), arr -> {
if (arr.succeeded()) {
ReadStream rs = arr.result();
vertx.fileSystem().open(testDir + pathSep + fileName2, new OpenOptions(), ar -> {
if (ar.succeeded()) {
WriteStream ws = ar.result();
Pump p = Pump.pump(rs, ws);
p.start();
rs.endHandler(v -> {
arr.result().close(car -> {
if (car.failed()) {
fail(ar.cause().getMessage());
} else {
ar.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertTrue(fileExists(fileName2));
byte[] readBytes;
try {
readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName2));
} catch (IOException e) {
fail(e.getMessage());
return;
}
assertEquals(Buffer.buffer(content), Buffer.buffer(readBytes));
testComplete();
}
});
}
});
});
} else {
fail(ar.cause().getMessage());
}
});
} else {
fail(arr.cause().getMessage());
}
});
await();
}
use of io.vertx.core.streams.WriteStream in project vertx-web by vert-x3.
the class WebClientTest method testResponseBodyStream.
@Test
public void testResponseBodyStream() throws Exception {
AtomicBoolean paused = new AtomicBoolean();
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setChunked(true);
vertx.setPeriodic(1, id -> {
if (!resp.writeQueueFull()) {
resp.write(TestUtils.randomAlphaString(1024));
} else {
resp.drainHandler(v -> resp.end());
paused.set(true);
vertx.cancelTimer(id);
}
});
});
startServer();
CompletableFuture<Void> resume = new CompletableFuture<>();
AtomicInteger size = new AtomicInteger();
AtomicBoolean ended = new AtomicBoolean();
WriteStream<Buffer> stream = new WriteStream<Buffer>() {
boolean paused = true;
Handler<Void> drainHandler;
{
resume.thenAccept(v -> {
paused = false;
if (drainHandler != null) {
drainHandler.handle(null);
}
});
}
@Override
public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
return this;
}
@Override
public WriteStream<Buffer> write(Buffer data) {
size.addAndGet(data.length());
return this;
}
@Override
public void end() {
ended.set(true);
}
@Override
public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
return this;
}
@Override
public boolean writeQueueFull() {
return paused;
}
@Override
public WriteStream<Buffer> drainHandler(Handler<Void> handler) {
drainHandler = handler;
return this;
}
};
HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get.as(BodyCodec.pipe(stream)).send(onSuccess(resp -> {
assertTrue(ended.get());
assertEquals(200, resp.statusCode());
assertEquals(null, resp.body());
testComplete();
}));
assertWaitUntil(paused::get);
resume.complete(null);
await();
}
use of io.vertx.core.streams.WriteStream in project georocket by georocket.
the class StoreClientImportTest method importProperties.
/**
* Test importing properties
* @param context the test context
* @throws Exception if something goes wrong
*/
@Test
public void importProperties(TestContext context) throws Exception {
String url = "/store?props=hello%3Aworld%2Ckey%3Avalue";
stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
Async async = context.async();
WriteStream<Buffer> w = client.getStore().startImport(null, null, Arrays.asList("hello:world", "key:value"), context.asyncAssertSuccess(v -> {
verifyPosted(url, XML, context);
async.complete();
}));
w.end(Buffer.buffer(XML));
}
use of io.vertx.core.streams.WriteStream in project georocket by georocket.
the class StoreClientImportTest method importTags.
/**
* Test importing with tags
* @param context the test context
* @throws Exception if something goes wrong
*/
@Test
public void importTags(TestContext context) throws Exception {
String url = "/store?tags=hello%2Cworld";
stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
Async async = context.async();
WriteStream<Buffer> w = client.getStore().startImport(null, Arrays.asList("hello", "world"), context.asyncAssertSuccess(v -> {
verifyPosted(url, XML, context);
async.complete();
}));
w.end(Buffer.buffer(XML));
}
Aggregations