use of io.vertx.core.net.NetClient 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(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(DEFAULT_HTTP_PORT, "localhost", socketAR -> {
assertTrue(socketAR.succeeded());
NetSocket socket = socketAR.result();
socket.write("GET / HTTP/1.1\r\n\r\n");
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(new RequestOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path)).onComplete(onSuccess(req -> {
req.send(onSuccess(resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}));
}));
});
});
});
await();
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class VertxTest method testFinalizeNetClient.
@Test
public void testFinalizeNetClient() throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx();
try {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<NetSocket> socketRef = new AtomicReference<>();
vertx.createNetServer().connectHandler(socketRef::set).listen(1234, "localhost").onComplete(onSuccess(server -> latch.countDown()));
awaitLatch(latch);
AtomicBoolean closed = new AtomicBoolean();
CloseFuture closeFuture = new CloseFuture();
NetClient client = vertx.createNetClient(new NetClientOptions(), closeFuture);
vertx.addCloseHook(closeFuture);
closeFuture.future().onComplete(ar -> closed.set(true));
closeFuture = null;
client.connect(1234, "localhost", onSuccess(so -> {
}));
WeakReference<NetClient> ref = new WeakReference<>(client);
client = null;
assertWaitUntil(() -> socketRef.get() != null);
for (int i = 0; i < 10; i++) {
Thread.sleep(10);
RUNNER.runSystemGC();
assertFalse(closed.get());
assertNotNull(ref.get());
}
socketRef.get().close();
long now = System.currentTimeMillis();
while (true) {
assertTrue(System.currentTimeMillis() - now < 20_000);
RUNNER.runSystemGC();
if (ref.get() == null) {
assertTrue(closed.get());
break;
}
}
} finally {
vertx.close(ar -> {
testComplete();
});
}
await();
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class ConnectionBaseTest method testQueueFlushFromEventLoop.
@Test
public void testQueueFlushFromEventLoop() {
NetClient client = vertx.createNetClient();
NetServer server = vertx.createNetServer();
try {
server.connectHandler(so -> {
ConnectionBase conn = (ConnectionBase) so;
ChannelHandlerContext ctx = conn.channelHandlerContext();
ChannelPipeline pipeline = ctx.pipeline();
List<String> order = new ArrayList<>();
Runnable checkOrder = () -> {
if (order.size() == 3) {
vertx.runOnContext(v -> {
assertEquals(Arrays.asList("msg1", "msg2", "flush"), order);
testComplete();
});
}
};
pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof String) {
String s = (String) msg;
order.add(s);
if ("msg1".equals(s)) {
// Flush a message why there are two messages queued on the connection
conn.flush();
}
checkOrder.run();
} else {
super.write(ctx, msg, promise);
}
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
order.add("flush");
checkOrder.run();
super.flush(ctx);
}
});
executeAsyncTask(() -> {
conn.writeToChannel("msg1");
conn.writeToChannel("msg2");
});
});
server.listen(1234, "localhost", onSuccess(s -> {
client.connect(1234, "localhost", onSuccess(so -> {
}));
}));
await();
} finally {
server.close();
client.close();
}
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class ConnectionBaseTest method testQueueMessageFromEvent.
@Test
public void testQueueMessageFromEvent() {
NetClient client = vertx.createNetClient();
NetServer server = vertx.createNetServer();
try {
server.connectHandler(so -> {
NetSocketInternal conn = (NetSocketInternal) so;
ChannelHandlerContext ctx = conn.channelHandlerContext();
ChannelPipeline pipeline = ctx.pipeline();
List<String> order = new ArrayList<>();
pipeline.addBefore("handler", "myhandler", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof String) {
String s = (String) msg;
order.add(s);
if ("msg1".equals(s)) {
// Write a message why there are two messages queued on the connection
conn.writeMessage("msg3");
}
if (order.size() == 3) {
vertx.runOnContext(v -> {
assertEquals(Arrays.asList("msg1", "msg2", "msg3"), order);
testComplete();
});
}
} else {
super.write(ctx, msg, promise);
}
}
});
executeAsyncTask(() -> {
conn.writeMessage("msg1");
conn.writeMessage("msg2");
});
});
server.listen(1234, "localhost", onSuccess(s -> {
client.connect(1234, "localhost", onSuccess(so -> {
}));
}));
await();
} finally {
server.close();
client.close();
}
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class CoreExamples method tcpClientWithDomainSockets.
public void tcpClientWithDomainSockets(Vertx vertx) {
NetClient netClient = vertx.createNetClient();
// Only available on BSD and Linux
SocketAddress addr = SocketAddress.domainSocketAddress("/var/tmp/myservice.sock");
// Connect to the server
netClient.connect(addr, ar -> {
if (ar.succeeded()) {
// Connected
} else {
ar.cause().printStackTrace();
}
});
}
Aggregations