diff options
author | Neil Fuller <nfuller@google.com> | 2015-04-22 17:17:24 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-04-22 17:17:24 +0000 |
commit | f897520855758dfd0053f7b32c52abfaa4a16e2d (patch) | |
tree | 8cc4d15c901eb323e469fa0342797653d4c4a777 /luni/src/test/java | |
parent | 97ca5855f9baebb03a6c65363c613d305d62c52e (diff) | |
parent | d4348724908290a8c09918a35587f1f6836c08ef (diff) | |
download | libcore-f897520855758dfd0053f7b32c52abfaa4a16e2d.zip libcore-f897520855758dfd0053f7b32c52abfaa4a16e2d.tar.gz libcore-f897520855758dfd0053f7b32c52abfaa4a16e2d.tar.bz2 |
Merge "Test that confirms a socket can be closed during connect()"
Diffstat (limited to 'luni/src/test/java')
-rw-r--r-- | luni/src/test/java/libcore/java/net/SocketTest.java | 33 |
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; |