author | dfuchs |
Wed, 07 Nov 2012 13:24:39 +0100 | |
changeset 14415 | 7a31b0e0cfaf |
parent 7668 | d4a77089c587 |
child 20176 | 59b7a49e7f8b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 .. |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
29 |
* @build SelectorLimit |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
30 |
* @run main/othervm SelectorLimit |
2 | 31 |
*/ |
32 |
||
33 |
import java.io.*; |
|
34 |
import java.net.*; |
|
35 |
import java.nio.*; |
|
36 |
import java.nio.channels.*; |
|
37 |
import java.util.*; |
|
38 |
||
39 |
||
40 |
public class SelectorLimit { |
|
41 |
||
42 |
static PrintStream log = System.err; |
|
43 |
||
44 |
static class Listener |
|
45 |
extends TestThread |
|
46 |
{ |
|
47 |
volatile int count = 0; |
|
48 |
private ServerSocketChannel ssc; |
|
49 |
||
50 |
Listener(ServerSocketChannel ssc) { |
|
51 |
super("Listener"); |
|
52 |
this.ssc = ssc; |
|
53 |
} |
|
54 |
||
55 |
void go() throws IOException { |
|
56 |
for (;;) { |
|
57 |
ssc.accept(); |
|
58 |
count++; |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
} |
|
63 |
||
64 |
static final int N_KEYS = 500; |
|
65 |
static final int MIN_KEYS = 100; |
|
66 |
||
67 |
public static void main(String[] args) throws Exception { |
|
68 |
// win9X can't handle many connections at once |
|
69 |
String osName = System.getProperty("os.name"); |
|
70 |
if (osName.toLowerCase().startsWith("win")) { |
|
71 |
if (!(osName.equals("Windows NT") |
|
72 |
|| osName.equals("Windows 2000") |
|
73 |
|| osName.equals("Windows XP"))) |
|
74 |
return; |
|
75 |
} |
|
76 |
||
77 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
78 |
TestUtil.bind(ssc); |
|
79 |
Listener lth = new Listener(ssc); |
|
80 |
lth.start(); |
|
81 |
||
82 |
Selector sel = Selector.open(); |
|
83 |
SocketChannel[] sca = new SocketChannel[N_KEYS]; |
|
84 |
for (int i = 0; i < N_KEYS; i++) { |
|
85 |
SocketChannel sc = SocketChannel.open(); |
|
86 |
sc.configureBlocking(false); |
|
87 |
sc.register(sel, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE); |
|
88 |
sc.connect(ssc.socket().getLocalSocketAddress()); |
|
89 |
} |
|
90 |
||
91 |
for (int i = 0; i < 10; i++) { |
|
92 |
if (lth.count >= MIN_KEYS) |
|
93 |
break; |
|
94 |
Thread.sleep(1000); |
|
95 |
} |
|
96 |
log.println(lth.count + " connections accepted"); |
|
97 |
Thread.sleep(500); |
|
98 |
||
99 |
int n = sel.select(); |
|
100 |
log.println(n + " keys selected"); |
|
101 |
if (n < MIN_KEYS) |
|
102 |
throw new Exception("Only selected " + n + " keys"); |
|
103 |
||
104 |
} |
|
105 |
||
106 |
} |