You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/08/30 07:30:56 UTC

[james-project] branch master updated: [BUILD] Reuse FakeSMTP container port 25 (#1163)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e8ac120197 [BUILD] Reuse FakeSMTP container port 25 (#1163)
e8ac120197 is described below

commit e8ac120197ce95acbbe8ad0b36eaea9b16b08db2
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Aug 30 14:30:51 2022 +0700

    [BUILD] Reuse FakeSMTP container port 25 (#1163)
    
    With this, I run mailet integration tests in 2 min locally
---
 .../main/java/org/apache/james/utils/FakeSmtp.java | 44 ++++++++++++++--------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java b/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java
index 8c598f6156..3108395c7c 100644
--- a/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java
+++ b/server/testing/src/main/java/org/apache/james/utils/FakeSmtp.java
@@ -25,6 +25,7 @@ import static io.restassured.config.RestAssuredConfig.newConfig;
 
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
+import java.util.Optional;
 import java.util.UUID;
 import java.util.function.Consumer;
 
@@ -32,9 +33,7 @@ import org.apache.james.util.docker.DockerContainer;
 import org.apache.james.util.docker.Images;
 import org.apache.james.util.docker.RateLimiters;
 import org.awaitility.Awaitility;
-import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
@@ -48,7 +47,7 @@ import io.restassured.response.ValidatableResponse;
 import io.restassured.specification.RequestSpecification;
 import io.restassured.specification.ResponseSpecification;
 
-public class FakeSmtp implements TestRule, BeforeAllCallback, AfterAllCallback, AfterEachCallback {
+public class FakeSmtp implements TestRule, AfterEachCallback {
     public static void clean(RequestSpecification requestSpecification) {
         given(requestSpecification, RESPONSE_SPECIFICATION)
             .get("/api/email")
@@ -62,13 +61,34 @@ public class FakeSmtp implements TestRule, BeforeAllCallback, AfterAllCallback,
     }
 
     public static FakeSmtp withSmtpPort(Integer smtpPort) {
-        DockerContainer container = fakeSmtpContainer()
-            .withExposedPorts(smtpPort)
-            .withCommands("node", "cli", "--listen", "80", "--smtp", smtpPort.toString());
+        DockerContainer container = reuseContainerIfPossible(smtpPort);
+
+        if (!container.isRunning()) {
+            container.start();
+        }
 
         return new FakeSmtp(container);
     }
 
+    private static DockerContainer reuseContainerIfPossible(Integer smtpPort) {
+        if (smtpPort == SMTP_PORT) {
+            return Optional.ofNullable(DEFAULT_PORT_25)
+                .orElseGet(() -> {
+                    DockerContainer newContainer = createContainer(smtpPort);
+                    DEFAULT_PORT_25 = newContainer;
+                    return newContainer;
+                });
+        } else {
+            return createContainer(smtpPort);
+        }
+    }
+
+    private static DockerContainer createContainer(Integer smtpPort) {
+        return fakeSmtpContainer()
+            .withExposedPorts(smtpPort)
+            .withCommands("node", "cli", "--listen", "80", "--smtp", smtpPort.toString());
+    }
+
     public static FakeSmtp withDefaultPort() {
         return withSmtpPort(SMTP_PORT);
     }
@@ -82,6 +102,8 @@ public class FakeSmtp implements TestRule, BeforeAllCallback, AfterAllCallback,
             .withName("james-testing-fake-smtp" + UUID.randomUUID());
     }
 
+    private static DockerContainer DEFAULT_PORT_25;
+
     private static final int SMTP_PORT = 25;
     public static final ResponseSpecification RESPONSE_SPECIFICATION = new ResponseSpecBuilder().build();
     private final DockerContainer container;
@@ -95,21 +117,11 @@ public class FakeSmtp implements TestRule, BeforeAllCallback, AfterAllCallback,
         return container.apply(statement, description);
     }
 
-    @Override
-    public void afterAll(ExtensionContext extensionContext) {
-        container.stop();
-    }
-
     @Override
     public void afterEach(ExtensionContext extensionContext) {
         clean();
     }
 
-    @Override
-    public void beforeAll(ExtensionContext extensionContext) {
-        container.start();
-    }
-
     public void assertEmailReceived(Consumer<ValidatableResponse> expectations) {
         Awaitility.await().atMost(Duration.ofMinutes(2))
             .untilAsserted(() -> expectations.accept(


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