Repository: eclipse-vertx/vert.x

Problem statement:
EventBus inbound interceptors are never removed
Since `4.3.0`, `EventBus` interceptors are managed with `AtomicReferenceFieldUpdater`.

But `io.vertx.core.eventbus.impl.EventBusImpl#removeInboundInterceptor` uses the wrong instance:

https://github.com/eclipse-vertx/vert.x/blob/a8d9a164df16c4f2f5eed5f176a5570c46f30d41/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java#L76-L80


Hints:

Patch:
diff --git a/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java b/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java
--- a/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java
+++ b/src/main/java/io/vertx/core/eventbus/impl/EventBusImpl.java
@@ -57,25 +57,25 @@ public EventBusImpl(VertxInternal vertx) {
 
   @Override
   public <T> EventBus addOutboundInterceptor(Handler<DeliveryContext<T>> interceptor) {
-    addInterceptor(OUTBOUND_INTERCEPTORS_UPDATER, interceptor);
+    addInterceptor(OUTBOUND_INTERCEPTORS_UPDATER, Objects.requireNonNull(interceptor));
     return this;
   }
 
   @Override
   public <T> EventBus addInboundInterceptor(Handler<DeliveryContext<T>> interceptor) {
-    addInterceptor(INBOUND_INTERCEPTORS_UPDATER, interceptor);
+    addInterceptor(INBOUND_INTERCEPTORS_UPDATER, Objects.requireNonNull(interceptor));
     return this;
   }
 
   @Override
   public <T> EventBus removeOutboundInterceptor(Handler<DeliveryContext<T>> interceptor) {
-    removeInterceptor(OUTBOUND_INTERCEPTORS_UPDATER, interceptor);
+    removeInterceptor(OUTBOUND_INTERCEPTORS_UPDATER, Objects.requireNonNull(interceptor));
     return this;
   }
 
   @Override
   public <T> EventBus removeInboundInterceptor(Handler<DeliveryContext<T>> interceptor) {
-    removeInterceptor(OUTBOUND_INTERCEPTORS_UPDATER, interceptor);
+    removeInterceptor(INBOUND_INTERCEPTORS_UPDATER, Objects.requireNonNull(interceptor));
     return this;
   }
 
@@ -455,7 +455,7 @@ private void removeInterceptor(AtomicReferenceFieldUpdater<EventBusImpl, Handler
       Handler[] interceptors = updater.get(this);
       int idx = -1;
       for (int i = 0;i < interceptors.length;i++) {
-        if (interceptors[i] == interceptor) {
+        if (interceptors[i].equals(interceptor)) {
           idx = i;
           break;
         }


Test patch:
diff --git a/src/test/java/io/vertx/core/eventbus/EventBusInterceptorTest.java b/src/test/java/io/vertx/core/eventbus/EventBusInterceptorTest.java
--- a/src/test/java/io/vertx/core/eventbus/EventBusInterceptorTest.java
+++ b/src/test/java/io/vertx/core/eventbus/EventBusInterceptorTest.java
@@ -147,7 +147,7 @@ public void testMultipleOutboundInterceptors() {
   }
 
   @Test
-  public void testRemoveOutboundInterceptor() {
+  public void testRemoveInterceptor() {
 
     AtomicInteger cnt1 = new AtomicInteger();
     AtomicInteger cnt2 = new AtomicInteger();
@@ -168,25 +168,28 @@ public void testRemoveOutboundInterceptor() {
       sc.next();
     };
 
-    eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2).addOutboundInterceptor(eb3);
+    eb
+      .addInboundInterceptor(eb1).addOutboundInterceptor(eb1)
+      .addInboundInterceptor(eb2).addOutboundInterceptor(eb2)
+      .addInboundInterceptor(eb3).addOutboundInterceptor(eb3);
 
     eb.consumer("some-address", msg -> {
       if (msg.body().equals("armadillo")) {
-        assertEquals(1, cnt1.get());
-        assertEquals(1, cnt2.get());
-        assertEquals(1, cnt3.get());
-        eb.removeOutboundInterceptor(eb2);
-        eb.send("some-address", "aardvark");
-      } else if (msg.body().equals("aardvark")) {
         assertEquals(2, cnt1.get());
-        assertEquals(1, cnt2.get());
+        assertEquals(2, cnt2.get());
         assertEquals(2, cnt3.get());
-        eb.removeOutboundInterceptor(eb3);
+        eb.removeInboundInterceptor(eb2).removeOutboundInterceptor(eb2);
+        eb.send("some-address", "aardvark");
+      } else if (msg.body().equals("aardvark")) {
+        assertEquals(4, cnt1.get());
+        assertEquals(2, cnt2.get());
+        assertEquals(4, cnt3.get());
+        eb.removeInboundInterceptor(eb3).removeOutboundInterceptor(eb3);
         eb.send("some-address", "anteater");
       } else if (msg.body().equals("anteater")) {
-        assertEquals(3, cnt1.get());
-        assertEquals(1, cnt2.get());
-        assertEquals(2, cnt3.get());
+        assertEquals(6, cnt1.get());
+        assertEquals(2, cnt2.get());
+        assertEquals(4, cnt3.get());
         testComplete();
       } else {
         fail("wrong body");