Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
38821 views
/*1* Copyright (c) 2008, 2010, 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 {43// create listener to accept connections44final AsynchronousServerSocketChannel listener =45AsynchronousServerSocketChannel.open()46.bind(new InetSocketAddress(0));47final List<AsynchronousSocketChannel> accepted = new ArrayList<AsynchronousSocketChannel>();48listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {49public void completed(AsynchronousSocketChannel ch, Void att) {50synchronized (accepted) {51accepted.add(ch);52}53listener.accept((Void)null, this);54}55public void failed(Throwable exc, Void att) {56}57});5859int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();60SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);6162test(sa, true, false);63test(sa, false, true);64test(sa, true, true);6566// clean-up67listener.close();68synchronized (accepted) {69for (AsynchronousSocketChannel ch: accepted) {70ch.close();71}72}73}7475static void test(SocketAddress sa,76final boolean closeChannel,77final boolean shutdownGroup)78throws Exception79{80// group with 1 thread81final AsynchronousChannelGroup group = AsynchronousChannelGroup82.withFixedThreadPool(1, new ThreadFactory() {83@Override84public Thread newThread(final Runnable r) {85return new Thread(r);86}});87final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);8889// the latch counts down when:90// 1. The read operation fails (expected)91// 2. the close/shutdown completes92final CountDownLatch latch = new CountDownLatch(2);9394ch.connect(sa, (Void)null, new CompletionHandler<Void,Void>() {95public void completed(Void result, Void att) {96System.out.println("Connected");9798// initiate I/O operation that does not complete (successfully)99ByteBuffer buf = ByteBuffer.allocate(100);100ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() {101public void completed(Integer bytesRead, Void att) {102throw new RuntimeException();103}104public void failed(Throwable exc, Void att) {105if (!(exc instanceof AsynchronousCloseException))106throw new RuntimeException(exc);107System.out.println("Read failed (expected)");108latch.countDown();109}110});111112// close channel or shutdown group113try {114if (closeChannel) {115System.out.print("Close channel ...");116ch.close();117System.out.println(" done.");118}119if (shutdownGroup) {120System.out.print("Shutdown group ...");121group.shutdownNow();122System.out.println(" done.");123}124latch.countDown();125} catch (IOException e) {126throw new RuntimeException();127}128}129public void failed(Throwable exc, Void att) {130throw new RuntimeException(exc);131}132});133134latch.await();135136// clean-up137group.shutdown();138boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);139if (!terminated)140throw new RuntimeException("Group did not terminate");141142System.out.println("TEST OKAY");143}144}145146147