test/java/rmi/transport/readTimeout/ReadTimeoutTest.java

Print this page

        

@@ -42,10 +42,12 @@
 
 import java.rmi.*;
 import java.rmi.server.RMISocketFactory;
 import java.io.*;
 import java.net.*;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 public class ReadTimeoutTest
 {
     private static final int DELAY = 5000;      // milliseconds
 

@@ -67,71 +69,57 @@
         // Create remote object
         TestImpl impl = new TestImpl();
 
         // Export and get which port.
         System.err.println("(exporting remote object)");
-        TestIface stub = impl.export();
-        Socket DoS = null;
-        try {
             int port = fac.whichPort();
 
             // Sanity
             if (port == 0)
                 throw new Error("TEST FAILED: export didn't reserve a port(?)");
-
+        try (Socket DoS = new Socket("127.0.0.1", port)) {
             // Now, connect to that port
             //Thread.sleep(2000);
             System.err.println("(connecting to listening port on 127.0.0.1:" +
                                port + ")");
-            DoS = new Socket("127.0.0.1", port);
             InputStream stream = DoS.getInputStream();
 
             // Read on the socket in the background
-            boolean[] successful = new boolean[] { false };
-            (new SomeReader(stream, successful)).start();
+            CountDownLatch done = new CountDownLatch(1);
+            (new SomeReader(stream, done)).start();
 
             // Wait for completion
-            int nretries = 4;
-            while (nretries-- > 0) {
-                if (successful[0])
-                    break;
-                Thread.sleep(DELAY);
-            }
-
-            if (successful[0]) {
+            if (done.await(DELAY * 4, TimeUnit.SECONDS)) {
                 System.err.println("TEST PASSED.");
             } else {
                 throw new Error("TEST FAILED.");
             }
-
+        } catch (InterruptedException ie) {
+            throw new Error("Unexpected error happen in reader:" + ie);
         } finally {
-            try {
-                if (DoS != null)
-                    DoS.close();        // aborts the reader if still blocked
                 impl.unexport();
-            } catch (Throwable unmatter) {
-            }
         }
-
         // Should exit here
     }
 
     private static class SomeFactory
         extends RMISocketFactory
     {
         private int servport = 0;
 
+        @Override
         public Socket createSocket(String h, int p)
             throws IOException
         {
             return (new Socket(h, p));
         }
 
         /** Create a server socket and remember which port it's on.
          * Aborts if createServerSocket(0) is called twice, because then
          * it doesn't know whether to remember the first or second port.
          */
+        @Override
         public ServerSocket createServerSocket(int p)
             throws IOException
         {
             ServerSocket ss;
             ss = new ServerSocket(p);

@@ -153,27 +141,27 @@
             return (servport);
         }
     } // end class SomeFactory
 
     protected static class SomeReader extends Thread {
-        private InputStream readon;
-        private boolean[] vec;
+        private final InputStream readon;
+        private final CountDownLatch done;
 
-        public SomeReader(InputStream s, boolean[] successvec) {
+        public SomeReader(InputStream s, CountDownLatch done) {
             super();
             this.setDaemon(true);
             this.readon = s;
-            this.vec = successvec;
+            this.done = done;
         }
 
+        @Override
         public void run() {
             try {
                 int c = this.readon.read();
                 if (c != -1)
                     throw new Error ("Server returned " + c);
-                this.vec[0] = true;
-
+                done.countDown();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     } // end class SomeReader