@Test public void testExceptionInInboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = dc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> dc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = dc -> { cnt.incrementAndGet(); dc.next(); }; eb.addInboundInterceptor(eb1).addInboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testInboundInterceptorOnReply() { AtomicInteger cnt = new AtomicInteger(); eb.addInboundInterceptor(dc -> { if (dc.message().body().equals("armadillo")) { assertEquals(0, cnt.get()); } else if (dc.message().body().equals("echidna")) { assertEquals(1, cnt.get()); } else { fail("wrong body"); } cnt.incrementAndGet(); dc.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 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 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 testExceptionInInboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = dc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> dc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = dc -> { cnt.incrementAndGet(); dc.next(); }; eb.addInboundInterceptor(eb1).addInboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
/** * Add an interceptor that will be called whenever a message is received by Vert.x * @param interceptor the interceptor * @return a reference to this, so the API can be used fluently */ public <T> io.vertx.rxjava.core.eventbus.EventBus addInboundInterceptor(Handler<io.vertx.rxjava.core.eventbus.DeliveryContext<T>> interceptor) { delegate.addInboundInterceptor(new Handler<io.vertx.core.eventbus.DeliveryContext<T>>() { public void handle(io.vertx.core.eventbus.DeliveryContext<T> event) { interceptor.handle(io.vertx.rxjava.core.eventbus.DeliveryContext.newInstance(event, io.vertx.lang.rx.TypeArg.unknown())); } }); return this; }
/** * Add an interceptor that will be called whenever a message is received by Vert.x * @param interceptor the interceptor * @return a reference to this, so the API can be used fluently */ public <T> io.vertx.rxjava.core.eventbus.EventBus addInboundInterceptor(Handler<io.vertx.rxjava.core.eventbus.DeliveryContext<T>> interceptor) { delegate.addInboundInterceptor(new Handler<io.vertx.core.eventbus.DeliveryContext<T>>() { public void handle(io.vertx.core.eventbus.DeliveryContext<T> event) { interceptor.handle(io.vertx.rxjava.core.eventbus.DeliveryContext.newInstance(event, io.vertx.lang.rx.TypeArg.unknown())); } }); return this; }
@Test public void testInboundInterceptorOnReply() { AtomicInteger cnt = new AtomicInteger(); eb.addInboundInterceptor(dc -> { if (dc.message().body().equals("armadillo")) { assertEquals(0, cnt.get()); } else if (dc.message().body().equals("echidna")) { assertEquals(1, cnt.get()); } else { fail("wrong body"); } cnt.incrementAndGet(); dc.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 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 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(); }