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/24 03:03:58 UTC

[7/8] james-project git commit: MAILBOX-374 DockerRabbitMQ should be able to provide a connection factory

MAILBOX-374 DockerRabbitMQ should be able to provide a connection factory

This avoids code duplication between JUNIT 4 & JUNIT 5


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

Branch: refs/heads/master
Commit: 2c84e321d681e848cdf666b72e64e56252b1b9ca
Parents: dc25108
Author: Benoit Tellier <bt...@linagora.com>
Authored: Tue Jan 22 16:48:36 2019 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Thu Jan 24 09:46:04 2019 +0700

----------------------------------------------------------------------
 .../james/backend/rabbitmq/DockerRabbitMQ.java  | 23 +++++++
 .../backend/rabbitmq/RabbitMQExtension.java     | 22 +-----
 .../host/RabbitMQEventBusHostSystem.java        | 72 ++++++--------------
 3 files changed, 46 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2c84e321/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
index 10f36ab..81567c7 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
@@ -18,13 +18,18 @@
  ****************************************************************/
 package org.apache.james.backend.rabbitmq;
 
+import static org.apache.james.backend.rabbitmq.RabbitMQFixture.DEFAULT_MANAGEMENT_CREDENTIAL;
+
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.time.Duration;
 import java.util.Optional;
 import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
 import org.apache.http.client.utils.URIBuilder;
+import org.apache.james.util.concurrent.NamedThreadFactory;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.RateLimiters;
 import org.slf4j.Logger;
@@ -38,12 +43,15 @@ import org.testcontainers.containers.wait.strategy.WaitStrategy;
 
 import com.github.fge.lambdas.consumers.ThrowingConsumer;
 import com.google.common.collect.ImmutableMap;
+import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
 import com.rabbitmq.client.Address;
 import com.rabbitmq.client.ConnectionFactory;
 
 public class DockerRabbitMQ {
     private static final Logger LOGGER = LoggerFactory.getLogger(DockerRabbitMQ.class);
 
+    private static final int MAX_THREE_RETRIES = 3;
+    private static final int MIN_DELAY_OF_ONE_HUNDRED_MILLISECONDS = 100;
     private static final String DEFAULT_RABBIT_HOST_NAME_PREFIX = "my-rabbit";
     private static final String DEFAULT_RABBIT_NODE_NAME_PREFIX = "rabbit";
     private static final int DEFAULT_RABBITMQ_PORT = 5672;
@@ -227,4 +235,19 @@ public class DockerRabbitMQ {
             actionPerform.accept(this);
         }
     }
