jdk/test/java/nio/channels/ServerSocketChannel/Basic.java
author alanb
Fri, 15 Jan 2016 10:58:09 +0000
changeset 34965 38232541c49a
parent 5506 202f599c92aa
permissions -rw-r--r--
8143100: (sc) java/nio/channels/ServerSocketChannel/Basic.java fails intermittently Reviewed-by: alanb Contributed-by: huaming.li@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34965
38232541c49a 8143100: (sc) java/nio/channels/ServerSocketChannel/Basic.java fails intermittently
alanb
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2016, 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
34965
38232541c49a 8143100: (sc) java/nio/channels/ServerSocketChannel/Basic.java fails intermittently
alanb
parents: 5506
diff changeset
    25
 * @bug 4286936 8143100
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for server-socket channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ..
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Basic {
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
    static class Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        extends TestThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        ServerSocketChannel ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        boolean block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Server(ServerSocketChannel ssc, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            super("Server", Basic.log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.ssc = ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.block = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            log.println("Server: Listening "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                        + (block ? "(blocking)" : "(non-blocking)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (!block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            log.println("  " + ssc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            //log.println("  " + ssc.options());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            SocketChannel sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                sc = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if (sc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                log.println("Server: Sleeping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            log.println("Server: Accepted " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ByteBuffer bb = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (sc.read(bb) != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                throw new Exception("Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            byte b = bb.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            log.println("Server: Read " + b + ", writing " + (b + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            bb.put((byte)43);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (sc.write(bb) != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                throw new Exception("Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            log.println("Server: Finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static class Client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        extends TestThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        boolean dally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Client(int port, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            super("Client", Basic.log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.dally = !block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (dally)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Thread.sleep(200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            InetSocketAddress isa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                = new InetSocketAddress(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            log.println("Client: Connecting to " + isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            sc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            log.println("Client: Connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ByteBuffer bb = ByteBuffer.allocateDirect(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            bb.put((byte)42).flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            log.println("Client: Writing " + bb.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (sc.write(bb) != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                throw new Exception("Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (sc.read(bb) != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new Exception("Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (bb.get() != 43)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new Exception("Read " + bb.get(bb.position() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            log.println("Client: Read " + bb.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            log.println("Client: Finished");
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
    static void test(boolean block) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ssc.socket().setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int port = TestUtil.bind(ssc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Server server = new Server(ssc, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Client client = new Client(port, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        client.start();
34965
38232541c49a 8143100: (sc) java/nio/channels/ServerSocketChannel/Basic.java fails intermittently
alanb
parents: 5506
diff changeset
   134
        if ((server.finish(0) & client.finish(0)) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        test(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        test(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}