Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(webapps): retry test server start when port bound #3398

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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