You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/03/29 16:51:01 UTC

[james-project] 03/03: JAMES-2691 add contract test, only an event approved by isHandling method should be received by the listener

This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit e16e9e27ddfa6352e51b0ef36435677f7d4fc2ab
Author: RĂ©mi Kowalski <rk...@linagora.com>
AuthorDate: Mon Mar 25 13:58:36 2019 +0100

    JAMES-2691 add contract test, only an event approved by isHandling method should be received by the listener
---
 .../apache/james/mailbox/events/EventBusTestFixture.java    |  3 +++
 .../java/org/apache/james/mailbox/events/GroupContract.java | 13 +++++++++++++
 .../java/org/apache/james/mailbox/events/KeyContract.java   | 13 +++++++++++++
 .../mailbox/caching/CacheInvalidatingMailboxListener.java   |  2 --
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java
index 3156d00..ed3087b 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java
@@ -20,6 +20,7 @@
 package org.apache.james.mailbox.events;
 
 import static com.jayway.awaitility.Awaitility.await;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -93,6 +94,7 @@ public interface EventBusTestFixture {
     Event.EventId EVENT_ID_2 = Event.EventId.of("5a7a9f3f-5f03-44be-b457-a51e93760645");
     MailboxListener.MailboxEvent EVENT = new MailboxListener.MailboxAdded(SESSION_ID, USER, MAILBOX_PATH, TEST_ID, EVENT_ID);
     MailboxListener.MailboxEvent EVENT_2 = new MailboxListener.MailboxAdded(SESSION_ID, USER, MAILBOX_PATH, TEST_ID, EVENT_ID_2);
+    MailboxListener.MailboxRenamed EVENT_UNSUPPORTED_BY_LISTENER = new MailboxListener.MailboxRenamed(SESSION_ID, USER, MAILBOX_PATH, TEST_ID, MAILBOX_PATH, EVENT_ID_2);
 
     java.time.Duration ONE_SECOND = java.time.Duration.ofSeconds(1);
     java.time.Duration THIRTY_SECONDS = java.time.Duration.ofSeconds(30);
@@ -114,6 +116,7 @@ public interface EventBusTestFixture {
     static MailboxListener newListener() {
         MailboxListener listener = mock(MailboxListener.class);
         when(listener.getExecutionMode()).thenReturn(MailboxListener.ExecutionMode.SYNCHRONOUS);
+        when(listener.isHandling(any(MailboxListener.MailboxAdded.class))).thenReturn(true);
         return listener;
     }
 }
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java
index 2ba6686..f68c6d7 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java
@@ -22,6 +22,7 @@ package org.apache.james.mailbox.events;
 import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT;
 import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT_2;
 import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT_ID;
+import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT_UNSUPPORTED_BY_LISTENER;
 import static org.apache.james.mailbox.events.EventBusTestFixture.FIVE_HUNDRED_MS;
 import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_A;
 import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_B;
@@ -148,6 +149,18 @@ public interface GroupContract {
         }
 
         @Test
+        default void groupListenersShouldReceiveOnlyHandledEvents() throws Exception {
+            MailboxListener listener = newListener();
+
+            eventBus().register(listener, GROUP_A);
+
+            eventBus().dispatch(EVENT_UNSUPPORTED_BY_LISTENER, NO_KEYS).block();
+
+            verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
+                .event(any());
+        }
+
+        @Test
         default void dispatchShouldNotThrowWhenAGroupListenerFails() throws Exception {
             MailboxListener listener = newListener();
             doThrow(new RuntimeException()).when(listener).event(any());
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java
index e007eff..d786dfa 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java
@@ -21,6 +21,7 @@ package org.apache.james.mailbox.events;
 
 import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT;
 import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT_2;
+import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT_UNSUPPORTED_BY_LISTENER;
 import static org.apache.james.mailbox.events.EventBusTestFixture.FIVE_HUNDRED_MS;
 import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_1;
 import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_2;
@@ -96,6 +97,18 @@ public interface KeyContract extends EventBusContract {
                 .event(any());
         }
 
+       @Test
+        default void registeredListenersShouldReceiveOnlyHandledEvents() throws Exception {
+            MailboxListener listener = newListener();
+
+            eventBus().register(listener, KEY_1);
+
+            eventBus().dispatch(EVENT_UNSUPPORTED_BY_LISTENER, KEY_1).block();
+
+            verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
+                .event(any());
+        }
+
         @Test
         default void dispatchShouldNotThrowWhenARegisteredListenerFails() throws Exception {
             MailboxListener listener = newListener();
diff --git a/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CacheInvalidatingMailboxListener.java b/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CacheInvalidatingMailboxListener.java
index 6612d6c..930b86f 100644
--- a/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CacheInvalidatingMailboxListener.java
+++ b/mailbox/caching/src/main/java/org/apache/james/mailbox/caching/CacheInvalidatingMailboxListener.java
@@ -48,9 +48,7 @@ public class CacheInvalidatingMailboxListener implements MailboxListener.GroupMa
 
     @Override
     public void event(Event event) {
-        if (isHandling(event)) {
             mailboxEvent((MailboxEvent) event);
-        }
     }
 
     private void mailboxEvent(MailboxEvent event) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org