Search in sources :

Example 1 with ReadStream

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();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Buffer(io.vertx.core.buffer.Buffer) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) ReadStream(io.vertx.core.streams.ReadStream) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClientOptions(io.vertx.core.net.NetClientOptions) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 2 with ReadStream

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();
}
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)

Aggregations

ReadStream (io.vertx.core.streams.ReadStream)2 Test (org.junit.Test)2 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 OpenOptions (io.vertx.core.file.OpenOptions)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 NetClient (io.vertx.core.net.NetClient)1 NetClientOptions (io.vertx.core.net.NetClientOptions)1 NetSocket (io.vertx.core.net.NetSocket)1 Pump (io.vertx.core.streams.Pump)1 WriteStream (io.vertx.core.streams.WriteStream)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1