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