test/jdk/java/net/DatagramPacket/ReuseBuf.java
author dfuchs
Fri, 30 Aug 2019 12:44:52 +0100
changeset 57965 ef15850629cd
parent 47216 71c04702a3d5
child 58688 2b1e684c3ce6
permissions -rw-r--r--
8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout Summary: The test is changed to bind to InetAddress.getLocalHost() instead of binding to the wildcard. Reviewed-by: alanb, dfuchs, msheppar Contributed-by: Patrick Concannon <catrick.concannon@oracle.com>
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
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4424096
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary DatagramPacket spec needs clarification (reuse buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ReuseBuf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static String msgs[] = {"Hello World", "Java", "Good Bye"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static class ServerThread extends Thread{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        DatagramSocket ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        public ServerThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            try {
57965
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    41
                InetAddress local = InetAddress.getLocalHost();
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    42
                InetSocketAddress bindaddr = new InetSocketAddress(local, 0);
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    43
                ds = new DatagramSocket(bindaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                port = ds.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            DatagramPacket dp = new DatagramPacket(b,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    ds.receive(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    String reply = new String(dp.getData(), dp.getOffset(), dp.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    ds.send(new DatagramPacket(reply.getBytes(),reply.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                               dp.getAddress(),dp.getPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    if (reply.equals(msgs[msgs.length-1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ServerThread st = new ServerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        st.start();
57965
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    73
        InetAddress local = InetAddress.getLocalHost();
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    74
        InetSocketAddress bindaddr = new InetSocketAddress(local, 0);
ef15850629cd 8193596: java/net/DatagramPacket/ReuseBuf.java failed due to timeout
dfuchs
parents: 47216
diff changeset
    75
        DatagramSocket ds = new DatagramSocket(bindaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        DatagramPacket dp = new DatagramPacket(b,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int i = 0; i < msgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            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
    80
                                       InetAddress.getLocalHost(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                       port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            ds.receive(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (!msgs[i].equals(new String(dp.getData(), dp.getOffset(), dp.getLength()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new RuntimeException("Msg expected: "+msgs[i] +msgs[i].length()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                           "msg received: "+new String(dp.getData(), dp.getOffset(), dp.getLength())+dp.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Test Passed!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}