提交 70bb8ffd 编写于 作者: A alanb

6965150: TEST_BUG: java/nio/channels/AsynchronousSocketChannel/Basic.java takes too long

Reviewed-by: chegar
上级 763725b1
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
/* @test /* @test
* @bug 4607272 6842687 6878369 6944810 7023403 * @bug 4607272 6842687 6878369 6944810 7023403
* @summary Unit test for AsynchronousSocketChannel * @summary Unit test for AsynchronousSocketChannel
* @run main/timeout=600 Basic * @run main Basic -skipSlowConnectTest
*/ */
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
...@@ -34,12 +34,25 @@ import java.net.*; ...@@ -34,12 +34,25 @@ import java.net.*;
import java.util.Random; import java.util.Random;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
public class Basic { public class Basic {
static final Random rand = new Random(); static final Random rand = new Random();
static boolean skipSlowConnectTest = false;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
for (String arg: args) {
switch (arg) {
case "-skipSlowConnectTest" :
skipSlowConnectTest = true;
break;
default:
throw new RuntimeException("Unrecognized argument: " + arg);
}
}
testBind(); testBind();
testSocketOptions(); testSocketOptions();
testConnect(); testConnect();
...@@ -54,7 +67,7 @@ public class Basic { ...@@ -54,7 +67,7 @@ public class Basic {
testShutdown(); testShutdown();
} }
static class Server { static class Server implements Closeable {
private final ServerSocketChannel ssc; private final ServerSocketChannel ssc;
private final InetSocketAddress address; private final InetSocketAddress address;
...@@ -74,10 +87,8 @@ public class Basic { ...@@ -74,10 +87,8 @@ public class Basic {
return ssc.accept(); return ssc.accept();
} }
void close() { public void close() throws IOException {
try {
ssc.close(); ssc.close();
} catch (IOException ignore) { }
} }
} }
...@@ -85,7 +96,7 @@ public class Basic { ...@@ -85,7 +96,7 @@ public class Basic {
static void testBind() throws Exception { static void testBind() throws Exception {
System.out.println("-- bind --"); System.out.println("-- bind --");
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {
if (ch.getLocalAddress() != null) if (ch.getLocalAddress() != null)
throw new RuntimeException("Local address should be 'null'"); throw new RuntimeException("Local address should be 'null'");
ch.bind(new InetSocketAddress(0)); ch.bind(new InetSocketAddress(0));
...@@ -103,10 +114,10 @@ public class Basic { ...@@ -103,10 +114,10 @@ public class Basic {
throw new RuntimeException("AlreadyBoundException expected"); throw new RuntimeException("AlreadyBoundException expected");
} catch (AlreadyBoundException x) { } catch (AlreadyBoundException x) {
} }
ch.close(); }
// check ClosedChannelException // check ClosedChannelException
ch = AsynchronousSocketChannel.open(); AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.close(); ch.close();
try { try {
ch.bind(new InetSocketAddress(0)); ch.bind(new InetSocketAddress(0));
...@@ -118,8 +129,8 @@ public class Basic { ...@@ -118,8 +129,8 @@ public class Basic {
static void testSocketOptions() throws Exception { static void testSocketOptions() throws Exception {
System.out.println("-- socket options --"); System.out.println("-- socket options --");
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open() try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {
.setOption(SO_RCVBUF, 128*1024) ch.setOption(SO_RCVBUF, 128*1024)
.setOption(SO_SNDBUF, 128*1024) .setOption(SO_SNDBUF, 128*1024)
.setOption(SO_REUSEADDR, true); .setOption(SO_REUSEADDR, true);
...@@ -137,31 +148,34 @@ public class Basic { ...@@ -137,31 +148,34 @@ public class Basic {
ch.bind(new InetSocketAddress(0)); ch.bind(new InetSocketAddress(0));
// default values // default values
if ((Boolean)ch.getOption(SO_KEEPALIVE)) if (ch.getOption(SO_KEEPALIVE))
throw new RuntimeException("Default of SO_KEEPALIVE should be 'false'"); throw new RuntimeException("Default of SO_KEEPALIVE should be 'false'");
if ((Boolean)ch.getOption(TCP_NODELAY)) if (ch.getOption(TCP_NODELAY))
throw new RuntimeException("Default of TCP_NODELAY should be 'false'"); throw new RuntimeException("Default of TCP_NODELAY should be 'false'");
// set and check // set and check
if (!(Boolean)ch.setOption(SO_KEEPALIVE, true).getOption(SO_KEEPALIVE)) if (!ch.setOption(SO_KEEPALIVE, true).getOption(SO_KEEPALIVE))
throw new RuntimeException("SO_KEEPALIVE did not change"); throw new RuntimeException("SO_KEEPALIVE did not change");
if (!(Boolean)ch.setOption(TCP_NODELAY, true).getOption(TCP_NODELAY)) if (!ch.setOption(TCP_NODELAY, true).getOption(TCP_NODELAY))
throw new RuntimeException("SO_KEEPALIVE did not change"); throw new RuntimeException("SO_KEEPALIVE did not change");
// read others (can't check as actual value is implementation dependent) // read others (can't check as actual value is implementation dependent)
ch.getOption(SO_RCVBUF); ch.getOption(SO_RCVBUF);
ch.getOption(SO_SNDBUF); ch.getOption(SO_SNDBUF);
}
ch.close();
} }
static void testConnect() throws Exception { static void testConnect() throws Exception {
System.out.println("-- connect --"); System.out.println("-- connect --");
Server server = new Server(); SocketAddress address;
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); try (Server server = new Server()) {
address = server.address();
// connect to server and check local/remote addresses
try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {
ch.connect(address).get();
// check local address // check local address
if (ch.getLocalAddress() == null) if (ch.getLocalAddress() == null)
throw new RuntimeException("Not bound to local address"); throw new RuntimeException("Not bound to local address");
...@@ -179,10 +193,13 @@ public class Basic { ...@@ -179,10 +193,13 @@ public class Basic {
throw new RuntimeException("AlreadyConnectedException expected"); throw new RuntimeException("AlreadyConnectedException expected");
} catch (AlreadyConnectedException x) { } catch (AlreadyConnectedException x) {
} }
ch.close();
// check that connect fails with ClosedChannelException) // clean-up
ch = AsynchronousSocketChannel.open(); server.accept().close();
}
// check that connect fails with ClosedChannelException
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.close(); ch.close();
try { try {
ch.connect(server.address()).get(); ch.connect(server.address()).get();
...@@ -191,8 +208,7 @@ public class Basic { ...@@ -191,8 +208,7 @@ public class Basic {
if (!(x.getCause() instanceof ClosedChannelException)) if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected"); throw new RuntimeException("Cause of ClosedChannelException expected");
} }
final AtomicReference<Throwable> connectException = final AtomicReference<Throwable> connectException = new AtomicReference<>();
new AtomicReference<Throwable>();
ch.connect(server.address(), (Void)null, new CompletionHandler<Void,Void>() { ch.connect(server.address(), (Void)null, new CompletionHandler<Void,Void>() {
public void completed(Void result, Void att) { public void completed(Void result, Void att) {
} }
...@@ -205,22 +221,32 @@ public class Basic { ...@@ -205,22 +221,32 @@ public class Basic {
} }
if (!(connectException.get() instanceof ClosedChannelException)) if (!(connectException.get() instanceof ClosedChannelException))
throw new RuntimeException("ClosedChannelException expected"); throw new RuntimeException("ClosedChannelException expected");
}
System.out.println("-- connect to non-existent host --");
// test that failure to connect closes the channel // test that failure to connect closes the channel
ch = AsynchronousSocketChannel.open(); try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {
try { try {
ch.connect(genSocketAddress()).get(); ch.connect(address).get();
} catch (ExecutionException x) { } catch (ExecutionException x) {
// failed to establish connection // failed to establish connection
if (ch.isOpen()) if (ch.isOpen())
throw new RuntimeException("Channel should be closed"); throw new RuntimeException("Channel should be closed");
} finally { }
ch.close();
} }
server.close(); // repeat test by connecting to a (probably) non-existent host. This
// improves the chance that the connect will not fail immediately.
if (!skipSlowConnectTest) {
try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {
try {
ch.connect(genSocketAddress()).get();
} catch (ExecutionException x) {
// failed to establish connection
if (ch.isOpen())
throw new RuntimeException("Channel should be closed");
}
}
}
} }
static void testCloseWhenPending() throws Exception { static void testCloseWhenPending() throws Exception {
...@@ -249,7 +275,7 @@ public class Basic { ...@@ -249,7 +275,7 @@ public class Basic {
System.out.println("-- asynchronous close when reading --"); System.out.println("-- asynchronous close when reading --");
Server server = new Server(); try (Server server = new Server()) {
ch = AsynchronousSocketChannel.open(); ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
...@@ -266,6 +292,7 @@ public class Basic { ...@@ -266,6 +292,7 @@ public class Basic {
// close channel (should cause initial read to complete) // close channel (should cause initial read to complete)
ch.close(); ch.close();
server.accept().close();
// check that AsynchronousCloseException is thrown // check that AsynchronousCloseException is thrown
try { try {
...@@ -306,6 +333,7 @@ public class Basic { ...@@ -306,6 +333,7 @@ public class Basic {
// close channel - should cause initial write to complete // close channel - should cause initial write to complete
ch.close(); ch.close();
server.accept().close();
// wait for exception // wait for exception
while (writeException.get() == null) { while (writeException.get() == null) {
...@@ -313,15 +341,13 @@ public class Basic { ...@@ -313,15 +341,13 @@ public class Basic {
} }
if (!(writeException.get() instanceof AsynchronousCloseException)) if (!(writeException.get() instanceof AsynchronousCloseException))
throw new RuntimeException("AsynchronousCloseException expected"); throw new RuntimeException("AsynchronousCloseException expected");
}
server.close();
} }
static void testCancel() throws Exception { static void testCancel() throws Exception {
System.out.println("-- cancel --"); System.out.println("-- cancel --");
Server server = new Server(); try (Server server = new Server()) {
for (int i=0; i<2; i++) { for (int i=0; i<2; i++) {
boolean mayInterruptIfRunning = (i == 0) ? false : true; boolean mayInterruptIfRunning = (i == 0) ? false : true;
...@@ -362,14 +388,13 @@ public class Basic { ...@@ -362,14 +388,13 @@ public class Basic {
ch.close(); ch.close();
peer.close(); peer.close();
} }
}
server.close();
} }
static void testRead1() throws Exception { static void testRead1() throws Exception {
System.out.println("-- read (1) --"); System.out.println("-- read (1) --");
Server server = new Server(); try (Server server = new Server()) {
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
...@@ -381,12 +406,12 @@ public class Basic { ...@@ -381,12 +406,12 @@ public class Basic {
throw new RuntimeException("0 expected"); throw new RuntimeException("0 expected");
// write bytes and close connection // write bytes and close connection
SocketChannel sc = server.accept();
ByteBuffer src = genBuffer(); ByteBuffer src = genBuffer();
sc.setOption(StandardSocketOptions.SO_SNDBUF, src.remaining()); try (SocketChannel sc = server.accept()) {
sc.setOption(SO_SNDBUF, src.remaining());
while (src.hasRemaining()) while (src.hasRemaining())
sc.write(src); sc.write(src);
sc.close(); }
// reads should complete immediately // reads should complete immediately
final ByteBuffer dst = ByteBuffer.allocateDirect(src.capacity() + 100); final ByteBuffer dst = ByteBuffer.allocateDirect(src.capacity() + 100);
...@@ -424,15 +449,13 @@ public class Basic { ...@@ -424,15 +449,13 @@ public class Basic {
if (!(x.getCause() instanceof ClosedChannelException)) if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected"); throw new RuntimeException("Cause of ClosedChannelException expected");
} }
}
server.close();
} }
static void testRead2() throws Exception { static void testRead2() throws Exception {
System.out.println("-- read (2) --"); System.out.println("-- read (2) --");
Server server = new Server(); try (Server server = new Server()) {
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
SocketChannel sc = server.accept(); SocketChannel sc = server.accept();
...@@ -479,14 +502,14 @@ public class Basic { ...@@ -479,14 +502,14 @@ public class Basic {
sc.close(); sc.close();
ch.close(); ch.close();
server.close(); }
} }
// exercise scattering read // exercise scattering read
static void testRead3() throws Exception { static void testRead3() throws Exception {
System.out.println("-- read (3) --"); System.out.println("-- read (3) --");
Server server = new Server(); try (Server server = new Server()) {
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
SocketChannel sc = server.accept(); SocketChannel sc = server.accept();
...@@ -540,13 +563,13 @@ public class Basic { ...@@ -540,13 +563,13 @@ public class Basic {
ch.close(); ch.close();
sc.close(); sc.close();
server.close(); }
} }
static void testWrite1() throws Exception { static void testWrite1() throws Exception {
System.out.println("-- write (1) --"); System.out.println("-- write (1) --");
Server server = new Server(); try (Server server = new Server()) {
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
SocketChannel sc = server.accept(); SocketChannel sc = server.accept();
...@@ -596,15 +619,14 @@ public class Basic { ...@@ -596,15 +619,14 @@ public class Basic {
if (!(x.getCause() instanceof ClosedChannelException)) if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected"); throw new RuntimeException("Cause of ClosedChannelException expected");
} }
}
server.close();
} }
// exercise gathering write // exercise gathering write
static void testWrite2() throws Exception { static void testWrite2() throws Exception {
System.out.println("-- write (2) --"); System.out.println("-- write (2) --");
Server server = new Server(); try (Server server = new Server()) {
final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
SocketChannel sc = server.accept(); SocketChannel sc = server.accept();
...@@ -671,17 +693,17 @@ public class Basic { ...@@ -671,17 +693,17 @@ public class Basic {
ch.close(); ch.close();
sc.close(); sc.close();
server.close(); }
} }
static void testShutdown() throws Exception { static void testShutdown() throws Exception {
System.out.println("-- shutdown--"); System.out.println("-- shutdown--");
Server server = new Server(); try (Server server = new Server();
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); AsynchronousSocketChannel ch = AsynchronousSocketChannel.open())
{
ch.connect(server.address()).get(); ch.connect(server.address()).get();
SocketChannel sc = server.accept(); try (SocketChannel peer = server.accept()) {
ByteBuffer buf = ByteBuffer.allocateDirect(1000); ByteBuffer buf = ByteBuffer.allocateDirect(1000);
int n; int n;
...@@ -705,10 +727,8 @@ public class Basic { ...@@ -705,10 +727,8 @@ public class Basic {
if (!(x.getCause() instanceof ClosedChannelException)) if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("ClosedChannelException expected"); throw new RuntimeException("ClosedChannelException expected");
} }
}
sc.close(); }
ch.close();
server.close();
} }
static void testTimeout() throws Exception { static void testTimeout() throws Exception {
...@@ -720,7 +740,7 @@ public class Basic { ...@@ -720,7 +740,7 @@ public class Basic {
} }
static void testTimeout(final long timeout, final TimeUnit unit) throws Exception { static void testTimeout(final long timeout, final TimeUnit unit) throws Exception {
Server server = new Server(); try (Server server = new Server()) {
AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(); AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
ch.connect(server.address()).get(); ch.connect(server.address()).get();
...@@ -801,7 +821,7 @@ public class Basic { ...@@ -801,7 +821,7 @@ public class Basic {
// clean-up // clean-up
server.accept().close(); server.accept().close();
ch.close(); ch.close();
server.close(); }
} }
// returns ByteBuffer with random bytes // returns ByteBuffer with random bytes
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册