提交 d536bcee 编写于 作者: D dfuchs

6720349: (ch) Channels tests depending on hosts inside Sun

Summary: This changeset make the nio tests start small TCP or UDP servers from within the tests, instead of relying on external services.
Reviewed-by: alanb
上级 776ce47e
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,29 +27,16 @@
* @library ..
*/
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*;
public class AdaptDatagramSocket {
static java.io.PrintStream out = System.out;
static Random rand = new Random();
static final int ECHO_PORT = 7;
static final int DISCARD_PORT = 9;
static final String REMOTE_HOST = TestUtil.HOST;
static final InetSocketAddress echoAddress
= new InetSocketAddress(REMOTE_HOST, ECHO_PORT);
static final InetSocketAddress discardAddress
= new InetSocketAddress(REMOTE_HOST, DISCARD_PORT);
static String toString(DatagramPacket dp) {
return ("DatagramPacket[off=" + dp.getOffset()
+ ", len=" + dp.getLength()
......@@ -88,10 +75,11 @@ public class AdaptDatagramSocket {
out.println("rtt: " + (System.currentTimeMillis() - start));
out.println("post op: " + toString(op) + " ip: " + toString(ip));
for (int i = 0; i < ip.getLength(); i++)
for (int i = 0; i < ip.getLength(); i++) {
if (ip.getData()[ip.getOffset() + i]
!= op.getData()[op.getOffset() + i])
throw new Exception("Incorrect data received");
}
if (!(ip.getSocketAddress().equals(dst))) {
throw new Exception("Incorrect sender address, expected: " + dst
......@@ -130,8 +118,9 @@ public class AdaptDatagramSocket {
ds.setSoTimeout(timeout);
out.println("timeout: " + ds.getSoTimeout());
for (int i = 0; i < 5; i++)
for (int i = 0; i < 5; i++) {
test(ds, dst, shouldTimeout);
}
// Leave the socket open so that we don't reuse the old src address
//ds.close();
......@@ -139,10 +128,23 @@ public class AdaptDatagramSocket {
}
public static void main(String[] args) throws Exception {
test(echoAddress, 0, false, false);
test(echoAddress, 0, false, true);
test(echoAddress, 5000, false, false);
test(discardAddress, 10, true, false);
// need an UDP echo server
try (TestServers.UdpEchoServer echoServer
= TestServers.UdpEchoServer.startNewServer(100)) {
final InetSocketAddress address
= new InetSocketAddress(echoServer.getAddress(),
echoServer.getPort());
test(address, 0, false, false);
test(address, 0, false, true);
test(address, 5000, false, false);
}
try (TestServers.UdpDiscardServer discardServer
= TestServers.UdpDiscardServer.startNewServer()) {
final InetSocketAddress address
= new InetSocketAddress(discardServer.getAddress(),
discardServer.getPort());
test(address, 10, true, false);
}
}
}
/*
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -34,8 +34,11 @@ import java.nio.channels.*;
public class IsBound {
public static void main(String argv[]) throws Exception {
try (TestServers.UdpDayTimeServer daytimeServer
= TestServers.UdpDayTimeServer.startNewServer(100)) {
InetSocketAddress isa = new InetSocketAddress(
InetAddress.getByName(TestUtil.HOST), 13);
daytimeServer.getAddress(),
daytimeServer.getPort());
ByteBuffer bb = ByteBuffer.allocateDirect(256);
bb.put("hello".getBytes());
bb.flip();
......@@ -51,4 +54,5 @@ public class IsBound {
throw new Exception("Test failed");
dc.close();
}
}
}
/*
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,14 +28,15 @@
*/
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class IsConnected {
public static void main(String argv[]) throws Exception {
try (TestServers.UdpDayTimeServer daytimeServer
= TestServers.UdpDayTimeServer.startNewServer(100)) {
InetSocketAddress isa = new InetSocketAddress(
InetAddress.getByName(TestUtil.HOST), 13);
daytimeServer.getAddress(), daytimeServer.getPort());
DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking(true);
dc.connect(isa);
......@@ -45,4 +46,5 @@ public class IsConnected {
throw new RuntimeException("socket.isConnected inconsistent");
dc.close();
}
}
}
/*
* Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,12 +27,11 @@
* @library ..
*/
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class Alias {
......@@ -40,18 +39,26 @@ public class Alias {
static int LIMIT = 20; // Hangs after just 1 if problem is present
public static void main(String[] args) throws Exception {
test1();
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
test1(daytimeServer);
}
}
public static void test1() throws Exception {
static void test1(TestServers.DayTimeServer daytimeServer) throws Exception {
Selector selector = SelectorProvider.provider().openSelector();
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST);
InetSocketAddress isa = new InetSocketAddress(myAddress,13);
InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa
= new InetSocketAddress(myAddress,
daytimeServer.getPort());
for (int j=0; j<LIMIT; j++) {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean result = sc.connect(isa);
// On some platforms - given that we're using a local server,
// we may not enter into the if () { } statement below...
if (!result) {
SelectionKey key = sc.register(selector,
SelectionKey.OP_CONNECT);
......
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,12 +27,10 @@
* @library ..
*/
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.*;
import java.util.*;
......@@ -44,17 +42,22 @@ import java.util.*;
public class BasicConnect {
static final int PORT = 7; // echo
static final String HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception {
Selector connectSelector =
SelectorProvider.provider().openSelector();
try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer(100)) {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(HOST), PORT);
= new InetSocketAddress(echoServer.getAddress(),
echoServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean result = sc.connect(isa);
if (result) {
System.out.println("Socket immediately connected on "
+ System.getProperty("os.name")
+ ": " + sc);
}
while (!result) {
SelectionKey connectKey = sc.register(connectSelector,
SelectionKey.OP_CONNECT);
......@@ -91,5 +94,5 @@ public class BasicConnect {
throw new Exception("Echoed bytes incorrect: Sent "
+ bb + ", got " + bb2);
}
}
}
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,12 +27,11 @@
* @library ..
*/
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class Connect {
......@@ -40,12 +39,18 @@ public class Connect {
static int LIMIT = 100;
public static void main(String[] args) throws Exception {
scaleTest();
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(50)) {
scaleTest(daytimeServer);
}
}
public static void scaleTest() throws Exception {
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST);
InetSocketAddress isa = new InetSocketAddress(myAddress,13);
static void scaleTest(TestServers.DayTimeServer daytimeServer)
throws Exception
{
InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa
= new InetSocketAddress(myAddress, daytimeServer.getPort());
for (int j=0; j<LIMIT; j++) {
SocketChannel sc = SocketChannel.open();
......
/*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,23 +27,25 @@
* @library ..
*/
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class ConnectWrite {
public static void main(String[] args) throws Exception {
test1(13);
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(25)) {
test1(daytimeServer);
}
}
public static void test1(int port) throws Exception {
static void test1(TestServers.DayTimeServer daytimeServer) throws Exception {
Selector selector = SelectorProvider.provider().openSelector();
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST);
InetSocketAddress isa = new InetSocketAddress(myAddress, port);
InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa
= new InetSocketAddress(myAddress, daytimeServer.getPort());
SocketChannel sc = SocketChannel.open();
try {
sc.configureBlocking(false);
......
/*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,21 +28,15 @@
*/
import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.nio.channels.spi.SelectorProvider;
public class KeysReady {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
static void test(TestServers.DayTimeServer dayTimeServer) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(dayTimeServer.getAddress(),
dayTimeServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
sc.connect(isa);
......@@ -64,7 +58,10 @@ public class KeysReady {
}
public static void main(String[] args) throws Exception {
test();
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(50)) {
test(daytimeServer);
}
}
}
......@@ -35,19 +35,16 @@ public class AdaptSocket {
static java.io.PrintStream out = System.out;
static final int ECHO_PORT = 7;
static final int DAYTIME_PORT = 13;
static final String REMOTE_HOST = TestUtil.HOST;
static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
static void test(String hn, int timeout, boolean shouldTimeout)
static void test(TestServers.DayTimeServer dayTimeServer,
int timeout,
boolean shouldTimeout)
throws Exception
{
out.println();
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(hn),
DAYTIME_PORT);
= new InetSocketAddress(dayTimeServer.getAddress(),
dayTimeServer.getPort());
SocketChannel sc = SocketChannel.open();
Socket so = sc.socket();
out.println("opened: " + so);
......@@ -116,13 +113,16 @@ public class AdaptSocket {
}
}
static void testRead(String hn, int timeout, boolean shouldTimeout)
static void testRead(TestServers.EchoServer echoServer,
int timeout,
boolean shouldTimeout)
throws Exception
{
out.println();
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
= new InetSocketAddress(echoServer.getAddress(),
echoServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.connect(isa);
Socket so = sc.socket();
......@@ -134,22 +134,38 @@ public class AdaptSocket {
out.println("timeout: " + so.getSoTimeout());
testRead(so, shouldTimeout);
for (int i = 0; i < 4; i++)
for (int i = 0; i < 4; i++) {
testRead(so, shouldTimeout);
}
sc.close();
}
public static void main(String[] args) throws Exception {
test(REMOTE_HOST, 0, false);
test(REMOTE_HOST, 1000, false);
test(VERY_REMOTE_HOST, 10, true);
try (TestServers.DayTimeServer dayTimeServer
= TestServers.DayTimeServer.startNewServer()) {
test(dayTimeServer, 0, false);
test(dayTimeServer, 1000, false);
}
testRead(REMOTE_HOST, 0, false);
testRead(REMOTE_HOST, 8000, false);
testRead(VERY_REMOTE_HOST, 10, true);
try (TestServers.DayTimeServer lingerDayTimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
// this test no longer really test the connection timeout
// since there is no way to prevent the server from eagerly
// accepting connection...
test(lingerDayTimeServer, 10, true);
}
try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer()) {
testRead(echoServer, 0, false);
testRead(echoServer, 8000, false);
}
try (TestServers.EchoServer lingerEchoServer
= TestServers.EchoServer.startNewServer(100)) {
testRead(lingerEchoServer, 10, true);
}
}
}
/*
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -36,13 +36,10 @@ public class Basic {
static java.io.PrintStream out = System.out;
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(isa);
out.println("opened: " + sc);
/*
......@@ -76,7 +73,10 @@ public class Basic {
}
public static void main(String[] args) throws Exception {
test();
try (TestServers.DayTimeServer dayTimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
test(dayTimeServer);
}
}
}
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,17 +28,10 @@
*/
import java.nio.channels.*;
import java.net.*;
public class BufferSize {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
ServerSocketChannel sc = ServerSocketChannel.open();
try {
sc.socket().setReceiveBufferSize(-1);
......
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,9 +27,9 @@
* @library ..
*/
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
public class Connect {
......@@ -37,21 +37,26 @@ public class Connect {
private static final long INCREMENTAL_DELAY = 30L * 1000L;
public static void main(String args[]) throws Exception {
test1(TestUtil.HOST);
try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer(1000)) {
test1(echoServer);
}
try {
test1(TestUtil.REFUSING_HOST);
TestServers.RefusingServer refusingServer
= TestServers.RefusingServer.startNewServer();
test1(refusingServer);
throw new Exception("Refused connection throws no exception");
} catch (ConnectException ce) {
// Correct result
}
}
static void test1(String hostname) throws Exception {
static void test1(TestServers.AbstractServer server) throws Exception {
Selector selector;
SocketChannel sc;
SelectionKey sk;
InetSocketAddress isa = new InetSocketAddress(
InetAddress.getByName (hostname), 80);
server.getAddress(), server.getPort());
sc = SocketChannel.open();
sc.configureBlocking(false);
......
/*
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -30,20 +30,39 @@ import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
public class ConnectState {
static PrintStream log = System.err;
static String REMOTE_HOST = TestUtil.HOST;
static int REMOTE_PORT = 7; // echo
static InetSocketAddress remote;
final static int ST_UNCONNECTED = 0;
final static int ST_PENDING = 1;
final static int ST_CONNECTED = 2;
final static int ST_CLOSED = 3;
final static int ST_PENDING_OR_CONNECTED = 4;
// NO exceptions expected
final static Collection<Class<?>> NONE = Collections.emptySet();
// make a set of expected exception.
static Collection<Class<?>> expectedExceptions(Class<?>... expected) {
final Collection<Class<?>> exceptions;
if (expected.length == 0) {
exceptions = NONE;
} else if (expected.length == 1) {
assert expected[0] != null;
exceptions = Collections.<Class<?>>singleton(expected[0]);
} else {
exceptions = new HashSet<>(Arrays.asList(expected));
}
return exceptions;
}
static abstract class Test {
......@@ -76,37 +95,65 @@ public class ConnectState {
check(!sc.isConnectionPending(), "!isConnectionPending");
check(sc.isOpen(), "isOpen");
break;
case ST_PENDING_OR_CONNECTED:
check(sc.isConnected() || sc.isConnectionPending(),
"isConnected || isConnectionPending");
check(sc.isOpen(), "isOpen");
break;
}
}
Test(String name, Class<?> exception, int state) throws Exception {
this(name, expectedExceptions(exception), state);
}
Test(String name, Class exception, int state) throws Exception {
// On some architecture we may need to accept several exceptions.
// For instance on Solaris, when using a server colocated on the
// machine we cannot guarantee that we will get a
// ConnectionPendingException when connecting twice on the same
// non-blocking socket. We may instead get a an
// AlreadyConnectedException, which is also valid: it simply means
// that the first connection has been immediately accepted.
Test(String name, Collection<Class<?>> exceptions, int state)
throws Exception {
SocketChannel sc = SocketChannel.open();
String note = null;
String note;
try {
try {
note = go(sc);
} catch (Exception x) {
if (exception != null) {
Class<?> expectedExceptionClass = null;
for (Class<?> exception : exceptions) {
if (exception.isInstance(x)) {
log.println(name + ": As expected: "
+ x);
expectedExceptionClass = exception;
check(sc, state);
return;
} else {
break;
}
}
if (expectedExceptionClass == null
&& !exceptions.isEmpty()) {
// we had an exception, but it's not of the set of
// exceptions we expected.
throw new Exception(name
+ ": Incorrect exception",
x);
}
} else {
} else if (exceptions.isEmpty()) {
// we didn't expect any exception
throw new Exception(name
+ ": Unexpected exception",
x);
}
// if we reach here, we have our expected exception
assert expectedExceptionClass != null;
return;
}
if (exception != null)
if (!exceptions.isEmpty()) {
throw new Exception(name
+ ": Expected exception not thrown: "
+ exception);
+ exceptions.iterator().next());
}
check(sc, state);
log.println(name + ": Returned normally"
+ ((note != null) ? ": " + note : ""));
......@@ -123,6 +170,7 @@ public class ConnectState {
new Test("Read unconnected", NotYetConnectedException.class,
ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
ByteBuffer b = ByteBuffer.allocateDirect(1024);
sc.read(b);
......@@ -131,19 +179,22 @@ public class ConnectState {
new Test("Write unconnected", NotYetConnectedException.class,
ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
ByteBuffer b = ByteBuffer.allocateDirect(1024);
sc.write(b);
return null;
}};
new Test("Simple connect", null, ST_CONNECTED) {
new Test("Simple connect", NONE, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.connect(remote);
return null;
}};
new Test("Simple connect & finish", null, ST_CONNECTED) {
new Test("Simple connect & finish", NONE, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.connect(remote);
if (!sc.finishConnect())
......@@ -153,6 +204,7 @@ public class ConnectState {
new Test("Double connect",
AlreadyConnectedException.class, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.connect(remote);
sc.connect(remote);
......@@ -161,12 +213,16 @@ public class ConnectState {
new Test("Finish w/o start",
NoConnectionPendingException.class, ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.finishConnect();
return null;
}};
new Test("NB simple connect", null, ST_CONNECTED) {
// Note: using our local EchoServer rather than echo on a distant
// host - we see that Tries to finish = 0 (instead of ~ 18).
new Test("NB simple connect", NONE, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false);
sc.connect(remote);
......@@ -179,8 +235,15 @@ public class ConnectState {
return ("Tries to finish = " + n);
}};
// Note: using our local EchoServer rather than echo on a distant
// host - we cannot guarantee that this test will get a
// a ConnectionPendingException: it may get an
// AlreadyConnectedException, so we should allow for both.
new Test("NB double connect",
ConnectionPendingException.class, ST_PENDING) {
expectedExceptions(ConnectionPendingException.class,
AlreadyConnectedException.class),
ST_PENDING_OR_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false);
sc.connect(remote);
......@@ -190,13 +253,15 @@ public class ConnectState {
new Test("NB finish w/o start",
NoConnectionPendingException.class, ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false);
sc.finishConnect();
return null;
}};
new Test("NB connect, B finish", null, ST_CONNECTED) {
new Test("NB connect, B finish", NONE, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false);
sc.connect(remote);
......@@ -208,9 +273,12 @@ public class ConnectState {
}
public static void main(String[] args) throws Exception {
remote = new InetSocketAddress(InetAddress.getByName(REMOTE_HOST),
REMOTE_PORT);
try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer(500)) {
remote = new InetSocketAddress(echoServer.getAddress(),
echoServer.getPort());
tests();
}
}
}
/*
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -36,21 +36,25 @@ import java.util.*;
public class FinishConnect {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception {
test1(true, true);
test1(true, false);
test1(false, true);
test1(false, false);
test2();
try (TestServers.DayTimeServer dayTimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
test1(dayTimeServer, true, true);
test1(dayTimeServer, true, false);
test1(dayTimeServer, false, true);
test1(dayTimeServer, false, false);
test2(dayTimeServer);
}
}
static void test1(boolean select, boolean setBlocking) throws Exception {
static void test1(TestServers.DayTimeServer daytimeServer,
boolean select,
boolean setBlocking)
throws Exception
{
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
......@@ -109,15 +113,27 @@ public class FinishConnect {
sc.close();
}
static void test2() throws Exception {
static void test2(TestServers.DayTimeServer daytimeServer) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
boolean done = false;
int globalAttempts = 0;
int connectSuccess = 0;
while (!done) {
if (globalAttempts++ > 50)
// When using a local daytime server it is not always possible
// to get a pending connection, as sc.connect(isa) may always
// return true.
// So we're going to throw the exception only if there was
// at least 1 case where we did not manage to connect.
if (globalAttempts++ > 50) {
if (globalAttempts == connectSuccess + 1) {
System.out.println("Can't fully test on "
+ System.getProperty("os.name"));
break;
}
throw new RuntimeException("Failed to connect");
}
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
......@@ -132,6 +148,9 @@ public class FinishConnect {
}
Thread.sleep(10);
}
if (connected) {
connectSuccess++;
}
sc.close();
}
}
......
/*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,24 +28,19 @@
*/
import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class IsConnectable {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
sc.connect(isa);
final boolean immediatelyConnected = sc.connect(isa);
Selector selector = SelectorProvider.provider().openSelector();
try {
......@@ -67,7 +62,12 @@ public class IsConnectable {
throw new Exception("Test failed: 4737146 detected");
}
} else {
if (!immediatelyConnected) {
throw new Exception("Select failed");
} else {
System.out.println("IsConnectable couldn't be fully tested for "
+ System.getProperty("os.name"));
}
}
} finally {
sc.close();
......@@ -76,7 +76,10 @@ public class IsConnectable {
}
public static void main(String[] args) throws Exception {
test();
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
test(daytimeServer);
}
}
}
/*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -28,18 +28,20 @@
*/
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class LocalAddress {
public static void main(String[] args) throws Exception {
test1();
try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer()) {
test1(echoServer);
}
}
static void test1() throws Exception {
static void test1(TestServers.AbstractServer server) throws Exception {
InetAddress bogus = InetAddress.getByName("0.0.0.0");
InetSocketAddress saddr = new InetSocketAddress(
InetAddress.getByName(TestUtil.HOST), 23);
server.getAddress(), server.getPort());
//Test1: connect only
SocketChannel sc = SocketChannel.open();
......
/*
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,22 +27,17 @@
* @library ..
*/
import java.net.*;
import java.io.*;
import java.nio.*;
import java.net.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class Stream {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
= new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.connect(isa);
sc.configureBlocking(false);
......@@ -58,7 +53,9 @@ public class Stream {
}
public static void main(String[] args) throws Exception {
test();
try (TestServers.DayTimeServer dayTimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
test(dayTimeServer);
}
}
}
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -27,31 +27,31 @@
* @library ..
*/
import java.net.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class VectorParams {
static java.io.PrintStream out = System.out;
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
static final int testSize = 10;
static ByteBuffer[] bufs = null;
static InetSocketAddress isa = null;
public static void main(String[] args) throws Exception {
initBufs();
try (TestServers.DayTimeServer daytimeServer
= TestServers.DayTimeServer.startNewServer(100)) {
initBufs(daytimeServer);
testSocketChannelVectorParams();
testDatagramChannelVectorParams();
testPipeVectorParams();
testFileVectorParams();
}
}
static void initBufs() throws Exception {
static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {
bufs = new ByteBuffer[testSize];
for(int i=0; i<testSize; i++) {
String source = "buffer" + i;
......@@ -59,8 +59,8 @@ public class VectorParams {
bufs[i].put(source.getBytes("8859_1"));
bufs[i].flip();
}
isa = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
isa = new InetSocketAddress(daytimeServer.getAddress(),
daytimeServer.getPort());
}
static void testSocketChannelVectorParams() throws Exception {
......
此差异已折叠。
......@@ -27,7 +27,6 @@
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.Random;
......@@ -36,9 +35,6 @@ public class TestUtil {
// Test hosts used by the channels tests - change these when
// executing in a different network.
public static final String HOST = "javaweb.sfbay.sun.com";
public static final String REFUSING_HOST = "jano1.sfbay.sun.com";
public static final String FAR_HOST = "irejano.ireland.sun.com";
public static final String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah";
private TestUtil() { }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册