Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/CloseWhenKeyIdle.java
38828 views
/*1* Copyright (c) 2007, 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 640393325* @summary POLLHUP or POLLERR on "idle" key can cause Selector to spin26*/2728import java.io.*;29import java.net.*;30import java.nio.*;31import java.nio.channels.*;32import java.net.*;33import java.nio.channels.*;3435public class CloseWhenKeyIdle {3637// indicates if the wakeup has happened38static volatile boolean wakeupDone = false;3940// Wakes up a Selector after a given delay41static class Waker implements Runnable {42private Selector sel;43private long delay;4445Waker(Selector sel, long delay) {46this.sel = sel;47this.delay = delay;48}4950public void run() {51try {52Thread.sleep(delay);53wakeupDone = true;54sel.wakeup();55} catch (Exception x) {56x.printStackTrace();57}58}59}606162public static void main(String[] args) throws Exception {6364// Skip test on pre-2.6 kernels until the poll SelectorProvider65// is updated66String osname = System.getProperty("os.name");67if (osname.equals("Linux")) {68String[] ver = System.getProperty("os.version").split("\\.", 0);69if (ver.length >=2 ) {70int major = Integer.parseInt(ver[0]);71int minor = Integer.parseInt(ver[1]);72if (major < 2 || (major == 2 && minor < 6)) {73System.out.println("Test passing on pre-2.6 kernel");74return;75}76}77}787980// establish loopback connection8182ServerSocketChannel ssc = ServerSocketChannel.open();83ssc.socket().bind(new InetSocketAddress(0));8485SocketAddress remote = new InetSocketAddress(InetAddress.getLocalHost(),86ssc.socket().getLocalPort());8788SocketChannel sc1 = SocketChannel.open(remote);89SocketChannel sc2 = ssc.accept();9091// register channel for one end with a Selector, interest set = 09293Selector sel = Selector.open();9495sc1.configureBlocking(false);96SelectionKey k = sc1.register(sel, 0);97sel.selectNow();9899// hard close to provoke POLLHUP100101sc2.socket().setSoLinger(true, 0);102sc2.close();103104// schedule wakeup after a few seconds105106Thread t = new Thread(new Waker(sel, 5000));107t.setDaemon(true);108t.start();109110// select should block111112int spinCount = 0;113boolean failed = false;114for (;;) {115int n = sel.select();116if (n > 0) {117System.err.println("Channel should not be selected!!!");118failed = true;119break;120}121122// wakeup123if (wakeupDone)124break;125126// wakeup for no reason - if it happens a few times then we have a127// problem128spinCount++;129if (spinCount >= 3) {130System.err.println("Selector appears to be spinning");131failed = true;132break;133}134}135136sc1.close();137sel.close();138139if (failed)140throw new RuntimeException("Test failed");141142System.out.println("PASS");143}144145}146147148