You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/12/30 03:35:28 UTC

[james-project] 16/29: JAMES-3046 Stop WebAdmin server between retries

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

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

commit fa68e4ac01880f956416f97af973555146861de2
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Dec 28 14:26:15 2020 +0700

    JAMES-3046 Stop WebAdmin server between retries
    
    Not doing so is prone to reusing the same port...
---
 .../apache/james/webadmin/WebAdminServerTest.java  | 30 ++++++++-----
 .../org/apache/james/webadmin/WebAdminUtils.java   | 51 ++++++++++++++++------
 .../apache/james/webadmin/WebAdminUtilsTest.java   |  4 +-
 3 files changed, 60 insertions(+), 25 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java
index 633cbd2..b1f31f9 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminServerTest.java
@@ -23,9 +23,14 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.hamcrest.CoreMatchers.is;
 
+import org.apache.james.metrics.tests.RecordingMetricFactory;
 import org.apache.james.util.Port;
+import org.apache.james.webadmin.authentication.NoAuthenticationFilter;
+import org.apache.james.webadmin.mdc.LoggingRequestFilter;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 import io.restassured.RestAssured;
 import spark.Service;
 
@@ -33,7 +38,12 @@ public class WebAdminServerTest {
 
     @Test
     public void getPortShouldThrowWhenNotConfigured() {
-        WebAdminServer server = WebAdminUtils.createWebAdminServer();
+        WebAdminServer server = new WebAdminServer(
+            WebAdminConfiguration.TEST_CONFIGURATION,
+            ImmutableList.of(),
+            new NoAuthenticationFilter(),
+            new RecordingMetricFactory(),
+            LoggingRequestFilter.create());
         assertThatThrownBy(server::getPort)
             .isInstanceOf(IllegalStateException.class);
     }
@@ -52,9 +62,9 @@ public class WebAdminServerTest {
         String firstAnswer = "1";
         String secondAnswer = "2";
         WebAdminServer server = WebAdminUtils.createWebAdminServer(
-            myPrivateRouteWithConstAnswer(firstAnswer),
-            myPrivateRouteWithConstAnswer(secondAnswer));
-        server.start();
+                myPrivateRouteWithConstAnswer(firstAnswer),
+                myPrivateRouteWithConstAnswer(secondAnswer))
+            .start();
 
         try {
             RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(server)
@@ -75,9 +85,9 @@ public class WebAdminServerTest {
         String firstAnswer = "1";
         String secondAnswer = "2";
         WebAdminServer server = WebAdminUtils.createWebAdminServer(
-            myPrivateRouteWithConstAnswer(firstAnswer),
-            myPublicRouteWithConstAnswer(secondAnswer));
-        server.start();
+                myPrivateRouteWithConstAnswer(firstAnswer),
+                myPublicRouteWithConstAnswer(secondAnswer))
+            .start();
 
         try {
             RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(server)
@@ -98,9 +108,9 @@ public class WebAdminServerTest {
         String firstAnswer = "1";
         String secondAnswer = "2";
         WebAdminServer server = WebAdminUtils.createWebAdminServer(
-            myPublicRouteWithConstAnswer(firstAnswer),
-            myPrivateRouteWithConstAnswer(secondAnswer));
-        server.start();
+                myPublicRouteWithConstAnswer(firstAnswer),
+                myPrivateRouteWithConstAnswer(secondAnswer))
+            .start();
 
         try {
             RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(server)
diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
index f63ac3f..f0ea50a 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
@@ -42,31 +42,56 @@ import io.restassured.http.ContentType;
 import io.restassured.specification.RequestSpecification;
 import reactor.core.publisher.Mono;
 import reactor.util.retry.Retry;
+import spark.Service;
 
 public class WebAdminUtils {
-    private static class ConcurrentSafeWebAdminServer extends WebAdminServer {
-        ConcurrentSafeWebAdminServer(WebAdminConfiguration configuration, List<Routes> routesList, AuthenticationFilter authenticationFilter, MetricFactory metricFactory) {
-            super(configuration, routesList, authenticationFilter, metricFactory, LoggingRequestFilter.create());
+    public interface Startable {
+        WebAdminServer start() ;
+    }
+
+    private static class ConcurrentSafeWebAdminServerFactory {
+
+        private final WebAdminConfiguration configuration;
+        private final List<Routes> privateRoutes;
+        private final AuthenticationFilter authenticationFilter;
+        private final MetricFactory metricFactory;
+
+        private ConcurrentSafeWebAdminServerFactory(WebAdminConfiguration configuration, List<Routes> privateRoutes, AuthenticationFilter authenticationFilter, MetricFactory metricFactory) {
+            this.configuration = configuration;
+            this.privateRoutes = privateRoutes;
+            this.authenticationFilter = authenticationFilter;
+            this.metricFactory = metricFactory;
         }
 
         /**
          * JVM-wide synchronized start method to avoid the all too common random port allocation conflict
          * that occurs when parallelly testing webadmin maven modules.
          */
-        @Override
-        public WebAdminServer start() {
-            Mono.fromRunnable(super::start)
-                .retryWhen(Retry.backoff(5, Duration.ofMillis(10)))
+        public Startable createServer() {
+            return () -> Mono.fromCallable(this::createServerSingleTry)
+                .retryWhen(Retry.backoff(10, Duration.ofMillis(10)))
                 .block();
-            return this;
+        }
+
+        public WebAdminServer createServerSingleTry() {
+            WebAdminServer webAdminServer = new WebAdminServer(configuration, privateRoutes, authenticationFilter, metricFactory, LoggingRequestFilter.create());
+            try {
+                return webAdminServer
+                    .start();
+            } catch (Exception e) {
+                webAdminServer.destroy();
+                throw e;
+            }
         }
     }
 
-    public static WebAdminServer createWebAdminServer(Routes... routes) {
-        return new ConcurrentSafeWebAdminServer(WebAdminConfiguration.TEST_CONFIGURATION,
-            ImmutableList.copyOf(routes),
-            new NoAuthenticationFilter(),
-            new RecordingMetricFactory());
+    public static Startable createWebAdminServer(Routes... routes) {
+        return new ConcurrentSafeWebAdminServerFactory(
+                WebAdminConfiguration.TEST_CONFIGURATION,
+                ImmutableList.copyOf(routes),
+                new NoAuthenticationFilter(),
+                new RecordingMetricFactory())
+            .createServer();
     }
 
     public static RequestSpecBuilder buildRequestSpecification(WebAdminServer webAdminServer) {
diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtilsTest.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtilsTest.java
index 3cdfe09..eb6c89b 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtilsTest.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtilsTest.java
@@ -32,8 +32,8 @@ class WebAdminUtilsTest {
     void serverShouldBeAbleToStartConcurrently() throws Exception {
         ConcurrentTestRunner.builder()
             .operation((a, b) -> {
-                WebAdminServer webAdminServer = WebAdminUtils.createWebAdminServer();
-                webAdminServer.start();
+                WebAdminServer webAdminServer = WebAdminUtils.createWebAdminServer()
+                    .start();
                 webAdminServer.destroy();
             })
             .threadCount(10)


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