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 package test.java.nio.channels.Selector;
24 
25 /* @test
26  * @bug 4639943
27  * @summary  Checks that Windows behavior matches Solaris for
28  *           various read/select combinations.
29  * @author kladko
30  */
31 
32 import java.nio.ByteBuffer;
33 import java.nio.channels.Selector;
34 import java.nio.channels.SelectionKey;
35 import java.nio.channels.SocketChannel;
36 import org.testng.annotations.Test;
37 
38 public class SelectAfterRead {
39 
40     private static final int TIMEOUT = 1000;
41 
42     @Test
testSelectAfterRead()43     public void testSelectAfterRead() throws Exception {
44 
45         // server: accept connection and write one byte
46         try (ByteServer server = new ByteServer();
47              SocketChannel sc = SocketChannel.open(server.address())) {
48 
49             server.acceptConnection();
50             server.write(1);
51 
52             try (Selector sel = Selector.open()) {
53                 sc.read(ByteBuffer.allocate(1));
54                 sc.configureBlocking(false);
55                 sc.register(sel, SelectionKey.OP_READ);
56                 // previously on Windows select would select channel here, although there was
57                 // nothing to read
58                 if (sel.selectNow() != 0)
59                     throw new Exception("Select returned nonzero value");
60             }
61         }
62 
63         // Now we will test a two reads combination
64         // server: accept connection and write two bytes
65         try (ByteServer server = new ByteServer();
66              SocketChannel sc = SocketChannel.open(server.address())) {
67 
68             server.acceptConnection();
69             server.write(2);
70 
71             try (Selector sel = Selector.open()) {
72                 sc.configureBlocking(false);
73                 sc.register(sel, SelectionKey.OP_READ);
74                 if (sel.select(TIMEOUT) != 1)
75                     throw new Exception("One selected key expected");
76                 sel.selectedKeys().clear();
77                 // previously on Windows a channel would get selected only once
78                 if (sel.selectNow() != 1)
79                     throw new Exception("One selected key expected");
80                 // Previously on Windows two consequent reads would cause select()
81                 // to select a channel, although there was nothing remaining to
82                 // read in the channel
83                 if (sc.read(ByteBuffer.allocate(1)) != 1)
84                     throw new Exception("One byte expected");
85                 if (sc.read(ByteBuffer.allocate(1)) != 1)
86                     throw new Exception("One byte expected");
87                 if (sel.selectNow() != 0)
88                     throw new Exception("Select returned nonzero value");
89             }
90         }
91     }
92 }