use of io.vertx.test.fakestream.FakeStream in project vert.x by eclipse.
the class HttpTest method testServerResponseSend.
private void testServerResponseSend(boolean chunked) throws Exception {
int num = 16;
List<Buffer> chunks = new ArrayList<>();
Buffer expected = Buffer.buffer();
for (int i = 0; i < num; i++) {
Buffer chunk = Buffer.buffer("chunk-" + i);
chunks.add(chunk);
expected.appendBuffer(chunk);
}
String contentLength = "" + expected.length();
server.requestHandler(req -> {
FakeStream<Buffer> stream = new FakeStream<>();
stream.pause();
stream.emit(chunks.stream());
stream.end();
HttpServerResponse resp = req.response();
if (!chunked) {
resp.putHeader(HttpHeaders.CONTENT_LENGTH, contentLength);
}
resp.send(stream);
});
startServer(testAddress);
client.request(requestOptions).compose(req -> req.send().compose(resp -> {
if (!chunked) {
assertEquals(contentLength, resp.getHeader(HttpHeaders.CONTENT_LENGTH));
}
List<Buffer> list = new ArrayList<>();
resp.handler(buff -> {
if (buff.length() > 0) {
// Last HTTP2/2 buffer is empty
list.add(buff);
}
});
return resp.end().map(list);
})).onComplete(onSuccess(list -> {
if (chunked) {
assertEquals(num, list.size());
}
Buffer result = Buffer.buffer();
list.forEach(result::appendBuffer);
assertEquals(expected, result);
testComplete();
}));
await();
}
use of io.vertx.test.fakestream.FakeStream in project vert.x by eclipse.
the class PipeTest method testEndWriteStreamFail.
@Test
public void testEndWriteStreamFail() {
Promise<Void> completion = Promise.promise();
dst.setEnd(completion.future());
FakeStream<Object> src = new FakeStream<>();
Pipe<Object> pipe = src.pipe();
AtomicReference<AsyncResult<Void>> ended = new AtomicReference<>();
pipe.to(dst, ended::set);
src.end();
assertNull(ended.get());
Exception failure = new Exception();
completion.fail(failure);
assertTrue(ended.get().failed());
assertEquals(failure, ended.get().cause());
}
use of io.vertx.test.fakestream.FakeStream in project vert.x by eclipse.
the class RecordParserTest method endOfWrappedStream.
private void endOfWrappedStream(boolean pauseParser, boolean finalDelimiter) throws Exception {
FakeStream<Buffer> stream = new FakeStream<>();
RecordParser parser = RecordParser.newDelimited("\n", stream);
List<Buffer> emitted = Collections.synchronizedList(new ArrayList<>());
CountDownLatch endLatch = new CountDownLatch(1);
parser.endHandler(v -> {
assertEquals(Arrays.asList(Buffer.buffer("toto"), Buffer.buffer("titi")), emitted);
endLatch.countDown();
});
parser.handler(emitted::add);
if (pauseParser) {
parser.pause();
}
stream.write(Buffer.buffer("toto\ntit"));
stream.write(Buffer.buffer(finalDelimiter ? "i\n" : "i"));
stream.end();
if (pauseParser) {
parser.fetch(1);
parser.resume();
}
endLatch.await();
}
use of io.vertx.test.fakestream.FakeStream in project vert.x by eclipse.
the class RecordParserTest method testWrapReadStream.
@Test
public void testWrapReadStream() {
FakeStream<Buffer> stream = new FakeStream<>();
RecordParser parser = RecordParser.newDelimited("\r\n", stream);
AtomicInteger ends = new AtomicInteger();
parser.endHandler(v -> ends.incrementAndGet());
Deque<String> records = new ArrayDeque<>();
parser.handler(record -> records.add(record.toString()));
assertFalse(stream.isPaused());
parser.pause();
parser.resume();
stream.write(Buffer.buffer("first\r\nsecond\r\nthird"));
assertEquals("first", records.poll());
assertEquals("second", records.poll());
assertNull(records.poll());
stream.write(Buffer.buffer("\r\n"));
assertEquals("third", records.poll());
assertNull(records.poll());
assertEquals(0, ends.get());
Throwable cause = new Throwable();
stream.fail(cause);
List<Throwable> failures = new ArrayList<>();
parser.exceptionHandler(failures::add);
stream.fail(cause);
assertEquals(Collections.singletonList(cause), failures);
assertFalse(stream.isPaused());
parser.pause();
assertFalse(stream.isPaused());
int count = 0;
do {
stream.write(Buffer.buffer("item-" + count++ + "\r\n"));
} while (!stream.isPaused());
assertNull(records.poll());
parser.resume();
for (int i = 0; i < count; i++) {
assertEquals("item-" + i, records.poll());
}
assertNull(records.poll());
assertFalse(stream.isPaused());
stream.end();
assertEquals(1, ends.get());
}
Aggregations