test/jdk/java/nio/channels/DatagramChannel/EmptyBuffer.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
/*
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
     2
 * Copyright (c) 2002, 2015, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4669040
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test DatagramChannel receive with empty buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    30
import java.io.IOException;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    31
import java.io.PrintStream;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    32
import java.net.InetAddress;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    33
import java.net.InetSocketAddress;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    34
import java.net.SocketAddress;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    35
import java.nio.ByteBuffer;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    36
import java.nio.ByteOrder;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    37
import java.nio.channels.ClosedByInterruptException;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    38
import java.nio.channels.DatagramChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class EmptyBuffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    42
    private static final PrintStream log = System.err;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    48
    private static void test() throws Exception {
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    49
        DatagramChannel dc = DatagramChannel.open();
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    50
        InetAddress localHost = InetAddress.getLocalHost();
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    51
        dc.bind(new InetSocketAddress(localHost, 0));
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    52
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    53
        Server server = new Server(dc.getLocalAddress());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Thread serverThread = new Thread(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        serverThread.start();
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    56
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    57
        try {
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    58
            InetSocketAddress isa = new InetSocketAddress(localHost, server.port());
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    59
            dc.connect(isa);
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    60
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    61
            ByteBuffer bb = ByteBuffer.allocateDirect(12);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    62
            bb.order(ByteOrder.BIG_ENDIAN);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    63
            bb.putInt(1).putLong(1);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    64
            bb.flip();
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    65
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    66
            dc.write(bb);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    67
            bb.rewind();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    68
            dc.write(bb);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    69
            bb.rewind();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    70
            dc.write(bb);
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    71
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    72
            Thread.sleep(2000);
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    73
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    74
            serverThread.interrupt();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    75
            server.throwException();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    76
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    77
            dc.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
    78
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    81
    private static class Server implements Runnable {
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    82
        private final DatagramChannel dc;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    83
        private final SocketAddress clientAddress;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    84
        private Exception e = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    86
        Server(SocketAddress clientAddress) throws IOException {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    87
            this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
    88
            this.clientAddress = clientAddress;
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    89
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    91
        int port() {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    92
            return dc.socket().getLocalPort();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    93
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    94
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    95
        void throwException() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        void showBuffer(String s, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            log.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            for (int i=0; i<bb.limit(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                byte element = bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                log.print(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   110
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                ByteBuffer bb = ByteBuffer.allocateDirect(12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // Only one clear. The buffer will be full after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // the first receive, but it should still block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                // and receive and discard the next two
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   118
                int numberReceived = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                while (!Thread.interrupted()) {
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   120
                    SocketAddress sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    } catch (ClosedByInterruptException cbie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        // Expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        log.println("Took expected exit");
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   126
                        // Verify that enough packets were received
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   127
                        if (numberReceived != 3)
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   128
                            throw new RuntimeException("Failed: Too few datagrams");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    if (sa != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        log.println("Client: " + sa);
31176
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   133
                        // Check client address so as not to count stray packets
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   134
                        if (sa.equals(clientAddress)) {
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   135
                            showBuffer("RECV", bb);
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   136
                            numberReceived++;
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   137
                        }
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   138
                        if (numberReceived > 3)
db59cb6634af 8087304: (ch) java/nio/channels/DatagramChannel/EmptyBuffer.java received 4 times while expected 3
bpb
parents: 7668
diff changeset
   139
                            throw new RuntimeException("Failed: Too many datagrams");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        sa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                e = ex;
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
   145
            } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5788
diff changeset
   146
                try { dc.close(); } catch (IOException ignore) { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}