test/jdk/java/nio/channels/Selector/OutOfBand.java
changeset 47216 71c04702a3d5
parent 44414 06222fd091b4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2010, 2017, 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 6213702
       
    26  * @requires (os.family != "mac") | (os.version == "10.10.5")
       
    27  *    | (os.simpleVersion != "10.8" & os.simpleVersion != "10.9"
       
    28  *        & os.simpleVersion != "10.10")
       
    29  * @summary OOB data causes a SocketChannel, with OOBINLINE disabled, to be
       
    30  *    selected
       
    31  */
       
    32 
       
    33 import java.net.*;
       
    34 import java.nio.ByteBuffer;
       
    35 import java.nio.channels.*;
       
    36 import java.io.IOException;
       
    37 
       
    38 public class OutOfBand {
       
    39 
       
    40     public static void main(String[] args) throws Exception {
       
    41         ServerSocketChannel ssc = null;
       
    42         SocketChannel sc = null;
       
    43         Selector sel = null;
       
    44         Socket s = null;
       
    45 
       
    46         try {
       
    47             // establish loopback connection.
       
    48             ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
       
    49             s = new Socket(InetAddress.getLocalHost(),
       
    50                            ssc.socket().getLocalPort());
       
    51             sc = ssc.accept();
       
    52 
       
    53             sel = Selector.open();
       
    54             sc.configureBlocking(false);
       
    55             sc.register(sel, SelectionKey.OP_READ);
       
    56 
       
    57             // OOB data should be disabled by default
       
    58             if (sc.socket().getOOBInline())
       
    59                 throw new RuntimeException("SO_OOBINLINE enabled");
       
    60             test(s, false, 0,   0,   sel);
       
    61             test(s, false, 512, 0,   sel);
       
    62             test(s, false, 0,   512, sel);
       
    63             test(s, false, 512, 512, sel);
       
    64 
       
    65             // enable SO_OOBINLINE
       
    66             sc.socket().setOOBInline(true);
       
    67 
       
    68             // OOB data should be received
       
    69             test(s, true, 0,   0,   sel);
       
    70             test(s, true, 512, 0,   sel);
       
    71             test(s, true, 0,   512, sel);
       
    72             test(s, true, 512, 512, sel);
       
    73 
       
    74         } finally {
       
    75             if (sel != null) sel.close();
       
    76             if (sc != null) sc.close();
       
    77             if (ssc != null) ssc.close();
       
    78             if (s != null) sc.close();
       
    79         }
       
    80     }
       
    81 
       
    82     static void test(Socket s, boolean urgentExpected,
       
    83                      int bytesBefore, int bytesAfter,
       
    84                      Selector sel)
       
    85         throws IOException
       
    86     {
       
    87         // send data
       
    88         int bytesExpected = 0;
       
    89         if (bytesBefore > 0) {
       
    90             s.getOutputStream().write(new byte[bytesBefore]);
       
    91             bytesExpected += bytesBefore;
       
    92         }
       
    93         s.sendUrgentData(0xff);
       
    94         if (urgentExpected)
       
    95             bytesExpected++;
       
    96         if (bytesAfter > 0) {
       
    97             s.getOutputStream().write(new byte[bytesAfter]);
       
    98             bytesExpected += bytesAfter;
       
    99         }
       
   100 
       
   101         // receive data, checking for spurious wakeups and reads
       
   102         int spuriousWakeups = 0;
       
   103         int spuriousReads = 0;
       
   104         int bytesRead = 0;
       
   105         ByteBuffer bb = ByteBuffer.allocate(100);
       
   106         for (;;) {
       
   107             int n = sel.select(2000);
       
   108             if (n == 0) {
       
   109                 if (bytesRead == bytesExpected) {
       
   110                     System.out.format("Selector wakeups %d\tSpurious reads %d%n",
       
   111                             spuriousWakeups, spuriousReads);
       
   112                     return;
       
   113                 }
       
   114                 if (++spuriousWakeups >= 3)
       
   115                     throw new RuntimeException("Selector appears to be spinning" +
       
   116                         " or data not received");
       
   117                 continue;
       
   118             }
       
   119             if (n > 1)
       
   120                 throw new RuntimeException("More than one key selected????");
       
   121             SelectionKey key = sel.selectedKeys().iterator().next();
       
   122             bb.clear();
       
   123             n = ((SocketChannel)key.channel()).read(bb);
       
   124             if (n == 0) {
       
   125                 if (++spuriousReads >=3)
       
   126                     throw new RuntimeException("Too many spurious reads");
       
   127             } else {
       
   128                 bytesRead += n;
       
   129                 if (bytesRead > bytesExpected)
       
   130                     throw new RuntimeException("Received more than expected");
       
   131             }
       
   132             sel.selectedKeys().clear();
       
   133         }
       
   134     }
       
   135 }