Search in sources :

Example 41 with NetClient

use of io.vertx.core.net.NetClient in project vert.x by eclipse.

the class MetricsTest method testHTTP2ConnectionCloseBeforePrefaceIsReceived.

@Test
public void testHTTP2ConnectionCloseBeforePrefaceIsReceived() throws Exception {
    // Let the server close the connection with an idle timeout
    HttpServerOptions options = Http2TestBase.createHttp2ServerOptions(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST).setIdleTimeout(1);
    HttpServer server = vertx.createHttpServer(options);
    server.requestHandler(req -> {
    }).listen().toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
    FakeHttpServerMetrics metrics = FakeVertxMetrics.getMetrics(server);
    NetClient client = vertx.createNetClient(new NetClientOptions().setSslEngineOptions(new JdkSSLEngineOptions()).setUseAlpn(true).setSsl(true).setTrustStoreOptions(Trust.SERVER_JKS.get()).setApplicationLayerProtocols(Collections.singletonList("h2")));
    CountDownLatch latch = new CountDownLatch(1);
    client.connect(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, onSuccess(so -> {
        assertEquals("h2", so.applicationLayerProtocol());
        so.closeHandler(v -> latch.countDown());
    }));
    awaitLatch(latch);
    assertEquals(0, metrics.connectionCount());
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) java.util(java.util) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) Is.is(org.hamcrest.core.Is.is) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) NetClient(io.vertx.core.net.NetClient) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) io.vertx.test.fakemetrics(io.vertx.test.fakemetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) EventLoop(io.netty.channel.EventLoop) Collectors(java.util.stream.Collectors) io.vertx.core.http(io.vertx.core.http) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocket(io.vertx.core.net.NetSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) NetClient(io.vertx.core.net.NetClient) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 42 with NetClient

use of io.vertx.core.net.NetClient in project vert.x by eclipse.

the class TransportTest method testNetServer.

private void testNetServer(VertxOptions options) {
    vertx = Vertx.vertx(options);
    NetServer server = vertx.createNetServer();
    server.connectHandler(so -> {
        so.handler(buff -> {
            assertEquals("ping", buff.toString());
            so.write("pong");
        });
        so.closeHandler(v -> {
            testComplete();
        });
    });
    server.listen(1234, onSuccess(v -> {
        NetClient client = vertx.createNetClient();
        client.connect(1234, "localhost", onSuccess(so -> {
            so.write("ping");
            so.handler(buff -> {
                assertEquals("pong", buff.toString());
                so.close();
            });
        }));
    }));
    await();
}
Also used : NetServer(io.vertx.core.net.NetServer) AsyncTestBase(io.vertx.test.core.AsyncTestBase) TestUtils(io.vertx.test.core.TestUtils) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) NetClient(io.vertx.core.net.NetClient) File(java.io.File) SocketAddress(io.vertx.core.net.SocketAddress) NetClient(io.vertx.core.net.NetClient) NetServer(io.vertx.core.net.NetServer)

Example 43 with NetClient

use of io.vertx.core.net.NetClient in project vert.x by eclipse.

the class TransportTest method testDomainSocketClient.

@Test
public void testDomainSocketClient() throws Exception {
    File sock = TestUtils.tmpFile(".sock");
    vertx = Vertx.vertx();
    NetClient client = vertx.createNetClient();
    client.connect(SocketAddress.domainSocketAddress(sock.getAbsolutePath()), onFailure(err -> {
        assertEquals(err.getClass(), IllegalArgumentException.class);
        testComplete();
    }));
    await();
}
Also used : NetServer(io.vertx.core.net.NetServer) AsyncTestBase(io.vertx.test.core.AsyncTestBase) TestUtils(io.vertx.test.core.TestUtils) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) NetClient(io.vertx.core.net.NetClient) File(java.io.File) SocketAddress(io.vertx.core.net.SocketAddress) NetClient(io.vertx.core.net.NetClient) File(java.io.File) Test(org.junit.Test)

Example 44 with NetClient

use of io.vertx.core.net.NetClient in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeInteropTest method testSendMessageWithDuplicateReplyID.

@Test
public void testSendMessageWithDuplicateReplyID(TestContext context) {
    // replies must always return to the same origin
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        vertx.eventBus().consumer("third-party-receiver", msg -> context.fail());
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("send", "hello", "third-party-receiver", new JsonObject().put("value", "vert.x"), socket);
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 45 with NetClient

use of io.vertx.core.net.NetClient in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeHookTest method testRegister.

@Test
public void testRegister(TestContext context) {
    final JsonObject payload = new JsonObject().put("value", "Francesco");
    // 0. Start the bridge
    // 1. Check if REGISTER hook is called
    // 2. Check if REGISTERED hook is called
    // 3. Try to send a message while managing REGISTERED event
    // 4. Check if client receives the message
    Async bridgeStart = context.async();
    Async register = context.async();
    Async registered = context.async();
    Async request = context.async();
    TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx, new BridgeOptions().addInboundPermitted(new PermittedOptions()).addOutboundPermitted(new PermittedOptions()), new NetServerOptions(), be -> {
        if (be.type() == BridgeEventType.REGISTER) {
            context.assertNotNull(be.socket());
            context.assertEquals(address, be.getRawMessage().getString("address"));
            register.complete();
        } else if (be.type() == BridgeEventType.REGISTERED) {
            context.assertNotNull(be.socket());
            context.assertEquals(address, be.getRawMessage().getString("address"));
            // The client should be able to receive this message
            vertx.eventBus().send(address, payload);
            registered.complete();
        }
        be.complete(true);
    });
    bridge.listen(7000, res -> {
        context.assertTrue(res.succeeded());
        bridgeStart.complete();
    });
    bridgeStart.await();
    NetClient client = vertx.createNetClient();
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        // 1 reply will arrive
        // MESSAGE for address
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertNotEquals("err", frame.getString("type"));
            context.assertEquals("Francesco", frame.getJsonObject("body").getString("value"));
            client.close();
            request.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", address, null, socket);
    });
    register.await();
    registered.await();
    request.await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) NetServerOptions(io.vertx.core.net.NetServerOptions) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Aggregations

NetClient (io.vertx.core.net.NetClient)52 NetSocket (io.vertx.core.net.NetSocket)35 Test (org.junit.Test)34 NetServerOptions (io.vertx.core.net.NetServerOptions)24 Vertx (io.vertx.core.Vertx)23 JsonObject (io.vertx.core.json.JsonObject)21 Async (io.vertx.ext.unit.Async)20 Handler (io.vertx.core.Handler)19 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 NetClientOptions (io.vertx.core.net.NetClientOptions)17 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)16 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Before (org.junit.Before)16 Message (io.vertx.core.eventbus.Message)15 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)15 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 After (org.junit.After)15