diff options
author | Elliott Hughes <enh@google.com> | 2010-11-16 10:28:32 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-11-16 11:13:22 -0800 |
commit | 5fc5dde4c719c1dfdac46b67d5d2e4884d07721e (patch) | |
tree | c9bc55d76f3d8206b0d4641a50779c85c4dd4e17 /support | |
parent | 056611da6dba28eb1dbbaebe897187fc5834d155 (diff) | |
download | libcore-5fc5dde4c719c1dfdac46b67d5d2e4884d07721e.zip libcore-5fc5dde4c719c1dfdac46b67d5d2e4884d07721e.tar.gz libcore-5fc5dde4c719c1dfdac46b67d5d2e4884d07721e.tar.bz2 |
Improve ConcurrentCloseTest.
No more flaky use of 10.* addresses.
Bug: 3044772
Change-Id: I5ca8dc431b50950efdc818efe73eb9aba76ea67f
Diffstat (limited to 'support')
-rw-r--r-- | support/src/test/java/tests/net/StuckServer.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/support/src/test/java/tests/net/StuckServer.java b/support/src/test/java/tests/net/StuckServer.java new file mode 100644 index 0000000..4230f17 --- /dev/null +++ b/support/src/test/java/tests/net/StuckServer.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.net; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; + +/** + * A test ServerSocket that you can't connect to --- connects will time out. + */ +public final class StuckServer { + private ServerSocket serverSocket; + private ArrayList<Socket> clients = new ArrayList<Socket>(); + + public StuckServer() throws IOException { + // Set a backlog and use it up so that we can expect the + // connection to time out. According to Steven's + // 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 InetSocketAddress getLocalSocketAddress() { + return (InetSocketAddress) serverSocket.getLocalSocketAddress(); + } + + public int getLocalPort() { + return serverSocket.getLocalPort(); + } + + public void close() throws IOException { + serverSocket.close(); + for (Socket client : clients) { + client.close(); + } + } +} |