test/jdk/java/net/DatagramPacket/ReuseBuf.java
author chegar
Fri, 18 Oct 2019 17:08:59 +0100
branchdatagramsocketimpl-branch
changeset 58688 2b1e684c3ce6
parent 57965 ef15850629cd
permissions -rw-r--r--
datagramsocketimpl-branch: fix issue with receive packet buf size
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57965
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 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
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    24
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4424096
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary DatagramPacket spec needs clarification (reuse buf)
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    28
 * @run main ReuseBuf
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv4Stack=true ReuseBuf
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    30
 * @run main/othervm -Djdk.net.usePlainDatagramSocketImpl=true ReuseBuf
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    33
import static java.lang.System.out;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class ReuseBuf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static String msgs[] = {"Hello World", "Java", "Good Bye"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static class ServerThread extends Thread{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        DatagramSocket ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public ServerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            try {
57965
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    43
                InetAddress local = InetAddress.getLocalHost();
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    44
                InetSocketAddress bindaddr = new InetSocketAddress(local, 0);
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    45
                ds = new DatagramSocket(bindaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                port = ds.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            DatagramPacket dp = new DatagramPacket(b,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    ds.receive(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    String reply = new String(dp.getData(), dp.getOffset(), dp.getLength());
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    59
                    out.println("server: received [" + reply + "]");
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    60
                    for (int i=0; i<2; i++) {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    61
                        out.println("server: sending [" + reply + "]");
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    62
                        ds.send(new DatagramPacket(reply.getBytes(), reply.length(),
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    63
                                dp.getAddress(), dp.getPort()));
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    64
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    if (reply.equals(msgs[msgs.length-1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ServerThread st = new ServerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        st.start();
57965
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    79
        InetAddress local = InetAddress.getLocalHost();
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    80
        InetSocketAddress bindaddr = new InetSocketAddress(local, 0);
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    81
        DatagramSocket ds = new DatagramSocket(bindaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        DatagramPacket dp = new DatagramPacket(b,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int i = 0; i < msgs.length; i++) {
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    85
            out.println("client: sending [" + msgs[i] + "]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ds.send(new DatagramPacket(msgs[i].getBytes(),msgs[i].length(),
13359
edb9f5468e9a 7081476: test/java/net/InetSocketAddress/B6469803.java failing intermittently
chegar
parents: 5506
diff changeset
    87
                                       InetAddress.getLocalHost(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                       port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ds.receive(dp);
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    90
            String recvmsg = new String(dp.getData(), dp.getOffset(), dp.getLength());
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    91
            out.println("client: received [" + recvmsg + "]");
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    92
            if (!msgs[i].equals(recvmsg)) {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    93
                throw new RuntimeException("Msg expected: " + msgs[i] +
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    94
                                           ", msg received: " + recvmsg);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    95
            }
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    96
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    97
            byte ba[] = new byte[1];
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    98
            DatagramPacket dp2 = new DatagramPacket(ba, ba.length);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
    99
            dp2.setData(new byte[99], 5, 3);  // only 3 bytes
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   100
            ds.receive(dp2);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   101
            String expectedMsg = msgs[i].substring(0, 3);
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   102
            recvmsg = new String(dp2.getData(), dp2.getOffset(), dp2.getLength());
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   103
            out.println("client: received [" + recvmsg + "]");
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   104
            if (!expectedMsg.equals(recvmsg)) {
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   105
                throw new RuntimeException("Msg truncated expected: " + expectedMsg +
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   106
                                            ", msg received: "+ recvmsg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ds.close();
58688
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   110
        out.println("Test Passed!!!");
2b1e684c3ce6 datagramsocketimpl-branch: fix issue with receive packet buf size
chegar
parents: 57965
diff changeset
   111
        st.join();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}