use of tests.net.StuckServer in project robovm by robovm.
the class ConcurrentCloseTest method test_connect.
public void test_connect() throws Exception {
StuckServer ss = new StuckServer(false);
Socket s = new Socket();
new Killer(s).start();
try {
System.err.println("connect...");
s.connect(ss.getLocalSocketAddress());
fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} finally {
ss.close();
}
}
use of tests.net.StuckServer in project robovm by robovm.
the class ConcurrentCloseTest method test_connect_timeout.
public void test_connect_timeout() throws Exception {
StuckServer ss = new StuckServer(false);
Socket s = new Socket();
new Killer(s).start();
try {
System.err.println("connect (with timeout)...");
s.connect(ss.getLocalSocketAddress(), 3600 * 1000);
fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} finally {
ss.close();
}
}
use of tests.net.StuckServer in project robovm by robovm.
the class ConcurrentCloseTest method test_connect_nonBlocking.
public void test_connect_nonBlocking() throws Exception {
StuckServer ss = new StuckServer(false);
SocketChannel s = SocketChannel.open();
new Killer(s.socket()).start();
try {
System.err.println("connect (non-blocking)...");
s.configureBlocking(false);
s.connect(ss.getLocalSocketAddress());
while (!s.finishConnect()) {
// Spin like a mad thing!
}
fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} catch (AsynchronousCloseException alsoOkay) {
// See below.
} catch (ClosedChannelException alsoOkay) {
// For now, I'm assuming that we're happy as long as we get any reasonable exception.
// It may be that we're supposed to guarantee only one or the other.
} finally {
ss.close();
}
}
use of tests.net.StuckServer in project robovm by robovm.
the class SelectorTest method testNonBlockingConnect_slow.
public void testNonBlockingConnect_slow() throws Exception {
// Test the case where we have to wait for the connection.
Selector selector = Selector.open();
StuckServer ss = new StuckServer(true);
try {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
sc.connect(ss.getLocalSocketAddress());
SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT);
assertEquals(1, selector.select());
assertEquals(SelectionKey.OP_CONNECT, key.readyOps());
sc.finishConnect();
} finally {
selector.close();
ss.close();
}
}
use of tests.net.StuckServer in project robovm by robovm.
the class URLConnectionTest method testConnectTimeouts.
/**
* Test that the timeout period is honored. The timeout may be doubled!
* HttpURLConnection will wait the full timeout for each of the server's IP
* addresses. This is typically one IPv4 address and one IPv6 address.
*/
public void testConnectTimeouts() throws IOException {
StuckServer ss = new StuckServer(true);
int serverPort = ss.getLocalPort();
String hostName = ss.getLocalSocketAddress().getAddress().getHostAddress();
URLConnection urlConnection = new URL("http://" + hostName + ":" + serverPort + "/").openConnection();
int timeout = 1000;
urlConnection.setConnectTimeout(timeout);
long start = System.currentTimeMillis();
try {
urlConnection.getInputStream();
fail();
} catch (SocketTimeoutException expected) {
long elapsed = System.currentTimeMillis() - start;
// one per IP address
int attempts = InetAddress.getAllByName("localhost").length;
assertTrue("timeout=" + timeout + ", elapsed=" + elapsed + ", attempts=" + attempts, Math.abs((attempts * timeout) - elapsed) < 500);
} finally {
ss.close();
}
}
Aggregations