0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4639943
0N/A * @summary Checks that Windows behavior matches Solaris for
0N/A * various read/select combinations.
0N/A * @author kladko
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/Apublic class SelectAfterRead {
0N/A
0N/A final static int TIMEOUT = 1000;
0N/A
0N/A public static void main(String[] argv) throws Exception {
2485N/A InetAddress lh = InetAddress.getByName(ByteServer.LOCALHOST);
2485N/A
0N/A // server: accept connection and write one byte
0N/A ByteServer server = new ByteServer(1);
0N/A server.start();
0N/A Selector sel = Selector.open();
0N/A SocketChannel sc = SocketChannel.open();
2485N/A sc.connect(new InetSocketAddress(lh, server.port()));
0N/A sc.read(ByteBuffer.allocate(1));
0N/A sc.configureBlocking(false);
0N/A sc.register(sel, SelectionKey.OP_READ);
0N/A // previously on Windows select would select channel here, although there was
0N/A // nothing to read
0N/A if (sel.selectNow() != 0)
0N/A throw new Exception("Select returned nonzero value");
0N/A sc.close();
0N/A sel.close();
0N/A server.exit();
0N/A
0N/A // Now we will test a two reads combination
0N/A // server: accept connection and write two bytes
0N/A server = new ByteServer(2);
0N/A server.start();
0N/A sc = SocketChannel.open();
2485N/A sc.connect(new InetSocketAddress(lh, server.port()));
0N/A sc.configureBlocking(false);
0N/A sel = Selector.open();
0N/A sc.register(sel, SelectionKey.OP_READ);
0N/A if (sel.select(TIMEOUT) != 1)
0N/A throw new Exception("One selected key expected");
0N/A sel.selectedKeys().clear();
0N/A // previously on Windows a channel would get selected only once
0N/A if (sel.selectNow() != 1)
0N/A throw new Exception("One selected key expected");
0N/A // Previously on Windows two consequent reads would cause select()
0N/A // to select a channel, although there was nothing remaining to
0N/A // read in the channel
0N/A if (sc.read(ByteBuffer.allocate(1)) != 1)
0N/A throw new Exception("One byte expected");
0N/A if (sc.read(ByteBuffer.allocate(1)) != 1)
0N/A throw new Exception("One byte expected");
0N/A if (sel.selectNow() != 0)
0N/A throw new Exception("Select returned nonzero value");
0N/A sc.close();
0N/A sel.close();
0N/A server.exit();
0N/A }
0N/A}