Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/OutOfBand.java
38828 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 621370225* @summary OOB data causes a SocketChannel, with OOBINLINE disabled, to be26* selected27*/2829import java.net.*;30import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.io.IOException;3334public class OutOfBand {3536public static void main(String[] args) throws Exception {37ServerSocketChannel ssc = null;38SocketChannel sc = null;39Selector sel = null;40Socket s = null;4142try {43// establish loopback connection.44ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));45s = new Socket(InetAddress.getLocalHost(),46ssc.socket().getLocalPort());47sc = ssc.accept();4849sel = Selector.open();50sc.configureBlocking(false);51sc.register(sel, SelectionKey.OP_READ);5253// OOB data should be disabled by default54if (sc.socket().getOOBInline())55throw new RuntimeException("SO_OOBINLINE enabled");56test(s, false, 0, 0, sel);57test(s, false, 512, 0, sel);58test(s, false, 0, 512, sel);59test(s, false, 512, 512, sel);6061// enable SO_OOBINLINE62sc.socket().setOOBInline(true);6364// OOB data should be received65test(s, true, 0, 0, sel);66test(s, true, 512, 0, sel);67test(s, true, 0, 512, sel);68test(s, true, 512, 512, sel);6970} finally {71if (sel != null) sel.close();72if (sc != null) sc.close();73if (ssc != null) ssc.close();74if (s != null) sc.close();75}76}7778static void test(Socket s, boolean urgentExpected,79int bytesBefore, int bytesAfter,80Selector sel)81throws IOException82{83// send data84int bytesExpected = 0;85if (bytesBefore > 0) {86s.getOutputStream().write(new byte[bytesBefore]);87bytesExpected += bytesBefore;88}89s.sendUrgentData(0xff);90if (urgentExpected)91bytesExpected++;92if (bytesAfter > 0) {93s.getOutputStream().write(new byte[bytesAfter]);94bytesExpected += bytesAfter;95}9697// receive data, checking for spurious wakeups and reads98int spuriousWakeups = 0;99int spuriousReads = 0;100int bytesRead = 0;101ByteBuffer bb = ByteBuffer.allocate(100);102for (;;) {103int n = sel.select(2000);104if (n == 0) {105if (bytesRead == bytesExpected) {106System.out.format("Selector wakeups %d\tSpurious reads %d%n",107spuriousWakeups, spuriousReads);108return;109}110if (++spuriousWakeups >= 3)111throw new RuntimeException("Selector appears to be spinning" +112" or data not received");113continue;114}115if (n > 1)116throw new RuntimeException("More than one key selected????");117SelectionKey key = sel.selectedKeys().iterator().next();118bb.clear();119n = ((SocketChannel)key.channel()).read(bb);120if (n == 0) {121if (++spuriousReads >=3)122throw new RuntimeException("Too many spurious reads");123} else {124bytesRead += n;125if (bytesRead > bytesExpected)126throw new RuntimeException("Received more than expected");127}128sel.selectedKeys().clear();129}130}131}132133134