diff options
3 files changed, 71 insertions, 17 deletions
diff --git a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java index 5968fdf..726b05a 100644 --- a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java +++ b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java @@ -28,6 +28,7 @@ import java.nio.channels.SocketChannel; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import tests.net.StuckServer; /** * Test that Socket.close called on another thread interrupts a thread that's blocked doing @@ -47,24 +48,28 @@ public class ConcurrentCloseTest extends junit.framework.TestCase { } public void test_connect() throws Exception { + StuckServer ss = new StuckServer(); Socket s = new Socket(); new Killer(s).start(); try { System.err.println("connect..."); - s.connect(new InetSocketAddress("10.0.0.1", 7)); + s.connect(ss.getLocalSocketAddress()); fail("connect returned!"); } catch (SocketException expected) { assertEquals("Socket closed", expected.getMessage()); + } finally { + ss.close(); } } public void test_connect_nonBlocking() throws Exception { + StuckServer ss = new StuckServer(); SocketChannel s = SocketChannel.open(); new Killer(s.socket()).start(); try { System.err.println("connect (non-blocking)..."); s.configureBlocking(false); - s.connect(new InetSocketAddress("10.0.0.2", 7)); + s.connect(ss.getLocalSocketAddress()); while (!s.finishConnect()) { // Spin like a mad thing! } @@ -76,6 +81,8 @@ public class ConcurrentCloseTest extends junit.framework.TestCase { } 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(); } } diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java index a1f99e9..1a11bd9 100644 --- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java +++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java @@ -68,6 +68,7 @@ import tests.http.DefaultResponseCache; import tests.http.MockResponse; import tests.http.MockWebServer; import tests.http.RecordedRequest; +import tests.net.StuckServer; public class URLConnectionTest extends junit.framework.TestCase { @@ -1393,27 +1394,16 @@ public class URLConnectionTest extends junit.framework.TestCase { } public void testConnectTimeouts() throws IOException { - // Set a backlog and use it up so that we can expect the - // URLConnection to properly timeout. 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 serverSocket = new ServerSocket(0, 1); - int serverPort = serverSocket.getLocalPort(); - Socket[] sockets = new Socket[4]; - for (int i = 0; i < sockets.length; i++) { - sockets[i] = new Socket("localhost", serverPort); - } - + StuckServer ss = new StuckServer(); + int serverPort = ss.getLocalPort(); URLConnection urlConnection = new URL("http://localhost:" + serverPort).openConnection(); urlConnection.setConnectTimeout(1000); try { urlConnection.getInputStream(); fail(); } catch (SocketTimeoutException expected) { - } - - for (Socket s : sockets) { - s.close(); + } finally { + ss.close(); } } 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(); + } + } +} |