jdk/test/java/nio/channels/DatagramChannel/Sender.java
author alanb
Tue, 15 Jun 2010 10:03:37 +0100
changeset 5788 50d4ad1a780f
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use" 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4669040
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test DatagramChannel subsequent receives with no datagram ready
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Sender {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void test() throws Exception {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    45
        Server server = new Server();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    46
        Client client = new Client(server.port());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Thread serverThread = new Thread(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Thread clientThread = new Thread(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        server.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        client.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    61
    public static class Client implements Runnable {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    62
        final int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    65
        Client(int port) {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    66
            this.port = port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    69
        void throwException() throws Exception {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    70
            if (e != null)
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    71
                throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                ByteBuffer bb = ByteBuffer.allocateDirect(12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                bb.order(ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                bb.putInt(1).putLong(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                InetAddress address = InetAddress.getLocalHost();
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    82
                InetSocketAddress isa = new InetSocketAddress(address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                dc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
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
    public static class Server implements Runnable {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    92
        final DatagramChannel dc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Exception e = null;
5788
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
        Server() throws IOException {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    96
            dc = DatagramChannel.open().bind(new InetSocketAddress(0));
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    97
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    99
        int port() {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   100
            return dc.socket().getLocalPort();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   101
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   102
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   103
        void throwException() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        void showBuffer(String s, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            log.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            for (int i=0; i<bb.limit(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                byte element = bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                log.print(element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            SocketAddress sa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ByteBuffer bb = ByteBuffer.allocateDirect(12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // Get the one valid datagram
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   125
                dc.configureBlocking(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                while (sa == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                sa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (sa != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}