Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/Unbounded.java
41152 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4607272 684268725* @summary Unit test for AsynchronousChannelGroup26*/2728import java.nio.ByteBuffer;29import java.nio.channels.*;30import java.net.*;31import java.util.concurrent.*;32import java.io.IOException;3334public class Unbounded {35// number of concurrent completion handlers36static final int CONCURRENCY_COUNT = 256;3738// set to true if an I/O operation fails39static volatile boolean failed;4041// set to true when the test is done42static volatile boolean finished;4344public static void main(String[] args) throws Exception {45// create listener to accept connections46AsynchronousServerSocketChannel listener =47AsynchronousServerSocketChannel.open()48.bind(new InetSocketAddress(0));4950// establish connections5152AsynchronousSocketChannel[] clients = new AsynchronousSocketChannel[CONCURRENCY_COUNT];53AsynchronousSocketChannel[] peers = new AsynchronousSocketChannel[CONCURRENCY_COUNT];5455int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();56SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);5758for (int i=0; i<CONCURRENCY_COUNT; i++) {59clients[i] = AsynchronousSocketChannel.open();60Future<Void> result = clients[i].connect(sa);61peers[i] = listener.accept().get();62result.get();63}64System.out.println("All connection established.");6566// the barrier where all threads (plus the main thread) wait67final CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY_COUNT+1);6869// initiate a read operation on each channel.70for (AsynchronousSocketChannel client: clients) {71ByteBuffer buf = ByteBuffer.allocateDirect(100);72client.read(buf, client,73new CompletionHandler<Integer,AsynchronousSocketChannel>() {74public void completed(Integer bytesRead, AsynchronousSocketChannel ch) {75try {76ch.close();77barrier.await();78} catch (Exception x) {79throw new AssertionError(x);80}81}82public void failed(Throwable exc, AsynchronousSocketChannel ch) {83failed = true;84System.err.println("read failed: " + exc);85completed(0, ch);86}87});88}89System.out.println("All read operations outstanding.");9091// write data to each of the accepted connections92for (AsynchronousSocketChannel peer: peers) {93peer.write(ByteBuffer.wrap("welcome".getBytes())).get();94peer.shutdownOutput();95peer.close();96}9798// wait for all threads to reach the barrier99System.out.println("Waiting for all threads to reach barrier");100barrier.await();101102// finish up103finished = true;104listener.close();105if (failed)106throw new RuntimeException("I/O operation failed, see log for details");107}108}109110111