author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 702 | 3bd5e70b2fe9 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2006-2008 Sun Microsystems, Inc. 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 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* @test |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
25 |
* @bug 6285901 6501089 |
2 | 26 |
* @summary Check no data is written to wrong socket channel during async closing. |
27 |
* @author Xueming Shen |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.net.*; |
|
34 |
||
35 |
public class AsyncCloseChannel { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
36 |
static volatile boolean failed = false; |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
37 |
static volatile boolean keepGoing = true; |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
38 |
static int maxAcceptCount = 100; |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
39 |
static volatile int acceptCount = 0; |
2 | 40 |
static String host = "127.0.0.1"; |
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
41 |
static int sensorPort; |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
42 |
static int targetPort; |
2 | 43 |
|
44 |
public static void main(String args[]) throws Exception { |
|
45 |
if (System.getProperty("os.name").startsWith("Windows")) { |
|
46 |
System.err.println("WARNING: Still does not work on Windows!"); |
|
47 |
return; |
|
48 |
} |
|
49 |
Thread ss = new SensorServer(); ss.start(); |
|
50 |
Thread ts = new TargetServer(); ts.start(); |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
51 |
|
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
52 |
sensorPort = ((ServerThread)ss).server.getLocalPort(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
53 |
targetPort = ((ServerThread)ts).server.getLocalPort(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
54 |
|
2 | 55 |
Thread sc = new SensorClient(); sc.start(); |
56 |
Thread tc = new TargetClient(); tc.start(); |
|
57 |
||
58 |
while(acceptCount < maxAcceptCount && !failed) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
59 |
Thread.sleep(10); |
2 | 60 |
} |
61 |
keepGoing = false; |
|
62 |
try { |
|
63 |
ss.interrupt(); |
|
64 |
ts.interrupt(); |
|
65 |
sc.interrupt(); |
|
66 |
tc.interrupt(); |
|
67 |
} catch (Exception e) {} |
|
68 |
if (failed) |
|
69 |
throw new RuntimeException("AsyncCloseChannel2 failed after <" |
|
70 |
+ acceptCount + "> times of accept!"); |
|
71 |
} |
|
72 |
||
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
73 |
static class SensorServer extends ServerThread { |
2 | 74 |
public void runEx() throws Exception { |
75 |
while(keepGoing) { |
|
76 |
try { |
|
77 |
final Socket s = server.accept(); |
|
78 |
new Thread() { |
|
79 |
public void run() { |
|
80 |
try { |
|
81 |
int c = s.getInputStream().read(); |
|
82 |
if(c != -1) { |
|
83 |
// No data is ever written to the peer's socket! |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
84 |
System.err.println("Oops: read a character: " |
2 | 85 |
+ (char) c); |
86 |
failed = true; |
|
87 |
} |
|
88 |
} catch (IOException ex) { |
|
89 |
ex.printStackTrace(); |
|
90 |
} finally { |
|
91 |
closeIt(s); |
|
92 |
} |
|
93 |
} |
|
94 |
}.start(); |
|
95 |
} catch (IOException ex) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
96 |
System.err.println("Exception on sensor server " + ex.getMessage()); |
2 | 97 |
} |
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
||
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
102 |
static class TargetServer extends ServerThread { |
2 | 103 |
public void runEx() throws Exception { |
104 |
while (keepGoing) { |
|
105 |
try { |
|
106 |
final Socket s = server.accept(); |
|
107 |
acceptCount++; |
|
108 |
new Thread() { |
|
109 |
public void run() { |
|
110 |
boolean empty = true; |
|
111 |
try { |
|
112 |
for(;;) { |
|
113 |
int c = s.getInputStream().read(); |
|
114 |
if(c == -1) { |
|
115 |
if(!empty) |
|
116 |
break; |
|
117 |
} |
|
118 |
empty = false; |
|
119 |
} |
|
120 |
} catch (IOException ex) { |
|
121 |
ex.printStackTrace(); |
|
122 |
} finally { |
|
123 |
closeIt(s); |
|
124 |
} |
|
125 |
} |
|
126 |
}.start(); |
|
127 |
} catch (IOException ex) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
128 |
System.err.println("Exception on target server " + ex.getMessage()); |
2 | 129 |
} |
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
static class SensorClient extends Thread { |
|
135 |
private static boolean wake; |
|
136 |
private static SensorClient theClient; |
|
137 |
public void run() { |
|
138 |
while (keepGoing) { |
|
139 |
Socket s = null; |
|
140 |
try { |
|
141 |
s = new Socket(); |
|
142 |
synchronized(this) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
143 |
while(!wake && keepGoing) { |
2 | 144 |
try { |
145 |
wait(); |
|
146 |
} catch (InterruptedException ex) { } |
|
147 |
} |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
148 |
wake = false; |
2 | 149 |
} |
150 |
s.connect(new InetSocketAddress(host, sensorPort)); |
|
151 |
try { |
|
152 |
Thread.sleep(10); |
|
153 |
} catch (InterruptedException ex) { } |
|
154 |
} catch (IOException ex) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
155 |
System.err.println("Exception on sensor client " + ex.getMessage()); |
2 | 156 |
} finally { |
157 |
if(s != null) { |
|
158 |
try { |
|
159 |
s.close(); |
|
160 |
} catch(IOException ex) { ex.printStackTrace();} |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
public SensorClient() { |
|
167 |
theClient = this; |
|
168 |
} |
|
169 |
||
170 |
public static void wakeMe() { |
|
171 |
synchronized(theClient) { |
|
172 |
wake = true; |
|
173 |
theClient.notify(); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
static class TargetClient extends Thread { |
|
179 |
volatile boolean ready = false; |
|
180 |
public void run() { |
|
181 |
while(keepGoing) { |
|
182 |
try { |
|
183 |
final SocketChannel s = SocketChannel.open( |
|
184 |
new InetSocketAddress(host, targetPort)); |
|
185 |
s.finishConnect(); |
|
186 |
s.socket().setSoLinger(false, 0); |
|
187 |
ready = false; |
|
188 |
Thread t = new Thread() { |
|
189 |
public void run() { |
|
190 |
ByteBuffer b = ByteBuffer.allocate(1); |
|
191 |
try { |
|
192 |
for(;;) { |
|
193 |
b.clear(); |
|
194 |
b.put((byte) 'A'); |
|
195 |
b.flip(); |
|
196 |
s.write(b); |
|
197 |
ready = true; |
|
198 |
} |
|
199 |
} catch (IOException ex) { |
|
200 |
if(!(ex instanceof ClosedChannelException)) |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
201 |
System.err.println("Exception in target client child " |
2 | 202 |
+ ex.toString()); |
203 |
} |
|
204 |
} |
|
205 |
}; |
|
206 |
t.start(); |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
207 |
while(!ready && keepGoing) { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
208 |
try { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
209 |
Thread.sleep(10); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
210 |
} catch (InterruptedException ex) {} |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
211 |
} |
2 | 212 |
s.close(); |
213 |
SensorClient.wakeMe(); |
|
214 |
t.join(); |
|
215 |
} catch (IOException ex) { |
|
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
216 |
System.err.println("Exception in target client parent " |
2 | 217 |
+ ex.getMessage()); |
218 |
} catch (InterruptedException ex) {} |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
||
702
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
223 |
static abstract class ServerThread extends Thread { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
224 |
ServerSocket server; |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
225 |
public ServerThread() { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
226 |
super(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
227 |
try { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
228 |
server = new ServerSocket(0); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
229 |
} catch (IOException ex) { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
230 |
ex.printStackTrace(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
231 |
} |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
232 |
} |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
233 |
|
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
234 |
public void interrupt() { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
235 |
super.interrupt(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
236 |
if (server != null) { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
237 |
try { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
238 |
server.close(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
239 |
} catch (IOException ex) { |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
240 |
ex.printStackTrace(); |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
241 |
} |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
242 |
} |
3bd5e70b2fe9
6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
sherman
parents:
2
diff
changeset
|
243 |
} |
2 | 244 |
public void run() { |
245 |
try { |
|
246 |
runEx(); |
|
247 |
} catch (Exception ex) { |
|
248 |
ex.printStackTrace(); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
abstract void runEx() throws Exception; |
|
253 |
} |
|
254 |
||
255 |
public static void closeIt(Socket s) { |
|
256 |
try { |
|
257 |
if(s != null) |
|
258 |
s.close(); |
|
259 |
} catch (IOException ex) { } |
|
260 |
} |
|
261 |
} |