db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1303652 - in /db/derby/code/branches/10.7: ./ java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
Date Thu, 22 Mar 2012 04:03:30 GMT
Author: mamta
Date: Thu Mar 22 04:03:30 2012
New Revision: 1303652

URL: http://svn.apache.org/viewvc?rev=1303652&view=rev
Log:
DERBY-4201 SecureServerTest AssertionFailedError: Timed out waiting for network server to
start

Backporting 1210846 to 10.7 codeline


Modified:
    db/derby/code/branches/10.7/   (props changed)
    db/derby/code/branches/10.7/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java

Propchange: db/derby/code/branches/10.7/
------------------------------------------------------------------------------
  Merged /db/derby/code/trunk:r1210846

Modified: db/derby/code/branches/10.7/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.7/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java?rev=1303652&r1=1303651&r2=1303652&view=diff
==============================================================================
--- db/derby/code/branches/10.7/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
(original)
+++ db/derby/code/branches/10.7/java/testing/org/apache/derbyTesting/junit/NetworkServerTestSetup.java
Thu Mar 22 04:03:30 2012
@@ -24,8 +24,8 @@ import java.net.InetAddress;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.PrintWriter;
+import java.net.ServerSocket;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
@@ -33,7 +33,6 @@ import java.security.PrivilegedException
 import java.util.ArrayList;
 import junit.framework.Test;
 import org.apache.derby.drda.NetworkServerControl;
-import org.apache.derbyTesting.junit.BaseTestCase;
 
 /**
  * Test decorator that starts the network server on startup
@@ -184,6 +183,11 @@ final public class NetworkServerTestSetu
 
         if (startServerAtSetup)
         {
+            // DERBY-4201: A network server instance used in an earlier test
+            // case might not have completely shut down and released the server
+            // port yet. Wait here until the port has been released.
+            waitForAvailablePort();
+
             if (useSeparateProcess)
             { spawnedServer = startSeparateProcess(); }
             else if (asCommand)
@@ -208,6 +212,56 @@ final public class NetworkServerTestSetu
         }
     }
 
+    /**
+     * Wait until the server port has been released by server instances used
+     * by earlier test cases, or until the timeout specified by
+     * {@link #getWaitTime()} has elapsed.
+     *
+     * @throws Exception if the port didn't become available before the timeout
+     */
+    private void waitForAvailablePort() throws Exception {
+        TestConfiguration conf = TestConfiguration.getCurrent();
+        InetAddress serverAddress = InetAddress.getByName(conf.getHostName());
+        int port = conf.getPort();
+        long giveUp = System.currentTimeMillis() + getWaitTime();
+
+        while (true) {
+            try {
+                probeServerPort(port, serverAddress);
+                break;
+            } catch (IOException ioe) {
+                if (System.currentTimeMillis() < giveUp) {
+                    Thread.sleep(SLEEP_TIME);
+                } else {
+                    BaseTestCase.fail(
+                        "Timed out waiting for server port to become available",
+                        ioe);
+                }
+            }
+        }
+    }
+
+    /**
+     * Check if a server socket can be opened on the specified port.
+     *
+     * @param port the port to check
+     * @param addr the address of the network interface
+     * @throws IOException if a server socket couldn't be opened
+     */
+    private void probeServerPort(final int port, final InetAddress addr)
+            throws IOException {
+        try {
+            AccessController.doPrivileged(new PrivilegedExceptionAction() {
+                public Object run() throws IOException {
+                    new ServerSocket(port, 0, addr).close();
+                    return null;
+                }
+            });
+        } catch (PrivilegedActionException pae) {
+            throw (IOException) pae.getCause();
+        }
+    }
+
     private void startWithAPI() throws Exception
     {
             



Mime
View raw message