J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4777504 |
| 26 | * @summary Ensure that a Selector can return at least 100 selected keys |
| 27 | * @author Mark Reinhold |
| 28 | * @library .. |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.net.*; |
| 33 | import java.nio.*; |
| 34 | import java.nio.channels.*; |
| 35 | import java.util.*; |
| 36 | |
| 37 | |
| 38 | public class SelectorLimit { |
| 39 | |
| 40 | static PrintStream log = System.err; |
| 41 | |
| 42 | static class Listener |
| 43 | extends TestThread |
| 44 | { |
| 45 | volatile int count = 0; |
| 46 | private ServerSocketChannel ssc; |
| 47 | |
| 48 | Listener(ServerSocketChannel ssc) { |
| 49 | super("Listener"); |
| 50 | this.ssc = ssc; |
| 51 | } |
| 52 | |
| 53 | void go() throws IOException { |
| 54 | for (;;) { |
| 55 | ssc.accept(); |
| 56 | count++; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | static final int N_KEYS = 500; |
| 63 | static final int MIN_KEYS = 100; |
| 64 | |
| 65 | public static void main(String[] args) throws Exception { |
| 66 | // win9X can't handle many connections at once |
| 67 | String osName = System.getProperty("os.name"); |
| 68 | if (osName.toLowerCase().startsWith("win")) { |
| 69 | if (!(osName.equals("Windows NT") |
| 70 | || osName.equals("Windows 2000") |
| 71 | || osName.equals("Windows XP"))) |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | ServerSocketChannel ssc = ServerSocketChannel.open(); |
| 76 | TestUtil.bind(ssc); |
| 77 | Listener lth = new Listener(ssc); |
| 78 | lth.start(); |
| 79 | |
| 80 | Selector sel = Selector.open(); |
| 81 | SocketChannel[] sca = new SocketChannel[N_KEYS]; |
| 82 | for (int i = 0; i < N_KEYS; i++) { |
| 83 | SocketChannel sc = SocketChannel.open(); |
| 84 | sc.configureBlocking(false); |
| 85 | sc.register(sel, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE); |
| 86 | sc.connect(ssc.socket().getLocalSocketAddress()); |
| 87 | } |
| 88 | |
| 89 | for (int i = 0; i < 10; i++) { |
| 90 | if (lth.count >= MIN_KEYS) |
| 91 | break; |
| 92 | Thread.sleep(1000); |
| 93 | } |
| 94 | log.println(lth.count + " connections accepted"); |
| 95 | Thread.sleep(500); |
| 96 | |
| 97 | int n = sel.select(); |
| 98 | log.println(n + " keys selected"); |
| 99 | if (n < MIN_KEYS) |
| 100 | throw new Exception("Only selected " + n + " keys"); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | } |