summaryrefslogtreecommitdiffstats
path: root/support
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-10-16 17:53:01 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2012-10-16 17:53:01 -0700
commit1b18a42ce11e536a12a21166b5e7790a73285af4 (patch)
treee1cf2c9af64611c3f4656c8384acea7c1e675a87 /support
parent7434c155a49ebe96d16ae29dbc2dde0b414d4f88 (diff)
parentd2f41fe210bf0fe958345f84e95e8f037c325005 (diff)
downloadlibcore-1b18a42ce11e536a12a21166b5e7790a73285af4.zip
libcore-1b18a42ce11e536a12a21166b5e7790a73285af4.tar.gz
libcore-1b18a42ce11e536a12a21166b5e7790a73285af4.tar.bz2
am d2f41fe2: Merge "Fix ConcurrentCloseTest flakiness."
* commit 'd2f41fe210bf0fe958345f84e95e8f037c325005': Fix ConcurrentCloseTest flakiness.
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();
}