Search in sources :

Example 11 with WriteStream

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();
}
Also used : Context(io.vertx.core.Context) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Buffer(io.vertx.core.buffer.Buffer) ChannelFuture(io.netty.channel.ChannelFuture) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Context(io.vertx.core.Context) Unpooled(io.netty.buffer.Unpooled) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Map(java.util.Map) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler) ReadStream(io.vertx.core.streams.ReadStream) AbstractHttp2ConnectionHandlerBuilder(io.netty.handler.codec.http2.AbstractHttp2ConnectionHandlerBuilder) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) StreamResetException(io.vertx.core.http.StreamResetException) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) Http2Flags(io.netty.handler.codec.http2.Http2Flags) Set(java.util.Set) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) Future(io.vertx.core.Future) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Base64(java.util.Base64) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) SslHandler(io.netty.handler.ssl.SslHandler) Http2Headers(io.netty.handler.codec.http2.Http2Headers) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Http2Error(io.netty.handler.codec.http2.Http2Error) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) Trust(io.vertx.test.core.tls.Trust) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ByteBuf(io.netty.buffer.ByteBuf) WriteStream(io.vertx.core.streams.WriteStream) Http2Stream(io.netty.handler.codec.http2.Http2Stream) BiConsumer(java.util.function.BiConsumer) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) ClosedChannelException(java.nio.channels.ClosedChannelException) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) ApplicationProtocolNames(io.netty.handler.ssl.ApplicationProtocolNames) Test(org.junit.Test) IOException(java.io.IOException) SSLHelper(io.vertx.core.net.impl.SSLHelper) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Http2Settings(io.netty.handler.codec.http2.Http2Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) AtomicLong(java.util.concurrent.atomic.AtomicLong) Http2Connection(io.netty.handler.codec.http2.Http2Connection) HttpMethod(io.vertx.core.http.HttpMethod) HttpUtils(io.vertx.core.http.impl.HttpUtils) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder)

Example 12 with WriteStream

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();
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) ReadStream(io.vertx.core.streams.ReadStream) WriteStream(io.vertx.core.streams.WriteStream) IOException(java.io.IOException) Pump(io.vertx.core.streams.Pump) Test(org.junit.Test)

Example 13 with WriteStream

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Arrays(java.util.Arrays) DecodeException(io.vertx.core.json.DecodeException) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Cert(io.vertx.test.core.tls.Cert) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpTestBase(io.vertx.test.core.HttpTestBase) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) ProxyOptions(io.vertx.core.net.ProxyOptions) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) HttpHeaders(io.vertx.core.http.HttpHeaders) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) WriteStream(io.vertx.core.streams.WriteStream) Handler(io.vertx.core.Handler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Test(org.junit.Test)

Example 14 with WriteStream

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));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 15 with WriteStream

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));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

WriteStream (io.vertx.core.streams.WriteStream)19 Buffer (io.vertx.core.buffer.Buffer)17 Test (org.junit.Test)17 Arrays (java.util.Arrays)15 Optional (java.util.Optional)9 VerificationException (com.github.tomakehurst.wiremock.client.VerificationException)8 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)8 WireMock.equalTo (com.github.tomakehurst.wiremock.client.WireMock.equalTo)8 WireMock.post (com.github.tomakehurst.wiremock.client.WireMock.post)8 WireMock.postRequestedFor (com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor)8 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)8 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)8 WireMock.verify (com.github.tomakehurst.wiremock.client.WireMock.verify)8 Handler (io.vertx.core.Handler)8 Async (io.vertx.ext.unit.Async)8 TestContext (io.vertx.ext.unit.TestContext)8 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)8 RunWith (org.junit.runner.RunWith)8 ReadStream (io.vertx.core.streams.ReadStream)7 File (java.io.File)7