test/jdk/java/nio/channels/DatagramChannel/ThereCanBeOnlyOne.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 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 4630914
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Should only be one PUE even if multiple ICMPs were received
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ThereCanBeOnlyOne {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static void doTest(InetAddress ia, boolean testSend) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        DatagramChannel dc1 = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        dc1.socket().bind((SocketAddress)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        int port = dc1.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        InetSocketAddress isa = new InetSocketAddress(ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        dc1.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        ByteBuffer bb = ByteBuffer.allocateDirect(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        bb.put("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
         * Send a bunch of packets to the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int outstanding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i=0; i<20; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                dc1.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                outstanding++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            } catch (PortUnreachableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                /* PUE throw => assume none outstanding now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                outstanding = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (outstanding > 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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (outstanding < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.err.println("Insufficient exceptions outstanding - Test Skipped (Passed).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            dc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return;
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
         * Give time for ICMP port unreachables to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Thread.currentThread().sleep(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * The next send or receive should cause a PUE to be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean gotPUE = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        boolean gotTimeout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        dc1.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (testSend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                dc1.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                dc1.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (PortUnreachableException pue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.err.println("Got one PUE...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            gotPUE = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * The next receive should not get another PUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (gotPUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                dc1.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } catch (PortUnreachableException pue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new Exception("PUs should have been consumed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // packets discarded. Okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        dc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        InetAddress ia = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        doTest(ia, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        doTest(ia, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}