Skip to content

Commit

Permalink
chore(webapps): retry test server start when port bound (camunda#3398)
Browse files Browse the repository at this point in the history
* 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 camunda#2598
  • Loading branch information
tmetzke authored Jun 9, 2023
1 parent d56030d commit 2de6229
Showing 1 changed file with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
*/
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
*/
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();
Expand All @@ -44,6 +46,7 @@ protected void before() {
}
}

@Override
protected void after() {
try {
server.stop();
Expand All @@ -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);
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2de6229

Please sign in to comment.