Path: blob/master/test/jdk/java/nio/channels/Selector/CloseWhenKeyIdle.java
41153 views
/*1* Copyright (c) 2007, 2018, 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// establish loopback connection6566ServerSocketChannel ssc = ServerSocketChannel.open();67ssc.socket().bind(new InetSocketAddress(0));6869SocketAddress remote = new InetSocketAddress(InetAddress.getLocalHost(),70ssc.socket().getLocalPort());7172SocketChannel sc1 = SocketChannel.open(remote);73SocketChannel sc2 = ssc.accept();7475// register channel for one end with a Selector, interest set = 07677Selector sel = Selector.open();7879sc1.configureBlocking(false);80SelectionKey k = sc1.register(sel, 0);81sel.selectNow();8283// hard close to provoke POLLHUP8485sc2.socket().setSoLinger(true, 0);86sc2.close();8788// schedule wakeup after a few seconds8990Thread t = new Thread(new Waker(sel, 5000));91t.setDaemon(true);92t.start();9394// select should block9596int spinCount = 0;97boolean failed = false;98for (;;) {99int n = sel.select();100if (n > 0) {101System.err.println("Channel should not be selected!!!");102failed = true;103break;104}105106// wakeup107if (wakeupDone)108break;109110// wakeup for no reason - if it happens a few times then we have a111// problem112spinCount++;113if (spinCount >= 3) {114System.err.println("Selector appears to be spinning");115failed = true;116break;117}118}119120sc1.close();121sel.close();122123if (failed)124throw new RuntimeException("Test failed");125126System.out.println("PASS");127}128129}130131132