diff options
author | alanb <none@none> | 2010-06-23 20:19:29 +0100 |
---|---|---|
committer | alanb <none@none> | 2010-06-23 20:19:29 +0100 |
commit | 9758b5d9166376b2e71397e101758ab5611f09ff (patch) | |
tree | cc4f40b066572d73030b2563f02871b4cc1e85ca /test/java/nio/channels | |
parent | 138ad052a7487d5e2fe7b95be32db6729eefbd06 (diff) |
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
Reviewed-by: ohair, sherman, chegar
Diffstat (limited to 'test/java/nio/channels')
69 files changed, 580 insertions, 354 deletions
diff --git a/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java b/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java index e22ee6813..5e479bd04 100644 --- a/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java +++ b/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java @@ -29,6 +29,7 @@ import java.nio.ByteBuffer; import java.nio.channels.*; import java.net.*; +import java.util.*; import java.util.concurrent.*; import java.io.IOException; @@ -44,8 +45,12 @@ public class GroupOfOne { final AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open() .bind(new InetSocketAddress(0)); + final List<AsynchronousSocketChannel> accepted = new ArrayList<AsynchronousSocketChannel>(); listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() { public void completed(AsynchronousSocketChannel ch, Void att) { + synchronized (accepted) { + accepted.add(ch); + } listener.accept((Void)null, this); } public void failed(Throwable exc, Void att) { @@ -58,6 +63,14 @@ public class GroupOfOne { test(sa, true, false); test(sa, false, true); test(sa, true, true); + + // clean-up + listener.close(); + synchronized (accepted) { + for (AsynchronousSocketChannel ch: accepted) { + ch.close(); + } + } } static void test(SocketAddress sa, diff --git a/test/java/nio/channels/AsynchronousChannelGroup/Identity.java b/test/java/nio/channels/AsynchronousChannelGroup/Identity.java index be2c2d078..9bfd2a928 100644 --- a/test/java/nio/channels/AsynchronousChannelGroup/Identity.java +++ b/test/java/nio/channels/AsynchronousChannelGroup/Identity.java @@ -32,6 +32,7 @@ import java.net.*; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; +import java.io.IOException; /** * Tests that the completion handler is invoked by a thread with @@ -81,14 +82,18 @@ public class Identity { listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() { public void completed(final AsynchronousSocketChannel ch, Void att) { listener.accept((Void)null, this); - final ByteBuffer buf = ByteBuffer.allocate(100); - ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() { - public void completed(Integer bytesRead, Void att) { - buf.clear(); - ch.read(buf, (Void)null, this); + ch.read(buf, ch, new CompletionHandler<Integer,AsynchronousSocketChannel>() { + public void completed(Integer bytesRead, AsynchronousSocketChannel ch) { + if (bytesRead < 0) { + try { ch.close(); } catch (IOException ignore) { } + } else { + buf.clear(); + ch.read(buf, ch, this); + } } - public void failed(Throwable exc, Void att) { + public void failed(Throwable exc, AsynchronousSocketChannel ch) { + try { ch.close(); } catch (IOException ignore) { } } }); } @@ -100,7 +105,8 @@ public class Identity { // create 3-10 channels, each in its own group final int groupCount = 3 + rand.nextInt(8); - final AsynchronousSocketChannel[] channel = new AsynchronousSocketChannel[groupCount]; + AsynchronousChannelGroup[] groups = new AsynchronousChannelGroup[groupCount]; + final AsynchronousSocketChannel[] channels = new AsynchronousSocketChannel[groupCount]; for (int i=0; i<groupCount; i++) { ThreadFactory factory = createThreadFactory(i); AsynchronousChannelGroup group; @@ -111,17 +117,18 @@ public class Identity { ExecutorService pool = Executors.newCachedThreadPool(factory); group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5)); } + groups[i] = group; // create channel in group and connect it to the server AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group); ch.connect(sa).get(); - channel[i] = ch; + channels[i] = ch; } // randomly write to each channel, ensuring that the completion handler // is always invoked by a thread with the right identity. final AtomicInteger writeCount = new AtomicInteger(100); - channel[0].write(getBuffer(), 0, new CompletionHandler<Integer,Integer>() { + channels[0].write(getBuffer(), 0, new CompletionHandler<Integer,Integer>() { public void completed(Integer bytesWritten, Integer groupId) { if (bytesWritten != 1) fail("Expected 1 byte to be written"); @@ -129,7 +136,7 @@ public class Identity { fail("Handler invoked by thread with the wrong identity"); if (writeCount.decrementAndGet() > 0) { int id = rand.nextInt(groupCount); - channel[id].write(getBuffer(), id, this); + channels[id].write(getBuffer(), id, this); } else { done.countDown(); } @@ -139,8 +146,16 @@ public class Identity { } }); - // wait until + // wait until done done.await(); + + // clean-up + for (AsynchronousSocketChannel ch: channels) + ch.close(); + for (AsynchronousChannelGroup group: groups) + group.shutdownNow(); + listener.close(); + if (failed.get()) throw new RuntimeException("Test failed - see log for details"); } diff --git a/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java b/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java index accdc75f1..7beb7d652 100644 --- a/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java +++ b/test/java/nio/channels/AsynchronousDatagramChannel/Basic.java @@ -371,17 +371,22 @@ public class Basic { static void doMulticastTests() throws Exception { final byte[] msg = "hello".getBytes(); + InetAddress lh = InetAddress.getLocalHost(); + NetworkInterface interf = NetworkInterface.getByInetAddress(lh); + if (interf.isLoopback() || !interf.supportsMulticast()) { + System.out.println("Multicasting not tested"); + return; + } + AsynchronousDatagramChannel ch = AsynchronousDatagramChannel .open(StandardProtocolFamily.INET, null) .setOption(StandardSocketOption.SO_REUSEADDR, true) .bind(new InetSocketAddress(0)); - InetAddress lh = InetAddress.getLocalHost(); int port = ((InetSocketAddress)(ch.getLocalAddress())).getPort(); // join group InetAddress group = InetAddress.getByName("225.4.5.6"); - NetworkInterface interf = NetworkInterface.getByInetAddress(lh); MembershipKey key = ch.join(group, interf); // check key diff --git a/test/java/nio/channels/AsynchronousFileChannel/Basic.java b/test/java/nio/channels/AsynchronousFileChannel/Basic.java index bd623bf83..728a12af0 100644 --- a/test/java/nio/channels/AsynchronousFileChannel/Basic.java +++ b/test/java/nio/channels/AsynchronousFileChannel/Basic.java @@ -45,16 +45,18 @@ public class Basic { File blah = File.createTempFile("blah", null); blah.deleteOnExit(); - final AsynchronousFileChannel ch = AsynchronousFileChannel + AsynchronousFileChannel ch = AsynchronousFileChannel .open(blah.toPath(), READ, WRITE); + try { + // run tests + testUsingCompletionHandlers(ch); + testUsingWaitOnResult(ch); + testInterruptHandlerThread(ch); + } finally { + ch.close(); + } - // run tests - testUsingCompletionHandlers(ch); - testUsingWaitOnResult(ch); - testInterruptHandlerThread(ch); - - // close channel and invoke test that expects channel to be closed - ch.close(); + // run test that expects channel to be closed testClosedChannel(ch); // these tests open the file themselves @@ -63,6 +65,9 @@ public class Basic { testAsynchronousClose(blah.toPath()); testCancel(blah.toPath()); testTruncate(blah.toPath()); + + // eagerly clean-up + blah.delete(); } /* diff --git a/test/java/nio/channels/AsynchronousFileChannel/Lock.java b/test/java/nio/channels/AsynchronousFileChannel/Lock.java index 0ebbea57c..648776832 100644 --- a/test/java/nio/channels/AsynchronousFileChannel/Lock.java +++ b/test/java/nio/channels/AsynchronousFileChannel/Lock.java @@ -53,13 +53,17 @@ public class Lock { LockSlaveMirror slave = startLockSlave(); try { - // create temporary file + // create temporary file File blah = File.createTempFile("blah", null); blah.deleteOnExit(); + // run tests testLockProtocol(blah, slave); testAsyncClose(blah, slave); + // eagerly clean-up + blah.delete(); + } finally { slave.shutdown(); } @@ -150,7 +154,12 @@ public class Lock { String sep = FileSystems.getDefault().getSeparator(); String command = System.getProperty("java.home") + - sep + "bin" + sep + "java Lock -lockslave " + port; + sep + "bin" + sep + "java"; + String testClasses = System.getProperty("test.classes"); + if (testClasses != null) + command += " -cp " + testClasses; + command += " Lock -lockslave " + port; + Process p = Runtime.getRuntime().exec(command); IOHandler.handle(p.getInputStream()); IOHandler.handle(p.getErrorStream()); diff --git a/test/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java b/test/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java index 526408269..02453e57f 100644 --- a/test/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java +++ b/test/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java @@ -135,6 +135,7 @@ public class LotsOfWrites { latch.await(); // verify content of each file + boolean failed = false; byte[] buf = new byte[8192]; for (int i=0; i<count ;i++) { Writer writer = writers[i]; @@ -145,18 +146,35 @@ public class LotsOfWrites { int nread = in.read(buf); while (nread > 0) { for (int j=0; j<nread; j++) { - if (buf[j] != expected) - throw new RuntimeException("Unexpected byte"); + if (buf[j] != expected) { + System.err.println("Unexpected contents"); + failed = true; + break; + } expected++; } + if (failed) + break; size += nread; nread = in.read(buf); } - if (size != writer.size()) - throw new RuntimeException("Unexpected size"); + if (!failed && size != writer.size()) { + System.err.println("Unexpected size"); + failed = true; + } + if (failed) + break; } finally { in.close(); } } + + // clean-up + for (int i=0; i<count; i++) { + writers[i].file().delete(); + } + + if (failed) + throw new RuntimeException("Test failed"); } } diff --git a/test/java/nio/channels/AsynchronousSocketChannel/Basic.java b/test/java/nio/channels/AsynchronousSocketChannel/Basic.java index f9755de04..df12b258c 100644 --- a/test/java/nio/channels/AsynchronousSocketChannel/Basic.java +++ b/test/java/nio/channels/AsynchronousSocketChannel/Basic.java @@ -776,6 +776,7 @@ public class Basic { throw new RuntimeException("RuntimeException expected after timeout."); ch.close(); + server.close(); } // returns ByteBuffer with random bytes diff --git a/test/java/nio/channels/Channels/Basic2.java b/test/java/nio/channels/Channels/Basic2.java index 6019ae63c..c98d58149 100644 --- a/test/java/nio/channels/Channels/Basic2.java +++ b/test/java/nio/channels/Channels/Basic2.java @@ -61,6 +61,9 @@ public class Basic2 { writerThread.join(); readerThread.join(); + // shutdown listener + listener.close(); + // check that reader received what we expected if (reader.total() != writer.total()) throw new RuntimeException("Unexpected number of bytes read"); diff --git a/test/java/nio/channels/Channels/Write.java b/test/java/nio/channels/Channels/Write.java index a454927ae..af8a463d2 100644 --- a/test/java/nio/channels/Channels/Write.java +++ b/test/java/nio/channels/Channels/Write.java @@ -47,5 +47,6 @@ public class Write { out.close(); fc.close(); fos.close(); + testFile.delete(); } } diff --git a/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java b/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java index 5a855acbd..b6d744a9d 100644 --- a/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java +++ b/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java @@ -82,8 +82,6 @@ public class AdaptDatagramSocket { } throw x; } - if (shouldTimeout) - throw new Exception("Receive did not time out"); break; } diff --git a/test/java/nio/channels/DatagramChannel/EmptyBuffer.java b/test/java/nio/channels/DatagramChannel/EmptyBuffer.java index f7c836a19..e0be48d1d 100644 --- a/test/java/nio/channels/DatagramChannel/EmptyBuffer.java +++ b/test/java/nio/channels/DatagramChannel/EmptyBuffer.java @@ -46,21 +46,25 @@ public class EmptyBuffer { Thread serverThread = new Thread(server); serverThread.start(); 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, server.port()); - dc.connect(isa); - dc.write(bb); - bb.rewind(); - dc.write(bb); - bb.rewind(); - dc.write(bb); - Thread.sleep(2000); - serverThread.interrupt(); - server.throwException(); + try { + 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, server.port()); + dc.connect(isa); + dc.write(bb); + bb.rewind(); + dc.write(bb); + bb.rewind(); + dc.write(bb); + Thread.sleep(2000); + serverThread.interrupt(); + server.throwException(); + } finally { + dc.close(); + } } public static class Server implements Runnable { @@ -118,6 +122,8 @@ public class EmptyBuffer { } } catch (Exception ex) { e = ex; + } finally { + try { dc.close(); } catch (IOException ignore) { } } } } diff --git a/test/java/nio/channels/DatagramChannel/ReceiveISA.java b/test/java/nio/channels/DatagramChannel/ReceiveISA.java index c63ec3026..2efc91421 100644 --- a/test/java/nio/channels/DatagramChannel/ReceiveISA.java +++ b/test/java/nio/channels/DatagramChannel/ReceiveISA.java @@ -66,6 +66,10 @@ public class ReceiveISA { rb.clear(); } + dc1.close(); + dc2.close(); + dc3.close(); + /* * Check that sa[0] equals sa[1] (both from dc1) * Check that sa[1] not equal to sa[2] (one from dc1, one from dc2) diff --git a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java index fdbbd3600..6d444d50f 100644 --- a/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java +++ b/test/java/nio/channels/DatagramChannel/SelectWhenRefused.java @@ -43,9 +43,9 @@ public class SelectWhenRefused { SocketAddress refuser = new InetSocketAddress(InetAddress.getLocalHost(), port); dc = DatagramChannel.open().bind(new InetSocketAddress(0)); + Selector sel = Selector.open(); try { dc.configureBlocking(false); - Selector sel = Selector.open(); dc.register(sel, SelectionKey.OP_READ); /* Test 1: not connected so ICMP port unreachable should not be received */ @@ -81,6 +81,7 @@ public class SelectWhenRefused { } } finally { + sel.close(); dc.close(); } } diff --git a/test/java/nio/channels/FileChannel/Args.java b/test/java/nio/channels/FileChannel/Args.java index 751806a02..367ab4e97 100644 --- a/test/java/nio/channels/FileChannel/Args.java +++ b/test/java/nio/channels/FileChannel/Args.java @@ -97,6 +97,8 @@ public class Args { (long)Integer.MAX_VALUE << 3); }}); + fc.close(); + f.delete(); } } diff --git a/test/java/nio/channels/FileChannel/ClosedChannelTransfer.java b/test/java/nio/channels/FileChannel/ClosedChannelTransfer.java index f81cbad60..78c971881 100644 --- a/test/java/nio/channels/FileChannel/ClosedChannelTransfer.java +++ b/test/java/nio/channels/FileChannel/ClosedChannelTransfer.java @@ -38,6 +38,7 @@ public class ClosedChannelTransfer { test1(channel); test2(channel); channel.close(); + file.delete(); } static void test1(FileChannel channel) throws Exception { diff --git a/test/java/nio/channels/FileChannel/ExpandingMap.java b/test/java/nio/channels/FileChannel/ExpandingMap.java index dd4ecde8b..21d091c30 100644 --- a/test/java/nio/channels/FileChannel/ExpandingMap.java +++ b/test/java/nio/channels/FileChannel/ExpandingMap.java @@ -25,6 +25,7 @@ * @bug 4938372 6541641 * @summary Flushing dirty pages prior to unmap can cause Cleaner thread to * abort VM if memory system has pages locked + * @run main/othervm ExpandingMap */ import java.io.File; import java.io.RandomAccessFile; diff --git a/test/java/nio/channels/FileChannel/Lock.java b/test/java/nio/channels/FileChannel/Lock.java index 4616df839..3f056be60 100644 --- a/test/java/nio/channels/FileChannel/Lock.java +++ b/test/java/nio/channels/FileChannel/Lock.java @@ -55,6 +55,7 @@ public class Lock { test2(blah, true); test2(blah, false); test3(blah); + blah.delete(); } private static void test2(File blah, boolean b) throws Exception { @@ -90,7 +91,11 @@ public class Lock { // Exec the tamperer String command = System.getProperty("java.home") + - File.separator + "bin" + File.separator + "java Lock " + str + " " + blah; + File.separator + "bin" + File.separator + "java"; + String testClasses = System.getProperty("test.classes"); + if (testClasses != null) + command += " -cp " + testClasses; + command += " Lock " + str + " " + blah; Process p = Runtime.getRuntime().exec(command); BufferedReader in = new BufferedReader diff --git a/test/java/nio/channels/FileChannel/MapOverEnd.java b/test/java/nio/channels/FileChannel/MapOverEnd.java index 258d4729a..582e2495c 100644 --- a/test/java/nio/channels/FileChannel/MapOverEnd.java +++ b/test/java/nio/channels/FileChannel/MapOverEnd.java @@ -24,6 +24,7 @@ /* @test * @bug 4463036 * @summary Check if file mapping extends beyond end of file + * @run main/othervm MapOverEnd */ import java.nio.*; diff --git a/test/java/nio/channels/FileChannel/MapReadOnly.java b/test/java/nio/channels/FileChannel/MapReadOnly.java index 7a050e7a7..6ab6215a7 100644 --- a/test/java/nio/channels/FileChannel/MapReadOnly.java +++ b/test/java/nio/channels/FileChannel/MapReadOnly.java @@ -24,6 +24,7 @@ /* @test * @bug 4510489 * @summary Verify IOUtil.java reads to buffer limits + * @run main/othervm MapReadOnly */ import java.io.*; import java.nio.*; diff --git a/test/java/nio/channels/FileChannel/MapTest.java b/test/java/nio/channels/FileChannel/MapTest.java index 9786c0f4f..9f11378e0 100644 --- a/test/java/nio/channels/FileChannel/MapTest.java +++ b/test/java/nio/channels/FileChannel/MapTest.java @@ -23,6 +23,7 @@ /* @test * @summary Test file mapping with FileChannel + * @run main/othervm MapTest */ import java.io.*; diff --git a/test/java/nio/channels/FileChannel/Mode.java b/test/java/nio/channels/FileChannel/Mode.java index 24da10191..da57f003a 100644 --- a/test/java/nio/channels/FileChannel/Mode.java +++ b/test/java/nio/channels/FileChannel/Mode.java @@ -24,6 +24,7 @@ /* @test @bug 4462298 * @summary Test FileChannel maps with different accesses + * @run main/othervm Mode */ import java.nio.channels.*; diff --git a/test/java/nio/channels/FileChannel/Position.java b/test/java/nio/channels/FileChannel/Position.java index f94147d97..e51b466ff 100644 --- a/test/java/nio/channels/FileChannel/Position.java +++ b/test/java/nio/channels/FileChannel/Position.java @@ -63,6 +63,7 @@ public class Position { c.close(); fis.close(); + blah.delete(); } /** diff --git a/test/java/nio/channels/FileChannel/Pread.java b/test/java/nio/channels/FileChannel/Pread.java index d48e12cde..d53a745a4 100644 --- a/test/java/nio/channels/FileChannel/Pread.java +++ b/test/java/nio/channels/FileChannel/Pread.java @@ -65,6 +65,9 @@ public class Pread { throw new RuntimeException("Expected exception not thrown"); } catch(IllegalArgumentException e) { // Correct result + } finally { + fc.close(); + blah.delete(); } } @@ -73,13 +76,18 @@ public class Pread { File blah = File.createTempFile("blah2", null); blah.deleteOnExit(); FileOutputStream fos = new FileOutputStream(blah); - fos.write(new byte[128]); - FileChannel fc = fos.getChannel(); try { - fc.read(ByteBuffer.allocate(256),1); - throw new RuntimeException("Expected exception not thrown"); - } catch(NonReadableChannelException e) { - // Correct result + fos.write(new byte[128]); + FileChannel fc = fos.getChannel(); + try { + fc.read(ByteBuffer.allocate(256),1); + throw new RuntimeException("Expected exception not thrown"); + } catch(NonReadableChannelException e) { + // Correct result + } + } finally { + fos.close(); + blah.delete(); } } @@ -130,6 +138,7 @@ public class Pread { c.close(); fis.close(); + blah.delete(); } /** diff --git a/test/java/nio/channels/FileChannel/Pwrite.java b/test/java/nio/channels/FileChannel/Pwrite.java index 95f2c8b05..d71390e86 100644 --- a/test/java/nio/channels/FileChannel/Pwrite.java +++ b/test/java/nio/channels/FileChannel/Pwrite.java @@ -62,6 +62,9 @@ public class Pwrite { throw new RuntimeException("Expected exception not thrown"); } catch(NonWritableChannelException e) { // Correct result + } finally { + fc.close(); + blah.delete(); } } @@ -123,6 +126,7 @@ public class Pwrite { } c.close(); raf.close(); + blah.delete(); } /** diff --git a/test/java/nio/channels/FileChannel/Read.java b/test/java/nio/channels/FileChannel/Read.java index b41735e2a..6161e6050 100644 --- a/test/java/nio/channels/FileChannel/Read.java +++ b/test/java/nio/channels/FileChannel/Read.java @@ -80,6 +80,7 @@ public class Read { c.close(); fis.close(); + blah.delete(); } /** diff --git a/test/java/nio/channels/FileChannel/ReadFull.java b/test/java/nio/channels/FileChannel/ReadFull.java index fd7d9fa8b..6977c2d3b 100644 --- a/test/java/nio/channels/FileChannel/ReadFull.java +++ b/test/java/nio/channels/FileChannel/ReadFull.java @@ -54,5 +54,6 @@ public class ReadFull { fc.close(); fis.close(); + blah.delete(); } } diff --git a/test/java/nio/channels/FileChannel/ReadToLimit.java b/test/java/nio/channels/FileChannel/ReadToLimit.java index 01da77169..cf12f76c0 100644 --- a/test/java/nio/channels/FileChannel/ReadToLimit.java +++ b/test/java/nio/channels/FileChannel/ReadToLimit.java @@ -48,6 +48,7 @@ public class ReadToLimit { throw new Exception("Test failed"); fc.close(); fis.close(); + blah.delete(); } /** diff --git a/test/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java b/test/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java index d7d7c120a..dd8808fd5 100644 --- a/test/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java +++ b/test/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java @@ -39,8 +39,12 @@ public class ReleaseOnCloseDeadlock { public static void main(String[] args) throws IOException { File blah = File.createTempFile("blah", null); blah.deleteOnExit(); - for (int i=0; i<100; i++) { - test(blah.toPath()); + try { + for (int i=0; i<100; i++) { + test(blah.toPath()); + } + } finally { + blah.delete(); } } diff --git a/test/java/nio/channels/FileChannel/ScatteringRead.java b/test/java/nio/channels/FileChannel/ScatteringRead.java index 8e9191f56..deead33b8 100644 --- a/test/java/nio/channels/FileChannel/ScatteringRead.java +++ b/test/java/nio/channels/FileChannel/ScatteringRead.java @@ -24,6 +24,7 @@ /* @test @bug 4452020 4629048 4638365 4869859 * @summary Test FileChannel scattering reads + * @run main/othervm ScatteringRead */ import java.nio.channels.*; diff --git a/test/java/nio/channels/FileChannel/Size.java b/test/java/nio/channels/FileChannel/Size.java index b46ba8c2c..fa669d739 100644 --- a/test/java/nio/channels/FileChannel/Size.java +++ b/test/java/nio/channels/FileChannel/Size.java @@ -24,6 +24,7 @@ /* @test * @bug 4563125 * @summary Test size method of FileChannel + * @run main/othervm Size */ import java.io.*; diff --git a/test/java/nio/channels/FileChannel/Transfer.java b/test/java/nio/channels/FileChannel/Transfer.java index be2fc005c..bee8401a1 100644 --- a/test/java/nio/channels/FileChannel/Transfer.java +++ b/test/java/nio/channels/FileChannel/Transfer.java @@ -25,7 +25,7 @@ * @bug 4434723 4482726 4559072 4638365 4795550 5081340 5103988 6253145 * @summary Test FileChannel.transferFrom and transferTo * @library .. - * @run main/timeout=180 Transfer + * @run main/timeout=240 Transfer */ import java.io.*; @@ -96,6 +96,9 @@ public class Transfer { sourceChannel.close(); sinkChannel.close(); + + source.delete(); + sink.delete(); } private static void testReadableByteChannel(int size) throws Exception { @@ -139,6 +142,8 @@ public class Transfer { fc.close(); sink.close(); source.close(); + + f.delete(); } public static void xferTest02() throws Exception { @@ -173,6 +178,9 @@ public class Transfer { fc2.close(); raf1.close(); raf2.close(); + + source.delete(); + dest.delete(); } public static void xferTest03() throws Exception { @@ -207,6 +215,9 @@ public class Transfer { fc2.close(); raf1.close(); raf2.close(); + + source.delete(); + dest.delete(); } // Test transferTo with large file @@ -245,6 +256,8 @@ public class Transfer { sourceChannel.close(); sinkChannel.close(); + source.delete(); + sink.delete(); } // Test transferFrom with large file @@ -293,6 +306,8 @@ public class Transfer { } sourceChannel.close(); sinkChannel.close(); + source.delete(); + sink.delete(); } static void checkFileData(File file, String expected) throws Exception { @@ -338,6 +353,8 @@ public class Transfer { checkFileData(source, data); checkFileData(sink, data.substring(7,data.length())); + + source.delete(); } // Test transferTo to non-blocking socket channel @@ -371,6 +388,7 @@ public class Transfer { sink.close(); other.close(); ssc.close(); + source.delete(); } @@ -473,6 +491,7 @@ public class Transfer { source.close(); ssc.close(); fc.close(); + file.delete(); } } diff --git a/test/java/nio/channels/FileChannel/TransferToChannel.java b/test/java/nio/channels/FileChannel/TransferToChannel.java index 9cb9aff2a..6c5bc267a 100644 --- a/test/java/nio/channels/FileChannel/TransferToChannel.java +++ b/test/java/nio/channels/FileChannel/TransferToChannel.java @@ -51,6 +51,8 @@ public class TransferToChannel { test1(); test2(); in.close(); + file.delete(); + outFile.delete(); } static void test1() throws Exception { diff --git a/test/java/nio/channels/FileChannel/TransferToNonWritable.java b/test/java/nio/channels/FileChannel/TransferToNonWritable.java index 648862ce0..ffc023d29 100644 --- a/test/java/nio/channels/FileChannel/TransferToNonWritable.java +++ b/test/java/nio/channels/FileChannel/TransferToNonWritable.java @@ -43,6 +43,9 @@ public class TransferToNonWritable { throw new RuntimeException("Test failed"); } catch (NonWritableChannelException nwce) { // Correct result + } finally { + channel.close(); + blah.delete(); } } } diff --git a/test/java/nio/channels/FileChannel/Transfers.java b/test/java/nio/channels/FileChannel/Transfers.java index dfefa041c..d204944c4 100644 --- a/test/java/nio/channels/FileChannel/Transfers.java +++ b/test/java/nio/channels/FileChannel/Transfers.java @@ -554,6 +554,10 @@ public class Transfers { break; } + sourceFile.delete(); + targetFile.delete(); + fn.delete(); + if (failures > 0) { out.println(); throw new RuntimeException("Some tests failed"); diff --git a/test/java/nio/channels/FileChannel/TryLock.java b/test/java/nio/channels/FileChannel/TryLock.java index 70cb354e9..cadf72317 100644 --- a/test/java/nio/channels/FileChannel/TryLock.java +++ b/test/java/nio/channels/FileChannel/TryLock.java @@ -56,7 +56,6 @@ public class TryLock { public static void test1(boolean shared, boolean trylock) throws Exception { File testFile = File.createTempFile("test1", null); - testFile.deleteOnExit(); FileInputStream fis = new FileInputStream(testFile); FileChannel fc = fis.getChannel(); FileLock fl = null; @@ -73,12 +72,13 @@ public class TryLock { } finally { if (fl != null) fl.release(); + fc.close(); + testFile.delete(); } } public static void test2(boolean shared, boolean trylock) throws Exception { File testFile = File.createTempFile("test2", null); - testFile.deleteOnExit(); FileOutputStream fis = new FileOutputStream(testFile); FileChannel fc = fis.getChannel(); FileLock fl = null; @@ -95,19 +95,25 @@ public class TryLock { } finally { if (fl != null) fl.release(); + fc.close(); + testFile.delete(); } } public static void test3(boolean shared, boolean trylock) throws Exception { File testFile = File.createTempFile("test3", null); - testFile.deleteOnExit(); RandomAccessFile fis = new RandomAccessFile(testFile, "rw"); FileChannel fc = fis.getChannel(); - FileLock fl = null; - if (trylock) - fl = fc.tryLock(0, fc.size(), shared); - else - fl = fc.lock(0, fc.size(), shared); - fl.release(); + try { + FileLock fl = null; + if (trylock) + fl = fc.tryLock(0, fc.size(), shared); + else + fl = fc.lock(0, fc.size(), shared); + fl.release(); + } finally { + fc.close(); + testFile.delete(); + } } } diff --git a/test/java/nio/channels/FileChannel/Write.java b/test/java/nio/channels/FileChannel/Write.java index 8b7fae71e..448444769 100644 --- a/test/java/nio/channels/FileChannel/Write.java +++ b/test/java/nio/channels/FileChannel/Write.java @@ -25,6 +25,7 @@ * @test * @bug 4475533 4698138 4638365 4796221 * @summary Test FileChannel write + * @run main/othervm Write */ import java.nio.channels.*; @@ -46,23 +47,25 @@ public class Write { // Test to see that offset > length does not throw exception static void test1() throws Exception { - File testFile = File.createTempFile("test1", null); - testFile.deleteOnExit(); - ByteBuffer[] dsts = new ByteBuffer[4]; for (int i=0; i<4; i++) dsts[i] = ByteBuffer.allocateDirect(10); - FileOutputStream fos = new FileOutputStream(testFile); - FileChannel fc = fos.getChannel(); - fc.write(dsts, 2, 1); - fos.close(); + File testFile = File.createTempFile("test1", null); + try { + FileOutputStream fos = new FileOutputStream(testFile); + FileChannel fc = fos.getChannel(); + fc.write(dsts, 2, 1); + fos.close(); + } finally { + testFile.delete(); + } } // Test to see that the appropriate buffers are updated static void test2() throws Exception { File testFile = File.createTempFile("test2", null); - testFile.deleteOnExit(); + testFile.delete(); ByteBuffer[] srcs = new ByteBuffer[4]; for (int i=0; i<4; i++) srcs[i] = ByteBuffer.allocateDirect(10); @@ -74,25 +77,34 @@ public class Write { FileOutputStream fos = new FileOutputStream(testFile); FileChannel fc = fos.getChannel(); - fc.write(srcs, 1, 2); - fos.close(); + try { + fc.write(srcs, 1, 2); + } finally { + fc.close(); + } FileInputStream fis = new FileInputStream(testFile); fc = fis.getChannel(); - ByteBuffer bb = ByteBuffer.allocateDirect(10); - fc.read(bb); - bb.flip(); - if (bb.get() != 2) - throw new RuntimeException("Write failure"); - if (bb.get() != 3) - throw new RuntimeException("Write failure"); try { - bb.get(); - throw new RuntimeException("Write failure"); - } catch (BufferUnderflowException bufe) { - // correct result + ByteBuffer bb = ByteBuffer.allocateDirect(10); + fc.read(bb); + bb.flip(); + if (bb.get() != 2) + throw new RuntimeException("Write failure"); + if (bb.get() != 3) + throw new RuntimeException("Write failure"); + try { + bb.get(); + throw new RuntimeException("Write failure"); + } catch (BufferUnderflowException bufe) { + // correct result + } + } finally { + fc.close(); } - fis.close(); + + // eagerly clean-up + testFile.delete(); } // Test write to a negative position (bug 4698138). diff --git a/test/java/nio/channels/Pipe/NonBlocking.java b/test/java/nio/channels/Pipe/NonBlocking.java index 841cbc46f..6a052e795 100644 --- a/test/java/nio/channels/Pipe/NonBlocking.java +++ b/test/java/nio/channels/Pipe/NonBlocking.java @@ -41,12 +41,17 @@ public class NonBlocking { static void test1() throws Exception { Pipe p = Pipe.open(); - p.sink().configureBlocking(false); - if (p.sink().isBlocking()) - throw new Exception("Sink still blocking"); - p.source().configureBlocking(false); - if (p.source().isBlocking()) - throw new Exception("Source still blocking"); + try { + p.sink().configureBlocking(false); + if (p.sink().isBlocking()) + throw new Exception("Sink still blocking"); + p.source().configureBlocking(false); + if (p.source().isBlocking()) + throw new Exception("Source still blocking"); + } finally { + p.sink().close(); + p.source().close(); + } } } diff --git a/test/java/nio/channels/Pipe/SelectPipe.java b/test/java/nio/channels/Pipe/SelectPipe.java index 7b85c3a53..1a942a69a 100644 --- a/test/java/nio/channels/Pipe/SelectPipe.java +++ b/test/java/nio/channels/Pipe/SelectPipe.java @@ -78,10 +78,12 @@ public class SelectPipe { totalRead += bytesRead; } while(totalRead < 10); + sink.close(); + source.close(); + selector.close(); + for(int i=0; i<10; i++) if (outgoingdata.get(i) != incomingdata.get(i)) throw new Exception("Pipe failed"); - sink.close(); - source.close(); } } diff --git a/test/java/nio/channels/SelectionKey/AtomicAttachTest.java b/test/java/nio/channels/SelectionKey/AtomicAttachTest.java index 412854fcd..1298f663d 100644 --- a/test/java/nio/channels/SelectionKey/AtomicAttachTest.java +++ b/test/java/nio/channels/SelectionKey/AtomicAttachTest.java @@ -58,6 +58,11 @@ public class AtomicAttachTest { }; t.join(); + + pipe.sink().close(); + pipe.source().close(); + selector.close(); + int count = errorCount.get(); if (count > 0) { throw new RuntimeException("Error count:" + count); diff --git a/test/java/nio/channels/Selector/BasicAccept.java b/test/java/nio/channels/Selector/BasicAccept.java index d4772032f..882fb34b8 100644 --- a/test/java/nio/channels/Selector/BasicAccept.java +++ b/test/java/nio/channels/Selector/BasicAccept.java @@ -36,49 +36,60 @@ import java.util.*; public class BasicAccept { - public static int TEST_PORT = 40170; - - static void server() throws Exception { + static void server(ServerSocketChannel ssc) throws Exception { Selector acceptSelector = Selector.open(); - ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.configureBlocking(false); - InetAddress lh = InetAddress.getLocalHost(); - InetSocketAddress isa - = new InetSocketAddress(lh, SelectorTest.TEST_PORT); - ssc.socket().bind(isa); - SelectionKey acceptKey - = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); - for (;;) { - if (acceptSelector.select() == 0) - continue; - Set readyKeys = acceptSelector.selectedKeys(); - Iterator i = readyKeys.iterator(); - while (i.hasNext()) { - SelectionKey sk = (SelectionKey)i.next(); - i.remove(); - ServerSocketChannel nextReady - = (ServerSocketChannel)sk.channel(); - SocketChannel sc = nextReady.accept(); - ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 }); - sc.write(bb); + try { + ssc.configureBlocking(false); + SelectionKey acceptKey + = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); + for (;;) { + int n = acceptSelector.select(); + if (Thread.interrupted()) + break; + if (n == 0) + continue; + Set<SelectionKey> readyKeys = acceptSelector.selectedKeys(); + Iterator<SelectionKey> i = readyKeys.iterator(); + while (i.hasNext()) { + SelectionKey sk = i.next(); + i.remove(); + ServerSocketChannel nextReady + = (ServerSocketChannel)sk.channel(); + SocketChannel sc = nextReady.accept(); + ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 }); + sc.write(bb); + sc.close(); + } } + } finally { + acceptSelector.close(); } } private static class Server extends TestThread { - Server() { + final ServerSocketChannel ssc; + Server() throws IOException { super("Server", System.err); + this.ssc = ServerSocketChannel.open() + .bind(new InetSocketAddress(0)); + } + int port() { + return ssc.socket().getLocalPort(); } void go() throws Exception { - server(); + try { + server(ssc); + } finally { + ssc.close(); + } } } - static void client() throws Exception { + static void client(int port) throws Exception { // Get a connection from the server InetAddress lh = InetAddress.getLocalHost(); InetSocketAddress isa - = new InetSocketAddress(lh, SelectorTest.TEST_PORT); + = new InetSocketAddress(lh, port); int connectFailures = 0; boolean result = false; SocketChannel sc = SocketChannel.open(); @@ -122,17 +133,17 @@ public class BasicAccept { if (bb.get(0) != 42) throw new RuntimeException("Read wrong byte from server"); System.err.println("Read from server"); + sc.close(); } public static void main(String[] args) throws Exception { - if (args.length == 0) { - Server server = new Server(); - server.start(); - client(); - } else if (args[0].equals("client")) { - client(); - } else if (args[0].equals("server")) { - server(); + Server server = new Server(); + server.start(); + try { + client(server.port()); + } finally { + server.interrupt(); + server.finish(2000); } } diff --git a/test/java/nio/channels/Selector/BasicConnect.java b/test/java/nio/channels/Selector/BasicConnect.java index a7043568c..5a282661b 100644 --- a/test/java/nio/channels/Selector/BasicConnect.java +++ b/test/java/nio/channels/Selector/BasicConnect.java @@ -83,10 +83,13 @@ public class BasicConnect { ByteBuffer bb2 = ByteBuffer.allocateDirect(100); int n = sc.read(bb2); bb2.flip(); + + sc.close(); + connectSelector.close(); + if (!bb.equals(bb2)) throw new Exception("Echoed bytes incorrect: Sent " + bb + ", got " + bb2); - sc.close(); } } diff --git a/test/java/nio/channels/Selector/CheckLocking.java b/test/java/nio/channels/Selector/CheckLocking.java index 237b3a07a..9a937aca5 100644 --- a/test/java/nio/channels/Selector/CheckLocking.java +++ b/test/java/nio/channels/Selector/CheckLocking.java @@ -61,5 +61,7 @@ public class CheckLocking implements Runnable { doSelect(); sk.interestOps(SelectionKey.OP_READ); selector.wakeup(); + sc.close(); + selector.close(); } } diff --git a/test/java/nio/channels/Selector/CloseInvalidatesKeys.java b/test/java/nio/channels/Selector/CloseInvalidatesKeys.java index 7ad9fec3f..d339d54c0 100644 --- a/test/java/nio/channels/Selector/CloseInvalidatesKeys.java +++ b/test/java/nio/channels/Selector/CloseInvalidatesKeys.java @@ -33,12 +33,16 @@ public class CloseInvalidatesKeys { public static void main (String [] args) throws Exception { DatagramChannel ch = DatagramChannel.open(); - ch.configureBlocking(false); - Selector sel = Selector.open(); - SelectionKey key = ch.register(sel, SelectionKey.OP_WRITE); - sel.close(); - if (key.isValid()) - throw new Exception("Key valid after selector closed"); + try { + ch.configureBlocking(false); + Selector sel = Selector.open(); + SelectionKey key = ch.register(sel, SelectionKey.OP_WRITE); + sel.close(); + if (key.isValid()) + throw new Exception("Key valid after selector closed"); + } finally { + ch.close(); + } } } diff --git a/test/java/nio/channels/Selector/CloseWhenKeyIdle.java b/test/java/nio/channels/Selector/CloseWhenKeyIdle.java index 5217a86d6..b206b5d36 100644 --- a/test/java/nio/channels/Selector/CloseWhenKeyIdle.java +++ b/test/java/nio/channels/Selector/CloseWhenKeyIdle.java @@ -111,10 +111,14 @@ public class CloseWhenKeyIdle { // select should block int spinCount = 0; + boolean failed = false; for (;;) { int n = sel.select(); - if (n > 0) - throw new RuntimeException("channel should not be selected"); + if (n > 0) { + System.err.println("Channel should not be selected!!!"); + failed = true; + break; + } // wakeup if (wakeupDone) @@ -123,10 +127,19 @@ public class CloseWhenKeyIdle { // wakeup for no reason - if it happens a few times then we have a // problem spinCount++; - if (spinCount >= 3) - throw new RuntimeException("Selector appears to be spinning"); + if (spinCount >= 3) { + System.err.println("Selector appears to be spinning"); + failed = true; + break; + } } + sc1.close(); + sel.close(); + + if (failed) + throw new RuntimeException("Test failed"); + System.out.println("PASS"); } diff --git a/test/java/nio/channels/Selector/Connect.java b/test/java/nio/channels/Selector/Connect.java index 3c9292a85..773e0f9cd 100644 --- a/test/java/nio/channels/Selector/Connect.java +++ b/test/java/nio/channels/Selector/Connect.java @@ -25,7 +25,6 @@ * @bug 4511624 * @summary Test Making lots of Selectors * @library .. - * @run main/timeout=240 Connect */ import java.io.*; @@ -38,7 +37,7 @@ import java.nio.channels.spi.SelectorProvider; public class Connect { static int success = 0; - static int LIMIT = 500; + static int LIMIT = 100; public static void main(String[] args) throws Exception { scaleTest(); @@ -51,29 +50,30 @@ public class Connect { for (int j=0; j<LIMIT; j++) { SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); - boolean result = sc.connect(isa); - if (!result) { + boolean connected = sc.connect(isa); + if (!connected) { Selector RSelector = SelectorProvider.provider().openSelector(); SelectionKey RKey = sc.register (RSelector, SelectionKey.OP_CONNECT); - while (!result) { + while (!connected) { int keysAdded = RSelector.select(100); if (keysAdded > 0) { - Set readyKeys = RSelector.selectedKeys(); - Iterator i = readyKeys.iterator(); + Set<SelectionKey> readyKeys = RSelector.selectedKeys(); + Iterator<SelectionKey> i = readyKeys.iterator(); while (i.hasNext()) { - SelectionKey sk = (SelectionKey)i.next(); + SelectionKey sk = i.next(); SocketChannel nextReady = (SocketChannel)sk.channel(); - result = nextReady.finishConnect(); + connected = nextReady.finishConnect(); } + readyKeys.clear(); } } RSelector.close(); } - read(sc); + readAndClose(sc); } } - static void read(SocketChannel sc) throws Exception { + static void readAndClose(SocketChannel sc) throws Exception { ByteBuffer bb = ByteBuffer.allocateDirect(100); int n = 0; while (n == 0) // Note this is not a rigorous check for done reading diff --git a/test/java/nio/channels/Selector/ConnectWrite.java b/test/java/nio/channels/Selector/ConnectWrite.java index c7684bf2a..16c7ece00 100644 --- a/test/java/nio/channels/Selector/ConnectWrite.java +++ b/test/java/nio/channels/Selector/ConnectWrite.java @@ -45,8 +45,8 @@ public class ConnectWrite { Selector selector = SelectorProvider.provider().openSelector(); InetAddress myAddress=InetAddress.getByName(TestUtil.HOST); InetSocketAddress isa = new InetSocketAddress(myAddress, port); + SocketChannel sc = SocketChannel.open(); try { - SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT); boolean result = sc.connect(isa); @@ -80,6 +80,7 @@ public class ConnectWrite { } } } finally { + sc.close(); selector.close(); } } diff --git a/test/java/nio/channels/Selector/HelperSlowToDie.java b/test/java/nio/channels/Selector/HelperSlowToDie.java index 93c710a60..221722abf 100644 --- a/test/java/nio/channels/Selector/HelperSlowToDie.java +++ b/test/java/nio/channels/Selector/HelperSlowToDie.java @@ -33,9 +33,15 @@ import java.io.IOException; public class HelperSlowToDie { private static final int CHANNELS_PER_THREAD = 1023; + private static final int TEST_ITERATIONS = 200; private static volatile boolean done; public static void main(String[] args) throws IOException { + if (!System.getProperty("os.name").startsWith("Windows")) { + System.out.println("Test skipped as it verifies a Windows specific bug"); + return; + } + Selector sel = Selector.open(); // register channels @@ -60,7 +66,7 @@ public class HelperSlowToDie { new Thread(busy).start(); // Loop changing the number of channels from 1023 to 1024 and back. - for (int i=0; i<1000; i++) { + for (int i=0; i<TEST_ITERATIONS; i++) { SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); sc.register(sel, SelectionKey.OP_CONNECT); @@ -71,5 +77,11 @@ public class HelperSlowToDie { // terminate busy threads done = true; + + // clean-up + for (int i=0; i<CHANNELS_PER_THREAD; i++) { + channels[i].close(); + } + sel.close(); } } diff --git a/test/java/nio/channels/Selector/KeysReady.java b/test/java/nio/channels/Selector/KeysReady.java index c5a914053..4fa9ea6d9 100644 --- a/test/java/nio/channels/Selector/KeysReady.java +++ b/test/java/nio/channels/Selector/KeysReady.java @@ -49,14 +49,18 @@ public class KeysReady { // Prepare a selector Selector selector = SelectorProvider.provider().openSelector(); - SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT); - int keysAdded = selector.select(); - if (keysAdded > 0) { - keysAdded = selector.select(1000); - if (keysAdded > 0) - throw new Exception("Same key reported added twice"); + try { + SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT); + int keysAdded = selector.select(); + if (keysAdded > 0) { + keysAdded = selector.select(1000); + if (keysAdded > 0) + throw new Exception("Same key reported added twice"); + } + } finally { + selector.close(); + sc.close(); } - sc.close(); } public static void main(String[] args) throws Exception { diff --git a/test/java/nio/channels/Selector/LotsOfChannels.java b/test/java/nio/channels/Selector/LotsOfChannels.java index 8d4597e24..0ffef3a8f 100644 --- a/test/java/nio/channels/Selector/LotsOfChannels.java +++ b/test/java/nio/channels/Selector/LotsOfChannels.java @@ -35,19 +35,11 @@ import java.nio.channels.*; public class LotsOfChannels { - private final static int PIPES_COUNT = 1900; + private final static int PIPES_COUNT = 256; private final static int BUF_SIZE = 8192; private final static int LOOPS = 10; public static void main(String[] argv) throws Exception { - - - String os = System.getProperty("os.name"); - if (!(os.equals("Windows NT") - || os.equals("Windows 2000") - || os.equals("Windows XP"))) - return; - Pipe[] pipes = new Pipe[PIPES_COUNT]; Pipe pipe = Pipe.open(); Pipe.SinkChannel sink = pipe.sink(); @@ -72,6 +64,13 @@ public class LotsOfChannels { sel.selectedKeys().clear(); source.read(ByteBuffer.allocate(BUF_SIZE)); } + + for (int i = 0; i < PIPES_COUNT; i++ ) { + pipes[i].sink().close(); + pipes[i].source().close(); + } + pipe.sink().close(); + pipe.source().close(); sel.close(); } } diff --git a/test/java/nio/channels/Selector/RegAfterPreClose.java b/test/java/nio/channels/Selector/RegAfterPreClose.java index 71c4cac32..e2128cf46 100644 --- a/test/java/nio/channels/Selector/RegAfterPreClose.java +++ b/test/java/nio/channels/Selector/RegAfterPreClose.java @@ -35,6 +35,7 @@ import java.io.IOException; public class RegAfterPreClose { + static final int TEST_ITERATIONS = 300; static volatile boolean done; /** @@ -96,26 +97,21 @@ public class RegAfterPreClose { } }; - // schedule test to run for 1 minute - Executors.newScheduledThreadPool(1, factory).schedule(new Runnable() { - public void run() { - done = true; - sel.wakeup(); - }}, 1, TimeUnit.MINUTES); - // create Executor that handles tasks that closes channels // "asynchronously" - this creates the conditions to provoke the bug. - Executor executor = Executors.newFixedThreadPool(2, factory); + ExecutorService executor = Executors.newFixedThreadPool(2, factory); // submit task that connects to listener executor.execute(new Connector(ssc.socket().getLocalPort())); // loop accepting connections until done (or an IOException is thrown) - while (!done) { + int remaining = TEST_ITERATIONS; + while (remaining > 0) { sel.select(); if (key.isAcceptable()) { SocketChannel sc = ssc.accept(); if (sc != null) { + remaining--; sc.configureBlocking(false); sc.register(sel, SelectionKey.OP_READ); executor.execute(new Closer(sc)); @@ -123,5 +119,8 @@ public class RegAfterPreClose { } sel.selectedKeys().clear(); } + done = true; + sel.close(); + executor.shutdown(); } } diff --git a/test/java/nio/channels/Selector/SelectAndCancel.java b/test/java/nio/channels/Selector/SelectAndCancel.java index e85c1bd93..2d717a080 100644 --- a/test/java/nio/channels/Selector/SelectAndCancel.java +++ b/test/java/nio/channels/Selector/SelectAndCancel.java @@ -31,11 +31,7 @@ import java.io.IOException; import java.net.*; public class SelectAndCancel { - static ServerSocketChannel ssc; - static Selector selector; static SelectionKey sk; - static InetSocketAddress isa; - public static int TEST_PORT = 40170; /* * CancelledKeyException is the failure symptom of 4729342 @@ -43,17 +39,17 @@ public class SelectAndCancel { * seen immediately when the bug is present. */ public static void main(String[] args) throws Exception { - InetAddress lh = InetAddress.getLocalHost(); - isa = new InetSocketAddress(lh, TEST_PORT); - selector = Selector.open(); - ssc = ServerSocketChannel.open(); + final Selector selector = Selector.open(); + final ServerSocketChannel ssc = + ServerSocketChannel.open().bind(new InetSocketAddress(0)); + final InetSocketAddress isa = + new InetSocketAddress(InetAddress.getLocalHost(), ssc.socket().getLocalPort()); // Create and start a selector in a separate thread. new Thread(new Runnable() { public void run() { try { ssc.configureBlocking(false); - ssc.socket().bind(isa); sk = ssc.register(selector, SelectionKey.OP_ACCEPT); selector.select(); } catch (IOException e) { diff --git a/test/java/nio/channels/Selector/SelectorLimit.java b/test/java/nio/channels/Selector/SelectorLimit.java index f84fa55a8..2c5594a2d 100644 --- a/test/java/nio/channels/Selector/SelectorLimit.java +++ b/test/java/nio/channels/Selector/SelectorLimit.java @@ -26,6 +26,8 @@ * @summary Ensure that a Selector can return at least 100 selected keys * @author Mark Reinhold * @library .. + * @build SelectorLimit + * @run main/othervm SelectorLimit */ import java.io.*; diff --git a/test/java/nio/channels/Selector/SelectorTest.java b/test/java/nio/channels/Selector/SelectorTest.java index 8b8118393..cee50d562 100644 --- a/test/java/nio/channels/Selector/SelectorTest.java +++ b/test/java/nio/channels/Selector/SelectorTest.java @@ -57,13 +57,13 @@ public class SelectorTest { */ public static void main(String[] args) throws Exception { if (args.length == 0) { - InetSocketAddress isa - = new InetSocketAddress(InetAddress.getLocalHost(), TEST_PORT); - Server server = new Server(isa); + Server server = new Server(0); server.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } + InetSocketAddress isa + = new InetSocketAddress(InetAddress.getLocalHost(), server.port()); Client client = new Client(isa); client.start(); if ((server.finish(FINISH_TIME) & client.finish(FINISH_TIME)) == 0) @@ -74,9 +74,7 @@ public class SelectorTest { if (args.length > 1) TEST_PORT = Integer.parseInt(args[1]); - InetSocketAddress isa - = new InetSocketAddress(InetAddress.getLocalHost(), TEST_PORT); - Server server = new Server(isa); + Server server = new Server(TEST_PORT); server.start(); if (server.finish(FINISH_TIME) == 0) throw new Exception("Failure"); @@ -136,18 +134,22 @@ public class SelectorTest { } static class Server extends TestThread { + private final ServerSocketChannel ssc; private List socketList = new ArrayList(); private ServerSocket ss; private int connectionsAccepted = 0; private Selector pollSelector; private Selector acceptSelector; - private InetSocketAddress isa; private Set pkeys; private Set pskeys; - Server(InetSocketAddress isa) { + Server(int port) throws IOException { super("Server", SelectorTest.log); - this.isa = isa; + this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(port)); + } + + int port() { + return ssc.socket().getLocalPort(); } public void go() throws Exception { @@ -162,11 +164,7 @@ public class SelectorTest { requestThread.start(); - ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); - ssc.socket().setReuseAddress(true); - ssc.socket().bind(isa); - SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); while(connectionsAccepted < SelectorTest.NUM_CLIENTS) { diff --git a/test/java/nio/channels/Selector/WakeupNow.java b/test/java/nio/channels/Selector/WakeupNow.java index 4c2452a6c..7934e9a85 100644 --- a/test/java/nio/channels/Selector/WakeupNow.java +++ b/test/java/nio/channels/Selector/WakeupNow.java @@ -44,10 +44,15 @@ public class WakeupNow { p.source().configureBlocking(false); p.source().register(sel, SelectionKey.OP_READ); sel.wakeup(); + // ensure wakeup is consumed by selectNow + Thread.sleep(2000); sel.selectNow(); long startTime = System.currentTimeMillis(); int n = sel.select(2000); long endTime = System.currentTimeMillis(); + p.source().close(); + p.sink().close(); + sel.close(); if (endTime - startTime < 1000) throw new RuntimeException("test failed"); } @@ -60,10 +65,13 @@ public class WakeupNow { Pipe p = Pipe.open(); p.source().configureBlocking(false); sel.wakeup(); + // ensure wakeup is consumed by selectNow + Thread.sleep(2000); sel.selectNow(); long startTime = System.currentTimeMillis(); int n = sel.select(2000); long endTime = System.currentTimeMillis(); + sel.close(); if (endTime - startTime < 1000) throw new RuntimeException("test failed"); } diff --git a/test/java/nio/channels/Selector/WakeupOverflow.java b/test/java/nio/channels/Selector/WakeupOverflow.java index d555bcb55..dc1c5fc4a 100644 --- a/test/java/nio/channels/Selector/WakeupOverflow.java +++ b/test/java/nio/channels/Selector/WakeupOverflow.java @@ -31,8 +31,12 @@ import java.nio.channels.*; public class WakeupOverflow { public static void main( String[] args ) throws Exception { Selector selector = Selector.open(); - for(int i=0; i<10000; i++) { - selector.wakeup(); + try { + for(int i=0; i<10000; i++) { + selector.wakeup(); + } + } finally { + selector.close(); } } } diff --git a/test/java/nio/channels/Selector/WakeupSpeed.java b/test/java/nio/channels/Selector/WakeupSpeed.java index d2aba7124..e9ce071f0 100644 --- a/test/java/nio/channels/Selector/WakeupSpeed.java +++ b/test/java/nio/channels/Selector/WakeupSpeed.java @@ -35,16 +35,19 @@ public class WakeupSpeed { public static void main(String argv[]) throws Exception { int waitTime = 4000; Selector selector = Selector.open(); - - selector.wakeup(); - - long t1 = System.currentTimeMillis(); - selector.select(waitTime); - long t2 = System.currentTimeMillis(); - long totalTime = t2 - t1; - - if (totalTime > waitTime) - throw new RuntimeException("Test failed"); + try { + selector.wakeup(); + + long t1 = System.currentTimeMillis(); + selector.select(waitTime); + long t2 = System.currentTimeMillis(); + long totalTime = t2 - t1; + + if (totalTime > waitTime) + throw new RuntimeException("Test failed"); + } finally { + selector.close(); + } } } diff --git a/test/java/nio/channels/ServerSocketChannel/AcceptAddress.java b/test/java/nio/channels/ServerSocketChannel/AcceptAddress.java deleted file mode 100644 index 0461ee682..000000000 --- a/test/java/nio/channels/ServerSocketChannel/AcceptAddress.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* @test - * @summary test the address returned in socket from accept - */ - -import java.io.*; -import java.net.*; -import java.nio.*; -import java.nio.channels.*; - - -public class AcceptAddress { - - public static void main(String[] args) throws Exception { - InetAddress local = InetAddress.getLocalHost(); - InetSocketAddress isa = new InetSocketAddress(local, 5555); - - ServerSocketChannel ssc; - ssc = ServerSocketChannel.open(); - ssc.socket().bind(isa); - - SocketChannel sc; - sc = SocketChannel.open(); - sc.connect(isa); - - SocketChannel sc2 = ssc.accept(); - System.err.println("Socket connected to " + sc2); - } -} diff --git a/test/java/nio/channels/SocketChannel/AdaptSocket.java b/test/java/nio/channels/SocketChannel/AdaptSocket.java index 17c085468..875fc268e 100644 --- a/test/java/nio/channels/SocketChannel/AdaptSocket.java +++ b/test/java/nio/channels/SocketChannel/AdaptSocket.java @@ -78,8 +78,6 @@ public class AdaptSocket { throw x; } } - if (shouldTimeout) - throw new Exception("Connection did not time out"); } out.println("connected: " + so); out.println(" " + sc); @@ -118,8 +116,6 @@ public class AdaptSocket { } throw x; } - if (shouldTimeout) - throw new Exception("Read did not time out"); } static void testRead(String hn, int timeout, boolean shouldTimeout) diff --git a/test/java/nio/channels/SocketChannel/Bind.java b/test/java/nio/channels/SocketChannel/Bind.java index d0cb8ea7c..cfa99aa88 100644 --- a/test/java/nio/channels/SocketChannel/Bind.java +++ b/test/java/nio/channels/SocketChannel/Bind.java @@ -31,13 +31,20 @@ import java.nio.channels.*; public class Bind { public static void main(String[] args) throws Exception { + SocketChannel sc1 = SocketChannel.open(); try { - SocketChannel channel1 = SocketChannel.open(); - channel1.socket().bind(new InetSocketAddress(5555)); - SocketChannel channel2 = SocketChannel.open(); - channel2.socket().bind(new InetSocketAddress(5555)); + sc1.bind(new InetSocketAddress(0)); + int port = sc1.socket().getLocalPort(); + SocketChannel sc2 = SocketChannel.open(); + try { + sc2.bind(new InetSocketAddress(port)); + } finally { + sc2.close(); + } } catch (BindException be) { // Correct result + } finally { + sc1.close(); } } } diff --git a/test/java/nio/channels/SocketChannel/Close.java b/test/java/nio/channels/SocketChannel/Close.java index f4b1c7df6..7a677f087 100644 --- a/test/java/nio/channels/SocketChannel/Close.java +++ b/test/java/nio/channels/SocketChannel/Close.java @@ -56,8 +56,12 @@ public class Close { static void testChannelClose() throws IOException { SelectionKey sk = open(); - sk.channel().close(); - check(sk); + try { + sk.channel().close(); + check(sk); + } finally { + sk.selector().close(); + } } public static void main(String[] args) throws Exception { diff --git a/test/java/nio/channels/SocketChannel/CloseRegisteredChannel.java b/test/java/nio/channels/SocketChannel/CloseRegisteredChannel.java index fb39cd0bc..92836a8b2 100644 --- a/test/java/nio/channels/SocketChannel/CloseRegisteredChannel.java +++ b/test/java/nio/channels/SocketChannel/CloseRegisteredChannel.java @@ -55,5 +55,8 @@ public class CloseRegisteredChannel { System.out.println ("Will hang here..."); int nb = slave.read (ByteBuffer.allocate (1024)); //System.out.println("read nb=" + nb); + + selector.close(); + server.close(); } } diff --git a/test/java/nio/channels/SocketChannel/CloseTimeoutChannel.java b/test/java/nio/channels/SocketChannel/CloseTimeoutChannel.java index 4b51e43f2..67e335090 100644 --- a/test/java/nio/channels/SocketChannel/CloseTimeoutChannel.java +++ b/test/java/nio/channels/SocketChannel/CloseTimeoutChannel.java @@ -33,11 +33,12 @@ import java.nio.channels.*; import java.net.*; public class CloseTimeoutChannel { - final static int PORT=6347; public static void main(String args[]) throws Exception { + int port = -1; try { ServerSocketChannel listener=ServerSocketChannel.open(); - listener.socket().bind(new InetSocketAddress(PORT)); + listener.socket().bind(new InetSocketAddress(0)); + port = listener.socket().getLocalPort(); AcceptorThread thread=new AcceptorThread(listener); thread.start(); } catch (IOException e) { @@ -50,7 +51,7 @@ public class CloseTimeoutChannel { try { System.out.println("Establishing connection"); Socket socket=SocketChannel.open( - new InetSocketAddress("127.0.0.1", PORT)).socket(); + new InetSocketAddress("127.0.0.1", port)).socket(); OutputStream out=socket.getOutputStream(); InputStream in=socket.getInputStream(); @@ -98,7 +99,8 @@ public class CloseTimeoutChannel { Thread.sleep(100); } catch (InterruptedException e) { } - System.out.println(INDENT+"Listening on port "+ PORT); + System.out.println(INDENT+"Listening on port "+ + _listener.socket().getLocalPort()); ByteBuffer buf=ByteBuffer.allocate(5); Socket client=_listener.accept().socket();; System.out.println(INDENT+"Accepted client"); @@ -123,6 +125,8 @@ public class CloseTimeoutChannel { client.close(); } catch (IOException e) { System.out.println(INDENT+"Error accepting!"); + } finally { + try { _listener.close(); } catch (IOException ignore) { } } } } diff --git a/test/java/nio/channels/SocketChannel/IsConnectable.java b/test/java/nio/channels/SocketChannel/IsConnectable.java index 71de28211..4bc22a190 100644 --- a/test/java/nio/channels/SocketChannel/IsConnectable.java +++ b/test/java/nio/channels/SocketChannel/IsConnectable.java @@ -48,27 +48,31 @@ public class IsConnectable { sc.connect(isa); Selector selector = SelectorProvider.provider().openSelector(); - SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT); - int keysAdded = selector.select(); - if (keysAdded > 0) { - boolean result = sc.finishConnect(); - if (result) { - keysAdded = selector.select(5000); - // 4750573: keysAdded should not be incremented when op is dropped - // from a key already in the selected key set - if (keysAdded > 0) - throw new Exception("Test failed: 4750573 detected"); - Set sel = selector.selectedKeys(); - Iterator i = sel.iterator(); - SelectionKey sk = (SelectionKey)i.next(); - // 4737146: isConnectable should be false while connected - if (sk.isConnectable()) - throw new Exception("Test failed: 4737146 detected"); + try { + SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT); + int keysAdded = selector.select(); + if (keysAdded > 0) { + boolean result = sc.finishConnect(); + if (result) { + keysAdded = selector.select(5000); + // 4750573: keysAdded should not be incremented when op is dropped + // from a key already in the selected key set + if (keysAdded > 0) + throw new Exception("Test failed: 4750573 detected"); + Set<SelectionKey> sel = selector.selectedKeys(); + Iterator<SelectionKey> i = sel.iterator(); + SelectionKey sk = i.next(); + // 4737146: isConnectable should be false while connected + if (sk.isConnectable()) + throw new Exception("Test failed: 4737146 detected"); + } + } else { + throw new Exception("Select failed"); } - } else { - throw new Exception("Select failed"); + } finally { + sc.close(); + selector.close(); } - sc.close(); } public static void main(String[] args) throws Exception { diff --git a/test/java/nio/channels/SocketChannel/LocalAddress.java b/test/java/nio/channels/SocketChannel/LocalAddress.java index a630bf121..9561e9752 100644 --- a/test/java/nio/channels/SocketChannel/LocalAddress.java +++ b/test/java/nio/channels/SocketChannel/LocalAddress.java @@ -38,25 +38,33 @@ public class LocalAddress { static void test1() throws Exception { InetAddress bogus = InetAddress.getByName("0.0.0.0"); - SocketChannel sc = SocketChannel.open(); InetSocketAddress saddr = new InetSocketAddress( InetAddress.getByName(TestUtil.HOST), 23); //Test1: connect only - sc.connect(saddr); - InetAddress isa = sc.socket().getLocalAddress(); - if (isa == null || isa.equals(bogus)) - throw new RuntimeException("test failed"); + SocketChannel sc = SocketChannel.open(); + try { + sc.connect(saddr); + InetAddress ia = sc.socket().getLocalAddress(); + if (ia == null || ia.equals(bogus)) + throw new RuntimeException("test failed"); + } finally { + sc.close(); + } //Test2: bind and connect sc = SocketChannel.open(); - sc.socket().bind(new InetSocketAddress(0)); - if (sc.socket().getLocalPort() == 0) - throw new RuntimeException("test failed"); - sc.socket().connect(saddr); - isa = sc.socket().getLocalAddress(); - if (isa == null || isa.isAnyLocalAddress()) - throw new RuntimeException("test failed"); + try { + sc.socket().bind(new InetSocketAddress(0)); + if (sc.socket().getLocalPort() == 0) + throw new RuntimeException("test failed"); + sc.socket().connect(saddr); + InetAddress ia = sc.socket().getLocalAddress(); + if (ia == null || ia.isAnyLocalAddress()) + throw new RuntimeException("test failed"); + } finally { + sc.close(); + } } } diff --git a/test/java/nio/channels/SocketChannel/SocketInheritance.java b/test/java/nio/channels/SocketChannel/SocketInheritance.java index aaeb1b2d1..387a06280 100644 --- a/test/java/nio/channels/SocketChannel/SocketInheritance.java +++ b/test/java/nio/channels/SocketChannel/SocketInheritance.java @@ -105,7 +105,11 @@ public class SocketInheritance { // launch the child String cmd = System.getProperty("java.home") + File.separator + "bin" + - File.separator + "java SocketInheritance -child " + port; + File.separator + "java"; + String testClasses = System.getProperty("test.classes"); + if (testClasses != null) + cmd += " -cp " + testClasses; + cmd += " SocketInheritance -child " + port; Process p = Runtime.getRuntime().exec(cmd); diff --git a/test/java/nio/channels/SocketChannel/Trivial.java b/test/java/nio/channels/SocketChannel/Trivial.java index 4eb182fc5..afe7bfa10 100644 --- a/test/java/nio/channels/SocketChannel/Trivial.java +++ b/test/java/nio/channels/SocketChannel/Trivial.java @@ -33,22 +33,27 @@ public class Trivial { public static void main(String[] args) throws Exception { SocketChannel sc = SocketChannel.open(); Selector sel = Selector.open(); - if (sc.keyFor(sel) != null) - throw new Exception("keyFor != null"); - sc.configureBlocking(false); - SelectionKey sk = sc.register(sel, SelectionKey.OP_READ, args); - if (sc.keyFor(sel) != sk) - throw new Exception("keyFor returned " + sc.keyFor(sel)); - if (sk.attachment() != args) - throw new Exception("attachment() returned " + sk.attachment()); - Trivial t = new Trivial(); - sk.attach(t); - if (sk.attachment() != t) - throw new Exception("Wrong attachment"); - sk.isReadable(); - sk.isWritable(); - sk.isConnectable(); - sk.isAcceptable(); + try { + if (sc.keyFor(sel) != null) + throw new Exception("keyFor != null"); + sc.configureBlocking(false); + SelectionKey sk = sc.register(sel, SelectionKey.OP_READ, args); + if (sc.keyFor(sel) != sk) + throw new Exception("keyFor returned " + sc.keyFor(sel)); + if (sk.attachment() != args) + throw new Exception("attachment() returned " + sk.attachment()); + Trivial t = new Trivial(); + sk.attach(t); + if (sk.attachment() != t) + throw new Exception("Wrong attachment"); + sk.isReadable(); + sk.isWritable(); + sk.isConnectable(); + sk.isAcceptable(); + } finally { + sel.close(); + sc.close(); + } } } diff --git a/test/java/nio/channels/SocketChannel/UnboundSocketTests.java b/test/java/nio/channels/SocketChannel/UnboundSocketTests.java index ae39e45da..b51e71405 100644 --- a/test/java/nio/channels/SocketChannel/UnboundSocketTests.java +++ b/test/java/nio/channels/SocketChannel/UnboundSocketTests.java @@ -57,39 +57,47 @@ public class UnboundSocketTests { System.out.println("\n-- SocketChannel --"); SocketChannel sc = SocketChannel.open(); - - check("getLocalPort()", sc.socket().getLocalPort(), -1); - checkIsAnyLocalAddress("getLocalAddress()", - sc.socket().getLocalAddress()); - check("getLocalSocketAddress()", sc.socket().getLocalSocketAddress(), null); - - check("getPort()", sc.socket().getPort(), 0); - check("getInetAddress()", sc.socket().getInetAddress(), null); - check("getRemoteSocketAddress()", sc.socket().getRemoteSocketAddress(), null); - + try { + check("getLocalPort()", sc.socket().getLocalPort(), -1); + checkIsAnyLocalAddress("getLocalAddress()", + sc.socket().getLocalAddress()); + check("getLocalSocketAddress()", sc.socket().getLocalSocketAddress(), null); + + check("getPort()", sc.socket().getPort(), 0); + check("getInetAddress()", sc.socket().getInetAddress(), null); + check("getRemoteSocketAddress()", sc.socket().getRemoteSocketAddress(), null); + } finally { + sc.close(); + } System.out.println("\n-- ServerSocketChannel --"); ServerSocketChannel ssc = ServerSocketChannel.open(); - - check("getLocalPort()", ssc.socket().getLocalPort(), -1); - check("getInetAddress()", ssc.socket().getInetAddress(), null); - check("getLocalSocketAddress()", ssc.socket().getLocalSocketAddress(), null); + try { + check("getLocalPort()", ssc.socket().getLocalPort(), -1); + check("getInetAddress()", ssc.socket().getInetAddress(), null); + check("getLocalSocketAddress()", ssc.socket().getLocalSocketAddress(), null); + } finally { + ssc.close(); + } System.out.println("\n-- DatagramChannel --"); DatagramChannel dc = DatagramChannel.open(); - - // not specified - check("getLocalPort()", dc.socket().getLocalPort(), 0); - - checkIsAnyLocalAddress("getLocalAddress()", - dc.socket().getLocalAddress()); - check("getLocalSocketAddress()", dc.socket().getLocalSocketAddress(), null); - - check("getPort()", dc.socket().getPort(), -1); - check("getInetAddress()", dc.socket().getInetAddress(), null); - check("getRemoteSocketAddress()", dc.socket().getRemoteSocketAddress(), null); + try { + // not specified + check("getLocalPort()", dc.socket().getLocalPort(), 0); + + checkIsAnyLocalAddress("getLocalAddress()", + dc.socket().getLocalAddress()); + check("getLocalSocketAddress()", dc.socket().getLocalSocketAddress(), null); + + check("getPort()", dc.socket().getPort(), -1); + check("getInetAddress()", dc.socket().getInetAddress(), null); + check("getRemoteSocketAddress()", dc.socket().getRemoteSocketAddress(), null); + } finally { + dc.close(); + } if (failures > 0) { throw new RuntimeException(failures + " sub-tests(s) failed."); diff --git a/test/java/nio/channels/etc/Shadow.java b/test/java/nio/channels/etc/Shadow.java index b99c80df0..fbdbd0e86 100644 --- a/test/java/nio/channels/etc/Shadow.java +++ b/test/java/nio/channels/etc/Shadow.java @@ -35,7 +35,6 @@ import java.nio.channels.*; public class Shadow { - private static final int PORT = 2019; static PrintStream log = System.err; private static void dump(ServerSocket s) { @@ -69,7 +68,7 @@ public class Shadow { public static void main(String[] args) throws Exception { boolean useChannels = ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue()); - int port = (args.length > 1 ? Integer.parseInt(args[1]) : PORT); + int port = (args.length > 1 ? Integer.parseInt(args[1]) : -1); // open server socket ServerSocket serverSocket; @@ -86,7 +85,8 @@ public class Shadow { } // bind server socket to port - SocketAddress bindAddr = new InetSocketAddress(port); + SocketAddress bindAddr = + new InetSocketAddress((port == -1) ? 0 : port); serverSocket.bind(bindAddr); log.println("bound ServerSocket: " + serverSocket); diff --git a/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/ClosedStreams.java b/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/ClosedStreams.java index 4d175e4b8..a469e1948 100644 --- a/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/ClosedStreams.java +++ b/test/java/nio/channels/spi/SelectorProvider/inheritedChannel/ClosedStreams.java @@ -26,6 +26,8 @@ * @bug 4997227 * @summary Calling inheritedChannel() after FileDescriptor.in was closed * caused an InternalError to be thrown. + * @build ClosedStreams + * @run main/othervm ClosedStreams */ import java.io.FileDescriptor; |