jdk/test/java/nio/channels/SocketChannel/VectorIO.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6845 0138b84aafdd
child 30046 cf2c86e1819e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6845
diff changeset
     2
 * Copyright (c) 2000, 2010, 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
 * @summary Test socketchannel vector IO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.*;
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 VectorIO {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static int testSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        testSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        for(int i=15; i<18; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            testSize = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.err.println("Length " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Server sv = new Server(testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        sv.start();
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    56
        bufferTest(sv.port());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (sv.finish(8000) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception("Failed: Length = " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
    61
    static void bufferTest(int port) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ByteBuffer[] bufs = new ByteBuffer[testSize];
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    63
        long total = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            String source = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (generator.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                bufs[i] = ByteBuffer.allocateDirect(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                bufs[i] = ByteBuffer.allocate(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            bufs[i].put(source.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            bufs[i].flip();
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    73
            total += bufs[i].remaining();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Get a connection to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        InetAddress lh = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        InetSocketAddress isa = new InetSocketAddress(lh, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        sc.connect(isa);
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    81
        sc.configureBlocking(generator.nextBoolean());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // Write the data out
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    84
        long rem = total;
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    85
        while (rem > 0L) {
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    86
            long bytesWritten = sc.write(bufs);
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    87
            if (bytesWritten == 0) {
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    88
                if (sc.isBlocking())
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    89
                    throw new RuntimeException("write did not block");
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    90
                Thread.sleep(50);
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    91
            } else {
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    92
                rem -= bytesWritten;
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    93
            }
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
    94
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static class Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        extends TestThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   105
        final int testSize;
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   106
        final ServerSocketChannel ssc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   108
        Server(int testSize) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            super("Server " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.testSize = testSize;
5798
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   111
            this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   112
        }
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   113
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   114
        int port() {
9cc262cd2a7a 6961630: TEST_BUG: Several SocketChannel and Selector tests can fail with "address already in use"
alanb
parents: 5506
diff changeset
   115
            return ssc.socket().getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            bufferTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        void bufferTest() throws Exception {
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   123
            long total = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ByteBuffer[] bufs = new ByteBuffer[testSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                String source = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (generator.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    bufs[i] = ByteBuffer.allocateDirect(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    bufs[i] = ByteBuffer.allocate(source.length());
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   131
                total += bufs[i].capacity();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Get a connection from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            SocketChannel sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    sc = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    if (sc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   148
                sc.configureBlocking(generator.nextBoolean());
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // Read data into multiple buffers
6845
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   151
                long avail = total;
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   152
                while (avail > 0) {
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   153
                    long bytesRead = sc.read(bufs);
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   154
                    if (bytesRead < 0)
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   155
                        break;
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   156
                    if (bytesRead == 0) {
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   157
                        if (sc.isBlocking())
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   158
                            throw new RuntimeException("read did not block");
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   159
                        Thread.sleep(50);
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   160
                    }
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   161
                    avail -= bytesRead;
0138b84aafdd 6987116: (so) test/java/nio/channels/SocketChannel/VectorIO.java failed on Solaris 11
alanb
parents: 5798
diff changeset
   162
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                // Check results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    String expected = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    bufs[i].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    int size = bufs[i].capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    byte[] data = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    for(int j=0; j<size; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        data[j] = bufs[i].get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    String message = new String(data, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    if (!message.equals(expected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        throw new Exception("Wrong data: Got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                            + message + ", expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                            + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (sc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}