< prev index next >
test/jdk/java/net/Socket/CloseAvailable.java
Print this page
@@ -21,32 +21,46 @@
* questions.
*/
/*
* @test
- * @bug 4091859
+ * @bug 4091859 8189366
* @summary Test Socket.available()
* @run main CloseAvailable
* @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable
*/
import java.net.*;
import java.io.*;
-public class CloseAvailable implements Runnable {
- static ServerSocket ss;
- static InetAddress addr;
- static int port;
+public class CloseAvailable {
public static void main(String[] args) throws Exception {
+ testClose();
+
+ testEOF(true);
+ testEOF(false);
+ }
+
+ static void testClose() throws IOException {
boolean error = true;
- addr = InetAddress.getLocalHost();
- ss = new ServerSocket(0);
- port = ss.getLocalPort();
+ InetAddress addr = InetAddress.getLocalHost();
+ ServerSocket ss = new ServerSocket(0);
+ int port = ss.getLocalPort();
+
+ Thread t = new Thread(new Thread("Close-Available-1") {
+ public void run() {
+ try {
+ Socket s = new Socket(addr, port);
+ s.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
- Thread t = new Thread(new CloseAvailable());
t.start();
Socket soc = ss.accept();
ss.close();
@@ -61,15 +75,44 @@
}
if (error)
throw new RuntimeException("Available() can be called after stream closed.");
}
- public void run() {
- try {
- Socket s = new Socket(addr, port);
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
+ // Verifies consistency of `available` behaviour when EOF reached, both
+ // explicitly and implicitly.
+ static void testEOF(boolean readUntilEOF) throws IOException {
+ System.out.println("testEOF, readUntilEOF: " + readUntilEOF);
+ InetAddress addr = InetAddress.getLoopbackAddress();
+ ServerSocket ss = new ServerSocket();
+ ss.bind(new InetSocketAddress(addr, 0), 0);
+ int port = ss.getLocalPort();
+
+ try (Socket s = new Socket(addr, port)) {
+ s.getOutputStream().write(0x42);
+ s.shutdownOutput();
+
+ try (Socket soc = ss.accept()) {
+ ss.close();
+
+ InputStream is = soc.getInputStream();
+ int b = is.read();
+ assert b == 0x42;
+ assert !s.isClosed();
+ if (readUntilEOF) {
+ b = is.read();
+ assert b == -1;
}
+ int a;
+ for (int i = 0; i < 100; i++) {
+ a = is.available();
+ System.out.print(a + ", ");
+ if (a != 0)
+ throw new RuntimeException("Unexpected non-zero available: " + a);
+ }
+ assert !s.isClosed();
+ assert is.read() == -1;
+ }
+ }
+ System.out.println("\ncomplete");
+ }
}
< prev index next >