@Test public void testExceptionInOutboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = sc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> sc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = sc -> { cnt.incrementAndGet(); sc.next(); }; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
}; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2).addOutboundInterceptor(eb3);
@Test public void testOutboundInterceptorOnReply() { AtomicInteger cnt = new AtomicInteger(); eb.addOutboundInterceptor(sc -> { if (sc.message().body().equals("armadillo")) { assertEquals(0, cnt.get()); } else if (sc.message().body().equals("echidna")) { assertEquals(1, cnt.get()); } else { fail("wrong body"); } cnt.incrementAndGet(); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(1, cnt.get()); msg.reply("echidna"); }); eb.send("some-address", "armadillo", reply -> { assertEquals("echidna", reply.result().body()); assertEquals(2, cnt.get()); testComplete(); }); await(); }
@Test public void testMultipleOutboundInterceptors() { AtomicInteger cnt = new AtomicInteger(); int interceptorNum = 10; for (int i = 0; i < interceptorNum; i++) { final int expectedCount = i; eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); int count = cnt.getAndIncrement(); assertEquals(expectedCount, count); sc.next(); }); } eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(interceptorNum, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); 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 testMessageBodyInterceptor() throws Exception { String content = TestUtils.randomUnicodeString(13); startNodes(2); waitFor(2); CountDownLatch latch = new CountDownLatch(1); vertices[0].eventBus().registerCodec(new StringLengthCodec()).<Integer>consumer("whatever", msg -> { assertEquals(content.length(), (int) msg.body()); complete(); }).completionHandler(ar -> latch.countDown()); awaitLatch(latch); StringLengthCodec codec = new StringLengthCodec(); vertices[1].eventBus().registerCodec(codec).addOutboundInterceptor(sc -> { if ("whatever".equals(sc.message().address())) { assertEquals(content, sc.body()); complete(); } sc.next(); }).send("whatever", content, new DeliveryOptions().setCodecName(codec.name())); await(); } }
@Test public void testOutboundInterceptorOnPublish() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertFalse(sc.send()); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.publish("some-address", "armadillo"); await(); }
@Test public void testOutboundInterceptorOnSend() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertSame(sc.body(), sc.message().body()); assertTrue(sc.send()); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testInterceptorsOnPublish() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertFalse(sc.send()); sc.next(); }).addInboundInterceptor(dc -> { assertEquals("armadillo", dc.message().body()); assertFalse(dc.send()); dc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.publish("some-address", "armadillo"); await(); }
@Test public void testInterceptorsOnSend() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertTrue(sc.send()); sc.next(); }).addInboundInterceptor(dc -> { assertEquals("armadillo", dc.message().body()); assertSame(dc.body(), dc.message().body()); assertTrue(dc.send()); dc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testExceptionInOutboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = sc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> sc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = sc -> { cnt.incrementAndGet(); sc.next(); }; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
}; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2).addOutboundInterceptor(eb3);
@Test public void testOutboundInterceptorOnReply() { AtomicInteger cnt = new AtomicInteger(); eb.addOutboundInterceptor(sc -> { if (sc.message().body().equals("armadillo")) { assertEquals(0, cnt.get()); } else if (sc.message().body().equals("echidna")) { assertEquals(1, cnt.get()); } else { fail("wrong body"); } cnt.incrementAndGet(); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(1, cnt.get()); msg.reply("echidna"); }); eb.send("some-address", "armadillo", reply -> { assertEquals("echidna", reply.result().body()); assertEquals(2, cnt.get()); testComplete(); }); await(); }
@Test public void testMultipleOutboundInterceptors() { AtomicInteger cnt = new AtomicInteger(); int interceptorNum = 10; for (int i = 0; i < interceptorNum; i++) { final int expectedCount = i; eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); int count = cnt.getAndIncrement(); assertEquals(expectedCount, count); sc.next(); }); } eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(interceptorNum, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); 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 testMessageBodyInterceptor() throws Exception { String content = TestUtils.randomUnicodeString(13); startNodes(2); waitFor(2); CountDownLatch latch = new CountDownLatch(1); vertices[0].eventBus().registerCodec(new StringLengthCodec()).<Integer>consumer("whatever", msg -> { assertEquals(content.length(), (int) msg.body()); complete(); }).completionHandler(ar -> latch.countDown()); awaitLatch(latch); StringLengthCodec codec = new StringLengthCodec(); vertices[1].eventBus().registerCodec(codec).addOutboundInterceptor(sc -> { if ("whatever".equals(sc.message().address())) { assertEquals(content, sc.body()); complete(); } sc.next(); }).send("whatever", content, new DeliveryOptions().setCodecName(codec.name())); await(); } }
@Test public void testOutboundInterceptorOnPublish() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertFalse(sc.send()); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.publish("some-address", "armadillo"); await(); }
@Test public void testOutboundInterceptorOnSend() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertSame(sc.body(), sc.message().body()); assertTrue(sc.send()); sc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testInterceptorsOnPublish() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertFalse(sc.send()); sc.next(); }).addInboundInterceptor(dc -> { assertEquals("armadillo", dc.message().body()); assertFalse(dc.send()); dc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.publish("some-address", "armadillo"); await(); }
@Test public void testInterceptorsOnSend() { eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); assertTrue(sc.send()); sc.next(); }).addInboundInterceptor(dc -> { assertEquals("armadillo", dc.message().body()); assertSame(dc.body(), dc.message().body()); assertTrue(dc.send()); dc.next(); }); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }