More tests for timeouts and shutdownXXXX niosocketimpl-branch
authoralanb
Thu, 28 Feb 2019 17:21:17 +0000
branchniosocketimpl-branch
changeset 57226 4d57d18f0ca5
parent 57225 debeede2898e
child 57227 aebe3e4fd2d1
More tests for timeouts and shutdownXXXX
test/jdk/java/net/Socket/AsyncShutdown.java
test/jdk/java/net/Socket/Timeouts.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/Socket/AsyncShutdown.java	Thu Feb 28 17:21:17 2019 +0000
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @requires (os.family == "linux" | os.family == "mac")
+ * @run testng AsyncShutdown
+ * @run testng/othervm -Djdk.net.usePlainSocketImpl AsyncShutdown
+ * @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
+ */
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+@Test
+public class AsyncShutdown {
+
+    public void testShutdownInput1() throws IOException {
+        withConnection((s1, s2) -> {
+            scheduleShutdownInput(s1, 2000);
+            int n = s1.getInputStream().read();
+            assertTrue(n == -1);
+        });
+    }
+
+    public void testShutdownInput2() throws IOException {
+        withConnection((s1, s2) -> {
+            scheduleShutdownInput(s1, 2000);
+            s1.setSoTimeout(30*1000);
+            int n = s1.getInputStream().read();
+            assertTrue(n == -1);
+        });
+    }
+
+    public void testShutdownOutput1() throws IOException {
+        withConnection((s1, s2) -> {
+            scheduleShutdownOutput(s1, 2000);
+            byte[] data = new byte[128*1024];
+            try {
+                while (true) {
+                    s1.getOutputStream().write(data);
+                }
+            } catch (IOException expected) { }
+        });
+    }
+
+    public void testShutdownOutput2() throws IOException {
+        withConnection((s1, s2) -> {
+            s1.setSoTimeout(100);
+            try {
+                s1.getInputStream().read();
+                assertTrue(false);
+            } catch (SocketTimeoutException e) { }
+
+            scheduleShutdownOutput(s1, 2000);
+            byte[] data = new byte[128*1024];
+            try {
+                while (true) {
+                    s1.getOutputStream().write(data);
+                }
+            } catch (IOException expected) { }
+        });
+    }
+
+    static void scheduleShutdownInput(Socket s, long delay) {
+        schedule(() -> {
+            try {
+                s.shutdownInput();
+            } catch (IOException ioe) { }
+        }, delay);
+    }
+
+    static void scheduleShutdownOutput(Socket s, long delay) {
+        schedule(() -> {
+            try {
+                s.shutdownOutput();
+            } catch (IOException ioe) { }
+        }, delay);
+    }
+
+    static void schedule(Runnable task, long delay) {
+        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
+        try {
+            executor.schedule(task, delay, TimeUnit.MILLISECONDS);
+        } finally {
+            executor.shutdown();
+        }
+    }
+
+    interface ThrowingBiConsumer<T, U> {
+        void accept(T t, U u) throws IOException;
+    }
+
+    static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
+        throws IOException
+    {
+        Socket s1 = null;
+        Socket s2 = null;
+        try (ServerSocket ss = new ServerSocket(0)) {
+            s1 = new Socket();
+            s1.connect(ss.getLocalSocketAddress());
+            s2 = ss.accept();
+            consumer.accept(s1, s2);
+        } finally {
+            if (s1 != null) s1.close();
+            if (s2 != null) s2.close();
+        }
+    }
+
+}
--- a/test/jdk/java/net/Socket/Timeouts.java	Thu Feb 28 11:08:30 2019 +0000
+++ b/test/jdk/java/net/Socket/Timeouts.java	Thu Feb 28 17:21:17 2019 +0000
@@ -21,7 +21,7 @@
  * questions.
  */
 
-/**
+/*
  * @test
  * @library /test/lib
  * @build jdk.test.lib.Utils
@@ -75,6 +75,32 @@
     }
 
     /**
+     * Test connect with a timeout of Integer.MAX_VALUE
+     */
+    public void testTimedConnect3() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            try (Socket s = new Socket()) {
+                s.connect(ss.getLocalSocketAddress(), Integer.MAX_VALUE);
+            }
+        }
+    }
+
+    /**
+     * Test connect with a negative timeout. This case is not currently specified
+     * but the long standing behavior is to throw IllegalArgumentException.
+     */
+    public void testTimedConnect4() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            try (Socket s = new Socket()) {
+                try {
+                    s.connect(ss.getLocalSocketAddress(), -1);
+                    assertTrue(false);
+                } catch (IllegalArgumentException expected) { }
+            }
+        }
+    }
+
+    /**
      * Test timed read where the read succeeds immediately
      */
     public void testTimedRead1() throws IOException {
@@ -195,6 +221,18 @@
     }
 
     /**
+     * Test read with a timeout of Integer.MAX_VALUE
+     */
+    public void testTimedRead9() throws IOException {
+        withConnection((s1, s2) -> {
+            scheduleWrite(s1.getOutputStream(), 99, 2000);
+            s2.setSoTimeout(Integer.MAX_VALUE);
+            int b = s2.getInputStream().read();
+            assertTrue(b == 99);
+        });
+    }
+
+    /**
      * Test writing after a timed read. The timed read changes the underlying
      * socket to non-blocking.
      */
