Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
41152 views
/*1* Copyright (c) 2008, 2016, 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.*;32import java.util.concurrent.*;33import java.io.IOException;3435/**36* This test verifies that a channel or channel group can be closed from a37* completion handler when there are no threads available to handle I/O events.38*/3940public class GroupOfOne {4142public static void main(String[] args) throws Exception {43final List<AsynchronousSocketChannel> accepted = new ArrayList<>();4445// create listener to accept connections46try (final AsynchronousServerSocketChannel listener =47AsynchronousServerSocketChannel.open()) {4849listener.bind(new InetSocketAddress(0));50listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {51public void completed(AsynchronousSocketChannel ch, Void att) {52synchronized (accepted) {53accepted.add(ch);54}55listener.accept((Void)null, this);56}57public void failed(Throwable exc, Void att) {58}59});6061int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();62SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);6364test(sa, true, false);65test(sa, false, true);66test(sa, true, true);67} finally {68// clean-up69synchronized (accepted) {70for (AsynchronousSocketChannel ch: accepted) {71ch.close();72}73}74}75}7677static void test(SocketAddress sa,78final boolean closeChannel,79final boolean shutdownGroup)80throws Exception81{82// group with 1 thread83final AsynchronousChannelGroup group = AsynchronousChannelGroup84.withFixedThreadPool(1, new ThreadFactory() {85@Override86public Thread newThread(final Runnable r) {87return new Thread(r);88}});89final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);90try {91// the latch counts down when:92// 1. The read operation fails (expected)93// 2. the close/shutdown completes94final CountDownLatch latch = new CountDownLatch(2);9596ch.connect(sa, (Void)null, new CompletionHandler<Void,Void>() {97public void completed(Void result, Void att) {98System.out.println("Connected");99100// initiate I/O operation that does not complete (successfully)101ByteBuffer buf = ByteBuffer.allocate(100);102ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() {103public void completed(Integer bytesRead, Void att) {104throw new RuntimeException();105}106public void failed(Throwable exc, Void att) {107if (!(exc instanceof AsynchronousCloseException))108throw new RuntimeException(exc);109System.out.println("Read failed (expected)");110latch.countDown();111}112});113114// close channel or shutdown group115try {116if (closeChannel) {117System.out.print("Close channel ...");118ch.close();119System.out.println(" done.");120}121if (shutdownGroup) {122System.out.print("Shutdown group ...");123group.shutdownNow();124System.out.println(" done.");125}126latch.countDown();127} catch (IOException e) {128throw new RuntimeException();129}130}131public void failed(Throwable exc, Void att) {132throw new RuntimeException(exc);133}134});135136latch.await();137} finally {138// clean-up139group.shutdown();140boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);141if (!terminated)142throw new RuntimeException("Group did not terminate");143}144System.out.println("TEST OKAY");145}146}147148149