test/jdk/java/nio/channels/DatagramChannel/Disconnect.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 54811 9db7c0f561a6
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     1
/*
45840
7eb973f73ce1 8183989: Mark java/nio/channels/DatagramChannel/Disconnect.java as intermittently failing
amlu
parents: 12547
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     4
 *
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     8
 *
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    13
 * accompanied this code).
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    14
 *
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    18
 *
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    21
 * questions.
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    22
 */
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    23
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    24
/* @test
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    25
 * @bug 7132924
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    26
 * @library /test/lib
45840
7eb973f73ce1 8183989: Mark java/nio/channels/DatagramChannel/Disconnect.java as intermittently failing
amlu
parents: 12547
diff changeset
    27
 * @key intermittent
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    28
 * @summary Test DatagramChannel.disconnect when DatagramChannel is connected to an IPv4 socket
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    29
 * @run main Disconnect
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv4Stack=true Disconnect
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    31
 */
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    32
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    33
import java.net.*;
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    34
import java.nio.*;
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    35
import java.nio.channels.*;
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    36
import java.io.IOException;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    37
import jdk.test.lib.net.IPSupport;
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    38
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    39
public class Disconnect {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    40
    public static void main(String[] args) throws IOException {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
    41
        IPSupport.throwSkippedExceptionIfNonOperational();
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    42
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    43
        // test with default protocol family
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    44
        try (DatagramChannel dc = DatagramChannel.open()) {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    45
            test(dc);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    46
            test(dc);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    47
        }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    48
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    49
        if (IPSupport.hasIPv4()) {
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    50
            // test with IPv4 only
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    51
            try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)) {
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    52
                test(dc);
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    53
                test(dc);
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    54
            }
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    55
        }
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    56
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    57
        if (IPSupport.hasIPv6()) {
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    58
            // test with IPv6 only
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    59
            try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET6)) {
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    60
                test(dc);
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    61
                test(dc);
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 47216
diff changeset
    62
            }
12547
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    63
        }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    64
    }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    65
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    66
    /**
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    67
     * Connect DatagramChannel to a server, write a datagram and disconnect. Invoke
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    68
     * a second or subsequent time with the same DatagramChannel instance to check
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    69
     * that disconnect works as expected.
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    70
     */
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    71
    static void test(DatagramChannel dc) throws IOException {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    72
        try (DatagramChannel server = DatagramChannel.open()) {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    73
            server.bind(new InetSocketAddress(0));
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    74
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    75
            InetAddress lh = InetAddress.getLocalHost();
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    76
            dc.connect(new InetSocketAddress(lh, server.socket().getLocalPort()));
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    77
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    78
            dc.write(ByteBuffer.wrap("hello".getBytes()));
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    79
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    80
            ByteBuffer bb = ByteBuffer.allocate(100);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    81
            server.receive(bb);
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    82
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    83
            dc.disconnect();
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    84
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    85
            try {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    86
                dc.write(ByteBuffer.wrap("another message".getBytes()));
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    87
                throw new RuntimeException("write should fail, not connected");
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    88
            } catch (NotYetConnectedException expected) {
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    89
            }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    90
        }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    91
    }
ae1b2051db5d 7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx]
alanb
parents:
diff changeset
    92
}