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