1/*
2 * Copyright (c) 2002, 2012, 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 4755720
26 * @summary Test if OP_READ is detected with OP_WRITE in interestOps
27 */
28
29import java.net.*;
30import java.nio.*;
31import java.nio.channels.*;
32import java.util.*;
33
34public class OpRead {
35
36    static void test() throws Exception {
37        ServerSocketChannel ssc = null;
38        SocketChannel sc = null;
39        SocketChannel peer = null;
40        try {
41            ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
42
43            // loopback connection
44            InetAddress lh = InetAddress.getLocalHost();
45            sc = SocketChannel.open(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
46            peer = ssc.accept();
47
48            // peer sends message so that "sc" will be readable
49            int n = peer.write(ByteBuffer.wrap("Hello".getBytes()));
50            assert n > 0;
51
52            sc.configureBlocking(false);
53
54            Selector selector = Selector.open();
55            SelectionKey key = sc.register(selector, SelectionKey.OP_READ |
56                SelectionKey.OP_WRITE);
57
58            boolean done = false;
59            int failCount = 0;
60            while (!done) {
61                int nSelected = selector.select();
62                if (nSelected > 0) {
63                    if (nSelected > 1)
64                        throw new RuntimeException("More than one channel selected");
65                    Set<SelectionKey> keys = selector.selectedKeys();
66                    Iterator<SelectionKey> iterator = keys.iterator();
67                    while (iterator.hasNext()) {
68                        key = iterator.next();
69                        iterator.remove();
70                        if (key.isWritable()) {
71                            failCount++;
72                            if (failCount > 10)
73                                throw new RuntimeException("Test failed");
74                            Thread.sleep(250);
75                        }
76                        if (key.isReadable()) {
77                            done = true;
78                        }
79                    }
80                }
81            }
82        } finally {
83            if (peer != null) peer.close();
84            if (sc != null) sc.close();
85            if (ssc != null) ssc.close();
86        }
87    }
88
89    public static void main(String[] args) throws Exception {
90        test();
91    }
92
93}
94