test/jdk/java/nio/channels/DatagramChannel/Connect.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 49284 a51ca91c2cde
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 7668
diff changeset
    25
 * @bug 4313882 7183800
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test DatagramChannel's send and receive methods
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class Connect {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    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
    46
        Reactor r = new Reactor();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    47
        Actor a = new Actor(r.port());
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    48
        invoke(a, r);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static void invoke(Sprintable reader, Sprintable writer) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Thread writerThread = new Thread(writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        writerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Thread readerThread = new Thread(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        readerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        writerThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        readerThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        reader.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        writer.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public interface Sprintable extends Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void throwException() throws Exception;
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 class Actor implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    71
        final int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    74
        Actor(int port) {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    75
            this.port = port;
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    76
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                // Send a message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                ByteBuffer bb = ByteBuffer.allocateDirect(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                bb.put("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                InetAddress address = InetAddress.getLocalHost();
16480
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 7668
diff changeset
    92
                if (address.isLoopbackAddress()) {
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 7668
diff changeset
    93
                    address = InetAddress.getLoopbackAddress();
fcad3b6ec812 7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents: 7668
diff changeset
    94
                }
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    95
                InetSocketAddress isa = new InetSocketAddress(address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                dc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                // Try to send to some other address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                address = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                InetSocketAddress bogus = new InetSocketAddress(address, 3333);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    dc.send(bb, bogus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    throw new RuntimeException("Allowed bogus send while connected");
49284
a51ca91c2cde 8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents: 47216
diff changeset
   105
                } catch (AlreadyConnectedException ace) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    // Correct behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                // Read a reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                dc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                CharBuffer cb = Charset.forName("US-ASCII").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                newDecoder().decode(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                log.println("From Reactor: "+isa+ " said " +cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                dc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static class Reactor implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   127
        final DatagramChannel dc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   130
        Reactor() throws IOException {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   131
            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
   132
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   133
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   134
        int port() {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   135
            return dc.socket().getLocalPort();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   136
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   137
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // Listen for a message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ByteBuffer bb = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                SocketAddress sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                CharBuffer cb = Charset.forName("US-ASCII").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                newDecoder().decode(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                log.println("From Actor: "+sa+ " said " +cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // Reply to sender
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                dc.connect(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                dc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}