1/*
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6823609
27 * @summary Selector.select can hangs on Windows for cases where a helper thread
28 *   becomes redudant but a new helper is immediately needed.
29 */
30
31import java.nio.channels.*;
32import java.io.IOException;
33
34public class HelperSlowToDie {
35    private static final int CHANNELS_PER_THREAD = 1023;
36    private static final int TEST_ITERATIONS = 200;
37    private static volatile boolean done;
38
39    public static void main(String[] args) throws IOException {
40        if (!System.getProperty("os.name").startsWith("Windows")) {
41            System.out.println("Test skipped as it verifies a Windows specific bug");
42            return;
43        }
44
45        Selector sel = Selector.open();
46
47        // register channels
48        SocketChannel[] channels = new SocketChannel[CHANNELS_PER_THREAD];
49        for (int i=0; i<CHANNELS_PER_THREAD; i++) {
50            SocketChannel sc = SocketChannel.open();
51            sc.configureBlocking(false);
52            sc.register(sel, SelectionKey.OP_CONNECT);
53            channels[i] = sc;
54        }
55        sel.selectNow();
56
57        // Start threads to swamp all cores but one. This improves the chances
58        // of duplicating the bug.
59        Runnable busy = new Runnable() {
60            public void run() {
61                while (!done) ;  // no nothing
62            }
63        };
64        int ncores = Runtime.getRuntime().availableProcessors();
65        for (int i=0; i<ncores-1; i++)
66            new Thread(busy).start();
67
68        // Loop changing the number of channels from 1023 to 1024 and back.
69        for (int i=0; i<TEST_ITERATIONS; i++) {
70            SocketChannel sc = SocketChannel.open();
71            sc.configureBlocking(false);
72            sc.register(sel, SelectionKey.OP_CONNECT);
73            sel.selectNow();   // cause helper to spin up
74            sc.close();
75            sel.selectNow();  // cause helper to retire
76        }
77
78        // terminate busy threads
79        done = true;
80
81        // clean-up
82        for (int i=0; i<CHANNELS_PER_THREAD; i++) {
83            channels[i].close();
84        }
85        sel.close();
86    }
87}
88