author | alanb |
Thu, 17 Jun 2010 17:49:59 +0100 | |
changeset 5804 | faf9bd47d6ce |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2008, 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 |
|
5804
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
25 |
* @bug 4460583 4470470 4840199 6419424 6710579 6596323 6824135 6395224 |
2 | 26 |
* @summary Comprehensive test of asynchronous closing and interruption |
27 |
* @author Mark Reinhold |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.net.*; |
|
32 |
import java.nio.*; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.util.*; |
|
35 |
||
36 |
||
37 |
public class AsyncCloseAndInterrupt { |
|
38 |
||
39 |
static PrintStream log = System.err; |
|
40 |
||
41 |
static void sleep(int ms) { |
|
42 |
try { |
|
43 |
Thread.sleep(ms); |
|
44 |
} catch (InterruptedException x) { } |
|
45 |
} |
|
46 |
||
47 |
// Wildcard address localized to this machine -- Windoze doesn't allow |
|
48 |
// connecting to a server socket that was previously bound to a true |
|
49 |
// wildcard, namely new InetSocketAddress((InetAddress)null, 0). |
|
50 |
// |
|
51 |
private static InetSocketAddress wildcardAddress; |
|
52 |
||
53 |
||
54 |
// Server socket that blindly accepts all connections |
|
55 |
||
56 |
static ServerSocketChannel acceptor; |
|
57 |
||
58 |
private static void initAcceptor() throws IOException { |
|
59 |
acceptor = ServerSocketChannel.open(); |
|
60 |
acceptor.socket().bind(wildcardAddress); |
|
61 |
||
62 |
Thread th = new Thread("Acceptor") { |
|
63 |
public void run() { |
|
64 |
try { |
|
65 |
for (;;) { |
|
66 |
SocketChannel sc = acceptor.accept(); |
|
67 |
} |
|
68 |
} catch (IOException x) { |
|
69 |
x.printStackTrace(); |
|
70 |
} |
|
71 |
} |
|
72 |
}; |
|
73 |
||
74 |
th.setDaemon(true); |
|
75 |
th.start(); |
|
76 |
} |
|
77 |
||
78 |
||
79 |
// Server socket that refuses all connections |
|
80 |
||
81 |
static ServerSocketChannel refuser; |
|
82 |
static List refuserClients = new ArrayList(); |
|
83 |
||
84 |
private static void initRefuser() throws IOException { |
|
85 |
refuser = ServerSocketChannel.open(); |
|
86 |
refuser.socket().bind(wildcardAddress); |
|
87 |
pumpRefuser("Initializing refuser..."); |
|
88 |
} |
|
89 |
||
90 |
private static void pumpRefuser(String msg) throws IOException { |
|
5804
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
91 |
// Can't reliably saturate connection backlog on Windows Server editions |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
92 |
assert !TestUtil.onWindows(); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
93 |
|
2 | 94 |
log.print(msg); |
95 |
int n = refuserClients.size(); |
|
96 |
||
97 |
// Saturate the refuser's connection backlog so that further connection |
|
98 |
// attempts will block |
|
99 |
// |
|
100 |
outer: |
|
101 |
for (;;) { |
|
102 |
SocketChannel sc = SocketChannel.open(); |
|
103 |
sc.configureBlocking(false); |
|
104 |
if (!sc.connect(refuser.socket().getLocalSocketAddress())) { |
|
105 |
for (int i = 0; i < 20; i++) { |
|
106 |
Thread.yield(); |
|
107 |
if (sc.finishConnect()) |
|
108 |
break; |
|
109 |
if (i >= 19) |
|
110 |
break outer; |
|
111 |
} |
|
112 |
} |
|
113 |
// Retain so that finalizer doesn't close |
|
114 |
refuserClients.add(sc); |
|
115 |
} |
|
116 |
||
117 |
log.println(" " + (refuserClients.size() - n) + " connections"); |
|
118 |
} |
|
119 |
||
120 |
||
121 |
// Dead pipe source and sink |
|
122 |
||
123 |
static Pipe.SourceChannel deadSource; |
|
124 |
static Pipe.SinkChannel deadSink; |
|
125 |
||
126 |
private static void initPipes() throws IOException { |
|
127 |
if (deadSource != null) |
|
128 |
deadSource.close(); |
|
129 |
deadSource = Pipe.open().source(); |
|
130 |
if (deadSink != null) |
|
131 |
deadSink.close(); |
|
132 |
deadSink = Pipe.open().sink(); |
|
133 |
} |
|
134 |
||
135 |
||
136 |
// Files |
|
137 |
||
138 |
private static File fifoFile = null; // File that blocks on reads and writes |
|
139 |
private static File diskFile = null; // Disk file |
|
140 |
||
141 |
private static void initFile() throws Exception { |
|
142 |
||
143 |
diskFile = File.createTempFile("aci", ".tmp"); |
|
144 |
diskFile.deleteOnExit(); |
|
145 |
FileChannel fc = new FileOutputStream(diskFile).getChannel(); |
|
146 |
buffer.clear(); |
|
147 |
if (fc.write(buffer) != buffer.capacity()) |
|
148 |
throw new RuntimeException("Cannot create disk file"); |
|
149 |
fc.close(); |
|
150 |
||
151 |
if (TestUtil.onWindows()) { |
|
152 |
log.println("WARNING: Cannot completely test FileChannels on Windows"); |
|
153 |
return; |
|
154 |
} |
|
155 |
fifoFile = new File("x.fifo"); |
|
156 |
if (fifoFile.exists()) { |
|
157 |
if (!fifoFile.delete()) |
|
158 |
throw new IOException("Cannot delete existing fifo " + fifoFile); |
|
159 |
} |
|
160 |
Process p = Runtime.getRuntime().exec("mkfifo " + fifoFile); |
|
161 |
if (p.waitFor() != 0) |
|
162 |
throw new IOException("Error creating fifo"); |
|
163 |
new RandomAccessFile(fifoFile, "rw").close(); |
|
164 |
||
165 |
} |
|
166 |
||
167 |
||
168 |
// Channel factories |
|
169 |
||
170 |
static abstract class ChannelFactory { |
|
171 |
private final String name; |
|
172 |
ChannelFactory(String name) { |
|
173 |
this.name = name; |
|
174 |
} |
|
175 |
public String toString() { |
|
176 |
return name; |
|
177 |
} |
|
178 |
abstract InterruptibleChannel create() throws IOException; |
|
179 |
} |
|
180 |
||
181 |
static ChannelFactory socketChannelFactory |
|
182 |
= new ChannelFactory("SocketChannel") { |
|
183 |
InterruptibleChannel create() throws IOException { |
|
184 |
return SocketChannel.open(); |
|
185 |
} |
|
186 |
}; |
|
187 |
||
188 |
static ChannelFactory connectedSocketChannelFactory |
|
189 |
= new ChannelFactory("SocketChannel") { |
|
190 |
InterruptibleChannel create() throws IOException { |
|
191 |
SocketAddress sa = acceptor.socket().getLocalSocketAddress(); |
|
192 |
return SocketChannel.open(sa); |
|
193 |
} |
|
194 |
}; |
|
195 |
||
196 |
static ChannelFactory serverSocketChannelFactory |
|
197 |
= new ChannelFactory("ServerSocketChannel") { |
|
198 |
InterruptibleChannel create() throws IOException { |
|
199 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
200 |
ssc.socket().bind(wildcardAddress); |
|
201 |
return ssc; |
|
202 |
} |
|
203 |
}; |
|
204 |
||
205 |
static ChannelFactory datagramChannelFactory |
|
206 |
= new ChannelFactory("DatagramChannel") { |
|
207 |
InterruptibleChannel create() throws IOException { |
|
208 |
DatagramChannel dc = DatagramChannel.open(); |
|
5804
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
209 |
InetAddress lb = InetAddress.getByName("127.0.0.1"); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
210 |
dc.bind(new InetSocketAddress(lb, 0)); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
211 |
dc.connect(new InetSocketAddress(lb, 80)); |
2 | 212 |
return dc; |
213 |
} |
|
214 |
}; |
|
215 |
||
216 |
static ChannelFactory pipeSourceChannelFactory |
|
217 |
= new ChannelFactory("Pipe.SourceChannel") { |
|
218 |
InterruptibleChannel create() throws IOException { |
|
219 |
// ## arrange to close sink |
|
220 |
return Pipe.open().source(); |
|
221 |
} |
|
222 |
}; |
|
223 |
||
224 |
static ChannelFactory pipeSinkChannelFactory |
|
225 |
= new ChannelFactory("Pipe.SinkChannel") { |
|
226 |
InterruptibleChannel create() throws IOException { |
|
227 |
// ## arrange to close source |
|
228 |
return Pipe.open().sink(); |
|
229 |
} |
|
230 |
}; |
|
231 |
||
232 |
static ChannelFactory fifoFileChannelFactory |
|
233 |
= new ChannelFactory("FileChannel") { |
|
234 |
InterruptibleChannel create() throws IOException { |
|
235 |
return new RandomAccessFile(fifoFile, "rw").getChannel(); |
|
236 |
} |
|
237 |
}; |
|
238 |
||
239 |
static ChannelFactory diskFileChannelFactory |
|
240 |
= new ChannelFactory("FileChannel") { |
|
241 |
InterruptibleChannel create() throws IOException { |
|
242 |
return new RandomAccessFile(diskFile, "rw").getChannel(); |
|
243 |
} |
|
244 |
}; |
|
245 |
||
246 |
||
247 |
// I/O operations |
|
248 |
||
249 |
static abstract class Op { |
|
250 |
private final String name; |
|
251 |
protected Op(String name) { |
|
252 |
this.name = name; |
|
253 |
} |
|
254 |
abstract void doIO(InterruptibleChannel ich) throws IOException; |
|
255 |
void setup() throws IOException { } |
|
256 |
public String toString() { return name; } |
|
257 |
} |
|
258 |
||
259 |
static ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 20); |
|
260 |
||
261 |
static ByteBuffer[] buffers = new ByteBuffer[] { |
|
262 |
ByteBuffer.allocateDirect(1 << 19), |
|
263 |
ByteBuffer.allocateDirect(1 << 19) |
|
264 |
}; |
|
265 |
||
266 |
static void clearBuffers() { |
|
267 |
buffers[0].clear(); |
|
268 |
buffers[1].clear(); |
|
269 |
} |
|
270 |
||
271 |
static void show(Channel ch) { |
|
272 |
log.print("Channel " + (ch.isOpen() ? "open" : "closed")); |
|
273 |
if (ch.isOpen() && (ch instanceof SocketChannel)) { |
|
274 |
SocketChannel sc = (SocketChannel)ch; |
|
275 |
if (sc.socket().isInputShutdown()) |
|
276 |
log.print(", input shutdown"); |
|
277 |
if (sc.socket().isOutputShutdown()) |
|
278 |
log.print(", output shutdown"); |
|
279 |
} |
|
280 |
log.println(); |
|
281 |
} |
|
282 |
||
283 |
static final Op READ = new Op("read") { |
|
284 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
285 |
ReadableByteChannel rbc = (ReadableByteChannel)ich; |
|
286 |
buffer.clear(); |
|
287 |
int n = rbc.read(buffer); |
|
288 |
log.println("Read returned " + n); |
|
289 |
show(rbc); |
|
290 |
if (rbc.isOpen() |
|
291 |
&& (n == -1) |
|
292 |
&& (rbc instanceof SocketChannel) |
|
293 |
&& ((SocketChannel)rbc).socket().isInputShutdown()) { |
|
294 |
return; |
|
295 |
} |
|
296 |
throw new RuntimeException("Read succeeded"); |
|
297 |
} |
|
298 |
}; |
|
299 |
||
300 |
static final Op READV = new Op("readv") { |
|
301 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
302 |
ScatteringByteChannel sbc = (ScatteringByteChannel)ich; |
|
303 |
clearBuffers(); |
|
304 |
int n = (int)sbc.read(buffers); |
|
305 |
log.println("Read returned " + n); |
|
306 |
show(sbc); |
|
307 |
if (sbc.isOpen() |
|
308 |
&& (n == -1) |
|
309 |
&& (sbc instanceof SocketChannel) |
|
310 |
&& ((SocketChannel)sbc).socket().isInputShutdown()) { |
|
311 |
return; |
|
312 |
} |
|
313 |
throw new RuntimeException("Read succeeded"); |
|
314 |
} |
|
315 |
}; |
|
316 |
||
317 |
static final Op RECEIVE = new Op("receive") { |
|
318 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
319 |
DatagramChannel dc = (DatagramChannel)ich; |
|
320 |
buffer.clear(); |
|
321 |
dc.receive(buffer); |
|
322 |
show(dc); |
|
323 |
throw new RuntimeException("Read succeeded"); |
|
324 |
} |
|
325 |
}; |
|
326 |
||
327 |
static final Op WRITE = new Op("write") { |
|
328 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
329 |
||
330 |
WritableByteChannel wbc = (WritableByteChannel)ich; |
|
331 |
||
332 |
SocketChannel sc = null; |
|
333 |
if (wbc instanceof SocketChannel) |
|
334 |
sc = (SocketChannel)wbc; |
|
335 |
||
336 |
int n = 0; |
|
337 |
for (;;) { |
|
338 |
buffer.clear(); |
|
339 |
int d = wbc.write(buffer); |
|
340 |
n += d; |
|
341 |
if (!wbc.isOpen()) |
|
342 |
break; |
|
343 |
if ((sc != null) && sc.socket().isOutputShutdown()) |
|
344 |
break; |
|
345 |
} |
|
346 |
log.println("Wrote " + n + " bytes"); |
|
347 |
show(wbc); |
|
348 |
} |
|
349 |
}; |
|
350 |
||
351 |
static final Op WRITEV = new Op("writev") { |
|
352 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
353 |
||
354 |
GatheringByteChannel gbc = (GatheringByteChannel)ich; |
|
355 |
||
356 |
SocketChannel sc = null; |
|
357 |
if (gbc instanceof SocketChannel) |
|
358 |
sc = (SocketChannel)gbc; |
|
359 |
||
360 |
int n = 0; |
|
361 |
for (;;) { |
|
362 |
clearBuffers(); |
|
363 |
int d = (int)gbc.write(buffers); |
|
364 |
n += d; |
|
365 |
if (!gbc.isOpen()) |
|
366 |
break; |
|
367 |
if ((sc != null) && sc.socket().isOutputShutdown()) |
|
368 |
break; |
|
369 |
} |
|
370 |
log.println("Wrote " + n + " bytes"); |
|
371 |
show(gbc); |
|
372 |
||
373 |
} |
|
374 |
}; |
|
375 |
||
376 |
static final Op CONNECT = new Op("connect") { |
|
377 |
void setup() throws IOException { |
|
378 |
pumpRefuser("Pumping refuser ..."); |
|
379 |
} |
|
380 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
381 |
SocketChannel sc = (SocketChannel)ich; |
|
382 |
if (sc.connect(refuser.socket().getLocalSocketAddress())) |
|
383 |
throw new RuntimeException("Connection succeeded"); |
|
384 |
throw new RuntimeException("Connection did not block"); |
|
385 |
} |
|
386 |
}; |
|
387 |
||
388 |
static final Op FINISH_CONNECT = new Op("finishConnect") { |
|
389 |
void setup() throws IOException { |
|
390 |
pumpRefuser("Pumping refuser ..."); |
|
391 |
} |
|
392 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
393 |
SocketChannel sc = (SocketChannel)ich; |
|
394 |
sc.configureBlocking(false); |
|
395 |
SocketAddress sa = refuser.socket().getLocalSocketAddress(); |
|
396 |
if (sc.connect(sa)) |
|
397 |
throw new RuntimeException("Connection succeeded"); |
|
398 |
sc.configureBlocking(true); |
|
399 |
if (sc.finishConnect()) |
|
400 |
throw new RuntimeException("Connection succeeded"); |
|
401 |
throw new RuntimeException("Connection did not block"); |
|
402 |
} |
|
403 |
}; |
|
404 |
||
405 |
static final Op ACCEPT = new Op("accept") { |
|
406 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
407 |
ServerSocketChannel ssc = (ServerSocketChannel)ich; |
|
408 |
ssc.accept(); |
|
409 |
throw new RuntimeException("Accept succeeded"); |
|
410 |
} |
|
411 |
}; |
|
412 |
||
413 |
// Use only with diskFileChannelFactory |
|
414 |
static final Op TRANSFER_TO = new Op("transferTo") { |
|
415 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
416 |
FileChannel fc = (FileChannel)ich; |
|
417 |
long n = fc.transferTo(0, fc.size(), deadSink); |
|
418 |
log.println("Transferred " + n + " bytes"); |
|
419 |
show(fc); |
|
420 |
} |
|
421 |
}; |
|
422 |
||
423 |
// Use only with diskFileChannelFactory |
|
424 |
static final Op TRANSFER_FROM = new Op("transferFrom") { |
|
425 |
void doIO(InterruptibleChannel ich) throws IOException { |
|
426 |
FileChannel fc = (FileChannel)ich; |
|
427 |
long n = fc.transferFrom(deadSource, 0, 1 << 20); |
|
428 |
log.println("Transferred " + n + " bytes"); |
|
429 |
show(fc); |
|
430 |
} |
|
431 |
}; |
|
432 |
||
433 |
||
434 |
||
435 |
// Test modes |
|
436 |
||
437 |
static final int TEST_PREINTR = 0; // Interrupt thread before I/O |
|
438 |
static final int TEST_INTR = 1; // Interrupt thread during I/O |
|
439 |
static final int TEST_CLOSE = 2; // Close channel during I/O |
|
440 |
static final int TEST_SHUTI = 3; // Shutdown input during I/O |
|
441 |
static final int TEST_SHUTO = 4; // Shutdown output during I/O |
|
442 |
||
443 |
static final String[] testName = new String[] { |
|
444 |
"pre-interrupt", "interrupt", "close", |
|
445 |
"shutdown-input", "shutdown-output" |
|
446 |
}; |
|
447 |
||
448 |
||
449 |
static class Tester extends TestThread { |
|
450 |
||
451 |
private InterruptibleChannel ch; |
|
452 |
private Op op; |
|
453 |
private int test; |
|
454 |
volatile boolean ready = false; |
|
455 |
||
456 |
protected Tester(ChannelFactory cf, InterruptibleChannel ch, |
|
457 |
Op op, int test) |
|
458 |
{ |
|
459 |
super(cf + "/" + op + "/" + testName[test]); |
|
460 |
this.ch = ch; |
|
461 |
this.op = op; |
|
462 |
this.test = test; |
|
463 |
} |
|
464 |
||
465 |
private void caught(Channel ch, IOException x) { |
|
466 |
String xn = x.getClass().getName(); |
|
467 |
switch (test) { |
|
468 |
||
469 |
case TEST_PREINTR: |
|
470 |
case TEST_INTR: |
|
471 |
if (!xn.equals("java.nio.channels.ClosedByInterruptException")) |
|
472 |
throw new RuntimeException("Wrong exception thrown: " + x); |
|
473 |
break; |
|
474 |
||
475 |
case TEST_CLOSE: |
|
476 |
case TEST_SHUTO: |
|
477 |
if (!xn.equals("java.nio.channels.AsynchronousCloseException")) |
|
478 |
throw new RuntimeException("Wrong exception thrown: " + x); |
|
479 |
break; |
|
480 |
||
481 |
case TEST_SHUTI: |
|
482 |
if (TestUtil.onWindows()) |
|
483 |
break; |
|
484 |
// FALL THROUGH |
|
485 |
||
486 |
default: |
|
487 |
throw new Error(x); |
|
488 |
} |
|
489 |
||
490 |
if (ch.isOpen()) { |
|
491 |
if (test == TEST_SHUTO) { |
|
492 |
SocketChannel sc = (SocketChannel)ch; |
|
493 |
if (!sc.socket().isOutputShutdown()) |
|
494 |
throw new RuntimeException("Output not shutdown"); |
|
495 |
} else if ((test == TEST_INTR) && (op == TRANSFER_FROM)) { |
|
496 |
// Let this case pass -- CBIE applies to other channel |
|
497 |
} else { |
|
498 |
throw new RuntimeException("Channel still open"); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
log.println("Thrown as expected: " + x); |
|
503 |
} |
|
504 |
||
505 |
final void go() throws Exception { |
|
506 |
if (test == TEST_PREINTR) |
|
507 |
Thread.currentThread().interrupt(); |
|
508 |
ready = true; |
|
509 |
try { |
|
510 |
op.doIO(ch); |
|
511 |
} catch (ClosedByInterruptException x) { |
|
512 |
caught(ch, x); |
|
513 |
} catch (AsynchronousCloseException x) { |
|
514 |
caught(ch, x); |
|
515 |
} finally { |
|
516 |
ch.close(); |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
} |
|
521 |
||
522 |
||
523 |
// Tests |
|
524 |
||
525 |
static void test(ChannelFactory cf, Op op, int test) |
|
526 |
throws Exception |
|
527 |
{ |
|
528 |
log.println(); |
|
529 |
initPipes(); |
|
530 |
InterruptibleChannel ch = cf.create(); |
|
531 |
Tester t = new Tester(cf, ch, op, test); |
|
532 |
log.println(t); |
|
533 |
op.setup(); |
|
534 |
t.start(); |
|
535 |
do { |
|
536 |
sleep(50); |
|
537 |
} while (!t.ready); |
|
538 |
||
539 |
sleep(100); |
|
540 |
||
541 |
switch (test) { |
|
542 |
||
543 |
case TEST_INTR: |
|
544 |
t.interrupt(); |
|
545 |
break; |
|
546 |
||
547 |
case TEST_CLOSE: |
|
548 |
ch.close(); |
|
549 |
break; |
|
550 |
||
551 |
case TEST_SHUTI: |
|
552 |
if (TestUtil.onWindows()) { |
|
553 |
log.println("WARNING: Asynchronous shutdown not working on Windows"); |
|
554 |
ch.close(); |
|
555 |
} else { |
|
556 |
((SocketChannel)ch).socket().shutdownInput(); |
|
557 |
} |
|
558 |
break; |
|
559 |
||
560 |
case TEST_SHUTO: |
|
561 |
if (TestUtil.onWindows()) { |
|
562 |
log.println("WARNING: Asynchronous shutdown not working on Windows"); |
|
563 |
ch.close(); |
|
564 |
} else { |
|
565 |
((SocketChannel)ch).socket().shutdownOutput(); |
|
566 |
} |
|
567 |
break; |
|
568 |
||
569 |
default: |
|
570 |
break; |
|
571 |
} |
|
572 |
||
573 |
t.finishAndThrow(500); |
|
574 |
} |
|
575 |
||
576 |
||
577 |
static void test(ChannelFactory cf, Op op) throws Exception { |
|
578 |
// Test INTR cases before PREINTER cases since sometimes |
|
579 |
// interrupted threads can't load classes |
|
580 |
test(cf, op, TEST_INTR); |
|
581 |
test(cf, op, TEST_PREINTR); |
|
582 |
||
583 |
// Bugs, see FileChannelImpl for details |
|
584 |
if (op == TRANSFER_FROM) { |
|
585 |
log.println("WARNING: transferFrom/close not tested"); |
|
586 |
return; |
|
587 |
} |
|
694
0da2a4a73721
6710579: (ch) test/java/nio/channels/AsyncCloseAndInterrupt fails (lnx)
alanb
parents:
693
diff
changeset
|
588 |
if ((op == TRANSFER_TO) && !TestUtil.onWindows()) { |
2 | 589 |
log.println("WARNING: transferTo/close not tested"); |
590 |
return; |
|
591 |
} |
|
592 |
||
593 |
test(cf, op, TEST_CLOSE); |
|
594 |
} |
|
595 |
||
596 |
static void test(ChannelFactory cf) |
|
597 |
throws Exception |
|
598 |
{ |
|
599 |
InterruptibleChannel ch = cf.create(); // Sample channel |
|
600 |
ch.close(); |
|
601 |
||
602 |
if (ch instanceof ReadableByteChannel) { |
|
603 |
test(cf, READ); |
|
604 |
if (ch instanceof SocketChannel) |
|
605 |
test(cf, READ, TEST_SHUTI); |
|
606 |
} |
|
607 |
||
608 |
if (ch instanceof ScatteringByteChannel) { |
|
609 |
test(cf, READV); |
|
610 |
if (ch instanceof SocketChannel) |
|
611 |
test(cf, READV, TEST_SHUTI); |
|
612 |
} |
|
613 |
||
614 |
if (ch instanceof DatagramChannel) { |
|
615 |
test(cf, RECEIVE); |
|
616 |
||
617 |
// Return here: We can't effectively test writes since, if they |
|
618 |
// block, they do so only for a fleeting moment unless the network |
|
619 |
// interface is overloaded. |
|
620 |
return; |
|
621 |
||
622 |
} |
|
623 |
||
624 |
if (ch instanceof WritableByteChannel) { |
|
625 |
test(cf, WRITE); |
|
626 |
if (ch instanceof SocketChannel) |
|
627 |
test(cf, WRITE, TEST_SHUTO); |
|
628 |
} |
|
629 |
||
630 |
if (ch instanceof GatheringByteChannel) { |
|
631 |
test(cf, WRITEV); |
|
632 |
if (ch instanceof SocketChannel) |
|
633 |
test(cf, WRITEV, TEST_SHUTO); |
|
634 |
} |
|
635 |
||
636 |
} |
|
637 |
||
638 |
public static void main(String[] args) throws Exception { |
|
639 |
||
640 |
wildcardAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); |
|
641 |
initAcceptor(); |
|
5804
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
642 |
if (!TestUtil.onWindows()) |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
643 |
initRefuser(); |
2 | 644 |
initPipes(); |
645 |
initFile(); |
|
646 |
||
647 |
if (TestUtil.onME()) { |
|
648 |
log.println("WARNING: Cannot test FileChannel transfer operations" |
|
649 |
+ " on Windows 95/98/ME"); |
|
650 |
} else { |
|
651 |
test(diskFileChannelFactory, TRANSFER_TO); |
|
652 |
test(diskFileChannelFactory, TRANSFER_FROM); |
|
653 |
} |
|
654 |
if (fifoFile != null) |
|
655 |
test(fifoFileChannelFactory); |
|
656 |
||
657 |
// Testing positional file reads and writes is impractical: It requires |
|
658 |
// access to a large file soft-mounted via NFS, and even then isn't |
|
659 |
// completely guaranteed to work. |
|
660 |
// |
|
661 |
// Testing map is impractical and arguably unnecessary: It's |
|
662 |
// unclear under what conditions mmap(2) will actually block. |
|
663 |
||
664 |
test(connectedSocketChannelFactory); |
|
5804
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
665 |
|
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
666 |
if (TestUtil.onWindows()) { |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
667 |
log.println("WARNING Cannot reliably test connect/finishConnect" |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
668 |
+ " operations on Windows"); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
669 |
} else { |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
670 |
test(socketChannelFactory, CONNECT); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
671 |
test(socketChannelFactory, FINISH_CONNECT); |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
672 |
} |
faf9bd47d6ce
6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents:
5506
diff
changeset
|
673 |
|
2 | 674 |
test(serverSocketChannelFactory, ACCEPT); |
675 |
test(datagramChannelFactory); |
|
676 |
test(pipeSourceChannelFactory); |
|
677 |
test(pipeSinkChannelFactory); |
|
678 |
||
679 |
} |
|
680 |
||
681 |
} |