test/jdk/java/net/SocketInputStream/SocketTimeout.java
author aefimov
Wed, 10 Jul 2019 16:09:20 +0100
changeset 55645 3203e857fa71
parent 54811 9db7c0f561a6
permissions -rw-r--r--
8226756: Replace wildcard address with loopback or local host in tests - part 18 Reviewed-by: dfuchs Contributed-by: Patrick Concannon <patrick.concannon@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55645
3203e857fa71 8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents: 54811
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4158021
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    29
 * @run main SocketTimeout
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv4Stack=true SocketTimeout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 49431
diff changeset
    35
import jdk.test.lib.net.IPSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SocketTimeout  {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    38
    static final int TIMEOUT = 1000;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
    41
    IPSupport.throwSkippedExceptionIfNonOperational();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    42
    InetAddress  sin = InetAddress.getLocalHost();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    Socket       soc = null,soc1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    InputStream  is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    ServerSocket srv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int          port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
55645
3203e857fa71 8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents: 54811
diff changeset
    48
    srv = new ServerSocket();
3203e857fa71 8226756: Replace wildcard address with loopback or local host in tests - part 18
aefimov
parents: 54811
diff changeset
    49
    srv.bind(new InetSocketAddress(sin, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    port = srv.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    soc = new Socket(sin, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    soc1 = srv.accept();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    53
    soc.setSoTimeout(TIMEOUT);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    54
    srv.setSoTimeout(TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      is = soc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    } catch(InterruptedIOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (! (e instanceof java.net.SocketTimeoutException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new Exception ("Wrong exception class thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch(NoClassDefFoundError e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new Exception ("SocketTimeoutException: not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    66
    } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    67
        soc.close();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    68
        soc1.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // now check accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      srv.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    } catch(InterruptedIOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (! (e instanceof java.net.SocketTimeoutException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new Exception ("Wrong exception class thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } catch(NoClassDefFoundError e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new Exception ("SocketTimeoutException: not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    82
    } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    83
        srv.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Now check DatagramSocket.receive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    DatagramSocket dg = new DatagramSocket ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    89
    dg.setSoTimeout (TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      dg.receive (new DatagramPacket (new byte [64], 64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    } catch(InterruptedIOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (! (e instanceof java.net.SocketTimeoutException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new Exception ("Wrong exception class thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch(NoClassDefFoundError e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new Exception ("SocketTimeoutException: not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   100
    } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   101
        dg.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}