author | alanb |
Fri, 10 Sep 2010 18:48:49 +0100 | |
changeset 6538 | cb0da65ba680 |
parent 5970 | d4e98bbfb0be |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 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 4729342 |
|
26 |
* @summary Check for CancelledKeyException when key cancelled during select |
|
27 |
*/ |
|
28 |
||
29 |
import java.nio.channels.*; |
|
30 |
import java.io.IOException; |
|
31 |
import java.net.*; |
|
32 |
||
33 |
public class SelectAndCancel { |
|
34 |
static SelectionKey sk; |
|
35 |
||
36 |
/* |
|
37 |
* CancelledKeyException is the failure symptom of 4729342 |
|
38 |
* NOTE: The failure is timing dependent and is not always |
|
39 |
* seen immediately when the bug is present. |
|
40 |
*/ |
|
41 |
public static void main(String[] args) throws Exception { |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
42 |
final Selector selector = Selector.open(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
43 |
final ServerSocketChannel ssc = |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
44 |
ServerSocketChannel.open().bind(new InetSocketAddress(0)); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
45 |
final InetSocketAddress isa = |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
46 |
new InetSocketAddress(InetAddress.getLocalHost(), ssc.socket().getLocalPort()); |
2 | 47 |
|
48 |
// Create and start a selector in a separate thread. |
|
49 |
new Thread(new Runnable() { |
|
50 |
public void run() { |
|
51 |
try { |
|
52 |
ssc.configureBlocking(false); |
|
53 |
sk = ssc.register(selector, SelectionKey.OP_ACCEPT); |
|
54 |
selector.select(); |
|
55 |
} catch (IOException e) { |
|
56 |
System.err.println("error in selecting thread"); |
|
57 |
e.printStackTrace(); |
|
58 |
} |
|
59 |
} |
|
60 |
}).start(); |
|
61 |
||
62 |
// Wait for above thread to get to select() before we call close. |
|
63 |
Thread.sleep(3000); |
|
64 |
||
65 |
// Try to close. This should wakeup select. |
|
66 |
new Thread(new Runnable() { |
|
67 |
public void run() { |
|
68 |
try { |
|
69 |
SocketChannel sc = SocketChannel.open(); |
|
70 |
sc.connect(isa); |
|
71 |
ssc.close(); |
|
72 |
sk.cancel(); |
|
73 |
sc.close(); |
|
74 |
} catch (IOException e) { |
|
75 |
System.err.println("error in closing thread"); |
|
76 |
System.err.println(e); |
|
77 |
} |
|
78 |
} |
|
79 |
}).start(); |
|
80 |
||
81 |
// Wait for select() to be awakened, which should be done by close. |
|
82 |
Thread.sleep(3000); |
|
83 |
||
84 |
selector.wakeup(); |
|
85 |
selector.close(); |
|
86 |
} |
|
87 |
} |