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

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

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