Search in sources :

Example 21 with NetClient

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

the class TcpEventBusBridgeTest method testSendVoidMessage.

@Test
public void testSendVoidMessage(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
        client.close();
        async.complete();
    });
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Message(io.vertx.core.eventbus.Message) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 22 with NetClient

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

the class TcpEventBusBridgeTest method testSendPing.

@Test
public void testSendPing(TestContext context) {
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    // MESSAGE for ping
    final FrameParser parser = new FrameParser(parse -> {
        context.assertTrue(parse.succeeded());
        JsonObject frame = parse.result();
        context.assertEquals("pong", frame.getString("type"));
        client.close();
        async.complete();
    });
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        socket.handler(parser);
        FrameHelper.sendFrame("register", "echo", null, socket);
        FrameHelper.sendFrame("ping", socket);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) 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 23 with NetClient

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

the class TcpEventBusBridgeTest method testRegister.

@Test
public void testRegister(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        // 1 reply will arrive
        // MESSAGE for echo
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertNotEquals("err", frame.getString("type"));
            context.assertEquals(false, frame.getBoolean("send"));
            context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", "echo", null, socket);
        // now try to publish a message so it gets delivered both to the consumer registred on the startup and to this
        // remote consumer
        FrameHelper.sendFrame("publish", "echo", new JsonObject().put("value", "Vert.x"), socket);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) 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 24 with NetClient

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

the class TcpEventBusBridgeTest method testReplyStringMessageFromClient.

@Test
public void testReplyStringMessageFromClient(TestContext context) {
    // Send a request from java and get a response from the client
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    final String address = "test";
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            if ("message".equals(frame.getString("type"))) {
                context.assertEquals(true, frame.getBoolean("send"));
                context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
                FrameHelper.sendFrame("send", frame.getString("replyAddress"), "You got it", socket);
            }
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", address, null, socket);
        // There is now way to know that the register actually happened, wait a bit before sending.
        vertx.setTimer(500L, timerId -> {
            vertx.eventBus().<JsonObject>request(address, new JsonObject().put("value", "Vert.x"), respMessage -> {
                context.assertTrue(respMessage.succeeded());
                context.assertEquals("You got it", respMessage.result().body());
                client.close();
                async.complete();
            });
        });
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) 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 25 with NetClient

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

the class TcpEventBusBridgeTest method testFailFromClient.

@Test
public void testFailFromClient(TestContext context) {
    // Send a request from java and get a response from the client
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    final String address = "test";
    client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            if ("message".equals(frame.getString("type"))) {
                context.assertEquals(true, frame.getBoolean("send"));
                context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
                FrameHelper.writeFrame(new JsonObject().put("type", "send").put("address", frame.getString("replyAddress")).put("failureCode", 1234).put("message", "ooops!"), socket);
            }
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", address, null, socket);
        // There is now way to know that the register actually happened, wait a bit before sending.
        vertx.setTimer(500L, timerId -> {
            vertx.eventBus().request(address, new JsonObject().put("value", "Vert.x"), respMessage -> {
                context.assertTrue(respMessage.failed());
                context.assertEquals("ooops!", respMessage.cause().getMessage());
                client.close();
                async.complete();
            });
        });
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) 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)

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