test/jdk/java/net/Socket/Timeouts.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 57355 ceb5c3fd71d2
parent 55102 59567035d279
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     1
/*
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     4
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     8
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    13
 * accompanied this code).
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    14
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    18
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    21
 * questions.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    22
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    23
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    24
/*
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    25
 * @test
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    26
 * @bug 8221481
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    27
 * @library /test/lib
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    28
 * @build jdk.test.lib.Utils
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    29
 * @run testng/timeout=180 Timeouts
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    30
 * @summary Test Socket timeouts
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    31
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    32
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    33
import java.io.Closeable;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    34
import java.io.IOException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    35
import java.io.InputStream;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    36
import java.io.OutputStream;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    37
import java.net.ConnectException;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    38
import java.net.InetAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    39
import java.net.InetSocketAddress;
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    40
import java.net.ServerSocket;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    41
import java.net.Socket;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    42
import java.net.SocketAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    43
import java.net.SocketException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    44
import java.net.SocketTimeoutException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    45
import java.util.concurrent.Executors;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    46
import java.util.concurrent.ExecutionException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    47
import java.util.concurrent.ExecutorService;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    48
import java.util.concurrent.Future;
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    49
import java.util.concurrent.ScheduledExecutorService;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    50
import java.util.concurrent.TimeUnit;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    51
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    52
import org.testng.annotations.Test;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    53
import static org.testng.Assert.*;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    54
import jdk.test.lib.Utils;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    55
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    56
@Test
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    57
public class Timeouts {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    58
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    59
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    60
     * Test timed connect where connection is established
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    61
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    62
    public void testTimedConnect1() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    63
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    64
            try (Socket s = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    65
                s.connect(ss.getLocalSocketAddress(), 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    66
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    67
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    68
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    69
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    70
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    71
     * Test timed connect where connection is refused
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    72
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    73
    public void testTimedConnect2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    74
        try (Socket s = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    75
            SocketAddress remote = Utils.refusingEndpoint();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    76
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    77
                s.connect(remote, 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    78
            } catch (ConnectException expected) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    79
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    80
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    81
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    82
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    83
     * Test connect with a timeout of Integer.MAX_VALUE
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    84
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    85
    public void testTimedConnect3() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    86
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    87
            try (Socket s = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    88
                s.connect(ss.getLocalSocketAddress(), Integer.MAX_VALUE);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    89
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    90
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    91
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    92
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    93
    /**
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54216
diff changeset
    94
     * Test connect with a negative timeout.
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    95
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    96
    public void testTimedConnect4() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    97
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    98
            try (Socket s = new Socket()) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
    99
                expectThrows(IllegalArgumentException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   100
                             () -> s.connect(ss.getLocalSocketAddress(), -1));
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   101
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   102
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   103
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   104
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   105
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   106
     * Test timed read where the read succeeds immediately
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   107
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   108
    public void testTimedRead1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   109
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   110
            s1.getOutputStream().write(99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   111
            s2.setSoTimeout(30*1000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   112
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   113
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   114
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   115
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   116
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   117
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   118
     * Test timed read where the read succeeds after a delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   119
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   120
    public void testTimedRead2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   121
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   122
            scheduleWrite(s1.getOutputStream(), 99, 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   123
            s2.setSoTimeout(30*1000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   124
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   125
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   126
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   127
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   128
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   129
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   130
     * Test timed read where the read times out
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   131
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   132
    public void testTimedRead3() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   133
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   134
            s2.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   135
            long startMillis = millisTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   136
            expectThrows(SocketTimeoutException.class, () -> s2.getInputStream().read());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   137
            int timeout = s2.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   138
            checkDuration(startMillis, timeout-100, timeout+2000);
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   139
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   140
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   141
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   142
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   143
     * Test timed read that succeeds after a previous read has timed out
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   144
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   145
    public void testTimedRead4() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   146
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   147
            s2.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   148
            expectThrows(SocketTimeoutException.class, () -> s2.getInputStream().read());
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   149
            s1.getOutputStream().write(99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   150
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   151
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   152
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   153
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   154
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   155
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   156
     * Test timed read that succeeds after a previous read has timed out and
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   157
     * after a short delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   158
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   159
    public void testTimedRead5() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   160
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   161
            s2.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   162
            expectThrows(SocketTimeoutException.class, () -> s2.getInputStream().read());
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   163
            s2.setSoTimeout(30*3000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   164
            scheduleWrite(s1.getOutputStream(), 99, 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   165
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   166
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   167
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   168
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   169
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   170
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   171
     * Test untimed read that succeeds after a previous read has timed out
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   172
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   173
    public void testTimedRead6() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   174
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   175
            s2.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   176
            expectThrows(SocketTimeoutException.class, () -> s2.getInputStream().read());
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   177
            s1.getOutputStream().write(99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   178
            s2.setSoTimeout(0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   179
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   180
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   181
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   182
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   183
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   184
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   185
     * Test untimed read that succeeds after a previous read has timed out and
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   186
     * after a short delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   187
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   188
    public void testTimedRead7() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   189
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   190
            s2.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   191
            expectThrows(SocketTimeoutException.class, () -> s2.getInputStream().read());
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   192
            scheduleWrite(s1.getOutputStream(), 99, 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   193
            s2.setSoTimeout(0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   194
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   195
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   196
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   197
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   198
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   199
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   200
     * Test async close of timed read
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   201
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   202
    public void testTimedRead8() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   203
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   204
            s2.setSoTimeout(30*1000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   205
            scheduleClose(s2, 2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   206
            expectThrows(SocketException.class, () -> s2.getInputStream().read());
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   207
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   208
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   209
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   210
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   211
     * Test read with a timeout of Integer.MAX_VALUE
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   212
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   213
    public void testTimedRead9() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   214
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   215
            scheduleWrite(s1.getOutputStream(), 99, 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   216
            s2.setSoTimeout(Integer.MAX_VALUE);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   217
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   218
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   219
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   220
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   221
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   222
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   223
     * Test writing after a timed read.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   224
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   225
    public void testTimedWrite1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   226
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   227
            s1.getOutputStream().write(99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   228
            s2.setSoTimeout(3000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   229
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   230
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   231
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   232
            // schedule thread to read s1 to EOF
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   233
            scheduleReadToEOF(s1.getInputStream(), 3000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   234
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   235
            // write a lot so that write blocks
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   236
            byte[] data = new byte[128*1024];
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   237
            for (int i = 0; i < 100; i++) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   238
                s2.getOutputStream().write(data);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   239
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   240
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   241
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   242
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   243
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   244
     * Test async close of writer (after a timed read).
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   245
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   246
    public void testTimedWrite2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   247
        withConnection((s1, s2) -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   248
            s1.getOutputStream().write(99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   249
            s2.setSoTimeout(3000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   250
            int b = s2.getInputStream().read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   251
            assertTrue(b == 99);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   252
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   253
            // schedule s2 to be be closed
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   254
            scheduleClose(s2, 3000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   255
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   256
            // write a lot so that write blocks
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   257
            byte[] data = new byte[128*1024];
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   258
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   259
                while (true) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   260
                    s2.getOutputStream().write(data);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   261
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   262
            } catch (SocketException expected) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   263
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   264
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   265
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   266
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   267
     * Test timed accept where a connection is established immediately
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   268
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   269
    public void testTimedAccept1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   270
        Socket s1 = null;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   271
        Socket s2 = null;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   272
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   273
            s1 = new Socket();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   274
            s1.connect(ss.getLocalSocketAddress());
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   275
            ss.setSoTimeout(30*1000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   276
            s2 = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   277
        } finally {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   278
            if (s1 != null) s1.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   279
            if (s2 != null) s2.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   280
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   281
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   282
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   283
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   284
     * Test timed accept where a connection is established after a short delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   285
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   286
    public void testTimedAccept2() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   287
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   288
            ss.setSoTimeout(30*1000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   289
            scheduleConnect(ss.getLocalSocketAddress(), 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   290
            Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   291
            s.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   292
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   293
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   294
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   295
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   296
     * Test timed accept where the accept times out
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   297
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   298
    public void testTimedAccept3() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   299
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   300
            ss.setSoTimeout(2000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   301
            long startMillis = millisTime();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   302
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   303
                Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   304
                s.close();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   305
                fail();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   306
            } catch (SocketTimeoutException expected) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   307
                int timeout = ss.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   308
                checkDuration(startMillis, timeout-100, timeout+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   309
            }
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   310
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   311
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   312
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   313
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   314
     * Test timed accept where a connection is established immediately after a
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   315
     * previous accept timed out.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   316
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   317
    public void testTimedAccept4() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   318
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   319
            ss.setSoTimeout(2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   320
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   321
                Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   322
                s.close();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   323
                fail();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   324
            } catch (SocketTimeoutException expected) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   325
            try (Socket s1 = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   326
                s1.connect(ss.getLocalSocketAddress());
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   327
                Socket s2 = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   328
                s2.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   329
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   330
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   331
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   332
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   333
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   334
     * Test untimed accept where a connection is established after a previous
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   335
     * accept timed out
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   336
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   337
    public void testTimedAccept5() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   338
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   339
            ss.setSoTimeout(2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   340
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   341
                Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   342
                s.close();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   343
                fail();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   344
            } catch (SocketTimeoutException expected) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   345
            ss.setSoTimeout(0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   346
            try (Socket s1 = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   347
                s1.connect(ss.getLocalSocketAddress());
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   348
                Socket s2 = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   349
                s2.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   350
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   351
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   352
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   353
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   354
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   355
     * Test untimed accept where a connection is established after a previous
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   356
     * accept timed out and after a short delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   357
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   358
    public void testTimedAccept6() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   359
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   360
            ss.setSoTimeout(2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   361
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   362
                Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   363
                s.close();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   364
                fail();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   365
            } catch (SocketTimeoutException expected) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   366
            ss.setSoTimeout(0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   367
            scheduleConnect(ss.getLocalSocketAddress(), 2000);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   368
            Socket s = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   369
            s.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   370
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   371
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   372
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   373
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   374
     * Test async close of a timed accept
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   375
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   376
    public void testTimedAccept7() throws IOException {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   377
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   378
            ss.setSoTimeout(30*1000);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   379
            long delay = 2000;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   380
            scheduleClose(ss, delay);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   381
            long startMillis = millisTime();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   382
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   383
                ss.accept().close();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   384
                fail();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   385
            } catch (SocketException expected) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   386
                checkDuration(startMillis, delay-100, delay+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   387
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   388
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   389
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   390
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   391
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   392
     * Test timed accept with the thread interrupt status set.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   393
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   394
    public void testTimedAccept8() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   395
        try (ServerSocket ss = boundServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   396
            ss.setSoTimeout(2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   397
            Thread.currentThread().interrupt();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   398
            long startMillis = millisTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   399
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   400
                Socket s = ss.accept();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   401
                s.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   402
                fail();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   403
            } catch (SocketTimeoutException expected) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   404
                // accept should have blocked for 2 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   405
                int timeout = ss.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   406
                checkDuration(startMillis, timeout-100, timeout+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   407
                assertTrue(Thread.currentThread().isInterrupted());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   408
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   409
                Thread.interrupted(); // clear interrupt status
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   410
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   411
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   412
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   413
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   414
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   415
     * Test interrupt of thread blocked in timed accept.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   416
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   417
    public void testTimedAccept9() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   418
        try (ServerSocket ss = boundServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   419
            ss.setSoTimeout(4000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   420
            // interrupt thread after 1 second
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   421
            Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   422
            long startMillis = millisTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   423
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   424
                Socket s = ss.accept();   // should block for 4 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   425
                s.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   426
                fail();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   427
            } catch (SocketTimeoutException expected) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   428
                // accept should have blocked for 4 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   429
                int timeout = ss.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   430
                checkDuration(startMillis, timeout-100, timeout+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   431
                assertTrue(Thread.currentThread().isInterrupted());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   432
            } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   433
                interrupter.cancel(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   434
                Thread.interrupted(); // clear interrupt status
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   435
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   436
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   437
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   438
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   439
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   440
     * Test two threads blocked in timed accept where no connection is established.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   441
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   442
    public void testTimedAccept10() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   443
        ExecutorService pool = Executors.newFixedThreadPool(2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   444
        try (ServerSocket ss = boundServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   445
            ss.setSoTimeout(4000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   446
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   447
            long startMillis = millisTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   448
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   449
            Future<Socket> result1 = pool.submit(ss::accept);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   450
            Future<Socket> result2 = pool.submit(ss::accept);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   451
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   452
            // both tasks should complete with SocketTimeoutException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   453
            Throwable e = expectThrows(ExecutionException.class, result1::get);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   454
            assertTrue(e.getCause() instanceof SocketTimeoutException);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   455
            e = expectThrows(ExecutionException.class, result2::get);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   456
            assertTrue(e.getCause() instanceof SocketTimeoutException);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   457
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   458
            // should get here in 4 seconds, not 8 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   459
            int timeout = ss.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   460
            checkDuration(startMillis, timeout-100, timeout+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   461
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   462
            pool.shutdown();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   463
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   464
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   465
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   466
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   467
     * Test two threads blocked in timed accept where one connection is established.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   468
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   469
    public void testTimedAccept11() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   470
        ExecutorService pool = Executors.newFixedThreadPool(2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   471
        try (ServerSocket ss = boundServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   472
            ss.setSoTimeout(4000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   473
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   474
            long startMillis = millisTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   475
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   476
            Future<Socket> result1 = pool.submit(ss::accept);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   477
            Future<Socket> result2 = pool.submit(ss::accept);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   478
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   479
            // establish connection after 2 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   480
            scheduleConnect(ss.getLocalSocketAddress(), 2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   481
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   482
            // one task should have accepted the connection, the other should
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   483
            // have completed with SocketTimeoutException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   484
            Socket s1 = null;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   485
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   486
                s1 = result1.get();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   487
                s1.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   488
            } catch (ExecutionException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   489
                assertTrue(e.getCause() instanceof SocketTimeoutException);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   490
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   491
            Socket s2 = null;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   492
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   493
                s2 = result2.get();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   494
                s2.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   495
            } catch (ExecutionException e) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   496
                assertTrue(e.getCause() instanceof SocketTimeoutException);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   497
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   498
            assertTrue((s1 != null) ^ (s2 != null));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   499
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   500
            // should get here in 4 seconds, not 8 seconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   501
            int timeout = ss.getSoTimeout();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   502
            checkDuration(startMillis, timeout-100, timeout+2000);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   503
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   504
            pool.shutdown();
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   505
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   506
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   507
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   508
    /**
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54216
diff changeset
   509
     * Test Socket setSoTimeout with a negative timeout.
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   510
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   511
    @Test(expectedExceptions = { IllegalArgumentException.class })
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   512
    public void testBadTimeout1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   513
        try (Socket s = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   514
            s.setSoTimeout(-1);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   515
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   516
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   517
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   518
    /**
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54216
diff changeset
   519
     * Test ServerSocket setSoTimeout with a negative timeout.
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   520
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   521
    @Test(expectedExceptions = { IllegalArgumentException.class })
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   522
    public void testBadTimeout2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   523
        try (ServerSocket ss = new ServerSocket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   524
            ss.setSoTimeout(-1);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   525
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   526
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   527
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   528
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   529
     * Returns a ServerSocket bound to a port on the loopback address
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   530
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   531
    static ServerSocket boundServerSocket() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   532
        var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   533
        ServerSocket ss = new ServerSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   534
        ss.bind(new InetSocketAddress(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   535
        return ss;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   536
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   537
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   538
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   539
     * An operation that accepts two arguments and may throw IOException
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   540
     */
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   541
    interface ThrowingBiConsumer<T, U> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   542
        void accept(T t, U u) throws IOException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   543
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   544
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   545
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   546
     * Invokes the consumer with a connected pair of sockets
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   547
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   548
    static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   549
        throws IOException
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   550
    {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   551
        Socket s1 = null;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   552
        Socket s2 = null;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   553
        try (ServerSocket ss = boundServerSocket()) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   554
            s1 = new Socket();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   555
            s1.connect(ss.getLocalSocketAddress());
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   556
            s2 = ss.accept();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   557
            consumer.accept(s1, s2);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   558
        } finally {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   559
            if (s1 != null) s1.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   560
            if (s2 != null) s2.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   561
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   562
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   563
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   564
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   565
     * Schedule c to be closed after a delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   566
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   567
    static void scheduleClose(Closeable c, long delay) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   568
        schedule(() -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   569
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   570
                c.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   571
            } catch (IOException ioe) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   572
        }, delay);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   573
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   574
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   575
    /**
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   576
     * Schedule thread to be interrupted after a delay
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   577
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   578
    static Future<?> scheduleInterrupt(Thread thread, long delay) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   579
        return schedule(() -> thread.interrupt(), delay);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   580
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   581
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   582
    /**
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   583
     * Schedule a thread to connect to the given end point after a delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   584
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   585
    static void scheduleConnect(SocketAddress remote, long delay) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   586
        schedule(() -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   587
            try (Socket s = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   588
                s.connect(remote);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   589
            } catch (IOException ioe) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   590
        }, delay);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   591
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   592
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   593
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   594
     * Schedule a thread to read to EOF after a delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   595
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   596
    static void scheduleReadToEOF(InputStream in, long delay) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   597
        schedule(() -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   598
            byte[] bytes = new byte[8192];
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   599
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   600
                while (in.read(bytes) != -1) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   601
            } catch (IOException ioe) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   602
        }, delay);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   603
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   604
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   605
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   606
     * Schedule a thread to write after a delay
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   607
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   608
    static void scheduleWrite(OutputStream out, byte[] data, long delay) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   609
        schedule(() -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   610
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   611
                out.write(data);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   612
            } catch (IOException ioe) { }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   613
        }, delay);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   614
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   615
    static void scheduleWrite(OutputStream out, int b, long delay) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   616
        scheduleWrite(out, new byte[] { (byte)b }, delay);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   617
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   618
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   619
    static Future<?> schedule(Runnable task, long delay) {
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   620
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   621
        try {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   622
            return executor.schedule(task, delay, TimeUnit.MILLISECONDS);
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   623
        } finally {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   624
            executor.shutdown();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   625
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   626
    }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   627
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   628
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   629
     * Returns the current time in milliseconds.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   630
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   631
    private static long millisTime() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   632
        long now = System.nanoTime();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   633
        return TimeUnit.MILLISECONDS.convert(now, TimeUnit.NANOSECONDS);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   634
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   635
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   636
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   637
     * Check the duration of a task
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   638
     * @param start start time, in milliseconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   639
     * @param min minimum expected duration, in milliseconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   640
     * @param max maximum expected duration, in milliseconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   641
     * @return the duration (now - start), in milliseconds
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   642
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   643
    private static long checkDuration(long start, long min, long max) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   644
        long duration = millisTime() - start;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   645
        assertTrue(duration >= min,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   646
                "Duration " + duration + "ms, expected >= " + min + "ms");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   647
        assertTrue(duration <= max,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   648
                "Duration " + duration + "ms, expected <= " + max + "ms");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   649
        return duration;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54289
diff changeset
   650
    }
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   651
}