Search in sources :

Example 16 with Message

use of io.vertx.core.eventbus.Message in project vert.x by eclipse.

the class JsonEventBusTest method testChangesNotVisibleArray3.

@Test
public void testChangesNotVisibleArray3() {
    List<Object> list = new ArrayList<>();
    final JsonArray obj = new JsonArray(list);
    eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> {
        vertx.setTimer(1000, id -> {
            assertEquals(0, msg.body().size());
            testComplete();
        });
    });
    eb.send("foo", obj);
    list.add("uhwqdiuh");
    await();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Message(io.vertx.core.eventbus.Message) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 17 with Message

use of io.vertx.core.eventbus.Message in project vert.x by eclipse.

the class JsonEventBusTest method testChangesNotVisibleArray2.

@Test
public void testChangesNotVisibleArray2() {
    final JsonArray obj = new JsonArray();
    eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> msg.body().add("blah"));
    eb.send("foo", obj);
    vertx.setTimer(1000, id -> {
        assertEquals(0, obj.size());
        testComplete();
    });
    await();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test)

Example 18 with Message

use of io.vertx.core.eventbus.Message in project vert.x by eclipse.

the class DeploymentTest method testIsolationGroup.

private void testIsolationGroup(String group1, String group2, int count1, int count2, List<String> isolatedClasses, String verticleID) throws Exception {
    Map<String, Integer> countMap = new ConcurrentHashMap<>();
    vertx.eventBus().<JsonObject>consumer("testcounts").handler((Message<JsonObject> msg) -> {
        countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count"));
    });
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<String> deploymentID1 = new AtomicReference<>();
    AtomicReference<String> deploymentID2 = new AtomicReference<>();
    boolean expectedSuccess = Thread.currentThread().getContextClassLoader() instanceof URLClassLoader;
    try {
        vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group1).setIsolatedClasses(isolatedClasses), onSuccess(id1 -> {
            deploymentID1.set(id1);
            assertEquals(0, TestVerticle.instanceCount.get());
            vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group2).setIsolatedClasses(isolatedClasses), onSuccess(id2 -> {
                deploymentID2.set(id2);
                assertEquals(0, TestVerticle.instanceCount.get());
                latch.countDown();
            }));
        }));
        awaitLatch(latch);
        // Wait until two entries in the map
        assertWaitUntil(() -> countMap.size() == 2);
        assertEquals(count1, countMap.get(deploymentID1.get()).intValue());
        assertEquals(count2, countMap.get(deploymentID2.get()).intValue());
        assertTrue(expectedSuccess);
    } catch (IllegalStateException e) {
        assertFalse(expectedSuccess);
    }
}
Also used : java.util(java.util) URL(java.net.URL) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) URLClassLoader(java.net.URLClassLoader) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) SourceVerticle(io.vertx.test.verticles.sourceverticle.SourceVerticle) VertxInternal(io.vertx.core.impl.VertxInternal) Files(java.nio.file.Files) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Deployment(io.vertx.core.impl.Deployment) CompilingClassLoader(io.vertx.core.impl.verticle.CompilingClassLoader) io.vertx.test.verticles(io.vertx.test.verticles) Message(io.vertx.core.eventbus.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URLClassLoader(java.net.URLClassLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 19 with Message

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

the class TcpEventBusBridgeEventTest method before.

@Before
public void before(TestContext context) {
    vertx = Vertx.vertx();
    final Async async = context.async();
    vertx.eventBus().consumer("hello", (Message<JsonObject> msg) -> msg.reply(new JsonObject().put("value", "Hello " + msg.body().getString("value"))));
    vertx.eventBus().consumer("echo", (Message<JsonObject> msg) -> msg.reply(msg.body()));
    vertx.setPeriodic(1000, __ -> vertx.eventBus().send("ping", new JsonObject().put("value", "hi")));
    sslKeyPairCerts = new SSLKeyPairCerts().createTwoWaySSL();
    TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx, new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("hello")).addInboundPermitted(new PermittedOptions().setAddress("echo")).addInboundPermitted(new PermittedOptions().setAddress("test")).addOutboundPermitted(new PermittedOptions().setAddress("echo")).addOutboundPermitted(new PermittedOptions().setAddress("ping")), new NetServerOptions().setClientAuth(ClientAuth.REQUEST).setSsl(true).setTrustStoreOptions(sslKeyPairCerts.getServerTrustStore()).setKeyStoreOptions(sslKeyPairCerts.getServerKeyStore()), be -> {
        logger.info("Handled a bridge event " + be.getRawMessage());
        if (be.socket().isSsl()) {
            try {
                for (Certificate c : be.socket().peerCertificates()) {
                    logger.info(((X509Certificate) c).getSubjectDN().toString());
                }
            } catch (SSLPeerUnverifiedException e) {
                throw new RuntimeException("Failed to get peer certificates chain", e);
            }
        }
        be.complete(true);
    });
    bridge.listen(7000, res -> {
        context.assertTrue(res.succeeded());
        async.complete();
    });
}
Also used : Message(io.vertx.core.eventbus.Message) Async(io.vertx.ext.unit.Async) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Before(org.junit.Before)

Example 20 with Message

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

the class TcpEventBusBridgeEventTest method testSendVoidMessage.

@Test
public void testSendVoidMessage(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient(new NetClientOptions().setSsl(true).setTrustStoreOptions(sslKeyPairCerts.getClientTrustStore()).setKeyStoreOptions(sslKeyPairCerts.getClientKeyStore()));
    final Async async = context.async();
    vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
        client.close();
        async.complete();
    });
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
    });
}
Also used : Message(io.vertx.core.eventbus.Message) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

Message (io.vertx.core.eventbus.Message)53 JsonObject (io.vertx.core.json.JsonObject)37 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)19 Vertx (io.vertx.core.Vertx)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Before (org.junit.Before)17 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Handler (io.vertx.core.Handler)15 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)14 After (org.junit.After)14 RunWith (org.junit.runner.RunWith)14 NetServerOptions (io.vertx.core.net.NetServerOptions)12 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)12 JsonArray (io.vertx.core.json.JsonArray)11 NetClient (io.vertx.core.net.NetClient)11 NetSocket (io.vertx.core.net.NetSocket)11 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)11