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/Selector | |
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/Selector')
17 files changed, 178 insertions, 119 deletions
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(); + } } } |