@@ -357,6 +395,10 @@
         }
     }
 
+    /**
+     * Test Socket setSoTimeout with a negative timeout. This case is not currently
+     * specified but the long standing behavior is to throw IllegalArgumentException.
+     */
     @Test(expectedExceptions = { IllegalArgumentException.class })
     public void testBadTimeout1() throws IOException {
         try (Socket s = new Socket()) {
@@ -364,17 +406,13 @@
         }
     }
 
+    /**
+     * Test ServerSocket setSoTimeout with a negative timeout. This case is not
+     * currently specified but the long standing behavior is to throw
+     * IllegalArgumentException.
+     */
     @Test(expectedExceptions = { IllegalArgumentException.class })
     public void testBadTimeout2() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
-            try (Socket s = new Socket()) {
-                s.connect(ss.getLocalSocketAddress(), -1);
-            }
-        }
-    }
-
-    @Test(expectedExceptions = { IllegalArgumentException.class })
-    public void testBadTimeout3() throws IOException {
         try (ServerSocket ss = new ServerSocket()) {
             ss.setSoTimeout(-1);
         }
@@ -407,67 +445,56 @@
      * Schedule c to be closed after a delay
      */
     static void scheduleClose(Closeable c, long delay) {
-        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
-        try {
-            Runnable task = () -> { try { c.close(); } catch (IOException ioe) { } };
-            executor.schedule(task, delay, TimeUnit.MILLISECONDS);
-        } finally {
-            executor.shutdown();
-        }
+        schedule(() -> {
+            try {
+                c.close();
+            } catch (IOException ioe) { }
+        }, delay);
     }
 
     /**
      * Schedule a thread to connect to the given end point after a delay
      */
     static void scheduleConnect(SocketAddress remote, long delay) {
-        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
-        try {
-            Runnable task = () -> {
-                try (Socket s = new Socket()) {
-                    s.connect(remote);
-                } catch (IOException ioe) { }
-            };
-            executor.schedule(task, delay, TimeUnit.MILLISECONDS);
-        } finally {
-            executor.shutdown();
-        }
+        schedule(() -> {
+            try (Socket s = new Socket()) {
+                s.connect(remote);
+            } catch (IOException ioe) { }
+        }, delay);
     }
 
     /**
      * Schedule a thread to read to EOF after a delay
      */
     static void scheduleReadToEOF(InputStream in, long delay) {
-        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
-        try {
-            Runnable task = () -> {
-                byte[] bytes = new byte[8192];
-                try {
-                    while (in.read(bytes) != -1) { }
-                } catch (IOException ioe) { }
-            };
-            executor.schedule(task, delay, TimeUnit.MILLISECONDS);
-        } finally {
-            executor.shutdown();
-        }
+        schedule(() -> {
+            byte[] bytes = new byte[8192];
+            try {
+                while (in.read(bytes) != -1) { }
+            } catch (IOException ioe) { }
+        }, delay);
     }
 
     /**
      * Schedule a thread to write after a delay
      */
     static void scheduleWrite(OutputStream out, byte[] data, long delay) {
+        schedule(() -> {
+            try {
+                out.write(data);
+            } catch (IOException ioe) { }
+        }, delay);
+    }
+    static void scheduleWrite(OutputStream out, int b, long delay) {
+        scheduleWrite(out, new byte[] { (byte)b }, delay);
+    }
+
+    static void schedule(Runnable task, long delay) {
         ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
         try {
-            Runnable task = () -> {
-                try {
-                    out.write(data);
-                } catch (IOException ioe) { }
-            };
             executor.schedule(task, delay, TimeUnit.MILLISECONDS);
         } finally {
             executor.shutdown();
         }
     }
-    static void scheduleWrite(OutputStream out, int b, long delay) {
-        scheduleWrite(out, new byte[] { (byte)b }, delay);
-    }
 }