Search in sources :

Example 16 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method importLayer.

/**
 * Test importing to a layer
 * @param context the test context
 */
@Test
public void importLayer(TestContext context) {
    String url = "/store/hello/world/";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport("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)

Example 17 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method simpleImport.

/**
 * Test a simple import
 * @param context the test context
 */
@Test
public void simpleImport(TestContext context) {
    String url = "/store";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport(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 18 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class ImportCommand method importFile.

/**
 * Upload a file to GeoRocket
 * @param path path to file to import
 * @param client the GeoRocket client
 * @param vertx the Vert.x instance
 * @return an observable that will emit when the file has been uploaded
 */
protected Observable<Void> importFile(String path, GeoRocketClient client, Vertx vertx) {
    // open file
    FileSystem fs = vertx.fileSystem();
    OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
    return fs.rxOpen(path, openOptions).flatMap(f -> fs.rxProps(path).map(props -> Pair.of(f, props.size()))).flatMapObservable(f -> {
        ObservableFuture<Void> o = RxHelper.observableFuture();
        Handler<AsyncResult<Void>> handler = o.toHandler();
        AsyncFile file = f.getLeft().getDelegate();
        WriteStream<Buffer> out = client.getStore().startImport(layer, tags, properties, Optional.of(f.getRight()), fallbackCRS, handler);
        AtomicBoolean fileClosed = new AtomicBoolean();
        Pump pump = Pump.pump(file, out);
        file.endHandler(v -> {
            file.close();
            out.end();
            fileClosed.set(true);
        });
        Handler<Throwable> exceptionHandler = t -> {
            if (!fileClosed.get()) {
                file.endHandler(null);
                file.close();
            }
            handler.handle(Future.failedFuture(t));
        };
        file.exceptionHandler(exceptionHandler);
        out.exceptionHandler(exceptionHandler);
        pump.start();
        return o;
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OptionParserException(de.undercouch.underline.OptionParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(org.apache.commons.lang3.tuple.Pair) FileSet(org.apache.tools.ant.types.FileSet) WriteStream(io.vertx.core.streams.WriteStream) FileSystem(io.vertx.rxjava.core.file.FileSystem) Project(org.apache.tools.ant.Project) UnknownAttributes(de.undercouch.underline.UnknownAttributes) Pump(io.vertx.core.streams.Pump) AsyncResult(io.vertx.core.AsyncResult) Splitter(com.google.common.base.Splitter) OptionDesc(de.undercouch.underline.OptionDesc) PrintWriter(java.io.PrintWriter) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) SystemUtils(org.apache.commons.lang3.SystemUtils) InputReader(de.undercouch.underline.InputReader) DurationFormat(io.georocket.util.DurationFormat) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) GeoRocketClient(io.georocket.client.GeoRocketClient) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) RxHelper(io.vertx.rx.java.RxHelper) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) ArgumentType(de.undercouch.underline.Option.ArgumentType) Handler(io.vertx.core.Handler) FilenameUtils(org.apache.commons.io.FilenameUtils) Vertx(io.vertx.rxjava.core.Vertx) Buffer(io.vertx.core.buffer.Buffer) Pump(io.vertx.core.streams.Pump) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileSystem(io.vertx.rxjava.core.file.FileSystem) AsyncFile(io.vertx.core.file.AsyncFile) AsyncResult(io.vertx.core.AsyncResult)

Example 19 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, Future<WriteStream<Buffer>>> streamProvider) throws Exception {
    Context ctx = vertx.getOrCreateContext();
    String content = TestUtils.randomAlphaString(1024);
    StringBuilder expected = new StringBuilder();
    Promise<Void> whenFull = Promise.promise();
    AtomicBoolean drain = new AtomicBoolean();
    server.requestHandler(req -> {
        Future<WriteStream<Buffer>> fut = streamProvider.apply(req);
        fut.onComplete(onSuccess(stream -> {
            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.future().onComplete(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) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) MultiMap(io.vertx.core.MultiMap) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http1xOrH2CHandler(io.vertx.core.http.impl.Http1xOrH2CHandler) Context(io.vertx.core.Context) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils(io.vertx.test.core.TestUtils) 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) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelInitializer(io.netty.channel.ChannelInitializer) Http2Flags(io.netty.handler.codec.http2.Http2Flags) Trust(io.vertx.test.tls.Trust) 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) Http2Error(io.netty.handler.codec.http2.Http2Error) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) WriteStream(io.vertx.core.streams.WriteStream) Http2Stream(io.netty.handler.codec.http2.Http2Stream) BiConsumer(java.util.function.BiConsumer) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) DetectFileDescriptorLeaks(io.vertx.test.core.DetectFileDescriptorLeaks) VertxInternal(io.vertx.core.impl.VertxInternal) ClosedChannelException(java.nio.channels.ClosedChannelException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Promise(io.vertx.core.Promise) 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) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) 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) HttpUtils(io.vertx.core.http.impl.HttpUtils) 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) Buffer(io.vertx.core.buffer.Buffer) ChannelFuture(io.netty.channel.ChannelFuture) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) WriteStream(io.vertx.core.streams.WriteStream) 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)

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