Search in sources :

Example 1 with FakeStream

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) IntStream(java.util.stream.IntStream) java.util(java.util) DecompressionException(io.netty.handler.codec.compression.DecompressionException) io.vertx.core(io.vertx.core) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Utils(io.vertx.core.impl.Utils) Collections.singletonList(java.util.Collections.singletonList) ServerSocket(java.net.ServerSocket) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2Exception(io.netty.handler.codec.http2.Http2Exception) ReadStream(io.vertx.core.streams.ReadStream) HAProxy(io.vertx.test.proxy.HAProxy) Pump(io.vertx.core.streams.Pump) Assume(org.junit.Assume) DetectFileDescriptorLeaks(io.vertx.test.core.DetectFileDescriptorLeaks) DateFormat(java.text.DateFormat) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) java.util.concurrent(java.util.concurrent) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Future(io.vertx.core.Future) io.vertx.core.net(io.vertx.core.net) Nullable(io.vertx.codegen.annotations.Nullable) HttpServerRequestInternal(io.vertx.core.http.impl.HttpServerRequestInternal) AtomicLong(java.util.concurrent.atomic.AtomicLong) Repeat(io.vertx.test.core.Repeat) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) java.io(java.io) PUT(io.vertx.core.http.HttpMethod.PUT) java.util.function(java.util.function) FakeStream(io.vertx.test.fakestream.FakeStream) TemporaryFolder(org.junit.rules.TemporaryFolder) ServerCookie(io.vertx.core.http.impl.ServerCookie) FakeStream(io.vertx.test.fakestream.FakeStream)

Example 2 with FakeStream

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());
}
Also used : FakeStream(io.vertx.test.fakestream.FakeStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncResult(io.vertx.core.AsyncResult) VertxException(io.vertx.core.VertxException) Test(org.junit.Test)

Example 3 with FakeStream

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) FakeStream(io.vertx.test.fakestream.FakeStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with FakeStream

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());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) FakeStream(io.vertx.test.fakestream.FakeStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

FakeStream (io.vertx.test.fakestream.FakeStream)4 Buffer (io.vertx.core.buffer.Buffer)3 Test (org.junit.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConnectTimeoutException (io.netty.channel.ConnectTimeoutException)1 DecompressionException (io.netty.handler.codec.compression.DecompressionException)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Http2Exception (io.netty.handler.codec.http2.Http2Exception)1 Nullable (io.vertx.codegen.annotations.Nullable)1 io.vertx.core (io.vertx.core)1 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 VertxException (io.vertx.core.VertxException)1 AddressResolverOptions (io.vertx.core.dns.AddressResolverOptions)1 PUT (io.vertx.core.http.HttpMethod.PUT)1 HttpServerRequestInternal (io.vertx.core.http.impl.HttpServerRequestInternal)1 ServerCookie (io.vertx.core.http.impl.ServerCookie)1 Utils (io.vertx.core.impl.Utils)1 io.vertx.core.net (io.vertx.core.net)1