test/jdk/java/nio/channels/Selector/SelectAfterRead.java
changeset 47216 71c04702a3d5
parent 22043 e77b5937792c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4639943
       
    26  * @summary  Checks that Windows behavior matches Solaris for
       
    27  *           various read/select combinations.
       
    28  * @author kladko
       
    29  */
       
    30 
       
    31 import java.nio.ByteBuffer;
       
    32 import java.nio.channels.Selector;
       
    33 import java.nio.channels.SelectionKey;
       
    34 import java.nio.channels.SocketChannel;
       
    35 
       
    36 public class SelectAfterRead {
       
    37 
       
    38     private static final int TIMEOUT = 1000;
       
    39 
       
    40     public static void main(String[] argv) throws Exception {
       
    41 
       
    42         // server: accept connection and write one byte
       
    43         try (ByteServer server = new ByteServer();
       
    44              SocketChannel sc = SocketChannel.open(server.address())) {
       
    45 
       
    46             server.acceptConnection();
       
    47             server.write(1);
       
    48 
       
    49             try (Selector sel = Selector.open()) {
       
    50                 sc.read(ByteBuffer.allocate(1));
       
    51                 sc.configureBlocking(false);
       
    52                 sc.register(sel, SelectionKey.OP_READ);
       
    53                 // previously on Windows select would select channel here, although there was
       
    54                 // nothing to read
       
    55                 if (sel.selectNow() != 0)
       
    56                     throw new Exception("Select returned nonzero value");
       
    57             }
       
    58         }
       
    59 
       
    60         // Now we will test a two reads combination
       
    61         // server: accept connection and write two bytes
       
    62         try (ByteServer server = new ByteServer();
       
    63              SocketChannel sc = SocketChannel.open(server.address())) {
       
    64 
       
    65             server.acceptConnection();
       
    66             server.write(2);
       
    67 
       
    68             try (Selector sel = Selector.open()) {
       
    69                 sc.configureBlocking(false);
       
    70                 sc.register(sel, SelectionKey.OP_READ);
       
    71                 if (sel.select(TIMEOUT) != 1)
       
    72                     throw new Exception("One selected key expected");
       
    73                 sel.selectedKeys().clear();
       
    74                 // previously on Windows a channel would get selected only once
       
    75                 if (sel.selectNow() != 1)
       
    76                     throw new Exception("One selected key expected");
       
    77                 // Previously on Windows two consequent reads would cause select()
       
    78                 // to select a channel, although there was nothing remaining to
       
    79                 // read in the channel
       
    80                 if (sc.read(ByteBuffer.allocate(1)) != 1)
       
    81                     throw new Exception("One byte expected");
       
    82                 if (sc.read(ByteBuffer.allocate(1)) != 1)
       
    83                     throw new Exception("One byte expected");
       
    84                 if (sel.selectNow() != 0)
       
    85                     throw new Exception("Select returned nonzero value");
       
    86             }
       
    87         }
       
    88     }
       
    89 }