6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
authoralanb
Tue, 15 Jun 2010 10:03:37 +0100
changeset 5788 50d4ad1a780f
parent 5787 a0af7b8e80ed
child 5791 f320be4d0cea
child 5793 b8ff20734cae
child 6858 8d04722ed7d2
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use" Reviewed-by: chegar
jdk/test/ProblemList.txt
jdk/test/java/nio/channels/DatagramChannel/Connect.java
jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java
jdk/test/java/nio/channels/DatagramChannel/NoSender.java
jdk/test/java/nio/channels/DatagramChannel/SRTest.java
jdk/test/java/nio/channels/DatagramChannel/Sender.java
--- a/jdk/test/ProblemList.txt	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/ProblemList.txt	Tue Jun 15 10:03:37 2010 +0100
@@ -752,18 +752,9 @@
 #    at SetLastModified.main(SetLastModified.java:107)
 java/io/File/SetLastModified.java                               generic-all
 
-# Fails on Solaris 10 x64, address already in use
-java/nio/channels/DatagramChannel/SRTest.java			generic-all
-
-# Fails on Solaris 10 x86, times out
-java/nio/channels/DatagramChannel/Sender.java			generic-all
-
 # Fails on Fedora 9 x86, address in use
 java/nio/channels/Selector/SelectWrite.java			generic-all
 
-# Fails on Fedora 9 32bit times out
-java/nio/channels/DatagramChannel/EmptyBuffer.java		generic-all
-
 # Fails on Windows 2000,  times out
 java/nio/channels/FileChannel/Transfer.java			generic-all
 
@@ -821,12 +812,6 @@
 java/nio/channels/AsynchronousSocketChannel/StressLoopback.java windows-5.0
 java/nio/channels/Channels/Basic2.java			 	windows-5.0
 
-# Solaris sparc timeout
-java/nio/channels/DatagramChannel/Connect.java		 	generic-all
-
-# Solaris i586 timeouts
-java/nio/channels/DatagramChannel/EmptyBuffer.java	 	solaris-all
-
 # Failed loopback connection? On windows 32bit? 
 #   Considered a stress test, can consume all resources.
 java/nio/channels/Selector/LotsOfChannels.java		 	generic-all
