1 /*
2  * Copyright (c) 2002, 2013, 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 /* @test
25  * @bug 4777504 8024883
26  * @summary Ensure that a Selector can return at least 100 selected keys
27  * @author Mark Reinhold
28  * @library ..
29  * @build SelectorLimit
30  * @run main/othervm SelectorLimit
31  * @run main/othervm -Dsun.nio.ch.maxUpdateArraySize=128 SelectorLimit
32  */
33 
34 import java.io.*;
35 import java.net.*;
36 import java.nio.*;
37 import java.nio.channels.*;
38 import java.util.*;
39 
40 
41 public class SelectorLimit {
42 
43     static PrintStream log = System.err;
44 
45     static class Listener
46         extends TestThread
47     {
48         volatile int count = 0;
49         private ServerSocketChannel ssc;
50 
Listener(ServerSocketChannel ssc)51         Listener(ServerSocketChannel ssc) {
52             super("Listener");
53             this.ssc = ssc;
54         }
55 
go()56         void go() throws IOException {
57             for (;;) {
58                 ssc.accept();
59                 count++;
60             }
61         }
62 
63     }
64 
65     static final int N_KEYS = 500;
66     static final int MIN_KEYS = 100;
67 
main(String[] args)68     public static void main(String[] args) throws Exception {
69         ServerSocketChannel ssc = ServerSocketChannel.open();
70         TestUtil.bind(ssc);
71         Listener lth = new Listener(ssc);
72         lth.start();
73 
74         Selector sel = Selector.open();
75         SocketChannel[] sca = new SocketChannel[N_KEYS];
76         for (int i = 0; i < N_KEYS; i++) {
77             SocketChannel sc = SocketChannel.open();
78             sc.configureBlocking(false);
79             sc.register(sel, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);
80             sc.connect(ssc.socket().getLocalSocketAddress());
81         }
82 
83         for (int i = 0; i < 10; i++) {
84             if (lth.count >= MIN_KEYS)
85                 break;
86             Thread.sleep(1000);
87         }
88         log.println(lth.count + " connections accepted");
89         Thread.sleep(500);
90 
91         int n = sel.select();
92         log.println(n + " keys selected");
93         if (n < MIN_KEYS)
94             throw new Exception("Only selected " + n + " keys");
95 
96     }
97 
98 }
99