You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by km...@apache.org on 2009/06/25 00:23:10 UTC
svn commit: r788198 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/tests/derbynet/ServerPropertiesTest.java
functionTests/tests/replicationTests/ReplicationRun.java
junit/TestConfiguration.java
Author: kmarsden
Date: Wed Jun 24 22:23:09 2009
New Revision: 788198
URL: http://svn.apache.org/viewvc?rev=788198&view=rev
Log:
DERBY-4217 Make the default port for the suites.All run configurable with a system property.
Contributed by Tiago R. Espinha (tiago at espinhas dot net)
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.java?rev=788198&r1=788197&r2=788198&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ServerPropertiesTest.java Wed Jun 24 22:23:09 2009
@@ -23,8 +23,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.net.InetAddress;
import java.security.AccessController;
import java.sql.SQLException;
import java.util.Enumeration;
@@ -42,7 +40,6 @@
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.SupportFilesSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
-import org.apache.derbyTesting.junit.Utilities;
/**
* This test tests the derby.properties, system properties and command line
@@ -246,48 +243,6 @@
p = null;
}
- public int getAlternativePort() throws SQLException {
-
- Exception failException = null;
- // start with the default port + 1
- // there may be a smarter way to get the starting point...
-
- int possiblePort = basePort + 1;
- try {
- boolean portOK = false;
- while (!portOK) {
- // check for first one in use
- NetworkServerControl networkServer =
- new NetworkServerControl(InetAddress.getByName("localhost"), possiblePort);
- // Ping and wait for the network server to reply
- boolean started = false;
-
- try {
- networkServer.ping();
- // If ping throws no exception the server is running
- started = true;
- } catch(Exception e) {
- failException = e;
- }
- // Check if we got a reply on ping
- if (!started) {
- // we'll assume we can use this port.
- // If there was some other problem with the pinging, it'll
- // become clear when someone attempts to use the port
- portOK = true;
- }
- else { // this port's in use.
- possiblePort = possiblePort + 1;
- }
- }
- } catch (Exception e) {
- SQLException se = new SQLException("Error pinging network server");
- se.initCause(failException);
- throw se;
- }
- return possiblePort;
- }
-
/**
* Ping for the server to be up - or down.
* @param port port number to be used in the ping
@@ -422,7 +377,7 @@
checkWhetherNeedToShutdown(new int[] {basePort}, actionResult);
// set derby.drda.portNumber to an alternate number in derby.properties
- int firstAlternatePort = getAlternativePort();
+ int firstAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
final Properties derbyProperties = new Properties();
derbyProperties.put("derby.drda.portNumber",
new Integer(firstAlternatePort).toString());
@@ -452,7 +407,7 @@
actionResult = startServer(firstAlternatePort, false);
checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort}, actionResult);
- final int secondAlternatePort = getAlternativePort();
+ final int secondAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
// Now set system properties.
setSystemProperty("derby.drda.portNumber",
new Integer(secondAlternatePort).toString());
@@ -464,13 +419,13 @@
// Note that we didn't unset the system property yet, nor did
// we get rid of derby.properties...
// command line parameter should take hold
- int thirdAlternatePort = getAlternativePort();
+ int thirdAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
actionResult = startServer(thirdAlternatePort, true);
checkWhetherNeedToShutdown(new int[] {basePort, firstAlternatePort, secondAlternatePort,
thirdAlternatePort}, actionResult);
// now with -p.
- int fourthAlternatePort = getAlternativePort();
+ int fourthAlternatePort = TestConfiguration.getCurrent().getNextAvailablePort();
String[] commandArray = {"-Dderby.system.home=" + derbyHome,
"org.apache.derby.drda.NetworkServerControl", "-p",
String.valueOf(fourthAlternatePort).toString(),
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java?rev=788198&r1=788197&r2=788198&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java Wed Jun 24 22:23:09 2009
@@ -68,9 +68,9 @@
static String masterServerHost = "localhost";
static int masterServerPort = TestConfiguration.getCurrent().getPort(); // .. get current ports..
static String slaveServerHost = "localhost";
- static int slaveServerPort = 3527; // .. ..
+ static int slaveServerPort = TestConfiguration.getCurrent().getNextAvailablePort();; // .. ..
static String testClientHost = "localhost";
- static int slaveReplPort = 6666;
+ static int slaveReplPort = TestConfiguration.getCurrent().getNextAvailablePort();;
static String masterDatabasePath = null;
static String slaveDatabasePath = null;
@@ -2046,16 +2046,13 @@
masterServerHost = "localhost";
util.DEBUG("masterServerHost: " + masterServerHost);
- masterServerPort = TestConfiguration.getCurrent().getPort();
util.DEBUG("masterServerPort: " + masterServerPort);
slaveServerHost = "localhost";
util.DEBUG("slaveServerHost: " + slaveServerHost);
- slaveServerPort = 4527;
util.DEBUG("slaveServerPort: " + slaveServerPort);
- slaveReplPort = 8888;
util.DEBUG("slaveReplPort: " + slaveReplPort);
testClientHost = "localhost";
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?rev=788198&r1=788197&r2=788198&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java Wed Jun 24 22:23:09 2009
@@ -23,7 +23,6 @@
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
-import java.net.InetAddress;
import java.security.*;
import java.sql.Connection;
import java.sql.SQLException;
@@ -70,8 +69,31 @@
private final static String DEFAULT_FRAMEWORK = "embedded";
public final static String DEFAULT_HOSTNAME = "localhost";
public final static String DEFAULT_SSL = "off";
- public final static int DEFAULT_JMX_PORT = 9999; // 9999 is arbitrary
+ /**
+ * Maximum number of ports used by Suites.All
+ * If this changes, this constant and the Wiki
+ * page at http://wiki.apache.org/db-derby/DerbyJUnitTesting
+ * need to be updated.
+ */
+ private final static int MAX_PORTS_USED = 10;
+
+ /** This is the base port. This does NOT change EVER during the run of a suite.
+ * It is set using the property derby.tests.basePort and it is set to default when
+ * a property isn't used. */
+ private final static int basePort;
+ private static int lastAssignedPort;
+ static {
+ String port = BaseTestCase.getSystemProperty("derby.tests.basePort");
+ if (port == null) {
+ lastAssignedPort = DEFAULT_PORT;
+ } else {
+ lastAssignedPort = Integer.parseInt(port);
+ }
+ basePort = lastAssignedPort;
+ }
+ private static int assignedPortCount = 1;
+
public final static String TEST_DBO = "TEST_DBO";
private FileOutputStream serverOutput;
@@ -551,7 +573,7 @@
if (!(Derby.hasClient() && Derby.hasServer())
|| JDBC.vmSupportsJSR169())
return new TestSuite("empty: no network server support");
- int port = getCurrent().getAlternativePort();
+ int port = getCurrent().getNextAvailablePort();
//
// This looks bogus to me. Shouldn't this get the hostname and port
@@ -921,12 +943,7 @@
this.userName = DEFAULT_USER_NAME;
this.userPassword = DEFAULT_USER_PASSWORD;
this.hostName = null;
- String port = BaseTestCase.getSystemProperty("derby.tests.port");
- if (port == null) {
- this.port = DEFAULT_PORT;
- } else {
- this.port = Integer.parseInt(port);
- }
+ this.port = basePort;
this.isVerbose = Boolean.valueOf(
getSystemProperties().getProperty(KEY_VERBOSE)).
booleanValue();
@@ -936,7 +953,7 @@
this.jdbcClient = JDBCClient.getDefaultEmbedded();
this.ssl = null;
- this.jmxPort = DEFAULT_JMX_PORT;
+ this.jmxPort = getNextAvailablePort();
url = createJDBCUrlWithDatabaseName(defaultDbName);
initConnector(null);
@@ -1102,25 +1119,8 @@
hostName = props.getProperty(KEY_HOSTNAME, DEFAULT_HOSTNAME);
isVerbose = Boolean.valueOf(props.getProperty(KEY_VERBOSE)).booleanValue();
doTrace = Boolean.valueOf(props.getProperty(KEY_TRACE)).booleanValue();
- String port = BaseTestCase.getSystemProperty("derby.tests.port");
- if (port == null) {
- this.port = DEFAULT_PORT;
- } else {
- this.port = Integer.parseInt(port);
- }
- String jmxPortStr = props.getProperty(KEY_JMX_PORT);
- if (jmxPortStr != null) {
- try {
- jmxPort = Integer.parseInt(jmxPortStr);
- } catch (NumberFormatException nfe) {
- // We lose stacktrace here, but it is not important.
- throw new NumberFormatException(
- "JMX Port number must be an integer. Value: "
- + jmxPortStr);
- }
- } else {
- jmxPort = DEFAULT_JMX_PORT;
- }
+ port = basePort;
+ jmxPort = getNextAvailablePort();
ssl = props.getProperty(KEY_SSL);
@@ -1292,19 +1292,30 @@
public int getPort() {
return port;
}
+
/**
- * Get an alternative port number for network server.
- *
+ * Get the next available port. This method is multi-purposed.
+ * It can be used for alternative servers and also for JMX and replication.
+ *
* @return port number.
*/
-
- public int getAlternativePort() {
- int possiblePort = getPort();
- if (!(possiblePort > 0))
- possiblePort = 1528;
- else
- possiblePort = getPort() + 1;
- return possiblePort;
+ public int getNextAvailablePort() {
+ /* We want to crash. If you are reading this, you have to increment
+ * the MAX_PORTS_USED constant and to edit the wiki page relative to
+ * concurrent test running */
+ if (assignedPortCount+1 > MAX_PORTS_USED) {
+ Assert.fail("Port "+(lastAssignedPort+1)+" exceeeds expected maximum. " +
+ "You may need to update TestConfiguration.MAX_PORTS_USED and "+
+ "the Wiki page at http://wiki.apache.org/db-derby/DerbyJUnitTesting "+
+ "if test runs now require more available ports");
+ }
+
+ int possiblePort = lastAssignedPort + 1;
+
+ assignedPortCount++;
+
+ lastAssignedPort = possiblePort;
+ return possiblePort;
}
/**