summaryrefslogtreecommitdiffstats
path: root/support
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-10-12 15:35:50 -0700
committerElliott Hughes <enh@google.com>2012-10-16 17:15:40 -0700
commitbabccbf9e429c4c78aca24c205825ceaaf7d3f37 (patch)
treea918eb0871d83ef4787269a30fbfe822ba8217ed /support
parent34e8086a14fafa26b1658ee1020c811a135cbaf1 (diff)
downloadlibcore-babccbf9e429c4c78aca24c205825ceaaf7d3f37.zip
libcore-babccbf9e429c4c78aca24c205825ceaaf7d3f37.tar.gz
libcore-babccbf9e429c4c78aca24c205825ceaaf7d3f37.tar.bz2
Fix ConcurrentCloseTest flakiness.
We can't rely on consuming all the listen(2) backlog. For the tests we've seen fail because they sometimes connect rather than time out, switch to an unroutable address. Bug: 6971145 Change-Id: I259d31b1a15123bcd78c36849d5ed863d392ac20
Diffstat (limited to 'support')
-rw-r--r--support/src/test/java/tests/net/StuckServer.java60
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();
}