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 bt...@apache.org on 2019/01/11 09:58:20 UTC

[7/8] james-project git commit: MAILBOX-368 Add EventBus Concurrent test for group registration

MAILBOX-368 Add EventBus Concurrent test for group registration


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/79cf1c26
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/79cf1c26
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/79cf1c26

Branch: refs/heads/master
Commit: 79cf1c26bea46d663b294a566488d720cd563d3c
Parents: 9fe9e62
Author: datph <dp...@linagora.com>
Authored: Tue Jan 8 17:14:49 2019 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Fri Jan 11 16:57:08 2019 +0700

----------------------------------------------------------------------
 .../events/EventBusConcurrentTestContract.java  | 109 +++++++++++++++++++
 .../mailbox/events/EventBusTestFixture.java     |   3 +-
 .../mailbox/events/RabbitMQEventBusTest.java    |   6 +-
 3 files changed, 116 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/79cf1c26/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusConcurrentTestContract.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusConcurrentTestContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusConcurrentTestContract.java
new file mode 100644
index 0000000..0746612
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusConcurrentTestContract.java
@@ -0,0 +1,109 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.events;
+
+import static com.jayway.awaitility.Awaitility.await;
+import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT;
+import static org.apache.james.mailbox.events.EventBusTestFixture.NO_KEYS;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.james.util.concurrency.ConcurrentTestRunner;
+import org.junit.jupiter.api.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.jayway.awaitility.core.ConditionFactory;
+
+public interface EventBusConcurrentTestContract {
+
+    Duration FIVE_SECONDS = Duration.ofSeconds(5);
+    ConditionFactory AWAIT_CONDITION = await().timeout(new com.jayway.awaitility.Duration(5, TimeUnit.SECONDS));
+
+    int THREAD_COUNT = 10;
+    int OPERATION_COUNT = 30;
+    int TOTAL_DISPATCH_OPERATIONS = THREAD_COUNT * OPERATION_COUNT;
+
+    static EventBusTestFixture.MailboxListenerCountingSuccessfulExecution newCountingListener() {
+        return new EventBusTestFixture.MailboxListenerCountingSuccessfulExecution();
+    }
+
+    static int totalEventsReceived(ImmutableList<EventBusTestFixture.MailboxListenerCountingSuccessfulExecution> allListeners) {
+        return allListeners.stream()
+            .mapToInt(listener -> listener.numberOfEventCalls())
+            .sum();
+    }
+
+    interface SingleEventBusConcurrentContract extends EventBusContract {
+
+        @Test
+        default void concurrentDispatchGroupShouldDeliverAllEventsToListenersWithSingleEventBus() throws Exception {
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener1 = newCountingListener();
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener2 = newCountingListener();
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener3 = newCountingListener();
+
+            eventBus().register(countingListener1, new EventBusTestFixture.GroupA());
+            eventBus().register(countingListener2, new EventBusTestFixture.GroupB());
+            eventBus().register(countingListener3, new EventBusTestFixture.GroupC());
+            int totalGlobalRegistrations = 3; // GroupA + GroupB + GroupC
+
+            ConcurrentTestRunner.builder()
+                .operation((threadNumber, operationNumber) -> eventBus().dispatch(EVENT, NO_KEYS))
+                .threadCount(THREAD_COUNT)
+                .operationCount(OPERATION_COUNT)
+                .runSuccessfullyWithin(FIVE_SECONDS);
+
+            AWAIT_CONDITION.until(() -> assertThat(totalEventsReceived(ImmutableList
+                    .of(countingListener1, countingListener2, countingListener3)))
+                .isEqualTo(totalGlobalRegistrations * TOTAL_DISPATCH_OPERATIONS));
+        }
+    }
+
+    interface MultiEventBusConcurrentContract extends EventBusContract.MultipleEventBusContract {
+
+        @Test
+        default void concurrentDispatchGroupShouldDeliverAllEventsToListenersWithMultipleEventBus() throws Exception {
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener1 = newCountingListener();
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener2 = newCountingListener();
+            EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener3 = newCountingListener();
+
+            eventBus().register(countingListener1, new EventBusTestFixture.GroupA());
+            eventBus().register(countingListener2, new EventBusTestFixture.GroupB());
+            eventBus().register(countingListener3, new EventBusTestFixture.GroupC());
+
+            eventBus2().register(countingListener1, new EventBusTestFixture.GroupA());
+            eventBus2().register(countingListener2, new EventBusTestFixture.GroupB());
+            eventBus2().register(countingListener3, new EventBusTestFixture.GroupC());
+
+            int totalGlobalRegistrations = 3; // GroupA + GroupB + GroupC
+
+            ConcurrentTestRunner.builder()
+                .operation((threadNumber, operationNumber) -> eventBus().dispatch(EVENT, NO_KEYS))
+                .threadCount(THREAD_COUNT)
+                .operationCount(OPERATION_COUNT)
+                .runSuccessfullyWithin(FIVE_SECONDS);
+
+            AWAIT_CONDITION.until(() -> assertThat(totalEventsReceived(ImmutableList
+                    .of(countingListener1, countingListener2, countingListener3)))
+                .isEqualTo(totalGlobalRegistrations * TOTAL_DISPATCH_OPERATIONS));
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/79cf1c26/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java
----------------------------------------------------------------------
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 ac6a883..5d87c89 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
@@ -61,6 +61,7 @@ public interface EventBusTestFixture {
 
     class GroupA extends Group {}
     class GroupB extends Group {}
+    class GroupC extends Group {}
 
     MailboxListener.MailboxEvent EVENT = new MailboxListener.MailboxAdded(
         MailboxSession.SessionId.of(42),
@@ -76,7 +77,7 @@ public interface EventBusTestFixture {
     ImmutableSet<RegistrationKey> NO_KEYS = ImmutableSet.of();
     MailboxIdRegistrationKey KEY_1 = new MailboxIdRegistrationKey(ID_1);
     MailboxIdRegistrationKey KEY_2 = new MailboxIdRegistrationKey(ID_2);
-    List<Class<? extends Group>> ALL_GROUPS = ImmutableList.of(GroupA.class, GroupB.class);
+    List<Class<? extends Group>> ALL_GROUPS = ImmutableList.of(GroupA.class, GroupB.class, GroupC.class);
 
     ConditionFactory WAIT_CONDITION = await().timeout(com.jayway.awaitility.Duration.ONE_SECOND);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/79cf1c26/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java
----------------------------------------------------------------------
diff --git a/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java
index 8f3ee9d..b8d5cee 100644
--- a/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java
+++ b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java
@@ -49,6 +49,7 @@ import com.rabbitmq.client.Connection;
 
 import reactor.core.publisher.Mono;
 import reactor.rabbitmq.BindingSpecification;
+import reactor.rabbitmq.ExchangeSpecification;
 import reactor.rabbitmq.QueueSpecification;
 import reactor.rabbitmq.RabbitFlux;
 import reactor.rabbitmq.Receiver;
@@ -56,7 +57,8 @@ import reactor.rabbitmq.ReceiverOptions;
 import reactor.rabbitmq.Sender;
 import reactor.rabbitmq.SenderOptions;
 
-class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract, GroupContract.MultipleEventBusGroupContract {
+class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract, GroupContract.MultipleEventBusGroupContract,
+    EventBusConcurrentTestContract.SingleEventBusConcurrentContract, EventBusConcurrentTestContract.MultiEventBusConcurrentContract {
 
     @RegisterExtension
     static RabbitMQExtension rabbitMQExtension = new RabbitMQExtension();
@@ -89,6 +91,8 @@ class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract,
         ALL_GROUPS.stream()
             .map(groupClass -> GroupRegistration.WorkQueueName.of(groupClass).asString())
             .forEach(queueName -> sender.delete(QueueSpecification.queue(queueName)).block());
+        sender.delete(ExchangeSpecification.exchange(MAILBOX_EVENT_EXCHANGE_NAME)).block();
+        sender.close();
     }
 
     @Override


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