jdk/test/java/nio/channels/AsynchronousChannelGroup/Unbounded.java
author alanb
Wed, 05 Jun 2013 11:12:31 +0100
changeset 17948 de14cee4b93d
parent 14505 1a758bd5b548
child 23010 6dadb192ad81
permissions -rw-r--r--
8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
     2
 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    21
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    22
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    23
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
/* @test
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 3327
diff changeset
    25
 * @bug 4607272 6842687
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
 * @summary Unit test for AsynchronousChannelGroup
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
import java.nio.ByteBuffer;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
import java.nio.channels.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    31
import java.net.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
import java.util.concurrent.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.io.IOException;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
public class Unbounded {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
    // number of concurrent completion handlers
4234
d9720475ab15 6900234: Several NIO tests should be removed from test/ProblemList.txt
alanb
parents: 3632
diff changeset
    37
    static final int CONCURRENCY_COUNT = 256;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
14501
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    39
    // set to true if an I/O operation fails
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    40
    static volatile boolean failed;
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    41
14505
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
    42
    // set to true when the test is done
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
    43
    static volatile boolean finished;
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
    44
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    46
        // create listener to accept connections
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    47
        AsynchronousServerSocketChannel listener =
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
            AsynchronousServerSocketChannel.open()
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
                .bind(new InetSocketAddress(0));
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    50
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    51
        // establish connections
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    53
        AsynchronousSocketChannel[] clients = new AsynchronousSocketChannel[CONCURRENCY_COUNT];
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    54
        AsynchronousSocketChannel[] peers = new AsynchronousSocketChannel[CONCURRENCY_COUNT];
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    55
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    56
        int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    57
        SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    58
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
        for (int i=0; i<CONCURRENCY_COUNT; i++) {
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    60
            clients[i] = AsynchronousSocketChannel.open();
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    61
            Future<Void> result = clients[i].connect(sa);
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    62
            peers[i] = listener.accept().get();
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    63
            result.get();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    65
        System.out.println("All connection established.");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    66
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    67
        // the barrier where all threads (plus the main thread) wait
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    68
        final CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY_COUNT+1);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    69
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    70
        // initiate a read operation on each channel.
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    71
        for (AsynchronousSocketChannel client: clients) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    72
            ByteBuffer buf = ByteBuffer.allocateDirect(100);
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    73
            client.read(buf, client,
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
                new CompletionHandler<Integer,AsynchronousSocketChannel>() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
                    public void completed(Integer bytesRead, AsynchronousSocketChannel ch) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
                        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    77
                            ch.close();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
                            barrier.await();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
                        } catch (Exception x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    80
                            throw new AssertionError(x);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    81
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    82
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    83
                    public void failed(Throwable exc, AsynchronousSocketChannel ch) {
14501
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    84
                        failed = true;
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    85
                        System.err.println("read failed: " + exc);
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
    86
                        completed(0, ch);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    87
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    88
                });
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    89
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
        System.out.println("All read operations outstanding.");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    92
        // write data to each of the accepted connections
17948
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    93
        for (AsynchronousSocketChannel peer: peers) {
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    94
            peer.write(ByteBuffer.wrap("welcome".getBytes())).get();
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    95
            peer.shutdownOutput();
de14cee4b93d 8003895: java/nio/channels/AsynchronousChannelGroup/Unbounded.java failing again [win64]
alanb
parents: 14505
diff changeset
    96
            peer.close();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    97
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    98
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    99
        // wait for all threads to reach the barrier
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   100
        System.out.println("Waiting for all threads to reach barrier");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   101
        barrier.await();
14505
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
   102
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
   103
        // finish up
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
   104
        finished = true;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   105
        listener.close();
14501
10121b56420c 8003253: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java hang intermittently [win]
alanb
parents: 5506
diff changeset
   106
        if (failed)
14505
1a758bd5b548 8003285: TEST_BUG: java/nio/channels/AsynchronousChannelGroup/Unbounded.java fails again [macosx]
alanb
parents: 14501
diff changeset
   107
            throw new RuntimeException("I/O operation failed, see log for details");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   108
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   109
}