7945N/A/*
7945N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
7945N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7945N/A *
7945N/A * This code is free software; you can redistribute it and/or modify it
7945N/A * under the terms of the GNU General Public License version 2 only, as
7945N/A * published by the Free Software Foundation.
7945N/A *
7945N/A * This code is distributed in the hope that it will be useful, but WITHOUT
7945N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7945N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
7945N/A * version 2 for more details (a copy is included in the LICENSE file that
7945N/A * accompanied this code).
7945N/A *
7945N/A * You should have received a copy of the GNU General Public License version
7945N/A * 2 along with this work; if not, write to the Free Software Foundation,
7945N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7945N/A *
7945N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7945N/A * or visit www.oracle.com if you need additional information or have any
7945N/A * questions.
7945N/A */
7945N/A
7945N/A/*
7945N/A * @test
7945N/A * @bug 6823609
7945N/A * @summary Selector.select can hangs on Windows for cases where a helper thread
7945N/A * becomes redudant but a new helper is immediately needed.
7945N/A */
7945N/A
8451N/Aimport java.nio.channels.*;
8451N/Aimport java.io.IOException;
7945N/A
7945N/Apublic class HelperSlowToDie {
7945N/A private static final int CHANNELS_PER_THREAD = 1023;
7945N/A private static final int TEST_ITERATIONS = 200;
7945N/A private static volatile boolean done;
7945N/A
7945N/A public static void main(String[] args) throws IOException {
7945N/A if (!System.getProperty("os.name").startsWith("Windows")) {
7945N/A System.out.println("Test skipped as it verifies a Windows specific bug");
7945N/A return;
7945N/A }
7945N/A
7945N/A Selector sel = Selector.open();
7945N/A
7945N/A // register channels
7945N/A SocketChannel[] channels = new SocketChannel[CHANNELS_PER_THREAD];
7945N/A for (int i=0; i<CHANNELS_PER_THREAD; i++) {
7945N/A SocketChannel sc = SocketChannel.open();
8451N/A sc.configureBlocking(false);
7945N/A sc.register(sel, SelectionKey.OP_CONNECT);
7945N/A channels[i] = sc;
7945N/A }
7945N/A sel.selectNow();
7945N/A
7945N/A // Start threads to swamp all cores but one. This improves the chances
7945N/A // of duplicating the bug.
7945N/A Runnable busy = new Runnable() {
7945N/A public void run() {
7945N/A while (!done) ; // no nothing
7945N/A }
7945N/A };
7945N/A int ncores = Runtime.getRuntime().availableProcessors();
for (int i=0; i<ncores-1; i++)
new Thread(busy).start();
// Loop changing the number of channels from 1023 to 1024 and back.
for (int i=0; i<TEST_ITERATIONS; i++) {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_CONNECT);
sel.selectNow(); // cause helper to spin up
sc.close();
sel.selectNow(); // cause helper to retire
}
// terminate busy threads
done = true;
// clean-up
for (int i=0; i<CHANNELS_PER_THREAD; i++) {
channels[i].close();
}
sel.close();
}
}