mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
04.49.2016 a18241e1d91b5212dc8e4fb9d41d9a2d6c0baa40
Fix unit test failures due to ports already in use

3 changes:

* avoid using ephemeral ports when allocating ports for listeners since
they may be immediately used as the local port in subsequent uses of
client tools. For example, in the following test code there is a risk
that dsconfig will re-use the replicationServerPort as its local port:

// Allocate random port and release it
replicationServerPort = TestCaseUtils.findFreePort();

// configure replication server to listen on replicationServerPort
TestCaseUtils.dsconfig(...);

* during server startup ensure that the LDAP, LDAPS, JMX, and admin
ports are unique

* avoid starting the server during TestNG initialization by moving
the startServer() call in the monitor test case constructor to
a @BeforeClass method.
2 files modified
60 ■■■■■ changed files
opendj-server-legacy/src/test/java/org/opends/server/TestCaseUtils.java 33 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/monitors/GenericMonitorTestCase.java 27 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/TestCaseUtils.java
@@ -34,6 +34,7 @@
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.net.BindException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
@@ -421,10 +422,11 @@
      // Find some free ports for the listeners and write them to the
      // config-chamges.ldif file.
      serverLdapPort = getFreePort(PROPERTY_LDAP_PORT);
      serverAdminPort = getFreePort(PROPERTY_ADMIN_PORT);
      serverJmxPort = findFreePort();
      serverLdapsPort = findFreePort();
      final int[] ports = findFreePorts(4);
      serverLdapPort = getFreePort(PROPERTY_LDAP_PORT, ports[0]);
      serverAdminPort = getFreePort(PROPERTY_ADMIN_PORT, ports[1]);
      serverJmxPort = ports[2];
      serverLdapsPort = ports[3];
      String defaultConfigChangeFile = testResourceDir + File.separator
          + "config-changes.ldif";
@@ -497,12 +499,12 @@
    }
  }
  private static int getFreePort(String portPropertyName) throws IOException
  private static int getFreePort(String portPropertyName, int defaultPort) throws IOException
  {
    String port = System.getProperty(portPropertyName);
    if (port == null)
    {
      return findFreePort();
      return defaultPort;
    }
    int portNb = Integer.parseInt(port);
    // Check this port is free
@@ -657,14 +659,29 @@
  }
  /**
   * Find and binds to a free server socket port on the local host.
   * Find and binds to a free server socket port on the local host. Avoid allocating ephemeral ports since these may
   * be used by client applications such as dsconfig. Instead scan through ports starting from a reasonably high number
   * which avoids most reserved services (see /etc/services) and continues up to the beginning of the ephemeral port
   * range. On most Linux OSes this is 32768, but may be higher.
   *
   * @return the bounded Server socket.
   *
   * @throws IOException in case of underlying exception.
   */
  public static ServerSocket bindFreePort() throws IOException
  {
    return bindPort(0);
    for (int port = 10000; port < 32768; port++)
    {
      try
      {
        return bindPort(port);
      }
      catch (BindException e)
      {
        // Try next port.
      }
    }
    throw new BindException("Unable to bind to a free port");
  }
  /**
opendj-server-legacy/src/test/java/org/opends/server/monitors/GenericMonitorTestCase.java
@@ -18,6 +18,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
@@ -38,33 +39,25 @@
public abstract class GenericMonitorTestCase
       extends MonitorTestCase
{
  /** The configuration entry for this test case. */
  private final String configEntryName;
  protected Entry configEntry;
  GenericMonitorTestCase(String dnString)
  {
    configEntryName = dnString;
  }
  /**
   * Creates a new instance of this monitor test case.
   *
   * @param  dnString  The DN of the configuration entry for this test case, or
   *                   <CODE>null</CODE> if there is none.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  protected GenericMonitorTestCase(String dnString)
            throws Exception
  @BeforeClass
  public void beforeClass() throws Exception
  {
    TestCaseUtils.startServer();
    if (dnString != null)
    if (configEntryName != null)
    {
      configEntry = DirectoryServer.getEntry(DN.valueOf(dnString));
      configEntry = DirectoryServer.getEntry(DN.valueOf(configEntryName));
      assertNotNull(configEntry);
    }
  }
  /**
   * Retrieves an initialized instance of the associated monitor provider.
   *