6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
authoralanb
Wed, 16 Jun 2010 14:24:46 +0100
changeset 5798 9cc262cd2a7a
parent 5797 c2641f6791be
child 5799 e0089b2436a4
6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use" Reviewed-by: chegar
jdk/test/ProblemList.txt
jdk/test/java/nio/channels/Selector/ByteServer.java
jdk/test/java/nio/channels/Selector/CloseThenRegister.java
jdk/test/java/nio/channels/Selector/ReadAfterConnect.java
jdk/test/java/nio/channels/Selector/SelectAfterRead.java
jdk/test/java/nio/channels/Selector/SelectWrite.java
jdk/test/java/nio/channels/SocketChannel/BigReadWrite.java
jdk/test/java/nio/channels/SocketChannel/VectorIO.java
jdk/test/java/nio/channels/SocketChannel/Write.java
jdk/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoTest.java
--- a/jdk/test/ProblemList.txt	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/ProblemList.txt	Wed Jun 16 14:24:46 2010 +0100
@@ -740,9 +740,6 @@
 #    at SetLastModified.main(SetLastModified.java:107)
 java/io/File/SetLastModified.java                               generic-all
 
-# Fails on Fedora 9 x86, address in use
-java/nio/channels/Selector/SelectWrite.java			generic-all
-
 # Fails on Windows 2000,  times out
 java/nio/channels/FileChannel/Transfer.java			generic-all
 
@@ -756,9 +753,6 @@
 # Triggers a hotspot crash on Fedora 9 32bit -server and Windows X64  samevm
 sun/nio/cs/TestUTF8.java					generic-all
 
-# Solaris sparc, socket timeout
-java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh generic-all
-
 # Runtime exception on windows X64, samevm mode
 java/nio/channels/Selector/WakeupNow.java			generic-all
 
@@ -818,15 +812,6 @@
 java/nio/channels/SocketChannel/ConnectState.java	 	windows-all
 java/nio/channels/SocketChannel/FinishConnect.java	 	windows-all
 
-# Gets java.net.BindException alot (static port number?)
-java/nio/channels/SocketChannel/VectorIO.java		 	generic-all
-
-# Solaris i586 java.net.BindExceptions
-java/nio/channels/SocketChannel/VectorParams.java	 	solaris-all
-
-# Linux i586 address already in use, samevm issues
-java/nio/channels/SocketChannel/Write.java		 	generic-all
-
 # Fails on all platforms due to overlap of JDK jar file contents:
 sun/nio/cs/Test4200310.sh				 	generic-all
 
