@Override public void start() throws Exception { synchronized (contexts) { if (contexts.contains(context)) { throw new IllegalStateException("Same context!"); } else { contexts.add(context); vertx.eventBus().send("tvstarted", "started"); } } }
@Override public void stop(Future<Void> stopFuture) throws Exception { vertx.eventBus().send("tvstopped", "stopped", reply -> { stopFuture.complete(null); }); } }
@Override public void start() throws Exception { processArgs = context.processArgs(); conf = context.config(); // if (Thread.currentThread().getContextClassLoader() != getClass().getClassLoader()) { // throw new IllegalStateException("Wrong tccl!"); // } vertx.eventBus().send("testcounts", new JsonObject().put("deploymentID", context.deploymentID()).put("count", instanceCount.incrementAndGet())); }
@Override public void start() throws Exception { vertx.eventBus().send("instanceCount", vertx.getOrCreateContext().getInstanceCount()); } }
@Test public void testChangesNotVisibleObject1() { JsonObject obj = new JsonObject(); eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> { assertFalse(msg.body().containsKey("b")); testComplete(); }); eb.send("foo", obj); obj.put("b", "blurrgg"); await(); }
@Test public void testNoRegisteredDecoder() throws Exception { try { vertx.eventBus().send(ADDRESS1, "foo", new DeliveryOptions().setCodecName("iqwjdoqiwd")); fail("Should throw exception"); } catch (IllegalArgumentException e) { // OK } }
@Test public void testReplyToSendWithNoReplyHandler() { eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> { msg.reply("a reply"); testComplete(); }); eb.send(ADDRESS1, "whatever"); await(); }
@Test public void testChangesNotVisibleArray1() { JsonArray obj = new JsonArray(); eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> { assertEquals(0, msg.body().size()); testComplete(); }); eb.send("foo", obj); obj.add("blah"); await(); }
@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(); }
@Test public void testRegisterLocal1() { String str = TestUtils.randomUnicodeString(100); eb.<String>localConsumer(ADDRESS1).handler((Message<String> msg) -> { assertEquals(str, msg.body()); testComplete(); }).completionHandler(ar -> { assertTrue(ar.succeeded()); eb.send(ADDRESS1, str); }); await(); }
@Test public void testUnregisterDefaultDecoder() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); vertx.eventBus().unregisterDefaultCodec(MyPOJO.class); assertIllegalArgumentException(() -> vertx.eventBus().send(ADDRESS1, new MyPOJO("foo"))); }
@Test public void testInboundInterceptorNoNext() { eb.addInboundInterceptor(dc -> { assertEquals("armadillo", dc.message().body()); }); eb.consumer("some-address", msg -> { fail("Should not receive message"); }); eb.send("some-address", "armadillo"); vertx.setTimer(200, tid -> testComplete()); await(); }
@Test public void testSendWithTimeoutNoTimeoutNoReply() { String str = TestUtils.randomUnicodeString(1000); eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> { assertEquals(str, msg.body()); testComplete(); }); long timeout = 1000; eb.send(ADDRESS1, str, new DeliveryOptions().setSendTimeout(timeout), ar -> { }); await(); }
@Test public void testLocalHandlerNotReceive() throws Exception { startNodes(2); vertices[1].eventBus().localConsumer(ADDRESS1).handler(msg -> { fail("Should not receive message"); }); vertices[0].eventBus().send(ADDRESS1, "foo"); vertices[0].setTimer(1000, id -> testComplete()); await(); }
@Test public void testOutboundInterceptorNoNext() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); }); eb.consumer("some-address", msg -> { fail("Should not receive message"); }); eb.send("some-address", "armadillo"); vertx.setTimer(200, tid -> testComplete()); await(); }
@Test public void testRegisterUnregister() { String str = TestUtils.randomUnicodeString(100); Handler<Message<String>> handler = msg -> fail("Should not receive message"); MessageConsumer reg = eb.<String>consumer(ADDRESS1).handler(handler); assertEquals(ADDRESS1, reg.address()); reg.unregister(); eb.send(ADDRESS1, str); vertx.setTimer(1000, id -> testComplete()); await(); }
@Test public void testDefaultCodecReplyExceptionSubclass() throws Exception { MyReplyException myReplyException = new MyReplyException(23, "my exception"); vertx.eventBus().registerDefaultCodec(MyReplyException.class, new MyReplyExceptionMessageCodec()); eb.<ReplyException>consumer(ADDRESS1, msg -> { assertTrue(msg.body() instanceof MyReplyException); testComplete(); }); vertx.eventBus().send(ADDRESS1, myReplyException); await(); }
@Test public void testUnregisterDecoder() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerCodec(codec); vertx.eventBus().unregisterCodec(codec.name()); try { vertx.eventBus().send(ADDRESS1, new MyPOJO("foo"), new DeliveryOptions().setCodecName(codec.name())); fail("Should throw exception"); } catch (IllegalArgumentException e) { // OK } }
@Test public void testLocalOnlyDoesNotApplyToReplies() { startNodes(2); vertices[1].eventBus().consumer(ADDRESS1).handler(msg -> { msg.reply("pong", new DeliveryOptions().setLocalOnly(true)); }).completionHandler(onSuccess(v -> { vertices[0].eventBus().send(ADDRESS1, "ping", new DeliveryOptions().setSendTimeout(500), onSuccess(msg -> testComplete())); })); await(); } }