Skip to content

Commit

Permalink
#1020 Regression - failure when starting two Karaf containers [PAXEXA…
Browse files Browse the repository at this point in the history
…M-940]

use free port finding method from Sling's TestSupport
  • Loading branch information
oliverlietz committed Aug 13, 2023
1 parent c663046 commit 3e25d53
Showing 1 changed file with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,25 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.URL;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
Expand Down Expand Up @@ -121,6 +129,15 @@ public KarafTestContainer(ExamSystem system,
this.runner = runner;
}

@SuppressWarnings({"java:S112", "checkstyle:IllegalCatch"})
private static int findFreePort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public synchronized TestContainer start() {
try {
Expand All @@ -131,7 +148,9 @@ public synchronized TestContainer start() {
//registry.selectGracefully();
String host = InetAddress.getLoopbackAddress().getHostAddress();
System.setProperty("java.rmi.server.hostname", host);
int port = openRegistryOnFreePort(host, 21000, 21099);
int port = findFreePort();
LOGGER.info("Creating RMI registry server on {}:{}", host, port);
registry = LocateRegistry.createRegistry(port);

ExamSystem subsystem = system
.fork(options(
Expand Down Expand Up @@ -221,27 +240,6 @@ private void restoreConfigFiles() {
}
}

private int openRegistryOnFreePort(String host, int minPort, int maxPort) throws RemoteException {
for (int port = minPort; port <= maxPort; port++) {
try {
LOGGER.trace("Creating RMI registry server on {}:{}", host, port);
registry = LocateRegistry.createRegistry(port);
LOGGER.info("Created RMI registry server on {}:{}", host, port);
return port;
} catch (RemoteException ex) {
if (ex.detail instanceof BindException) {
LOGGER.trace("Tried to open RMI registry on {}: {} but failed.", host, port, ex);
if (port >= maxPort) {
throw ex;
}
} else {
throw ex;
}
}
}
throw new IllegalStateException("Could not open RMI registry");
}

private boolean shouldInjectJUnitBundles(ExamSystem _system) {
Option[] options = _system.getOptions(OverrideJUnitBundlesOption.class);
LOGGER.info("Found {} options when requesting OverrideJUnitBundlesOption.class", options.length);
Expand Down

0 comments on commit 3e25d53

Please sign in to comment.