diff options
Diffstat (limited to 'support/src/test')
-rw-r--r-- | support/src/test/java/tests/net/StuckServer.java | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/support/src/test/java/tests/net/StuckServer.java b/support/src/test/java/tests/net/StuckServer.java index eababce..f7a3118 100644 --- a/support/src/test/java/tests/net/StuckServer.java +++ b/support/src/test/java/tests/net/StuckServer.java @@ -17,6 +17,7 @@ package tests.net; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; @@ -26,48 +27,57 @@ import java.util.ArrayList; * A test ServerSocket that you can't connect to --- connects will time out. */ public final class StuckServer { + private static final boolean DEBUG = false; + private ServerSocket serverSocket; + private InetSocketAddress address; private ArrayList<Socket> clients = new ArrayList<Socket>(); - public StuckServer() throws IOException { + public StuckServer(boolean useBacklog) throws IOException { // Set a backlog and use it up so that we can expect the - // connection to time out. According to Steven's + // connection to time out. According to Stevens // 4.5 "listen function", Linux adds 3 to the specified // backlog, so we need to connect 4 times before it will hang. - serverSocket = new ServerSocket(0, 1); - for (int i = 0; i < 4; i++) { - clients.add(new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort())); - } - } - - public void unblockAfterMs(final int ms) { - Thread t = new Thread(new Runnable() { - @Override public void run() { - try { - Thread.sleep(ms); - for (Socket client : clients) { - client.close(); - } - clients.clear(); - clients.add(serverSocket.accept()); - } catch (Exception ex) { - ex.printStackTrace(); + // The trouble with this is that it won't hang forever. + // After 10s or so, the kernel allows a couple more connections. + // This mode is ony useful if you actually want to continue eventually; we use it to + // test non-blocking connects, for example, where you want to test every part of the code. + if (useBacklog) { + this.serverSocket = new ServerSocket(0, 1); + this.address = (InetSocketAddress) serverSocket.getLocalSocketAddress(); + if (DEBUG) { + System.err.println("StuckServer: " + serverSocket); + } + for (int i = 0; i < 4; ++i) { + Socket client = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort()); + clients.add(client); + if (DEBUG) { + System.err.println("StuckServer client " + i + " - " + client); } } - }); - t.start(); + } else { + // In general, though, you don't want to rely on listen(2) backlog. http://b/6971145. + // RFC 5737 implies this network will be unreachable. (There are two other networks + // to try if we have trouble with this one.) + // We've had trouble with 10.* in the past (because test labs running CTS often use + // net 10!) but hopefully this network will be better. + InetAddress testNet1 = InetAddress.getByAddress(new byte[] { (byte) 192, 0, 2, 0 }); + this.address = new InetSocketAddress(testNet1, 80); + } } public InetSocketAddress getLocalSocketAddress() { - return (InetSocketAddress) serverSocket.getLocalSocketAddress(); + return address; } public int getLocalPort() { - return serverSocket.getLocalPort(); + return address.getPort(); } public void close() throws IOException { - serverSocket.close(); + if (serverSocket != null) { + serverSocket.close(); + } for (Socket client : clients) { client.close(); } |