author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
13028
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
1 |
/* |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
4 |
* |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
8 |
* |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
14 |
* |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
18 |
* |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
21 |
* questions. |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
22 |
*/ |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
23 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
24 |
/* @test |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
25 |
* @summary Test asynchronous close during a blocking write |
30046 | 26 |
* @key randomness |
13028
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
27 |
*/ |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
28 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
29 |
import java.io.Closeable; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
30 |
import java.io.IOException; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
31 |
import java.nio.ByteBuffer; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
32 |
import java.nio.channels.*; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
33 |
import java.net.*; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
34 |
import java.util.concurrent.*; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
35 |
import java.util.Random; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
36 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
37 |
public class CloseDuringWrite { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
38 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
39 |
static final Random rand = new Random(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
40 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
41 |
/** |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
42 |
* A task that closes a Closeable |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
43 |
*/ |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
44 |
static class Closer implements Callable<Void> { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
45 |
final Closeable c; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
46 |
Closer(Closeable c) { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
47 |
this.c = c; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
48 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
49 |
public Void call() throws IOException { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
50 |
c.close(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
51 |
return null; |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
52 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
53 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
54 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
55 |
public static void main(String[] args) throws Exception { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
56 |
ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
57 |
try { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
58 |
try (ServerSocketChannel ssc = ServerSocketChannel.open()) { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
59 |
ssc.bind(new InetSocketAddress(0)); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
60 |
InetAddress lh = InetAddress.getLocalHost(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
61 |
int port = ssc.socket().getLocalPort(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
62 |
SocketAddress sa = new InetSocketAddress(lh, port); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
63 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
64 |
ByteBuffer bb = ByteBuffer.allocate(2*1024*1024); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
65 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
66 |
for (int i=0; i<20; i++) { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
67 |
try (SocketChannel source = SocketChannel.open(sa); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
68 |
SocketChannel sink = ssc.accept()) |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
69 |
{ |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
70 |
// schedule channel to be closed |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
71 |
Closer c = new Closer(source); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
72 |
int when = 1000 + rand.nextInt(2000); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
73 |
Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
74 |
|
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
75 |
// the write should either succeed or else throw a |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
76 |
// ClosedChannelException (more likely an |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
77 |
// AsynchronousCloseException) |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
78 |
try { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
79 |
for (;;) { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
80 |
int limit = rand.nextInt(bb.capacity()); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
81 |
bb.position(0); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
82 |
bb.limit(limit); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
83 |
int n = source.write(bb); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
84 |
System.out.format("wrote %d, expected %d%n", n, limit); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
85 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
86 |
} catch (ClosedChannelException expected) { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
87 |
System.out.println(expected + " (expected)"); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
88 |
} finally { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
89 |
result.get(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
90 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
91 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
92 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
93 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
94 |
} finally { |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
95 |
pool.shutdown(); |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
96 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
97 |
} |
f72c7e618ed0
7177617: TEST_BUG: java/nio/channels/AsyncCloseAndInterrupt.java failing (win)
alanb
parents:
diff
changeset
|
98 |
} |