use of io.vertx.core.streams.ReadStream in project vert.x by eclipse.
the class HttpRequestStreamTest method testReadStreamPauseResume.
@Test
public void testReadStreamPauseResume() {
String path = "/some/path";
this.server = vertx.createHttpServer(new HttpServerOptions().setAcceptBacklog(10).setPort(HttpTestBase.DEFAULT_HTTP_PORT));
ReadStream<HttpServerRequest> httpStream = server.requestStream();
AtomicBoolean paused = new AtomicBoolean();
httpStream.handler(req -> {
assertFalse(paused.get());
HttpServerResponse response = req.response();
response.setStatusCode(200).end();
response.close();
});
server.listen(listenAR -> {
assertTrue(listenAR.succeeded());
paused.set(true);
httpStream.pause();
netClient = vertx.createNetClient(new NetClientOptions().setConnectTimeout(1000));
netClient.connect(HttpTestBase.DEFAULT_HTTP_PORT, "localhost", socketAR -> {
assertTrue(socketAR.succeeded());
NetSocket socket = socketAR.result();
Buffer buffer = Buffer.buffer();
socket.handler(buffer::appendBuffer);
socket.closeHandler(v -> {
assertEquals(0, buffer.length());
paused.set(false);
httpStream.resume();
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, "localhost", path, resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}).end();
});
});
});
await();
}
use of io.vertx.core.streams.ReadStream 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();
}
Aggregations