+
+    public RabbitMQConnectionFactory createRabbitConnectionFactory() throws URISyntaxException {
+        RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder()
+            .amqpUri(amqpUri())
+            .managementUri(managementUri())
+            .managementCredentials(DEFAULT_MANAGEMENT_CREDENTIAL)
+            .maxRetries(MAX_THREE_RETRIES)
+            .minDelay(MIN_DELAY_OF_ONE_HUNDRED_MILLISECONDS)
+            .build();
+
+        ThreadFactory threadFactory = NamedThreadFactory.withClassName(getClass());
+        return new RabbitMQConnectionFactory(
+            rabbitMQConfiguration,
+            new AsyncRetryExecutor(Executors.newSingleThreadScheduledExecutor(threadFactory)));
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/2c84e321/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQExtension.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQExtension.java b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQExtension.java
index 47e6488..4e3f388 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQExtension.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQExtension.java
@@ -21,10 +21,7 @@ package org.apache.james.backend.rabbitmq;
 import static org.apache.james.backend.rabbitmq.RabbitMQFixture.DEFAULT_MANAGEMENT_CREDENTIAL;
 
 import java.net.URISyntaxException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
 
-import org.apache.james.util.concurrent.NamedThreadFactory;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
@@ -34,13 +31,7 @@ import org.junit.jupiter.api.extension.ParameterContext;
 import org.junit.jupiter.api.extension.ParameterResolutionException;
 import org.junit.jupiter.api.extension.ParameterResolver;
 
-import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
-
 public class RabbitMQExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback, ParameterResolver {
-
-    private static final int THREE_RETRIES = 3;
-    private static final int ONE_HUNDRED_MILLISECONDS = 100;
-
     private DockerRabbitMQ rabbitMQ;
     private SimpleChannelPool simpleChannelPool;
     private RabbitMQConnectionFactory connectionFactory;
@@ -98,17 +89,6 @@ public class RabbitMQExtension implements BeforeAllCallback, BeforeEachCallback,
     }
 
     private RabbitMQConnectionFactory createRabbitConnectionFactory() throws URISyntaxException {
-        RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder()
-            .amqpUri(rabbitMQ.amqpUri())
-            .managementUri(rabbitMQ.managementUri())
-            .managementCredentials(DEFAULT_MANAGEMENT_CREDENTIAL)
-            .maxRetries(THREE_RETRIES)
-            .minDelay(ONE_HUNDRED_MILLISECONDS)
-            .build();
-
-        ThreadFactory threadFactory = NamedThreadFactory.withClassName(getClass());
-        return new RabbitMQConnectionFactory(
-            rabbitMQConfiguration,
-            new AsyncRetryExecutor(Executors.newSingleThreadScheduledExecutor(threadFactory)));
+        return rabbitMQ.createRabbitConnectionFactory();
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/2c84e321/mpt/impl/imap-mailbox/rabbitmq/src/test/java/org/apache/james/mpt/imapmailbox/rabbitmq/host/RabbitMQEventBusHostSystem.java
----------------------------------------------------------------------
diff --git a/mpt/impl/imap-mailbox/rabbitmq/src/test/java/org/apache/james/mpt/imapmailbox/rabbitmq/host/RabbitMQEventBusHostSystem.java b/mpt/impl/imap-mailbox/rabbitmq/src/test/java/org/apache/james/mpt/imapmailbox/rabbitmq/host/RabbitMQEventBusHostSystem.java
index e6b4760..14b8592 100644
--- a/mpt/impl/imap-mailbox/rabbitmq/src/test/java/org/apache/james/mpt/imapmailbox/rabbitmq/host/RabbitMQEventBusHostSystem.java
+++ b/mpt/impl/imap-mailbox/rabbitmq/src/test/java/org/apache/james/mpt/imapmailbox/rabbitmq/host/RabbitMQEventBusHostSystem.java
@@ -20,15 +20,10 @@
 
 package org.apache.james.mpt.imapmailbox.rabbitmq.host;
 
-import static org.apache.james.backend.rabbitmq.RabbitMQFixture.DEFAULT_MANAGEMENT_CREDENTIAL;
-
 import java.net.URISyntaxException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.james.backend.rabbitmq.DockerRabbitMQ;
-import org.apache.james.backend.rabbitmq.RabbitMQConfiguration;
 import org.apache.james.backend.rabbitmq.RabbitMQConnectionFactory;
 import org.apache.james.core.quota.QuotaCount;
 import org.apache.james.core.quota.QuotaSize;
@@ -46,19 +41,16 @@ import org.apache.james.mailbox.events.RoutingKeyConverter;
 import org.apache.james.mailbox.inmemory.InMemoryId;
 import org.apache.james.mailbox.inmemory.InMemoryMessageId;
 import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
-import org.apache.james.mailbox.store.StoreMailboxManager;
 import org.apache.james.mailbox.store.StoreSubscriptionManager;
+import org.apache.james.metrics.api.NoopMetricFactory;
 import org.apache.james.metrics.logger.DefaultMetricFactory;
 import org.apache.james.mpt.api.ImapFeatures;
 import org.apache.james.mpt.api.ImapFeatures.Feature;
 import org.apache.james.mpt.host.JamesImapHostSystem;
-import org.apache.james.util.concurrent.NamedThreadFactory;
 
 import com.google.common.collect.ImmutableSet;
-import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
 
 public class RabbitMQEventBusHostSystem extends JamesImapHostSystem {
-
     private static final ImapFeatures SUPPORTED_FEATURES = ImapFeatures.of(Feature.NAMESPACE_SUPPORT,
         Feature.MOVE_SUPPORT,
         Feature.USER_FLAGS_SUPPORT,
@@ -66,16 +58,11 @@ public class RabbitMQEventBusHostSystem extends JamesImapHostSystem {
         Feature.ANNOTATION_SUPPORT,
         Feature.MOD_SEQ_SEARCH);
 
-
-    private static final int THREE_RETRIES = 3;
-    private static final int ONE_HUNDRED_MILLISECONDS = 100;
-
     private final DockerRabbitMQ dockerRabbitMQ;
-    private StoreMailboxManager mailboxManager;
     private RabbitMQEventBus eventBus;
-    private InMemoryIntegrationResources integrationResources;
+    private InMemoryIntegrationResources.Resources resources;
 
-    public RabbitMQEventBusHostSystem(DockerRabbitMQ dockerRabbitMQ) {
+    RabbitMQEventBusHostSystem(DockerRabbitMQ dockerRabbitMQ) {
         this.dockerRabbitMQ = dockerRabbitMQ;
     }
 
@@ -83,45 +70,34 @@ public class RabbitMQEventBusHostSystem extends JamesImapHostSystem {
     public void beforeTest() throws Exception {
         super.beforeTest();
 
-        InMemoryMessageId.Factory messageIdFactory = new InMemoryMessageId.Factory();
-        InMemoryId.Factory mailboxIdFactory = new InMemoryId.Factory();
-        EventSerializer eventSerializer = new EventSerializer(mailboxIdFactory, messageIdFactory);
-        RoutingKeyConverter routingKeyConverter = new RoutingKeyConverter(ImmutableSet.of(new MailboxIdRegistrationKey.Factory(mailboxIdFactory)));
-        RabbitMQConnectionFactory rabbitConnectionFactory = createRabbitConnectionFactory();
-        eventBus = new RabbitMQEventBus(rabbitConnectionFactory, eventSerializer, RetryBackoffConfiguration.DEFAULT, routingKeyConverter, new MemoryEventDeadLetters());
+        eventBus = createEventBus();
         eventBus.start();
 
-        integrationResources = new InMemoryIntegrationResources();
-        mailboxManager = integrationResources.createResources(eventBus, authenticator, authorizator).getMailboxManager();
-
+        InMemoryIntegrationResources integrationResources = new InMemoryIntegrationResources();
+        resources = integrationResources.createResources(eventBus, authenticator, authorizator);
 
         ImapProcessor defaultImapProcessorFactory =
             DefaultImapProcessorFactory.createDefaultProcessor(
-                mailboxManager,
+                resources.getMailboxManager(),
                 eventBus,
-                new StoreSubscriptionManager(mailboxManager.getMapperFactory()),
-                integrationResources.retrieveQuotaManager(mailboxManager),
-                integrationResources.retrieveQuotaRootResolver(mailboxManager),
+                new StoreSubscriptionManager(resources.getMailboxManager().getMapperFactory()),
+                integrationResources.retrieveQuotaManager(resources.getMailboxManager()),
+                resources.getDefaultUserQuotaRootResolver(),
                 new DefaultMetricFactory());
+
         configure(new DefaultImapDecoderFactory().buildImapDecoder(),
             new DefaultImapEncoderFactory().buildImapEncoder(),
             defaultImapProcessorFactory);
     }
 
-
-    private RabbitMQConnectionFactory createRabbitConnectionFactory() throws URISyntaxException {
-        RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder()
-            .amqpUri(dockerRabbitMQ.amqpUri())
-            .managementUri(dockerRabbitMQ.managementUri())
-            .managementCredentials(DEFAULT_MANAGEMENT_CREDENTIAL)
-            .maxRetries(THREE_RETRIES)
-            .minDelay(ONE_HUNDRED_MILLISECONDS)
-            .build();
-
-        ThreadFactory threadFactory = NamedThreadFactory.withClassName(getClass());
-        return new RabbitMQConnectionFactory(
-            rabbitMQConfiguration,
-            new AsyncRetryExecutor(Executors.newSingleThreadScheduledExecutor(threadFactory)));
+    private RabbitMQEventBus createEventBus() throws URISyntaxException {
+        InMemoryMessageId.Factory messageIdFactory = new InMemoryMessageId.Factory();
+        InMemoryId.Factory mailboxIdFactory = new InMemoryId.Factory();
+        EventSerializer eventSerializer = new EventSerializer(mailboxIdFactory, messageIdFactory);
+        RoutingKeyConverter routingKeyConverter = new RoutingKeyConverter(ImmutableSet.of(new MailboxIdRegistrationKey.Factory(mailboxIdFactory)));
+        RabbitMQConnectionFactory rabbitConnectionFactory = dockerRabbitMQ.createRabbitConnectionFactory();
+        return new RabbitMQEventBus(rabbitConnectionFactory, eventSerializer, RetryBackoffConfiguration.DEFAULT,
+            routingKeyConverter, new MemoryEventDeadLetters(), new NoopMetricFactory());
     }
 
     @Override
@@ -131,7 +107,7 @@ public class RabbitMQEventBusHostSystem extends JamesImapHostSystem {
 
     @Override
     protected MailboxManager getMailboxManager() {
-        return mailboxManager;
+        return resources.getMailboxManager();
     }
 
     @Override
@@ -141,12 +117,8 @@ public class RabbitMQEventBusHostSystem extends JamesImapHostSystem {
 
     @Override
     public void setQuotaLimits(QuotaCount maxMessageQuota, QuotaSize maxStorageQuota) {
-        try {
-            integrationResources.retrieveMaxQuotaManager(mailboxManager).setGlobalMaxMessage(maxMessageQuota);
-            integrationResources.retrieveMaxQuotaManager(mailboxManager).setGlobalMaxStorage(maxStorageQuota);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+        resources.getMaxQuotaManager().setGlobalMaxMessage(maxMessageQuota);
+        resources.getMaxQuotaManager().setGlobalMaxStorage(maxStorageQuota);
     }
 
     @Override


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