author | alanb |
Fri, 23 Mar 2018 14:18:18 +0000 | |
changeset 49290 | 07779973cbe2 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49290 | 2 |
* Copyright (c) 2007, 2018, 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 6403933 |
|
26 |
* @summary POLLHUP or POLLERR on "idle" key can cause Selector to spin |
|
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.net.*; |
|
34 |
import java.nio.channels.*; |
|
35 |
||
36 |
public class CloseWhenKeyIdle { |
|
37 |
||
38 |
// indicates if the wakeup has happened |
|
39 |
static volatile boolean wakeupDone = false; |
|
40 |
||
41 |
// Wakes up a Selector after a given delay |
|
42 |
static class Waker implements Runnable { |
|
43 |
private Selector sel; |
|
44 |
private long delay; |
|
45 |
||
46 |
Waker(Selector sel, long delay) { |
|
47 |
this.sel = sel; |
|
48 |
this.delay = delay; |
|
49 |
} |
|
50 |
||
51 |
public void run() { |
|
52 |
try { |
|
53 |
Thread.sleep(delay); |
|
54 |
wakeupDone = true; |
|
55 |
sel.wakeup(); |
|
56 |
} catch (Exception x) { |
|
57 |
x.printStackTrace(); |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
||
63 |
public static void main(String[] args) throws Exception { |
|
64 |
||
65 |
// establish loopback connection |
|
66 |
||
67 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
68 |
ssc.socket().bind(new InetSocketAddress(0)); |
|
69 |
||
70 |
SocketAddress remote = new InetSocketAddress(InetAddress.getLocalHost(), |
|
71 |
ssc.socket().getLocalPort()); |
|
72 |
||
73 |
SocketChannel sc1 = SocketChannel.open(remote); |
|
74 |
SocketChannel sc2 = ssc.accept(); |
|
75 |
||
76 |
// register channel for one end with a Selector, interest set = 0 |
|
77 |
||
78 |
Selector sel = Selector.open(); |
|
79 |
||
80 |
sc1.configureBlocking(false); |
|
81 |
SelectionKey k = sc1.register(sel, 0); |
|
82 |
sel.selectNow(); |
|
83 |
||
84 |
// hard close to provoke POLLHUP |
|
85 |
||
86 |
sc2.socket().setSoLinger(true, 0); |
|
87 |
sc2.close(); |
|
88 |
||
89 |
// schedule wakeup after a few seconds |
|
90 |
||
91 |
Thread t = new Thread(new Waker(sel, 5000)); |
|
92 |
t.setDaemon(true); |
|
93 |
t.start(); |
|
94 |
||
95 |
// select should block |
|
96 |
||
97 |
int spinCount = 0; |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
98 |
boolean failed = false; |
2 | 99 |
for (;;) { |
100 |
int n = sel.select(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
101 |
if (n > 0) { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
102 |
System.err.println("Channel should not be selected!!!"); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
103 |
failed = true; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
104 |
break; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
105 |
} |
2 | 106 |
|
107 |
// wakeup |
|
108 |
if (wakeupDone) |
|
109 |
break; |
|
110 |
||
111 |
// wakeup for no reason - if it happens a few times then we have a |
|
112 |
// problem |
|
113 |
spinCount++; |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
114 |
if (spinCount >= 3) { |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
115 |
System.err.println("Selector appears to be spinning"); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
116 |
failed = true; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
117 |
break; |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
118 |
} |
2 | 119 |
} |
120 |
||
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
121 |
sc1.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
122 |
sel.close(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
123 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
124 |
if (failed) |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
125 |
throw new RuntimeException("Test failed"); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
126 |
|
2 | 127 |
System.out.println("PASS"); |
128 |
} |
|
129 |
||
130 |
} |