1 /*
   2  * Copyright (c) 2002, 2018, 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 8195160
  26  * @summary Test if OP_READ is detected with OP_WRITE in interestOps
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build RsocketTest
  30  * @run main/othervm -Djava.net.preferIPv4Stack=true OpRead
  31  */
  32 
  33 import java.net.*;
  34 import java.nio.*;
  35 import java.nio.channels.*;
  36 import java.util.*;
  37 import jdk.net.Sockets;
  38 
  39 public class OpRead implements Runnable {
  40 
  41     static ServerSocketChannel ssc = null;
  42     static SocketChannel sc = null;
  43     static SocketChannel peer = null;
  44     static InetAddress lh;
  45 
  46     static void test() throws Exception {
  47         try {
  48             lh = InetAddress.getLocalHost();
  49             ssc = Sockets.openRdmaServerSocketChannel().bind(new InetSocketAddress(lh, 0));
  50 
  51             // loopback connection
  52             sc = Sockets.openRdmaSocketChannel();
  53             Thread t = new Thread(new OpRead());
  54             t.start();
  55 
  56             peer = ssc.accept();
  57 
  58             // peer sends message so that "sc" will be readable
  59             int n = peer.write(ByteBuffer.wrap("Hello".getBytes()));
  60             assert n > 0;
  61 
  62             sc.configureBlocking(false);
  63 
  64             Selector selector = Sockets.openRdmaSelector();
  65             SelectionKey key = sc.register(selector, SelectionKey.OP_READ |
  66                 SelectionKey.OP_WRITE);
  67 
  68             boolean done = false;
  69             int failCount = 0;
  70             while (!done) {
  71                 int nSelected = selector.select();
  72                 if (nSelected > 0) {
  73                     if (nSelected > 1)
  74                         throw new RuntimeException("More than one channel selected");
  75                     Set<SelectionKey> keys = selector.selectedKeys();
  76                     Iterator<SelectionKey> iterator = keys.iterator();
  77                     while (iterator.hasNext()) {
  78                         key = iterator.next();
  79                         iterator.remove();
  80                         if (key.isWritable()) {
  81                             failCount++;
  82                             if (failCount > 10)
  83                                 throw new RuntimeException("Test failed");
  84                             Thread.sleep(250);
  85                         }
  86                         if (key.isReadable()) {
  87                             done = true;
  88                         }
  89                     }
  90                 }
  91             }
  92         } finally {
  93             if (peer != null) peer.close();
  94             if (sc != null) sc.close();
  95             if (ssc != null) ssc.close();
  96         }
  97     }
  98 
  99     public void run() {
 100         try {
 101             sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
 102         } catch (Exception e) {
 103             e.printStackTrace();
 104             throw new RuntimeException("Test Failed!");
 105         }
 106     }
 107 
 108     public static void main(String[] args) throws Exception {
 109         if (!RsocketTest.isRsocketAvailable())
 110             return;
 111 
 112         test();
 113     }
 114 
 115 }