57115
|
1 |
/*
|
57160
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
57115
|
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 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.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 8195160
|
|
26 |
* @summary Test asynchronous close during a blocking write
|
|
27 |
* @requires (os.family == "linux")
|
|
28 |
* @library .. /test/lib
|
|
29 |
* @build RsocketTest
|
|
30 |
* @run main/othervm CloseDuringWrite
|
|
31 |
* @key randomness
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.io.Closeable;
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.net.Inet6Address;
|
|
37 |
import java.net.InetAddress;
|
|
38 |
import java.net.InetSocketAddress;
|
|
39 |
import java.net.ProtocolFamily;
|
|
40 |
import java.net.SocketAddress;
|
|
41 |
import java.nio.ByteBuffer;
|
|
42 |
import java.nio.channels.ClosedChannelException;
|
|
43 |
import java.nio.channels.ServerSocketChannel;
|
|
44 |
import java.nio.channels.SocketChannel;
|
|
45 |
import java.util.concurrent.Callable;
|
|
46 |
import java.util.concurrent.Executors;
|
|
47 |
import java.util.concurrent.Future;
|
|
48 |
import java.util.concurrent.ScheduledExecutorService;
|
|
49 |
import java.util.concurrent.TimeUnit;
|
|
50 |
import java.util.Random;
|
|
51 |
import jdk.net.RdmaSockets;
|
|
52 |
import jtreg.SkippedException;
|
|
53 |
import static java.net.StandardProtocolFamily.INET;
|
|
54 |
import static java.net.StandardProtocolFamily.INET6;
|
|
55 |
|
|
56 |
public class CloseDuringWrite {
|
|
57 |
|
|
58 |
static final Random rand = new Random();
|
|
59 |
|
|
60 |
/**
|
|
61 |
* A task that closes a Closeable
|
|
62 |
*/
|
|
63 |
static class Closer implements Callable<Void> {
|
|
64 |
final Closeable c;
|
|
65 |
Closer(Closeable c) {
|
|
66 |
this.c = c;
|
|
67 |
}
|
|
68 |
public Void call() throws IOException {
|
|
69 |
c.close();
|
|
70 |
return null;
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
public static void main(String[] args) throws Exception {
|
|
75 |
if (!RsocketTest.isRsocketAvailable())
|
|
76 |
throw new SkippedException("rsocket is not available");
|
|
77 |
|
|
78 |
InetAddress lh = InetAddress.getLocalHost();
|
|
79 |
ProtocolFamily family = lh instanceof Inet6Address ? INET6 : INET;
|
|
80 |
System.out.printf("local address: %s%n", lh);
|
|
81 |
|
|
82 |
ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
|
|
83 |
try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
|
|
84 |
ssc.bind(new InetSocketAddress(lh, 0));
|
|
85 |
int port = ssc.socket().getLocalPort();
|
|
86 |
SocketAddress sa = new InetSocketAddress(lh, port);
|
|
87 |
|
|
88 |
ByteBuffer bb = ByteBuffer.allocate(2 * 1024 * 1024);
|
|
89 |
|
|
90 |
for (int i = 0; i < 20; i++) {
|
|
91 |
System.out.println(i);
|
|
92 |
try (SocketChannel source = RdmaSockets.openSocketChannel(family)) {
|
|
93 |
Runnable runnable = new Runnable() {
|
|
94 |
@Override
|
|
95 |
public void run() {
|
|
96 |
try {
|
|
97 |
System.out.println("connecting ...");
|
|
98 |
source.connect(sa);
|
|
99 |
System.out.println("connected");
|
|
100 |
} catch (Exception e) {
|
|
101 |
e.printStackTrace();
|
|
102 |
throw new RuntimeException("Test Failed");
|
|
103 |
}
|
|
104 |
}
|
|
105 |
};
|
|
106 |
|
|
107 |
Thread t = new Thread(runnable);
|
|
108 |
t.start();
|
|
109 |
try (SocketChannel sink = ssc.accept()) {
|
|
110 |
System.out.println("accepted new connection");
|
|
111 |
// schedule channel to be closed
|
|
112 |
Closer c = new Closer(source);
|
|
113 |
int when = 1000 + rand.nextInt(2000);
|
|
114 |
Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
|
|
115 |
|
|
116 |
// the write should either succeed or else throw a
|
|
117 |
// ClosedChannelException (more likely an
|
|
118 |
// AsynchronousCloseException)
|
|
119 |
try {
|
|
120 |
for (;;) {
|
|
121 |
int limit = rand.nextInt(bb.capacity());
|
|
122 |
bb.position(0);
|
|
123 |
bb.limit(limit);
|
|
124 |
int n = source.write(bb);
|
|
125 |
System.out.format("wrote %d, expected %d%n", n, limit);
|
|
126 |
}
|
|
127 |
} catch (ClosedChannelException expected) {
|
|
128 |
System.out.println(expected + " (expected)");
|
|
129 |
} finally {
|
|
130 |
result.get();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
} catch (Exception e) {
|
|
134 |
e.printStackTrace();
|
|
135 |
throw new RuntimeException("Test Failed");
|
|
136 |
}
|
|
137 |
}
|
|
138 |
} finally {
|
|
139 |
pool.shutdown();
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|