0N/A/*
3261N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 6403933
0N/A * @summary POLLHUP or POLLERR on "idle" key can cause Selector to spin
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/Apublic class CloseWhenKeyIdle {
0N/A
0N/A // indicates if the wakeup has happened
0N/A static volatile boolean wakeupDone = false;
0N/A
0N/A // Wakes up a Selector after a given delay
0N/A static class Waker implements Runnable {
0N/A private Selector sel;
0N/A private long delay;
0N/A
0N/A Waker(Selector sel, long delay) {
0N/A this.sel = sel;
0N/A this.delay = delay;
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A Thread.sleep(delay);
0N/A wakeupDone = true;
0N/A sel.wakeup();
0N/A } catch (Exception x) {
0N/A x.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A // Skip test on pre-2.6 kernels until the poll SelectorProvider
0N/A // is updated
0N/A String osname = System.getProperty("os.name");
0N/A if (osname.equals("Linux")) {
0N/A String[] ver = System.getProperty("os.version").split("\\.", 0);
0N/A if (ver.length >=2 ) {
0N/A int major = Integer.parseInt(ver[0]);
0N/A int minor = Integer.parseInt(ver[1]);
0N/A if (major < 2 || (major == 2 && minor < 6)) {
0N/A System.out.println("Test passing on pre-2.6 kernel");
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A // establish loopback connection
0N/A
0N/A ServerSocketChannel ssc = ServerSocketChannel.open();
0N/A ssc.socket().bind(new InetSocketAddress(0));
0N/A
0N/A SocketAddress remote = new InetSocketAddress(InetAddress.getLocalHost(),
0N/A ssc.socket().getLocalPort());
0N/A
0N/A SocketChannel sc1 = SocketChannel.open(remote);
0N/A SocketChannel sc2 = ssc.accept();
0N/A
0N/A // register channel for one end with a Selector, interest set = 0
0N/A
0N/A Selector sel = Selector.open();
0N/A
0N/A sc1.configureBlocking(false);
0N/A SelectionKey k = sc1.register(sel, 0);
0N/A sel.selectNow();
0N/A
0N/A // hard close to provoke POLLHUP
0N/A
0N/A sc2.socket().setSoLinger(true, 0);
0N/A sc2.close();
0N/A
0N/A // schedule wakeup after a few seconds
0N/A
0N/A Thread t = new Thread(new Waker(sel, 5000));
0N/A t.setDaemon(true);
0N/A t.start();
0N/A
0N/A // select should block
0N/A
0N/A int spinCount = 0;
2546N/A boolean failed = false;
0N/A for (;;) {
0N/A int n = sel.select();
2546N/A if (n > 0) {
2546N/A System.err.println("Channel should not be selected!!!");
2546N/A failed = true;
2546N/A break;
2546N/A }
0N/A
0N/A // wakeup
0N/A if (wakeupDone)
0N/A break;
0N/A
0N/A // wakeup for no reason - if it happens a few times then we have a
0N/A // problem
0N/A spinCount++;
2546N/A if (spinCount >= 3) {
2546N/A System.err.println("Selector appears to be spinning");
2546N/A failed = true;
2546N/A break;
2546N/A }
0N/A }
0N/A
2546N/A sc1.close();
2546N/A sel.close();
2546N/A
2546N/A if (failed)
2546N/A throw new RuntimeException("Test failed");
2546N/A
0N/A System.out.println("PASS");
0N/A }
0N/A
0N/A}