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
893N/A * @bug 4607272
893N/A * @summary Unit test for AsynchronousChannelGroup
893N/A * @build Basic
893N/A * @run main/othervm -XX:-UseVMInterruptibleIO Basic
893N/A */
893N/A
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.channels.*;
893N/Aimport java.net.*;
893N/Aimport java.util.*;
893N/Aimport java.util.concurrent.*;
893N/Aimport java.io.IOException;
893N/A
893N/Apublic class Basic {
893N/A static final Random rand = new Random();
893N/A static final ThreadFactory threadFactory = new ThreadFactory() {
893N/A @Override
893N/A public Thread newThread(final Runnable r) {
893N/A return new Thread(r);
893N/A }};
893N/A
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A shutdownTests();
893N/A shutdownNowTests();
893N/A afterShutdownTests();
893N/A miscTests();
893N/A }
893N/A
893N/A static void shutdownTests() throws Exception {
893N/A System.out.println("-- test shutdown --");
893N/A
893N/A // test shutdown with no channels in groups
893N/A for (int i=0; i<500; i++) {
893N/A ExecutorService pool = null;
893N/A AsynchronousChannelGroup group;
893N/A if (rand.nextBoolean()) {
893N/A pool = Executors.newCachedThreadPool();
893N/A group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
893N/A } else {
893N/A int nThreads = 1 + rand.nextInt(8);
893N/A group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, threadFactory);
893N/A }
893N/A group.shutdown();
893N/A if (!group.isShutdown())
893N/A throw new RuntimeException("Group should be shutdown");
893N/A // group should terminate quickly
893N/A boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
893N/A if (!terminated)
893N/A throw new RuntimeException("Group should have terminated");
893N/A if (pool != null && !pool.isTerminated())
893N/A throw new RuntimeException("Executor should have terminated");
893N/A }
893N/A
893N/A // shutdown with channel in group
893N/A for (int i=0; i<500; i++) {
893N/A ExecutorService pool = null;
893N/A AsynchronousChannelGroup group;
893N/A if (rand.nextBoolean()) {
893N/A pool = Executors.newCachedThreadPool();
893N/A group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(10));
893N/A } else {
893N/A int nThreads = 1 + rand.nextInt(8);
893N/A group = AsynchronousChannelGroup.withFixedThreadPool(nThreads, threadFactory);
893N/A }
893N/A // create channel that is bound to group
893N/A AsynchronousChannel ch;
3003N/A switch (rand.nextInt(2)) {
893N/A case 0 : ch = AsynchronousSocketChannel.open(group); break;
893N/A case 1 : ch = AsynchronousServerSocketChannel.open(group); break;
893N/A default : throw new AssertionError();
893N/A }
893N/A group.shutdown();
893N/A if (!group.isShutdown())
893N/A throw new RuntimeException("Group should be shutdown");
893N/A
893N/A // last channel so should terminate after this channel is closed
893N/A ch.close();
893N/A
893N/A // group should terminate quickly
893N/A boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
893N/A if (!terminated)
893N/A throw new RuntimeException("Group should have terminated");
893N/A if (pool != null && !pool.isTerminated())
893N/A throw new RuntimeException("Executor should have terminated");
893N/A }
893N/A }
893N/A
893N/A static void shutdownNowTests() throws Exception {
893N/A System.out.println("-- test shutdownNow --");
893N/A
893N/A for (int i=0; i< 10; i++) {
893N/A ExecutorService pool = null;
893N/A AsynchronousChannelGroup group;
893N/A if (rand.nextBoolean()) {
893N/A pool = Executors.newCachedThreadPool();
893N/A group = AsynchronousChannelGroup
893N/A .withCachedThreadPool(pool, rand.nextInt(5));
893N/A } else {
893N/A int nThreads = 1 + rand.nextInt(8);
893N/A group = AsynchronousChannelGroup
893N/A .withFixedThreadPool(nThreads, threadFactory);
893N/A }
893N/A
893N/A // I/O in progress
3003N/A AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
3003N/A .open(group).bind(new InetSocketAddress(0));
3003N/A ch.accept();
893N/A
893N/A // forceful shutdown
893N/A group.shutdownNow();
893N/A
893N/A // shutdownNow is required to close all channels
893N/A if (ch.isOpen())
893N/A throw new RuntimeException("Channel should be closed");
893N/A
893N/A boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
893N/A if (!terminated)
893N/A throw new RuntimeException("Group should have terminated");
893N/A if (pool != null && !pool.isTerminated())
893N/A throw new RuntimeException("Executor should have terminated");
893N/A }
893N/A }
893N/A
893N/A // test creating channels in group after group is shutdown
893N/A static void afterShutdownTests() throws Exception {
893N/A System.out.println("-- test operations after group is shutdown --");
893N/A AsynchronousChannelGroup group =
893N/A AsynchronousChannelGroup.withFixedThreadPool(1, threadFactory);
893N/A
893N/A AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);
893N/A AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open(group);
893N/A
893N/A // initiate accept
893N/A listener.bind(new InetSocketAddress(0));
893N/A Future<AsynchronousSocketChannel> result = listener.accept();
893N/A
893N/A // shutdown group
893N/A group.shutdown();
893N/A if (!group.isShutdown())
893N/A throw new RuntimeException("Group should be shutdown");
893N/A
893N/A // attempt to create another channel
893N/A try {
893N/A AsynchronousSocketChannel.open(group);
893N/A throw new RuntimeException("ShutdownChannelGroupException expected");
893N/A } catch (ShutdownChannelGroupException x) {
893N/A }
893N/A try {
893N/A AsynchronousServerSocketChannel.open(group);
893N/A throw new RuntimeException("ShutdownChannelGroupException expected");
893N/A } catch (ShutdownChannelGroupException x) {
893N/A }
893N/A
893N/A // attempt to create another channel by connecting. This should cause
893N/A // the accept operation to fail.
893N/A InetAddress lh = InetAddress.getLocalHost();
893N/A int port = ((InetSocketAddress)listener.getLocalAddress()).getPort();
893N/A InetSocketAddress isa = new InetSocketAddress(lh, port);
893N/A ch.connect(isa).get();
893N/A try {
893N/A result.get();
893N/A throw new RuntimeException("Connection was accepted");
893N/A } catch (ExecutionException x) {
893N/A Throwable cause = x.getCause();
893N/A if (!(cause instanceof IOException))
893N/A throw new RuntimeException("Cause should be IOException");
893N/A cause = cause.getCause();
893N/A if (!(cause instanceof ShutdownChannelGroupException))
893N/A throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");
893N/A }
893N/A
893N/A // initiate another accept even though channel group is shutdown.
893N/A Future<AsynchronousSocketChannel> res = listener.accept();
893N/A try {
893N/A res.get(3, TimeUnit.SECONDS);
893N/A throw new RuntimeException("TimeoutException expected");
893N/A } catch (TimeoutException x) {
893N/A }
893N/A // connect to the listener which should cause the accept to complete
893N/A AsynchronousSocketChannel.open().connect(isa);
893N/A try {
893N/A res.get();
893N/A throw new RuntimeException("Connection was accepted");
893N/A } catch (ExecutionException x) {
893N/A Throwable cause = x.getCause();
893N/A if (!(cause instanceof IOException))
893N/A throw new RuntimeException("Cause should be IOException");
893N/A cause = cause.getCause();
893N/A if (!(cause instanceof ShutdownChannelGroupException))
893N/A throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");
893N/A }
893N/A
893N/A // group should *not* terminate as channels are open
893N/A boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
893N/A if (terminated)
893N/A throw new RuntimeException("Group should not have terminated");
893N/A
893N/A // close channel; group should terminate quickly
893N/A ch.close();
893N/A listener.close();
893N/A terminated = group.awaitTermination(3, TimeUnit.SECONDS);
893N/A if (!terminated)
893N/A throw new RuntimeException("Group should have terminated");
893N/A }
893N/A
893N/A static void miscTests() throws Exception {
893N/A System.out.println("-- miscellenous tests --");
893N/A try {
893N/A AsynchronousChannelGroup.withFixedThreadPool(1, null);
893N/A throw new RuntimeException("NPE expected");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A try {
893N/A AsynchronousChannelGroup.withFixedThreadPool(0, threadFactory);
893N/A throw new RuntimeException("IAE expected");
893N/A } catch (IllegalArgumentException e) {
893N/A }
893N/A try {
893N/A AsynchronousChannelGroup.withCachedThreadPool(null, 0);
893N/A throw new RuntimeException("NPE expected");
893N/A } catch (NullPointerException x) {
893N/A }
893N/A }
893N/A}