Search in sources :

Example 1 with StuckServer

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();
    }
}
Also used : SocketException(java.net.SocketException) StuckServer(tests.net.StuckServer) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket)

Example 2 with StuckServer

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();
    }
}
Also used : SocketException(java.net.SocketException) StuckServer(tests.net.StuckServer) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket)

Example 3 with StuckServer

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();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) StuckServer(tests.net.StuckServer)

Example 4 with StuckServer

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();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) StuckServer(tests.net.StuckServer) Selector(java.nio.channels.Selector)

Example 5 with StuckServer

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();
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) StuckServer(tests.net.StuckServer) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL)

Aggregations

StuckServer (tests.net.StuckServer)5 SocketException (java.net.SocketException)3 DatagramSocket (java.net.DatagramSocket)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 SocketChannel (java.nio.channels.SocketChannel)2 HttpURLConnection (java.net.HttpURLConnection)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1