|
1 /* |
|
2 * Copyright (c) 2001, 2012, 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 4513011 |
|
26 * @summary Registering and cancelling same fd many times |
|
27 * @library .. |
|
28 */ |
|
29 |
|
30 import java.net.*; |
|
31 import java.nio.*; |
|
32 import java.nio.channels.*; |
|
33 import java.nio.channels.spi.SelectorProvider; |
|
34 import java.util.*; |
|
35 |
|
36 public class Alias { |
|
37 |
|
38 static int success = 0; |
|
39 static int LIMIT = 20; // Hangs after just 1 if problem is present |
|
40 |
|
41 public static void main(String[] args) throws Exception { |
|
42 try (TestServers.DayTimeServer daytimeServer |
|
43 = TestServers.DayTimeServer.startNewServer(100)) { |
|
44 test1(daytimeServer); |
|
45 } |
|
46 } |
|
47 |
|
48 static void test1(TestServers.DayTimeServer daytimeServer) throws Exception { |
|
49 Selector selector = SelectorProvider.provider().openSelector(); |
|
50 InetAddress myAddress = daytimeServer.getAddress(); |
|
51 InetSocketAddress isa |
|
52 = new InetSocketAddress(myAddress, |
|
53 daytimeServer.getPort()); |
|
54 |
|
55 for (int j=0; j<LIMIT; j++) { |
|
56 SocketChannel sc = SocketChannel.open(); |
|
57 sc.configureBlocking(false); |
|
58 boolean result = sc.connect(isa); |
|
59 |
|
60 // On some platforms - given that we're using a local server, |
|
61 // we may not enter into the if () { } statement below... |
|
62 if (!result) { |
|
63 SelectionKey key = sc.register(selector, |
|
64 SelectionKey.OP_CONNECT); |
|
65 while (!result) { |
|
66 int keysAdded = selector.select(100); |
|
67 if (keysAdded > 0) { |
|
68 Set readyKeys = selector.selectedKeys(); |
|
69 Iterator i = readyKeys.iterator(); |
|
70 while (i.hasNext()) { |
|
71 SelectionKey sk = (SelectionKey)i.next(); |
|
72 SocketChannel nextReady = |
|
73 (SocketChannel)sk.channel(); |
|
74 result = nextReady.finishConnect(); |
|
75 } |
|
76 } |
|
77 } |
|
78 key.cancel(); |
|
79 } |
|
80 read(sc); |
|
81 } |
|
82 selector.close(); |
|
83 } |
|
84 |
|
85 static void read(SocketChannel sc) throws Exception { |
|
86 ByteBuffer bb = ByteBuffer.allocateDirect(100); |
|
87 int n = 0; |
|
88 while (n == 0) // Note this is not a rigorous check for done reading |
|
89 n = sc.read(bb); |
|
90 //bb.position(bb.position() - 2); |
|
91 //bb.flip(); |
|
92 //CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); |
|
93 //System.out.println("Received: \"" + cb + "\""); |
|
94 sc.close(); |
|
95 success++; |
|
96 } |
|
97 } |