--- old/test/java/nio/channels/Selector/SelectAfterRead.java 2016-01-15 03:29:29.153139426 -0800 +++ new/test/java/nio/channels/Selector/SelectAfterRead.java 2016-01-15 03:29:29.034139421 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2016, 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 @@ -22,12 +22,13 @@ */ /* @test - * @bug 4639943 + * @bug 4639943 8085792 * @summary Checks that Windows behavior matches Solaris for * various read/select combinations. * @author kladko */ +import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.nio.channels.Selector; import java.nio.channels.SelectionKey; @@ -36,54 +37,75 @@ public class SelectAfterRead { private static final int TIMEOUT = 1000; + private static final int SERVER_ACCEPT_TIMEOUT = 100; public static void main(String[] argv) throws Exception { - // server: accept connection and write one byte - try (ByteServer server = new ByteServer(); - SocketChannel sc = SocketChannel.open(server.address())) { - - server.acceptConnection(); - server.write(1); - - try (Selector sel = Selector.open()) { - sc.read(ByteBuffer.allocate(1)); - sc.configureBlocking(false); - sc.register(sel, SelectionKey.OP_READ); - // previously on Windows select would select channel here, although there was - // nothing to read - if (sel.selectNow() != 0) - throw new Exception("Select returned nonzero value"); + while (true) { + // server: accept connection and write one byte + try (ByteServer server = new ByteServer(); + SocketChannel sc = SocketChannel.open(server.address())) { + + server.setTimeout(SERVER_ACCEPT_TIMEOUT); + try { + server.acceptConnection(); + } catch (SocketTimeoutException ex) { + System.err.println("accept connection(write one byte) timeout..."); + continue; + } + server.write(1); + + try (Selector sel = Selector.open()) { + sc.read(ByteBuffer.allocate(1)); + sc.configureBlocking(false); + sc.register(sel, SelectionKey.OP_READ); + // previously on Windows select would select channel here, although there was + // nothing to read + if (sel.selectNow() != 0) + throw new Exception("Select returned nonzero value"); + } } + System.out.println("Test(write one byte) successes."); + break; } - // Now we will test a two reads combination - // server: accept connection and write two bytes - try (ByteServer server = new ByteServer(); - SocketChannel sc = SocketChannel.open(server.address())) { - - server.acceptConnection(); - server.write(2); - - try (Selector sel = Selector.open()) { - sc.configureBlocking(false); - sc.register(sel, SelectionKey.OP_READ); - if (sel.select(TIMEOUT) != 1) - throw new Exception("One selected key expected"); - sel.selectedKeys().clear(); - // previously on Windows a channel would get selected only once - if (sel.selectNow() != 1) - throw new Exception("One selected key expected"); - // Previously on Windows two consequent reads would cause select() - // to select a channel, although there was nothing remaining to - // read in the channel - if (sc.read(ByteBuffer.allocate(1)) != 1) - throw new Exception("One byte expected"); - if (sc.read(ByteBuffer.allocate(1)) != 1) - throw new Exception("One byte expected"); - if (sel.selectNow() != 0) - throw new Exception("Select returned nonzero value"); + while (true) { + // Now we will test a two reads combination + // server: accept connection and write two bytes + try (ByteServer server = new ByteServer(); + SocketChannel sc = SocketChannel.open(server.address())) { + + server.setTimeout(SERVER_ACCEPT_TIMEOUT); + try { + server.acceptConnection(); + } catch (SocketTimeoutException ex) { + System.err.println("accept connection(write two bytes) timeout..."); + continue; + } + server.write(2); + + try (Selector sel = Selector.open()) { + sc.configureBlocking(false); + sc.register(sel, SelectionKey.OP_READ); + if (sel.select(TIMEOUT) != 1) + throw new Exception("One selected key expected"); + sel.selectedKeys().clear(); + // previously on Windows a channel would get selected only once + if (sel.selectNow() != 1) + throw new Exception("One selected key expected"); + // Previously on Windows two consequent reads would cause select() + // to select a channel, although there was nothing remaining to + // read in the channel + if (sc.read(ByteBuffer.allocate(1)) != 1) + throw new Exception("One byte expected"); + if (sc.read(ByteBuffer.allocate(1)) != 1) + throw new Exception("One byte expected"); + if (sel.selectNow() != 0) + throw new Exception("Select returned nonzero value"); + } } + System.out.println("Test(write two bytes) successes."); + break; } } }