summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2015-04-22 10:51:07 +0100
committerNeil Fuller <nfuller@google.com>2015-04-22 18:01:05 +0100
commitd4348724908290a8c09918a35587f1f6836c08ef (patch)
tree4e47a84a04d5b005983a66b1f05bc73f1b963834 /luni/src/test/java
parent0ed6c7db9f92dc3f0082328637f8550e87099b3d (diff)
downloadlibcore-d4348724908290a8c09918a35587f1f6836c08ef.zip
libcore-d4348724908290a8c09918a35587f1f6836c08ef.tar.gz
libcore-d4348724908290a8c09918a35587f1f6836c08ef.tar.bz2
Test that confirms a socket can be closed during connect()
Added to confirm that a thread blocked during an infinite connect() will unblock if the socket is closed. Change-Id: If373d810cd5db7f68a28bab712d8a58ea1fc68be
Diffstat (limited to 'luni/src/test/java')
-rw-r--r--luni/src/test/java/libcore/java/net/SocketTest.java33
1 files changed, 33 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/java/net/SocketTest.java b/luni/src/test/java/libcore/java/net/SocketTest.java
index fb09be0..9765a45 100644
--- a/luni/src/test/java/libcore/java/net/SocketTest.java
+++ b/luni/src/test/java/libcore/java/net/SocketTest.java
@@ -31,9 +31,11 @@ import java.net.SocketImpl;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
public class SocketTest extends junit.framework.TestCase {
// See http://b/2980559.
@@ -353,6 +355,37 @@ public class SocketTest extends junit.framework.TestCase {
assertEquals(boundAddress.getPort(), localAddressAfterClose.getPort());
}
+ public void testCloseDuringConnect() throws Exception {
+ final CountDownLatch signal = new CountDownLatch(1);
+
+ final Socket s = new Socket();
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ // This address is reserved for documentation: should never be reachable.
+ InetSocketAddress unreachableIp = new InetSocketAddress("192.0.2.0", 80);
+ // This should never return.
+ s.connect(unreachableIp, 0 /* infinite */);
+ fail("Connect returned unexpectedly for: " + unreachableIp);
+ } catch (SocketException expected) {
+ assertTrue(expected.getMessage().contains("Socket closed"));
+ signal.countDown();
+ } catch (IOException e) {
+ fail("Unexpected exception: " + e);
+ }
+ }
+ }.start();
+
+ // Wait for the connect() thread to run and start connect()
+ Thread.sleep(2000);
+
+ s.close();
+
+ boolean connectUnblocked = signal.await(2000, TimeUnit.MILLISECONDS);
+ assertTrue(connectUnblocked);
+ }
+
static class MockServer {
private ExecutorService executor;
private ServerSocket serverSocket;