893N/A/*
3261N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
1580N/A * @bug 4607272 6842687
893N/A * @summary Unit test for AsynchronousChannelGroup
893N/A */
893N/A
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.channels.*;
893N/Aimport java.net.*;
2546N/Aimport java.util.*;
893N/Aimport java.util.concurrent.*;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * This test verifies that a channel or channel group can be closed from a
893N/A * completion handler when there are no threads available to handle I/O events.
893N/A */
893N/A
893N/Apublic class GroupOfOne {
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A // create listener to accept connections
893N/A final AsynchronousServerSocketChannel listener =
893N/A AsynchronousServerSocketChannel.open()
893N/A .bind(new InetSocketAddress(0));
2546N/A final List<AsynchronousSocketChannel> accepted = new ArrayList<AsynchronousSocketChannel>();
1434N/A listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
893N/A public void completed(AsynchronousSocketChannel ch, Void att) {
2546N/A synchronized (accepted) {
2546N/A accepted.add(ch);
2546N/A }
1434N/A listener.accept((Void)null, this);
893N/A }
893N/A public void failed(Throwable exc, Void att) {
893N/A }
893N/A });
893N/A
893N/A int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
893N/A SocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), port);
893N/A
893N/A test(sa, true, false);
893N/A test(sa, false, true);
893N/A test(sa, true, true);
2546N/A
2546N/A // clean-up
2546N/A listener.close();
2546N/A synchronized (accepted) {
2546N/A for (AsynchronousSocketChannel ch: accepted) {
2546N/A ch.close();
2546N/A }
2546N/A }
893N/A }
893N/A
893N/A static void test(SocketAddress sa,
893N/A final boolean closeChannel,
893N/A final boolean shutdownGroup)
893N/A throws Exception
893N/A {
893N/A // group with 1 thread
893N/A final AsynchronousChannelGroup group = AsynchronousChannelGroup
893N/A .withFixedThreadPool(1, new ThreadFactory() {
893N/A @Override
893N/A public Thread newThread(final Runnable r) {
893N/A return new Thread(r);
893N/A }});
893N/A final AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);
893N/A
893N/A // the latch counts down when:
893N/A // 1. The read operation fails (expected)
893N/A // 2. the close/shutdown completes
893N/A final CountDownLatch latch = new CountDownLatch(2);
893N/A
1434N/A ch.connect(sa, (Void)null, new CompletionHandler<Void,Void>() {
893N/A public void completed(Void result, Void att) {
893N/A System.out.println("Connected");
893N/A
893N/A // initiate I/O operation that does not complete (successfully)
893N/A ByteBuffer buf = ByteBuffer.allocate(100);
1434N/A ch.read(buf, (Void)null, new CompletionHandler<Integer,Void>() {
893N/A public void completed(Integer bytesRead, Void att) {
893N/A throw new RuntimeException();
893N/A }
893N/A public void failed(Throwable exc, Void att) {
893N/A if (!(exc instanceof AsynchronousCloseException))
893N/A throw new RuntimeException(exc);
893N/A System.out.println("Read failed (expected)");
893N/A latch.countDown();
893N/A }
893N/A });
893N/A
893N/A // close channel or shutdown group
893N/A try {
893N/A if (closeChannel) {
893N/A System.out.print("Close channel ...");
893N/A ch.close();
893N/A System.out.println(" done.");
893N/A }
893N/A if (shutdownGroup) {
893N/A System.out.print("Shutdown group ...");
893N/A group.shutdownNow();
893N/A System.out.println(" done.");
893N/A }
893N/A latch.countDown();
893N/A } catch (IOException e) {
893N/A throw new RuntimeException();
893N/A }
893N/A }
893N/A public void failed(Throwable exc, Void att) {
893N/A throw new RuntimeException(exc);
893N/A }
893N/A });
893N/A
893N/A latch.await();
893N/A
893N/A // clean-up
893N/A group.shutdown();
893N/A boolean terminated = group.awaitTermination(5, TimeUnit.SECONDS);
893N/A if (!terminated)
893N/A throw new RuntimeException("Group did not terminate");
893N/A
893N/A System.out.println("TEST OKAY");
893N/A }
893N/A}