提交 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,29 +27,16 @@ ...@@ -27,29 +27,16 @@
* @library .. * @library ..
*/ */
import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*; import java.util.*;
public class AdaptDatagramSocket { public class AdaptDatagramSocket {
static java.io.PrintStream out = System.out; static java.io.PrintStream out = System.out;
static Random rand = new Random(); 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) { static String toString(DatagramPacket dp) {
return ("DatagramPacket[off=" + dp.getOffset() return ("DatagramPacket[off=" + dp.getOffset()
+ ", len=" + dp.getLength() + ", len=" + dp.getLength()
...@@ -88,10 +75,11 @@ public class AdaptDatagramSocket { ...@@ -88,10 +75,11 @@ public class AdaptDatagramSocket {
out.println("rtt: " + (System.currentTimeMillis() - start)); out.println("rtt: " + (System.currentTimeMillis() - start));
out.println("post op: " + toString(op) + " ip: " + toString(ip)); 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] if (ip.getData()[ip.getOffset() + i]
!= op.getData()[op.getOffset() + i]) != op.getData()[op.getOffset() + i])
throw new Exception("Incorrect data received"); throw new Exception("Incorrect data received");
}
if (!(ip.getSocketAddress().equals(dst))) { if (!(ip.getSocketAddress().equals(dst))) {
throw new Exception("Incorrect sender address, expected: " + dst throw new Exception("Incorrect sender address, expected: " + dst
...@@ -130,8 +118,9 @@ public class AdaptDatagramSocket { ...@@ -130,8 +118,9 @@ public class AdaptDatagramSocket {
ds.setSoTimeout(timeout); ds.setSoTimeout(timeout);
out.println("timeout: " + ds.getSoTimeout()); out.println("timeout: " + ds.getSoTimeout());
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++) {
test(ds, dst, shouldTimeout); test(ds, dst, shouldTimeout);
}
// Leave the socket open so that we don't reuse the old src address // Leave the socket open so that we don't reuse the old src address
//ds.close(); //ds.close();
...@@ -139,10 +128,23 @@ public class AdaptDatagramSocket { ...@@ -139,10 +128,23 @@ public class AdaptDatagramSocket {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test(echoAddress, 0, false, false); // need an UDP echo server
test(echoAddress, 0, false, true); try (TestServers.UdpEchoServer echoServer
test(echoAddress, 5000, false, false); = TestServers.UdpEchoServer.startNewServer(100)) {
test(discardAddress, 10, true, false); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -34,21 +34,25 @@ import java.nio.channels.*; ...@@ -34,21 +34,25 @@ import java.nio.channels.*;
public class IsBound { public class IsBound {
public static void main(String argv[]) throws Exception { public static void main(String argv[]) throws Exception {
InetSocketAddress isa = new InetSocketAddress( try (TestServers.UdpDayTimeServer daytimeServer
InetAddress.getByName(TestUtil.HOST), 13); = TestServers.UdpDayTimeServer.startNewServer(100)) {
ByteBuffer bb = ByteBuffer.allocateDirect(256); InetSocketAddress isa = new InetSocketAddress(
bb.put("hello".getBytes()); daytimeServer.getAddress(),
bb.flip(); daytimeServer.getPort());
ByteBuffer bb = ByteBuffer.allocateDirect(256);
bb.put("hello".getBytes());
bb.flip();
DatagramChannel dc = DatagramChannel.open(); DatagramChannel dc = DatagramChannel.open();
dc.send(bb, isa); dc.send(bb, isa);
if(!dc.socket().isBound()) if(!dc.socket().isBound())
throw new Exception("Test failed"); throw new Exception("Test failed");
dc.close(); dc.close();
dc = DatagramChannel.open(); dc = DatagramChannel.open();
if(dc.socket().isBound()) if(dc.socket().isBound())
throw new Exception("Test failed"); throw new Exception("Test failed");
dc.close(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,21 +28,23 @@ ...@@ -28,21 +28,23 @@
*/ */
import java.net.*; import java.net.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
public class IsConnected { public class IsConnected {
public static void main(String argv[]) throws Exception { public static void main(String argv[]) throws Exception {
InetSocketAddress isa = new InetSocketAddress( try (TestServers.UdpDayTimeServer daytimeServer
InetAddress.getByName(TestUtil.HOST), 13); = TestServers.UdpDayTimeServer.startNewServer(100)) {
DatagramChannel dc = DatagramChannel.open(); InetSocketAddress isa = new InetSocketAddress(
dc.configureBlocking(true); daytimeServer.getAddress(), daytimeServer.getPort());
dc.connect(isa); DatagramChannel dc = DatagramChannel.open();
if (!dc.isConnected()) dc.configureBlocking(true);
throw new RuntimeException("channel.isConnected inconsistent"); dc.connect(isa);
if (!dc.socket().isConnected()) if (!dc.isConnected())
throw new RuntimeException("socket.isConnected inconsistent"); throw new RuntimeException("channel.isConnected inconsistent");
dc.close(); if (!dc.socket().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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,12 +27,11 @@ ...@@ -27,12 +27,11 @@
* @library .. * @library ..
*/ */
import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class Alias { public class Alias {
...@@ -40,18 +39,26 @@ public class Alias { ...@@ -40,18 +39,26 @@ public class Alias {
static int LIMIT = 20; // Hangs after just 1 if problem is present static int LIMIT = 20; // Hangs after just 1 if problem is present
public static void main(String[] args) throws Exception { 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(); Selector selector = SelectorProvider.provider().openSelector();
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST); InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa = new InetSocketAddress(myAddress,13); InetSocketAddress isa
= new InetSocketAddress(myAddress,
daytimeServer.getPort());
for (int j=0; j<LIMIT; j++) { for (int j=0; j<LIMIT; j++) {
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false); sc.configureBlocking(false);
boolean result = sc.connect(isa); 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) { if (!result) {
SelectionKey key = sc.register(selector, SelectionKey key = sc.register(selector,
SelectionKey.OP_CONNECT); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,12 +27,10 @@ ...@@ -27,12 +27,10 @@
* @library .. * @library ..
*/ */
import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.*;
import java.util.*; import java.util.*;
...@@ -44,52 +42,57 @@ import java.util.*; ...@@ -44,52 +42,57 @@ import java.util.*;
public class BasicConnect { public class BasicConnect {
static final int PORT = 7; // echo
static final String HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Selector connectSelector = Selector connectSelector =
SelectorProvider.provider().openSelector(); SelectorProvider.provider().openSelector();
InetSocketAddress isa try (TestServers.EchoServer echoServer
= new InetSocketAddress(InetAddress.getByName(HOST), PORT); = TestServers.EchoServer.startNewServer(100)) {
SocketChannel sc = SocketChannel.open(); InetSocketAddress isa
sc.configureBlocking(false); = new InetSocketAddress(echoServer.getAddress(),
boolean result = sc.connect(isa); echoServer.getPort());
while (!result) { SocketChannel sc = SocketChannel.open();
SelectionKey connectKey = sc.register(connectSelector, sc.configureBlocking(false);
SelectionKey.OP_CONNECT); boolean result = sc.connect(isa);
int keysAdded = connectSelector.select(); if (result) {
if (keysAdded > 0) { System.out.println("Socket immediately connected on "
Set readyKeys = connectSelector.selectedKeys(); + System.getProperty("os.name")
Iterator i = readyKeys.iterator(); + ": " + sc);
while (i.hasNext()) { }
SelectionKey sk = (SelectionKey)i.next(); while (!result) {
i.remove(); SelectionKey connectKey = sc.register(connectSelector,
SocketChannel nextReady = (SocketChannel)sk.channel(); SelectionKey.OP_CONNECT);
result = nextReady.finishConnect(); int keysAdded = connectSelector.select();
if (result) if (keysAdded > 0) {
sk.cancel(); Set readyKeys = connectSelector.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext()) {
SelectionKey sk = (SelectionKey)i.next();
i.remove();
SocketChannel nextReady = (SocketChannel)sk.channel();
result = nextReady.finishConnect();
if (result)
sk.cancel();
}
} }
} }
}
byte[] bs = new byte[] { (byte)0xca, (byte)0xfe, byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,
(byte)0xba, (byte)0xbe }; (byte)0xba, (byte)0xbe };
ByteBuffer bb = ByteBuffer.wrap(bs); ByteBuffer bb = ByteBuffer.wrap(bs);
sc.configureBlocking(true); sc.configureBlocking(true);
sc.write(bb); sc.write(bb);
bb.rewind(); bb.rewind();
ByteBuffer bb2 = ByteBuffer.allocateDirect(100); ByteBuffer bb2 = ByteBuffer.allocateDirect(100);
int n = sc.read(bb2); int n = sc.read(bb2);
bb2.flip(); bb2.flip();
sc.close(); sc.close();
connectSelector.close(); connectSelector.close();
if (!bb.equals(bb2)) if (!bb.equals(bb2))
throw new Exception("Echoed bytes incorrect: Sent " throw new Exception("Echoed bytes incorrect: Sent "
+ bb + ", got " + bb2); + 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,12 +27,11 @@ ...@@ -27,12 +27,11 @@
* @library .. * @library ..
*/ */
import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class Connect { public class Connect {
...@@ -40,12 +39,18 @@ public class Connect { ...@@ -40,12 +39,18 @@ public class Connect {
static int LIMIT = 100; static int LIMIT = 100;
public static void main(String[] args) throws Exception { 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 { static void scaleTest(TestServers.DayTimeServer daytimeServer)
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST); throws Exception
InetSocketAddress isa = new InetSocketAddress(myAddress,13); {
InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa
= new InetSocketAddress(myAddress, daytimeServer.getPort());
for (int j=0; j<LIMIT; j++) { for (int j=0; j<LIMIT; j++) {
SocketChannel sc = SocketChannel.open(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,23 +27,25 @@ ...@@ -27,23 +27,25 @@
* @library .. * @library ..
*/ */
import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class ConnectWrite { public class ConnectWrite {
public static void main(String[] args) throws Exception { 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(); Selector selector = SelectorProvider.provider().openSelector();
InetAddress myAddress=InetAddress.getByName(TestUtil.HOST); InetAddress myAddress = daytimeServer.getAddress();
InetSocketAddress isa = new InetSocketAddress(myAddress, port); InetSocketAddress isa
= new InetSocketAddress(myAddress, daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
try { try {
sc.configureBlocking(false); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,21 +28,15 @@ ...@@ -28,21 +28,15 @@
*/ */
import java.net.*; import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.charset.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
public class KeysReady { public class KeysReady {
static final int DAYTIME_PORT = 13; static void test(TestServers.DayTimeServer dayTimeServer) throws Exception {
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(dayTimeServer.getAddress(),
DAYTIME_PORT); dayTimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false); sc.configureBlocking(false);
sc.connect(isa); sc.connect(isa);
...@@ -64,7 +58,10 @@ public class KeysReady { ...@@ -64,7 +58,10 @@ public class KeysReady {
} }
public static void main(String[] args) throws Exception { 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 { ...@@ -35,19 +35,16 @@ public class AdaptSocket {
static java.io.PrintStream out = System.out; static java.io.PrintStream out = System.out;
static final int ECHO_PORT = 7; static void test(TestServers.DayTimeServer dayTimeServer,
static final int DAYTIME_PORT = 13; int timeout,
static final String REMOTE_HOST = TestUtil.HOST; boolean shouldTimeout)
static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
static void test(String hn, int timeout, boolean shouldTimeout)
throws Exception throws Exception
{ {
out.println(); out.println();
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(hn), = new InetSocketAddress(dayTimeServer.getAddress(),
DAYTIME_PORT); dayTimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
Socket so = sc.socket(); Socket so = sc.socket();
out.println("opened: " + so); out.println("opened: " + so);
...@@ -116,13 +113,16 @@ public class AdaptSocket { ...@@ -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 throws Exception
{ {
out.println(); out.println();
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT); = new InetSocketAddress(echoServer.getAddress(),
echoServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.connect(isa); sc.connect(isa);
Socket so = sc.socket(); Socket so = sc.socket();
...@@ -134,22 +134,38 @@ public class AdaptSocket { ...@@ -134,22 +134,38 @@ public class AdaptSocket {
out.println("timeout: " + so.getSoTimeout()); out.println("timeout: " + so.getSoTimeout());
testRead(so, shouldTimeout); testRead(so, shouldTimeout);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++) {
testRead(so, shouldTimeout); testRead(so, shouldTimeout);
}
sc.close(); sc.close();
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test(REMOTE_HOST, 0, false); try (TestServers.DayTimeServer dayTimeServer
test(REMOTE_HOST, 1000, false); = TestServers.DayTimeServer.startNewServer()) {
test(VERY_REMOTE_HOST, 10, true); test(dayTimeServer, 0, false);
test(dayTimeServer, 1000, false);
}
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);
}
testRead(REMOTE_HOST, 0, false); try (TestServers.EchoServer echoServer
testRead(REMOTE_HOST, 8000, false); = TestServers.EchoServer.startNewServer()) {
testRead(VERY_REMOTE_HOST, 10, true); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,13 +36,10 @@ public class Basic { ...@@ -36,13 +36,10 @@ public class Basic {
static java.io.PrintStream out = System.out; static java.io.PrintStream out = System.out;
static final int DAYTIME_PORT = 13; static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(isa); SocketChannel sc = SocketChannel.open(isa);
out.println("opened: " + sc); out.println("opened: " + sc);
/* /*
...@@ -76,7 +73,10 @@ public class Basic { ...@@ -76,7 +73,10 @@ public class Basic {
} }
public static void main(String[] args) throws Exception { 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,17 +28,10 @@ ...@@ -28,17 +28,10 @@
*/ */
import java.nio.channels.*; import java.nio.channels.*;
import java.net.*;
public class BufferSize { public class BufferSize {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
DAYTIME_PORT);
ServerSocketChannel sc = ServerSocketChannel.open(); ServerSocketChannel sc = ServerSocketChannel.open();
try { try {
sc.socket().setReceiveBufferSize(-1); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,9 +27,9 @@ ...@@ -27,9 +27,9 @@
* @library .. * @library ..
*/ */
import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.net.*;
import java.util.*; import java.util.*;
public class Connect { public class Connect {
...@@ -37,21 +37,26 @@ public class Connect { ...@@ -37,21 +37,26 @@ public class Connect {
private static final long INCREMENTAL_DELAY = 30L * 1000L; private static final long INCREMENTAL_DELAY = 30L * 1000L;
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
test1(TestUtil.HOST); try (TestServers.EchoServer echoServer
= TestServers.EchoServer.startNewServer(1000)) {
test1(echoServer);
}
try { try {
test1(TestUtil.REFUSING_HOST); TestServers.RefusingServer refusingServer
= TestServers.RefusingServer.startNewServer();
test1(refusingServer);
throw new Exception("Refused connection throws no exception"); throw new Exception("Refused connection throws no exception");
} catch (ConnectException ce) { } catch (ConnectException ce) {
// Correct result // Correct result
} }
} }
static void test1(String hostname) throws Exception { static void test1(TestServers.AbstractServer server) throws Exception {
Selector selector; Selector selector;
SocketChannel sc; SocketChannel sc;
SelectionKey sk; SelectionKey sk;
InetSocketAddress isa = new InetSocketAddress( InetSocketAddress isa = new InetSocketAddress(
InetAddress.getByName (hostname), 80); server.getAddress(), server.getPort());
sc = SocketChannel.open(); sc = SocketChannel.open();
sc.configureBlocking(false); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -30,20 +30,39 @@ import java.io.*; ...@@ -30,20 +30,39 @@ import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
public class ConnectState { public class ConnectState {
static PrintStream log = System.err; static PrintStream log = System.err;
static String REMOTE_HOST = TestUtil.HOST;
static int REMOTE_PORT = 7; // echo
static InetSocketAddress remote; static InetSocketAddress remote;
final static int ST_UNCONNECTED = 0; final static int ST_UNCONNECTED = 0;
final static int ST_PENDING = 1; final static int ST_PENDING = 1;
final static int ST_CONNECTED = 2; final static int ST_CONNECTED = 2;
final static int ST_CLOSED = 3; 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 { static abstract class Test {
...@@ -76,37 +95,65 @@ public class ConnectState { ...@@ -76,37 +95,65 @@ public class ConnectState {
check(!sc.isConnectionPending(), "!isConnectionPending"); check(!sc.isConnectionPending(), "!isConnectionPending");
check(sc.isOpen(), "isOpen"); check(sc.isOpen(), "isOpen");
break; 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 { Test(String name, Class<?> exception, int state) throws Exception {
this(name, expectedExceptions(exception), state);
}
// 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(); SocketChannel sc = SocketChannel.open();
String note = null; String note;
try { try {
try { try {
note = go(sc); note = go(sc);
} catch (Exception x) { } catch (Exception x) {
if (exception != null) { Class<?> expectedExceptionClass = null;
for (Class<?> exception : exceptions) {
if (exception.isInstance(x)) { if (exception.isInstance(x)) {
log.println(name + ": As expected: " log.println(name + ": As expected: "
+ x); + x);
expectedExceptionClass = exception;
check(sc, state); check(sc, state);
return; break;
} else { }
throw new Exception(name }
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", + ": Incorrect exception",
x); x);
} } else if (exceptions.isEmpty()) {
} else { // we didn't expect any exception
throw new Exception(name throw new Exception(name
+ ": Unexpected exception", + ": Unexpected exception",
x); x);
} }
// if we reach here, we have our expected exception
assert expectedExceptionClass != null;
return;
} }
if (exception != null) if (!exceptions.isEmpty()) {
throw new Exception(name throw new Exception(name
+ ": Expected exception not thrown: " + ": Expected exception not thrown: "
+ exception); + exceptions.iterator().next());
}
check(sc, state); check(sc, state);
log.println(name + ": Returned normally" log.println(name + ": Returned normally"
+ ((note != null) ? ": " + note : "")); + ((note != null) ? ": " + note : ""));
...@@ -123,6 +170,7 @@ public class ConnectState { ...@@ -123,6 +170,7 @@ public class ConnectState {
new Test("Read unconnected", NotYetConnectedException.class, new Test("Read unconnected", NotYetConnectedException.class,
ST_UNCONNECTED) { ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
ByteBuffer b = ByteBuffer.allocateDirect(1024); ByteBuffer b = ByteBuffer.allocateDirect(1024);
sc.read(b); sc.read(b);
...@@ -131,19 +179,22 @@ public class ConnectState { ...@@ -131,19 +179,22 @@ public class ConnectState {
new Test("Write unconnected", NotYetConnectedException.class, new Test("Write unconnected", NotYetConnectedException.class,
ST_UNCONNECTED) { ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
ByteBuffer b = ByteBuffer.allocateDirect(1024); ByteBuffer b = ByteBuffer.allocateDirect(1024);
sc.write(b); sc.write(b);
return null; return null;
}}; }};
new Test("Simple connect", null, ST_CONNECTED) { new Test("Simple connect", NONE, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
sc.connect(remote); sc.connect(remote);
return null; 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 { String go(SocketChannel sc) throws Exception {
sc.connect(remote); sc.connect(remote);
if (!sc.finishConnect()) if (!sc.finishConnect())
...@@ -153,6 +204,7 @@ public class ConnectState { ...@@ -153,6 +204,7 @@ public class ConnectState {
new Test("Double connect", new Test("Double connect",
AlreadyConnectedException.class, ST_CONNECTED) { AlreadyConnectedException.class, ST_CONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
sc.connect(remote); sc.connect(remote);
sc.connect(remote); sc.connect(remote);
...@@ -161,12 +213,16 @@ public class ConnectState { ...@@ -161,12 +213,16 @@ public class ConnectState {
new Test("Finish w/o start", new Test("Finish w/o start",
NoConnectionPendingException.class, ST_UNCONNECTED) { NoConnectionPendingException.class, ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
sc.finishConnect(); sc.finishConnect();
return null; 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 { String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false); sc.configureBlocking(false);
sc.connect(remote); sc.connect(remote);
...@@ -179,8 +235,15 @@ public class ConnectState { ...@@ -179,8 +235,15 @@ public class ConnectState {
return ("Tries to finish = " + n); 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", 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 { String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false); sc.configureBlocking(false);
sc.connect(remote); sc.connect(remote);
...@@ -190,13 +253,15 @@ public class ConnectState { ...@@ -190,13 +253,15 @@ public class ConnectState {
new Test("NB finish w/o start", new Test("NB finish w/o start",
NoConnectionPendingException.class, ST_UNCONNECTED) { NoConnectionPendingException.class, ST_UNCONNECTED) {
@Override
String go(SocketChannel sc) throws Exception { String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false); sc.configureBlocking(false);
sc.finishConnect(); sc.finishConnect();
return null; 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 { String go(SocketChannel sc) throws Exception {
sc.configureBlocking(false); sc.configureBlocking(false);
sc.connect(remote); sc.connect(remote);
...@@ -208,9 +273,12 @@ public class ConnectState { ...@@ -208,9 +273,12 @@ public class ConnectState {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
remote = new InetSocketAddress(InetAddress.getByName(REMOTE_HOST), try (TestServers.EchoServer echoServer
REMOTE_PORT); = TestServers.EchoServer.startNewServer(500)) {
tests(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,21 +36,25 @@ import java.util.*; ...@@ -36,21 +36,25 @@ import java.util.*;
public class FinishConnect { public class FinishConnect {
static final int DAYTIME_PORT = 13;
static final String DAYTIME_HOST = TestUtil.HOST;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
test1(true, true); try (TestServers.DayTimeServer dayTimeServer
test1(true, false); = TestServers.DayTimeServer.startNewServer(100)) {
test1(false, true); test1(dayTimeServer, true, true);
test1(false, false); test1(dayTimeServer, true, false);
test2(); 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 InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false); sc.configureBlocking(false);
boolean connected = sc.connect(isa); boolean connected = sc.connect(isa);
...@@ -109,15 +113,27 @@ public class FinishConnect { ...@@ -109,15 +113,27 @@ public class FinishConnect {
sc.close(); sc.close();
} }
static void test2() throws Exception { static void test2(TestServers.DayTimeServer daytimeServer) throws Exception {
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
boolean done = false; boolean done = false;
int globalAttempts = 0; int globalAttempts = 0;
int connectSuccess = 0;
while (!done) { 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"); throw new RuntimeException("Failed to connect");
}
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false); sc.configureBlocking(false);
boolean connected = sc.connect(isa); boolean connected = sc.connect(isa);
...@@ -132,6 +148,9 @@ public class FinishConnect { ...@@ -132,6 +148,9 @@ public class FinishConnect {
} }
Thread.sleep(10); Thread.sleep(10);
} }
if (connected) {
connectSuccess++;
}
sc.close(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,24 +28,19 @@ ...@@ -28,24 +28,19 @@
*/ */
import java.net.*; import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;
import java.util.*; import java.util.*;
public class IsConnectable { public class IsConnectable {
static final int DAYTIME_PORT = 13; static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false); sc.configureBlocking(false);
sc.connect(isa); final boolean immediatelyConnected = sc.connect(isa);
Selector selector = SelectorProvider.provider().openSelector(); Selector selector = SelectorProvider.provider().openSelector();
try { try {
...@@ -67,7 +62,12 @@ public class IsConnectable { ...@@ -67,7 +62,12 @@ public class IsConnectable {
throw new Exception("Test failed: 4737146 detected"); throw new Exception("Test failed: 4737146 detected");
} }
} else { } else {
throw new Exception("Select failed"); if (!immediatelyConnected) {
throw new Exception("Select failed");
} else {
System.out.println("IsConnectable couldn't be fully tested for "
+ System.getProperty("os.name"));
}
} }
} finally { } finally {
sc.close(); sc.close();
...@@ -76,7 +76,10 @@ public class IsConnectable { ...@@ -76,7 +76,10 @@ public class IsConnectable {
} }
public static void main(String[] args) throws Exception { 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -28,18 +28,20 @@ ...@@ -28,18 +28,20 @@
*/ */
import java.net.*; import java.net.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
public class LocalAddress { public class LocalAddress {
public static void main(String[] args) throws Exception { 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"); InetAddress bogus = InetAddress.getByName("0.0.0.0");
InetSocketAddress saddr = new InetSocketAddress( InetSocketAddress saddr = new InetSocketAddress(
InetAddress.getByName(TestUtil.HOST), 23); server.getAddress(), server.getPort());
//Test1: connect only //Test1: connect only
SocketChannel sc = SocketChannel.open(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,22 +27,17 @@ ...@@ -27,22 +27,17 @@
* @library .. * @library ..
*/ */
import java.net.*;
import java.io.*; import java.io.*;
import java.nio.*; import java.net.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.charset.*;
public class Stream { public class Stream {
static final int DAYTIME_PORT = 13; static void test(TestServers.DayTimeServer daytimeServer) throws Exception {
static final String DAYTIME_HOST = TestUtil.HOST;
static void test() throws Exception {
InetSocketAddress isa InetSocketAddress isa
= new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
SocketChannel sc = SocketChannel.open(); SocketChannel sc = SocketChannel.open();
sc.connect(isa); sc.connect(isa);
sc.configureBlocking(false); sc.configureBlocking(false);
...@@ -58,7 +53,9 @@ public class Stream { ...@@ -58,7 +53,9 @@ public class Stream {
} }
public static void main(String[] args) throws Exception { 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,31 +27,31 @@ ...@@ -27,31 +27,31 @@
* @library .. * @library ..
*/ */
import java.net.*;
import java.io.*; import java.io.*;
import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.charset.*;
public class VectorParams { public class VectorParams {
static java.io.PrintStream out = System.out; 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 final int testSize = 10;
static ByteBuffer[] bufs = null; static ByteBuffer[] bufs = null;
static InetSocketAddress isa = null; static InetSocketAddress isa = null;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
initBufs(); try (TestServers.DayTimeServer daytimeServer
testSocketChannelVectorParams(); = TestServers.DayTimeServer.startNewServer(100)) {
testDatagramChannelVectorParams(); initBufs(daytimeServer);
testPipeVectorParams(); testSocketChannelVectorParams();
testFileVectorParams(); testDatagramChannelVectorParams();
testPipeVectorParams();
testFileVectorParams();
}
} }
static void initBufs() throws Exception { static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {
bufs = new ByteBuffer[testSize]; bufs = new ByteBuffer[testSize];
for(int i=0; i<testSize; i++) { for(int i=0; i<testSize; i++) {
String source = "buffer" + i; String source = "buffer" + i;
...@@ -59,8 +59,8 @@ public class VectorParams { ...@@ -59,8 +59,8 @@ public class VectorParams {
bufs[i].put(source.getBytes("8859_1")); bufs[i].put(source.getBytes("8859_1"));
bufs[i].flip(); bufs[i].flip();
} }
isa = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), isa = new InetSocketAddress(daytimeServer.getAddress(),
DAYTIME_PORT); daytimeServer.getPort());
} }
static void testSocketChannelVectorParams() throws Exception { static void testSocketChannelVectorParams() throws Exception {
......
此差异已折叠。
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.Random; import java.util.Random;
...@@ -36,9 +35,6 @@ public class TestUtil { ...@@ -36,9 +35,6 @@ public class TestUtil {
// Test hosts used by the channels tests - change these when // Test hosts used by the channels tests - change these when
// executing in a different network. // 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"; public static final String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah";
private TestUtil() { } private TestUtil() { }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册