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 2a949a778dbe3cd907b18717e26841f179210c05 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Tue Dec 17 10:09:21 2019 +0100 [FIX] Retry WebAdmin startup upon BindException This avoids port allocation conflicts --- .../org/apache/james/webadmin/WebAdminServer.java | 3 ++ .../org/apache/james/webadmin/WebAdminUtils.java | 24 ++++++++++++- .../apache/james/webadmin/WebAdminUtilsTest.java | 40 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java index 6706c56..92b28fc 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java @@ -91,6 +91,9 @@ public class WebAdminServer implements Startable { } public WebAdminServer start() { + service.initExceptionHandler(e -> { + throw new RuntimeException(e); + }); if (configuration.isEnabled()) { service.port(configuration.getPort().get().getValue()); configureExceptionHanding(); 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 97de868..5b3d07b 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 @@ -24,9 +24,13 @@ import static io.restassured.config.EncoderConfig.encoderConfig; import static io.restassured.config.RestAssuredConfig.newConfig; import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.List; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.util.Port; +import org.apache.james.webadmin.authentication.AuthenticationFilter; import org.apache.james.webadmin.authentication.NoAuthenticationFilter; import com.google.common.collect.ImmutableList; @@ -35,11 +39,29 @@ import io.restassured.builder.RequestSpecBuilder; import io.restassured.config.RestAssuredConfig; import io.restassured.http.ContentType; import io.restassured.specification.RequestSpecification; +import reactor.core.publisher.Mono; public class WebAdminUtils { + private static class ConcurrentSafeWebAdminServer extends WebAdminServer { + ConcurrentSafeWebAdminServer(WebAdminConfiguration configuration, List<Routes> routesList, AuthenticationFilter authenticationFilter, MetricFactory metricFactory) { + super(configuration, routesList, authenticationFilter, 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) + .retryBackoff(5, Duration.ofMillis(10)) + .block(); + return this; + } + } public static WebAdminServer createWebAdminServer(Routes... routes) { - return new WebAdminServer(WebAdminConfiguration.TEST_CONFIGURATION, + return new ConcurrentSafeWebAdminServer(WebAdminConfiguration.TEST_CONFIGURATION, ImmutableList.copyOf(routes), new NoAuthenticationFilter(), new RecordingMetricFactory()); 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 new file mode 100644 index 0000000..f5e61d6 --- /dev/null +++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtilsTest.java @@ -0,0 +1,40 @@ +/**************************************************************** + * 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.webadmin; + +import java.time.Duration; + +import org.apache.james.util.concurrency.ConcurrentTestRunner; +import org.junit.jupiter.api.Test; + +class WebAdminUtilsTest { + @Test + void serverShouldBeAbletoStartConcurrently() throws Exception { + ConcurrentTestRunner.builder() + .operation((a, b) -> { + WebAdminServer webAdminServer = WebAdminUtils.createWebAdminServer(); + webAdminServer.start(); + webAdminServer.destroy(); + }) + .threadCount(10) + .operationCount(100) + .runSuccessfullyWithin(Duration.ofMinutes(1)); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org