Path: blob/master/test/jdk/java/nio/channels/Selector/SelectAfterRead.java
41153 views
/*1* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 463994325* @summary Checks that Windows behavior matches Solaris for26* various read/select combinations.27* @author kladko28*/2930import java.nio.ByteBuffer;31import java.nio.channels.Selector;32import java.nio.channels.SelectionKey;33import java.nio.channels.SocketChannel;3435public class SelectAfterRead {3637private static final int TIMEOUT = 1000;3839public static void main(String[] argv) throws Exception {4041// server: accept connection and write one byte42try (ByteServer server = new ByteServer();43SocketChannel sc = SocketChannel.open(server.address())) {4445server.acceptConnection();46server.write(1);4748try (Selector sel = Selector.open()) {49sc.read(ByteBuffer.allocate(1));50sc.configureBlocking(false);51sc.register(sel, SelectionKey.OP_READ);52// previously on Windows select would select channel here, although there was53// nothing to read54if (sel.selectNow() != 0)55throw new Exception("Select returned nonzero value");56}57}5859// Now we will test a two reads combination60// server: accept connection and write two bytes61try (ByteServer server = new ByteServer();62SocketChannel sc = SocketChannel.open(server.address())) {6364server.acceptConnection();65server.write(2);6667try (Selector sel = Selector.open()) {68sc.configureBlocking(false);69sc.register(sel, SelectionKey.OP_READ);70if (sel.select(TIMEOUT) != 1)71throw new Exception("One selected key expected");72sel.selectedKeys().clear();73// previously on Windows a channel would get selected only once74if (sel.selectNow() != 1)75throw new Exception("One selected key expected");76// Previously on Windows two consequent reads would cause select()77// to select a channel, although there was nothing remaining to78// read in the channel79if (sc.read(ByteBuffer.allocate(1)) != 1)80throw new Exception("One byte expected");81if (sc.read(ByteBuffer.allocate(1)) != 1)82throw new Exception("One byte expected");83if (sel.selectNow() != 0)84throw new Exception("Select returned nonzero value");85}86}87}88}899091