--- old/test/java/rmi/transport/readTimeout/ReadTimeoutTest.java 2013-12-17 17:03:27.514195240 +0800 +++ new/test/java/rmi/transport/readTimeout/ReadTimeoutTest.java 2013-12-17 17:03:27.346195233 +0800 @@ -44,6 +44,8 @@ import java.rmi.server.RMISocketFactory; import java.io.*; import java.net.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; public class ReadTimeoutTest { @@ -69,49 +71,33 @@ // 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(?)"); + 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) { - } + impl.unexport(); } - // Should exit here } @@ -120,6 +106,7 @@ { private int servport = 0; + @Override public Socket createSocket(String h, int p) throws IOException { @@ -130,6 +117,7 @@ * 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 { @@ -155,23 +143,23 @@ } // 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(); }