--- a/jdk/test/java/nio/channels/DatagramChannel/Connect.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/Connect.java	Tue Jun 15 10:03:37 2010 +0100
@@ -42,15 +42,15 @@
     }
 
     static void test() throws Exception {
-        invoke(new Actor(), new Reactor());
+        Reactor r = new Reactor();
+        Actor a = new Actor(r.port());
+        invoke(a, r);
     }
 
     static void invoke(Sprintable reader, Sprintable writer) throws Exception {
 
         Thread writerThread = new Thread(writer);
         writerThread.start();
-        while (!writer.ready())
-            Thread.sleep(50);
 
         Thread readerThread = new Thread(reader);
         readerThread.start();
@@ -64,34 +64,31 @@
 
     public interface Sprintable extends Runnable {
         public void throwException() throws Exception;
-        public boolean ready();
     }
 
     public static class Actor implements Sprintable {
+        final int port;
         Exception e = null;
 
+        Actor(int port) {
+            this.port = port;
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
         }
 
-        private volatile boolean ready = false;
-
-        public boolean ready() {
-            return ready;
-        }
-
         public void run() {
             try {
                 DatagramChannel dc = DatagramChannel.open();
-                ready = true;
 
                 // Send a message
                 ByteBuffer bb = ByteBuffer.allocateDirect(256);
                 bb.put("hello".getBytes());
                 bb.flip();
                 InetAddress address = InetAddress.getLocalHost();
-                InetSocketAddress isa = new InetSocketAddress(address, 8888);
+                InetSocketAddress isa = new InetSocketAddress(address, port);
                 dc.connect(isa);
                 dc.write(bb);
 
@@ -123,26 +120,26 @@
     }
 
     public static class Reactor implements Sprintable {
+        final DatagramChannel dc;
         Exception e = null;
 
+        Reactor() throws IOException {
+            dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        }
+
+        int port() {
+            return dc.socket().getLocalPort();
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
         }
 
-        private volatile boolean ready = false;
-
-        public boolean ready() {
-            return ready;
-        }
-
         public void run() {
             try {
                 // Listen for a message
-                DatagramChannel dc = DatagramChannel.open();
-                dc.socket().bind(new InetSocketAddress(8888));
                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
-                ready = true;
                 SocketAddress sa = dc.receive(bb);
                 bb.flip();
                 CharBuffer cb = Charset.forName("US-ASCII").
--- a/jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/EmptyBuffer.java	Tue Jun 15 10:03:37 2010 +0100
@@ -42,18 +42,16 @@
     }
 
     static void test() throws Exception {
-        Sprintable server = new Server();
+        Server server = new Server();
         Thread serverThread = new Thread(server);
         serverThread.start();
-        while (!server.ready())
-            Thread.sleep(50);
         DatagramChannel dc = DatagramChannel.open();
         ByteBuffer bb = ByteBuffer.allocateDirect(12);
         bb.order(ByteOrder.BIG_ENDIAN);
         bb.putInt(1).putLong(1);
         bb.flip();
         InetAddress address = InetAddress.getLocalHost();
-        InetSocketAddress isa = new InetSocketAddress(address, 8888);
+        InetSocketAddress isa = new InetSocketAddress(address, server.port());
         dc.connect(isa);
         dc.write(bb);
         bb.rewind();
@@ -65,24 +63,23 @@
         server.throwException();
     }
 
-    public interface Sprintable extends Runnable {
-        public void throwException() throws Exception;
-        public boolean ready();
-    }
+    public static class Server implements Runnable {
+        final DatagramChannel dc;
+        Exception e = null;
 
-    public static class Server implements Sprintable {
-        Exception e = null;
-        private volatile boolean ready = false;
+        Server() throws IOException {
+            this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        }
 
-        public void throwException() throws Exception {
+        int port() {
+            return dc.socket().getLocalPort();
+        }
+
+        void throwException() throws Exception {
             if (e != null)
                 throw e;
         }
 
-        public boolean ready() {
-            return ready;
-        }
-
         void showBuffer(String s, ByteBuffer bb) {
             log.println(s);
             bb.rewind();
@@ -97,9 +94,6 @@
             SocketAddress sa = null;
             int numberReceived = 0;
             try {
-                DatagramChannel dc = DatagramChannel.open();
-                dc.socket().bind(new InetSocketAddress(8888));
-                ready = true;
                 ByteBuffer bb = ByteBuffer.allocateDirect(12);
                 bb.clear();
                 // Only one clear. The buffer will be full after
--- a/jdk/test/java/nio/channels/DatagramChannel/NoSender.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/NoSender.java	Tue Jun 15 10:03:37 2010 +0100
@@ -33,7 +33,7 @@
 public class NoSender {
     public static void main(String argv[]) throws Exception {
         DatagramChannel dc = DatagramChannel.open();
-        dc.socket().bind(new InetSocketAddress(5441));
+        dc.socket().bind(new InetSocketAddress(0));
         dc.configureBlocking(false);
         ByteBuffer buf1 = ByteBuffer.allocateDirect(256);
         SocketAddress sa1 = dc.receive(buf1);
--- a/jdk/test/java/nio/channels/DatagramChannel/SRTest.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/SRTest.java	Tue Jun 15 10:03:37 2010 +0100
@@ -42,16 +42,23 @@
     }
 
     static void test() throws Exception {
-        invoke(new ClassicReader(), new ClassicWriter());
+        ClassicReader classicReader;
+        NioReader nioReader;
+
+        classicReader = new ClassicReader();
+        invoke(classicReader, new ClassicWriter(classicReader.port()));
         log.println("Classic RW: OK");
 
-        invoke(new ClassicReader(), new NioWriter());
+        classicReader = new ClassicReader();
+        invoke(classicReader, new NioWriter(classicReader.port()));
         log.println("Classic R, Nio W: OK");
 
-        invoke(new NioReader(), new ClassicWriter());
+        nioReader = new NioReader();
+        invoke(nioReader, new ClassicWriter(nioReader.port()));
         log.println("Classic W, Nio R: OK");
 
-        invoke(new NioReader(), new NioWriter());
+        nioReader = new NioReader();
+        invoke(nioReader, new NioWriter(nioReader.port()));
         log.println("Nio RW: OK");
     }
 
@@ -75,8 +82,13 @@
     }
 
     public static class ClassicWriter implements Sprintable {
+        final int port;
         Exception e = null;
 
+        ClassicWriter(int port) {
+            this.port = port;
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
@@ -89,7 +101,7 @@
                 byte[] data = dataString.getBytes();
                 InetAddress address = InetAddress.getLocalHost();
                 DatagramPacket dp = new DatagramPacket(data, data.length,
-                                                       address, 8888);
+                                                       address, port);
                 ds.send(dp);
                 Thread.sleep(50);
                 ds.send(dp);
@@ -100,8 +112,13 @@
     }
 
     public static class NioWriter implements Sprintable {
+        final int port;
         Exception e = null;
 
+        NioWriter(int port) {
+            this.port = port;
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
@@ -114,7 +131,7 @@
                 bb.put("hello".getBytes());
                 bb.flip();
                 InetAddress address = InetAddress.getLocalHost();
-                InetSocketAddress isa = new InetSocketAddress(address, 8888);
+                InetSocketAddress isa = new InetSocketAddress(address, port);
                 dc.send(bb, isa);
                 Thread.sleep(50);
                 dc.send(bb, isa);
@@ -125,8 +142,17 @@
     }
 
     public static class ClassicReader implements Sprintable {
+        final DatagramSocket ds;
         Exception e = null;
 
+        ClassicReader() throws IOException {
+            this.ds = new DatagramSocket();
+        }
+
+        int port() {
+            return ds.getLocalPort();
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
@@ -136,7 +162,6 @@
             try {
                 byte[] buf = new byte[256];
                 DatagramPacket dp = new DatagramPacket(buf, buf.length);
-                DatagramSocket ds = new DatagramSocket(8888);
                 ds.receive(dp);
                 String received = new String(dp.getData());
                 log.println(received);
@@ -148,8 +173,17 @@
     }
 
     public static class NioReader implements Sprintable {
+        final DatagramChannel dc;
         Exception e = null;
 
+        NioReader() throws IOException {
+            this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        }
+
+        int port() {
+            return dc.socket().getLocalPort();
+        }
+
         public void throwException() throws Exception {
             if (e != null)
                 throw e;
@@ -157,8 +191,6 @@
 
         public void run() {
             try {
-                DatagramChannel dc = DatagramChannel.open();
-                dc.socket().bind(new InetSocketAddress(8888));
                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
                 SocketAddress sa = dc.receive(bb);
                 bb.flip();
--- a/jdk/test/java/nio/channels/DatagramChannel/Sender.java	Sun Jun 13 17:19:22 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/Sender.java	Tue Jun 15 10:03:37 2010 +0100
@@ -42,13 +42,11 @@
     }
 
     static void test() throws Exception {
-        Sprintable server = new Server();
-        Sprintable client = new Client();
+        Server server = new Server();
+        Client client = new Client(server.port());
 
         Thread serverThread = new Thread(server);
         serverThread.start();
-        while (!server.ready())
-            Thread.sleep(50);
 
         Thread clientThread = new Thread(client);
         clientThread.start();
@@ -60,23 +58,17 @@
         client.throwException();
     }
 
-    public interface Sprintable extends Runnable {
-        public void throwException() throws Exception;
-        public boolean ready();
-    }
-
-    public static class Client implements Sprintable {
+    public static class Client implements Runnable {
+        final int port;
         Exception e = null;
 
-        public void throwException() throws Exception {
-            if (e != null)
-                throw e;
+        Client(int port) {
+            this.port = port;
         }
 
-        private volatile boolean ready = false;
-
-        public boolean ready() {
-            return ready;
+        void throwException() throws Exception {
+            if (e != null)
+                throw e;
         }
 
         public void run() {
@@ -87,7 +79,7 @@
                 bb.putInt(1).putLong(1);
                 bb.flip();
                 InetAddress address = InetAddress.getLocalHost();
-                InetSocketAddress isa = new InetSocketAddress(address, 8888);
+                InetSocketAddress isa = new InetSocketAddress(address, port);
                 dc.connect(isa);
                 dc.write(bb);
             } catch (Exception ex) {
@@ -96,19 +88,23 @@
         }
     }
 
-    public static class Server implements Sprintable {
+    public static class Server implements Runnable {
+        final DatagramChannel dc;
         Exception e = null;
-        private volatile boolean ready = false;
+
+        Server() throws IOException {
+            dc = DatagramChannel.open().bind(new InetSocketAddress(0));
+        }
 
-        public void throwException() throws Exception {
+        int port() {
+            return dc.socket().getLocalPort();
+        }
+
+        void throwException() throws Exception {
             if (e != null)
                 throw e;
         }
 
-        public boolean ready() {
-            return ready;
-        }
-
         void showBuffer(String s, ByteBuffer bb) {
             log.println(s);
             bb.rewind();
@@ -123,13 +119,10 @@
             SocketAddress sa = null;
 
             try {
-                DatagramChannel dc = DatagramChannel.open();
-                dc.socket().bind(new InetSocketAddress(8888));
-                dc.configureBlocking(false);
-                ready = true;
                 ByteBuffer bb = ByteBuffer.allocateDirect(12);
                 bb.clear();
                 // Get the one valid datagram
+                dc.configureBlocking(false);
                 while (sa == null)
                     sa = dc.receive(bb);
                 sa = null;