From 2de6229a25dfbeb15c6a8e3a961f05d7380fd334 Mon Sep 17 00:00:00 2001 From: Tobias Metzke-Bernstein Date: Fri, 9 Jun 2023 11:49:53 +0200 Subject: [PATCH] chore(webapps): retry test server start when port bound (#3398) * Retries a failed test server start in case the port is still bound. The start will be retried 3 times at maximum. There will be a backoff of half a second between retries. related to https://github.com/camunda/camunda-bpm-platform/issues/2598 --- .../bpm/webapp/impl/util/HeaderRule.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/webapps/assembly/src/test/java/org/camunda/bpm/webapp/impl/util/HeaderRule.java b/webapps/assembly/src/test/java/org/camunda/bpm/webapp/impl/util/HeaderRule.java index 922e71a792a..dae92c726db 100644 --- a/webapps/assembly/src/test/java/org/camunda/bpm/webapp/impl/util/HeaderRule.java +++ b/webapps/assembly/src/test/java/org/camunda/bpm/webapp/impl/util/HeaderRule.java @@ -16,14 +16,14 @@ */ package org.camunda.bpm.webapp.impl.util; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.webapp.WebAppContext; -import org.junit.rules.ExternalResource; - import java.io.IOException; +import java.net.BindException; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.rules.ExternalResource; /** * @author Tassilo Weidner @@ -31,11 +31,13 @@ public class HeaderRule extends ExternalResource { protected static final int SERVER_PORT = 8085; + protected static final int RETRIES = 3; protected Server server = new Server(SERVER_PORT); protected WebAppContext webAppContext = new WebAppContext(); protected HttpURLConnection connection = null; + @Override protected void before() { try { server.stop(); @@ -44,6 +46,7 @@ protected void before() { } } + @Override protected void after() { try { server.stop(); @@ -57,6 +60,10 @@ public void startServer(String webDescriptor, String scope) { } public void startServer(String webDescriptor, String scope, String contextPath) { + startServer(webDescriptor, scope, contextPath, RETRIES); + } + + protected void startServer(String webDescriptor, String scope, String contextPath, int startUpRetries) { webAppContext.setContextPath(contextPath); webAppContext.setResourceBase("/"); webAppContext.setDescriptor("src/test/resources/WEB-INF/" + scope + "/" + webDescriptor); @@ -66,7 +73,15 @@ public void startServer(String webDescriptor, String scope, String contextPath) try { server.start(); } catch (Exception e) { - throw new RuntimeException(e); + if (e.getCause() instanceof BindException && startUpRetries > 0) { + try { + Thread.sleep(500L); + } catch (Exception ex) { + } + startServer(webDescriptor, scope, contextPath, --startUpRetries); + } else { + throw new RuntimeException(e); + } } } @@ -147,11 +162,11 @@ public int getResponseCode() { throw new RuntimeException(e); } } - + public String getSessionCookieRegex(String path, String sameSite, boolean secure) { return getSessionCookieRegex(path, "JSESSIONID", sameSite, secure); } - + public String getSessionCookieRegex(String path, String cookieName, String sameSite, boolean secure) { StringBuilder regex = new StringBuilder(cookieName + "=.*;\\W*Path=/"); if (path != null) {