--- a/jdk/test/java/nio/channels/Selector/ByteServer.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/Selector/ByteServer.java	Wed Jun 16 14:24:46 2010 +0100
@@ -33,7 +33,6 @@
 
 public class ByteServer {
 
-    public static final int PORT = 31415;
     public static final String LOCALHOST = "localhost";
     private int bytecount;
     private Socket  socket;
@@ -43,7 +42,11 @@
 
     public ByteServer(int bytecount) throws Exception{
         this.bytecount = bytecount;
-        serversocket = new ServerSocket(PORT);
+        serversocket = new ServerSocket(0);
+    }
+
+    public int port() {
+        return serversocket.getLocalPort();
     }
 
     public void start() {
--- a/jdk/test/java/nio/channels/Selector/CloseThenRegister.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/Selector/CloseThenRegister.java	Wed Jun 16 14:24:46 2010 +0100
@@ -32,17 +32,19 @@
 public class CloseThenRegister {
 
     public static void main (String [] args) throws Exception {
+        Selector sel = Selector.open();
+        sel.close();
+        ServerSocketChannel ssc = ServerSocketChannel.open();
         try {
-            Selector s = Selector.open();
-            s.close();
-            ServerSocketChannel c = ServerSocketChannel.open();
-            c.socket().bind(new InetSocketAddress(40000));
-            c.configureBlocking(false);
-            c.register(s, SelectionKey.OP_ACCEPT);
+            ssc.bind(new InetSocketAddress(0));
+            ssc.configureBlocking(false);
+            ssc.register(sel, SelectionKey.OP_ACCEPT);
+            throw new RuntimeException("register after close does not cause CSE!");
         } catch (ClosedSelectorException cse) {
-            return;
+            // expected
+        } finally {
+            ssc.close();
         }
-        throw new RuntimeException("register after close does not cause CSE!");
     }
 
 }
--- a/jdk/test/java/nio/channels/Selector/ReadAfterConnect.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/Selector/ReadAfterConnect.java	Wed Jun 16 14:24:46 2010 +0100
@@ -37,7 +37,7 @@
         ByteServer server = new ByteServer(0); // server: accept connection and do nothing
         server.start();
         InetSocketAddress isa = new InetSocketAddress(
-                InetAddress.getByName(ByteServer.LOCALHOST), ByteServer.PORT);
+                InetAddress.getByName(ByteServer.LOCALHOST), server.port());
         Selector sel = Selector.open();
         SocketChannel sc = SocketChannel.open();
         sc.connect(isa);
--- a/jdk/test/java/nio/channels/Selector/SelectAfterRead.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/Selector/SelectAfterRead.java	Wed Jun 16 14:24:46 2010 +0100
@@ -37,14 +37,14 @@
     final static int TIMEOUT = 1000;
 
     public static void main(String[] argv) throws Exception {
+        InetAddress lh = InetAddress.getByName(ByteServer.LOCALHOST);
+
         // server: accept connection and write one byte
         ByteServer server = new ByteServer(1);
         server.start();
-        InetSocketAddress isa = new InetSocketAddress(
-                InetAddress.getByName(ByteServer.LOCALHOST), ByteServer.PORT);
         Selector sel = Selector.open();
         SocketChannel sc = SocketChannel.open();
-        sc.connect(isa);
+        sc.connect(new InetSocketAddress(lh, server.port()));
         sc.read(ByteBuffer.allocate(1));
         sc.configureBlocking(false);
         sc.register(sel, SelectionKey.OP_READ);
@@ -61,7 +61,7 @@
         server = new ByteServer(2);
         server.start();
         sc = SocketChannel.open();
-        sc.connect(isa);
+        sc.connect(new InetSocketAddress(lh, server.port()));
         sc.configureBlocking(false);
         sel = Selector.open();
         sc.register(sel, SelectionKey.OP_READ);
--- a/jdk/test/java/nio/channels/Selector/SelectWrite.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/Selector/SelectWrite.java	Wed Jun 16 14:24:46 2010 +0100
@@ -39,7 +39,7 @@
         // server: accept connection and do nothing
         server.start();
         InetSocketAddress isa = new InetSocketAddress(
-                InetAddress.getByName(ByteServer.LOCALHOST), ByteServer.PORT);
+                InetAddress.getByName(ByteServer.LOCALHOST), server.port());
         Selector sel = Selector.open();
         SocketChannel sc = SocketChannel.open();
         sc.connect(isa);
--- a/jdk/test/java/nio/channels/SocketChannel/BigReadWrite.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/SocketChannel/BigReadWrite.java	Wed Jun 16 14:24:46 2010 +0100
@@ -32,7 +32,6 @@
 
 public class BigReadWrite {
 
-    static int port = 40170;
     static int testSize = 15;
 
     public static void main(String[] args) throws Exception {
--- a/jdk/test/java/nio/channels/SocketChannel/VectorIO.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/SocketChannel/VectorIO.java	Wed Jun 16 14:24:46 2010 +0100
@@ -36,8 +36,6 @@
 
 public class VectorIO {
 
-    static int port = 40170;
-
     static Random generator = new Random();
 
     static int testSize;
@@ -55,20 +53,12 @@
         System.err.println("Length " + testSize);
         Server sv = new Server(testSize);
         sv.start();
-        do {
-            try {
-                Thread.currentThread().sleep(200);
-            } catch (InterruptedException x) {
-                if (sv.finish(8000) == 0)
-                    throw new Exception("Failed: Error in server thread");
-            }
-        } while (!sv.ready);
-        bufferTest();
+        bufferTest(sv.port());
         if (sv.finish(8000) == 0)
             throw new Exception("Failed: Length = " + testSize);
     }
 
-    static void bufferTest() throws Exception {
+    static void bufferTest(int port) throws Exception {
         ByteBuffer[] bufs = new ByteBuffer[testSize];
         for(int i=0; i<testSize; i++) {
             String source = "buffer" + i;
@@ -105,17 +95,19 @@
     static class Server
         extends TestThread
     {
-        static int port = 40170;
-
         static Random generator = new Random();
 
-        int testSize;
+        final int testSize;
+        final ServerSocketChannel ssc;
 
-        volatile boolean ready = false;
-
-        Server(int testSize) {
+        Server(int testSize) throws IOException {
             super("Server " + testSize);
             this.testSize = testSize;
+            this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
+        }
+
+        int port() {
+            return ssc.socket().getLocalPort();
         }
 
         void go() throws Exception {
@@ -133,16 +125,11 @@
             }
 
             // Get a connection from client
-            ServerSocketChannel ssc = ServerSocketChannel.open();
             SocketChannel sc = null;
 
             try {
 
                 ssc.configureBlocking(false);
-                InetAddress lh = InetAddress.getLocalHost();
-                InetSocketAddress isa = new InetSocketAddress(lh, port);
-                ssc.socket().bind(isa);
-                ready = true;
 
                 for (;;) {
                     sc = ssc.accept();
--- a/jdk/test/java/nio/channels/SocketChannel/Write.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/SocketChannel/Write.java	Wed Jun 16 14:24:46 2010 +0100
@@ -37,8 +37,6 @@
 
 public class Write {
 
-    static int port = 40170;
-
     static Random generator = new Random();
 
     static int testSize = 15;
@@ -46,20 +44,12 @@
     public static void main(String[] args) throws Exception {
         WriteServer sv = new WriteServer();
         sv.start();
-        do {
-            try {
-                Thread.currentThread().sleep(200);
-            } catch (InterruptedException x) {
-                if (sv.finish(8000) == 0)
-                    throw new Exception("Failed: Error in server thread");
-            }
-        } while (!sv.ready);
-        bufferTest();
+        bufferTest(sv.port());
         if (sv.finish(8000) == 0)
             throw new Exception("Failed" );
     }
 
-    static void bufferTest() throws Exception {
+    static void bufferTest(int port) throws Exception {
         ByteBuffer[] bufs = new ByteBuffer[testSize];
         for(int i=0; i<testSize; i++) {
             String source =
@@ -94,14 +84,18 @@
 
 class WriteServer extends TestThread {
 
-    static int port = 40170;
-
     static Random generator = new Random();
 
-    volatile boolean ready = false;
+
+    final ServerSocketChannel ssc;
 
-    WriteServer() {
+    WriteServer() throws IOException {
         super("WriteServer");
+        this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
+    }
+
+    int port() {
+        return ssc.socket().getLocalPort();
     }
 
     void go() throws Exception {
@@ -112,15 +106,10 @@
         ByteBuffer buf = ByteBuffer.allocateDirect(5);
 
         // Get a connection from client
-        ServerSocketChannel ssc = ServerSocketChannel.open();
         SocketChannel sc = null;
 
         try {
             ssc.configureBlocking(false);
-            InetAddress lh = InetAddress.getLocalHost();
-            InetSocketAddress isa = new InetSocketAddress(lh, port);
-            ssc.socket().bind(isa);
-            ready = true;
 
             for (;;) {
                 sc = ssc.accept();
--- a/jdk/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoTest.java	Tue Jun 15 20:34:49 2010 -0700
+++ b/jdk/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoTest.java	Wed Jun 16 14:24:46 2010 +0100
@@ -141,7 +141,7 @@
         // and receive the echo
         byte b[] = new byte[msg.length() + 100];
         DatagramPacket pkt2 = new DatagramPacket(b, b.length);
-        dc.socket().setSoTimeout(2000);
+        dc.socket().setSoTimeout(5000);
         dc.socket().receive(pkt2);
 
         if (pkt2.getLength() != msg.length()) {