summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java')
-rw-r--r--luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java97
1 files changed, 50 insertions, 47 deletions
diff --git a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
index d33c5f3..99a479c2 100644
--- a/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
+++ b/luni/src/test/java/libcore/java/net/ConcurrentCloseTest.java
@@ -16,11 +16,13 @@
package libcore.java.net;
+import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedChannelException;
@@ -36,25 +38,25 @@ import tests.net.StuckServer;
*/
public class ConcurrentCloseTest extends junit.framework.TestCase {
public void test_accept() throws Exception {
- ServerSocket s = new ServerSocket(0);
- new Killer(s).start();
+ ServerSocket ss = new ServerSocket(0);
+ new Killer(ss).start();
try {
System.err.println("accept...");
- s.accept();
- fail("accept returned!");
+ Socket s = ss.accept();
+ fail("accept returned " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
}
}
public void test_connect() throws Exception {
- StuckServer ss = new StuckServer();
+ StuckServer ss = new StuckServer(false);
Socket s = new Socket();
new Killer(s).start();
try {
System.err.println("connect...");
s.connect(ss.getLocalSocketAddress());
- fail("connect returned!");
+ fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} finally {
@@ -63,13 +65,13 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
}
public void test_connect_timeout() throws Exception {
- StuckServer ss = new StuckServer();
+ StuckServer ss = new StuckServer(false);
Socket s = new Socket();
new Killer(s).start();
try {
System.err.println("connect (with timeout)...");
s.connect(ss.getLocalSocketAddress(), 3600 * 1000);
- fail("connect returned!");
+ fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} finally {
@@ -78,7 +80,7 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
}
public void test_connect_nonBlocking() throws Exception {
- StuckServer ss = new StuckServer();
+ StuckServer ss = new StuckServer(false);
SocketChannel s = SocketChannel.open();
new Killer(s.socket()).start();
try {
@@ -88,7 +90,7 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
while (!s.finishConnect()) {
// Spin like a mad thing!
}
- fail("connect returned!");
+ fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} catch (AsynchronousCloseException alsoOkay) {
@@ -102,23 +104,14 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
}
public void test_read() throws Exception {
- final ServerSocket ss = new ServerSocket(0);
- new Thread(new Runnable() {
- public void run() {
- try {
- ss.accept();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }).start();
+ SilentServer ss = new SilentServer();
Socket s = new Socket();
s.connect(ss.getLocalSocketAddress());
new Killer(s).start();
try {
System.err.println("read...");
int i = s.getInputStream().read();
- fail("read returned " + i);
+ fail("read returned: " + i);
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
}
@@ -126,16 +119,7 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
}
public void test_read_multiple() throws Throwable {
- final ServerSocket ss = new ServerSocket(0);
- new Thread(new Runnable() {
- public void run() {
- try {
- ss.accept();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }).start();
+ SilentServer ss = new SilentServer();
final Socket s = new Socket();
s.connect(ss.getLocalSocketAddress());
@@ -152,7 +136,7 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
try {
System.err.println("read...");
int i = s.getInputStream().read();
- fail("read returned " + i);
+ fail("read returned: " + i);
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
}
@@ -173,6 +157,8 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
for (Throwable exception : thrownExceptions) {
throw exception;
}
+
+ ss.close();
}
public void test_recv() throws Exception {
@@ -190,21 +176,7 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
}
public void test_write() throws Exception {
- final ServerSocket ss = new ServerSocket(0);
- new Thread(new Runnable() {
- public void run() {
- try {
- System.err.println("accepting...");
-
- Socket client = ss.accept();
- System.err.println("accepted...");
- Thread.sleep(30 * 1000);
- System.err.println("server exiting...");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }).start();
+ final SilentServer ss = new SilentServer();
Socket s = new Socket();
s.connect(ss.getLocalSocketAddress());
new Killer(s).start();
@@ -224,6 +196,37 @@ public class ConcurrentCloseTest extends junit.framework.TestCase {
ss.close();
}
+ // This server accepts connections, but doesn't read or write anything.
+ // It holds on to the Socket connecting to the client so it won't be GCed.
+ // Call "close" to close both the server socket and its client connection.
+ static class SilentServer {
+ private final ServerSocket ss;
+ private Socket client;
+
+ public SilentServer() throws IOException {
+ ss = new ServerSocket(0);
+ new Thread(new Runnable() {
+ public void run() {
+ try {
+ client = ss.accept();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ }).start();
+ }
+
+ public SocketAddress getLocalSocketAddress() {
+ return ss.getLocalSocketAddress();
+ }
+
+ public void close() throws IOException {
+ client.close();
+ ss.close();
+ }
+ }
+
+ // This thread calls the "close" method on the supplied T after 2s.
static class Killer<T> extends Thread {
private final T s;