test/jdk/java/net/Socket/AsyncShutdown.java
changeset 54216 f10ca228b22f
child 55649 ad8e3b295615
child 57277 d2b2a4edbfe7
equal deleted inserted replaced
54215:b00a4187d5ec 54216:f10ca228b22f
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @requires (os.family == "linux" | os.family == "mac")
       
    27  * @run testng AsyncShutdown
       
    28  * @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
       
    29  */
       
    30 
       
    31 import java.io.IOException;
       
    32 import java.net.ServerSocket;
       
    33 import java.net.Socket;
       
    34 import java.net.SocketTimeoutException;
       
    35 import java.util.concurrent.Executors;
       
    36 import java.util.concurrent.ScheduledExecutorService;
       
    37 import java.util.concurrent.TimeUnit;
       
    38 
       
    39 import org.testng.annotations.Test;
       
    40 import static org.testng.Assert.*;
       
    41 
       
    42 @Test
       
    43 public class AsyncShutdown {
       
    44 
       
    45     public void testShutdownInput1() throws IOException {
       
    46         withConnection((s1, s2) -> {
       
    47             scheduleShutdownInput(s1, 2000);
       
    48             int n = s1.getInputStream().read();
       
    49             assertTrue(n == -1);
       
    50         });
       
    51     }
       
    52 
       
    53     public void testShutdownInput2() throws IOException {
       
    54         withConnection((s1, s2) -> {
       
    55             scheduleShutdownInput(s1, 2000);
       
    56             s1.setSoTimeout(30*1000);
       
    57             int n = s1.getInputStream().read();
       
    58             assertTrue(n == -1);
       
    59         });
       
    60     }
       
    61 
       
    62     public void testShutdownOutput1() throws IOException {
       
    63         withConnection((s1, s2) -> {
       
    64             scheduleShutdownOutput(s1, 2000);
       
    65             byte[] data = new byte[128*1024];
       
    66             try {
       
    67                 while (true) {
       
    68                     s1.getOutputStream().write(data);
       
    69                 }
       
    70             } catch (IOException expected) { }
       
    71         });
       
    72     }
       
    73 
       
    74     public void testShutdownOutput2() throws IOException {
       
    75         withConnection((s1, s2) -> {
       
    76             s1.setSoTimeout(100);
       
    77             try {
       
    78                 s1.getInputStream().read();
       
    79                 assertTrue(false);
       
    80             } catch (SocketTimeoutException e) { }
       
    81 
       
    82             scheduleShutdownOutput(s1, 2000);
       
    83             byte[] data = new byte[128*1024];
       
    84             try {
       
    85                 while (true) {
       
    86                     s1.getOutputStream().write(data);
       
    87                 }
       
    88             } catch (IOException expected) { }
       
    89         });
       
    90     }
       
    91 
       
    92     static void scheduleShutdownInput(Socket s, long delay) {
       
    93         schedule(() -> {
       
    94             try {
       
    95                 s.shutdownInput();
       
    96             } catch (IOException ioe) { }
       
    97         }, delay);
       
    98     }
       
    99 
       
   100     static void scheduleShutdownOutput(Socket s, long delay) {
       
   101         schedule(() -> {
       
   102             try {
       
   103                 s.shutdownOutput();
       
   104             } catch (IOException ioe) { }
       
   105         }, delay);
       
   106     }
       
   107 
       
   108     static void schedule(Runnable task, long delay) {
       
   109         ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
       
   110         try {
       
   111             executor.schedule(task, delay, TimeUnit.MILLISECONDS);
       
   112         } finally {
       
   113             executor.shutdown();
       
   114         }
       
   115     }
       
   116 
       
   117     interface ThrowingBiConsumer<T, U> {
       
   118         void accept(T t, U u) throws IOException;
       
   119     }
       
   120 
       
   121     static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
       
   122         throws IOException
       
   123     {
       
   124         Socket s1 = null;
       
   125         Socket s2 = null;
       
   126         try (ServerSocket ss = new ServerSocket(0)) {
       
   127             s1 = new Socket();
       
   128             s1.connect(ss.getLocalSocketAddress());
       
   129             s2 = ss.accept();
       
   130             consumer.accept(s1, s2);
       
   131         } finally {
       
   132             if (s1 != null) s1.close();
       
   133             if (s2 != null) s2.close();
       
   134         }
       
   135     }
       
   136 
       
   137 }