test/jdk/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java
author mli
Fri, 16 Mar 2018 18:15:31 +0800
changeset 49255 acdb8531cc8b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199215: Re-examine getFreePort method in test infrastructure library Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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
/* @test
36410
c2ef247d0b6f 8143610: (dc) java/nio/channels/DatagramChannel/AdaptDatagramSocket.java failed intermittently due to SocketTimeoutException
mli
parents: 30046
diff changeset
    25
 * @bug 4313882 4981129 8143610
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for datagram-socket-channel adaptors
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    27
 * @library .. /test/lib
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    28
 * @build jdk.test.lib.Utils TestServers
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    29
 * @run main AdaptDatagramSocket
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 23010
diff changeset
    30
 * @key randomness
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.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class AdaptDatagramSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static java.io.PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static Random rand = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static String toString(DatagramPacket dp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return ("DatagramPacket[off=" + dp.getOffset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                + ", len=" + dp.getLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void test(DatagramSocket ds, InetSocketAddress dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                     boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        rand.nextBytes(op.getData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        out.println("pre  op: " + toString(op) + "  ip: " + toString(ip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ds.send(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                ds.receive(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (ip.getLength() == 0) { // ## Not sure why this happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    ip.setLength(100 - 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    out.println("Receive timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        out.println("rtt: " + (System.currentTimeMillis() - start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        out.println("post op: " + toString(op) + "  ip: " + toString(ip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
    81
        for (int i = 0; i < ip.getLength(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (ip.getData()[ip.getOffset() + i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                != op.getData()[op.getOffset() + i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new Exception("Incorrect data received");
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
    85
        }
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    86
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    87
        if (!(ip.getSocketAddress().equals(dst))) {
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    88
            throw new Exception("Incorrect sender address, expected: " + dst
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    89
                + " actual: " + ip.getSocketAddress());
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    90
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static void test(InetSocketAddress dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                     int timeout, boolean shouldTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                     boolean connect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        out.println("dst: " + dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        DatagramSocket ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // Original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ds = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            ds = dc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ds.bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        out.println("socket: " + ds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (connect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ds.connect(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            out.println("connect: " + ds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                                      ds.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        out.println("src: " + src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (timeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ds.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        out.println("timeout: " + ds.getSoTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   124
        for (int i = 0; i < 5; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            test(ds, dst, shouldTimeout);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   126
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Leave the socket open so that we don't reuse the old src address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        //ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static void main(String[] args) throws Exception {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   134
        // need an UDP echo server
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   135
        try (TestServers.UdpEchoServer echoServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   136
                = TestServers.UdpEchoServer.startNewServer(100)) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   137
            final InetSocketAddress address
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   138
                = new InetSocketAddress(echoServer.getAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   139
                                        echoServer.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   140
            test(address, 0, false, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   141
            test(address, 0, false, true);
36410
c2ef247d0b6f 8143610: (dc) java/nio/channels/DatagramChannel/AdaptDatagramSocket.java failed intermittently due to SocketTimeoutException
mli
parents: 30046
diff changeset
   142
            test(address, Integer.MAX_VALUE, false, false);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   143
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   144
        try (TestServers.UdpDiscardServer discardServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   145
                = TestServers.UdpDiscardServer.startNewServer()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   146
            final InetSocketAddress address
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   147
                = new InetSocketAddress(discardServer.getAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   148
                                        discardServer.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   149
            test(address, 10, true, false);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 7668
diff changeset
   150
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}