Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/Basic.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 460727225* @summary Unit test for AsynchronousChannelGroup26* @key randomness27*/2829import java.nio.ByteBuffer;30import java.nio.channels.*;31import java.net.*;32import java.util.*;33import java.util.concurrent.*;34import java.io.IOException;3536public class Basic {37static final Random rand = new Random();38static final ThreadFactory threadFactory = (Runnable r) -> {39return new Thread(r);40};4142public static void main(String[] args) throws Exception {43shutdownTests();44shutdownNowTests();45afterShutdownTests();46miscTests();47}4849static void awaitTermination(AsynchronousChannelGroup group) throws InterruptedException {50boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);51if (!terminated)52throw new RuntimeException("Group should have terminated");53}5455static void testShutdownWithNoChannels(ExecutorService pool,56AsynchronousChannelGroup group)57throws Exception58{59group.shutdown();60if (!group.isShutdown())61throw new RuntimeException("Group should be shutdown");62// group should terminate quickly63awaitTermination(group);64if (pool != null && !pool.isTerminated())65throw new RuntimeException("Executor should have terminated");66}6768static void testShutdownWithChannels(ExecutorService pool,69AsynchronousChannelGroup group)70throws Exception71{7273// create channel that is bound to group74AsynchronousChannel ch;75switch (rand.nextInt(2)) {76case 0 : ch = AsynchronousSocketChannel.open(group); break;77case 1 : ch = AsynchronousServerSocketChannel.open(group); break;78default : throw new AssertionError();79}80group.shutdown();81if (!group.isShutdown())82throw new RuntimeException("Group should be shutdown");8384// last channel so should terminate after this channel is closed85ch.close();8687// group should terminate quickly88awaitTermination(group);89if (pool != null && !pool.isTerminated())90throw new RuntimeException("Executor should have terminated");91}9293static void shutdownTests() throws Exception {94System.out.println("-- test shutdown --");9596// test shutdown with no channels in groups97for (int i = 0; i < 100; i++) {98ExecutorService pool = Executors.newCachedThreadPool();99AsynchronousChannelGroup group = AsynchronousChannelGroup100.withCachedThreadPool(pool, rand.nextInt(5));101testShutdownWithNoChannels(pool, group);102}103for (int i = 0; i < 100; i++) {104int nThreads = 1 + rand.nextInt(8);105AsynchronousChannelGroup group = AsynchronousChannelGroup106.withFixedThreadPool(nThreads, threadFactory);107testShutdownWithNoChannels(null, group);108}109for (int i = 0; i < 100; i++) {110ExecutorService pool = Executors.newCachedThreadPool();111AsynchronousChannelGroup group = AsynchronousChannelGroup112.withThreadPool(pool);113testShutdownWithNoChannels(pool, group);114}115116// test shutdown with channel in group117for (int i = 0; i < 100; i++) {118ExecutorService pool = Executors.newCachedThreadPool();119AsynchronousChannelGroup group = AsynchronousChannelGroup120.withCachedThreadPool(pool, rand.nextInt(10));121try {122testShutdownWithChannels(pool, group);123} finally {124group.shutdown();125}126}127for (int i = 0; i < 100; i++) {128int nThreads = 1 + rand.nextInt(8);129AsynchronousChannelGroup group = AsynchronousChannelGroup130.withFixedThreadPool(nThreads, threadFactory);131try {132testShutdownWithChannels(null, group);133} finally {134group.shutdown();135}136}137for (int i = 0; i < 100; i++) {138ExecutorService pool = Executors.newCachedThreadPool();139AsynchronousChannelGroup group = AsynchronousChannelGroup140.withThreadPool(pool);141try {142testShutdownWithChannels(pool, group);143} finally {144group.shutdown();145}146}147}148149static void testShutdownNow(ExecutorService pool,150AsynchronousChannelGroup group)151throws Exception152{153// I/O in progress154AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel155.open(group).bind(new InetSocketAddress(0));156ch.accept();157158// forceful shutdown159group.shutdownNow();160161// shutdownNow is required to close all channels162if (ch.isOpen())163throw new RuntimeException("Channel should be closed");164165awaitTermination(group);166167if (pool != null && !pool.isTerminated())168throw new RuntimeException("Executor should have terminated");169}170171static void shutdownNowTests() throws Exception {172System.out.println("-- test shutdownNow --");173174for (int i = 0; i < 10; i++) {175ExecutorService pool = pool = Executors.newCachedThreadPool();176AsynchronousChannelGroup group = AsynchronousChannelGroup177.withCachedThreadPool(pool, rand.nextInt(5));178try {179testShutdownNow(pool, group);180} finally {181group.shutdown();182}183}184for (int i = 0; i < 10; i++) {185int nThreads = 1 + rand.nextInt(8);186AsynchronousChannelGroup group = AsynchronousChannelGroup187.withFixedThreadPool(nThreads, threadFactory);188try {189testShutdownNow(null, group);190} finally {191group.shutdown();192}193}194for (int i = 0; i < 10; i++) {195ExecutorService pool = Executors.newCachedThreadPool();196AsynchronousChannelGroup group = AsynchronousChannelGroup197.withThreadPool(pool);198try {199testShutdownNow(pool, group);200} finally {201group.shutdown();202}203}204}205206// test creating channels in group after group is shutdown207static void afterShutdownTests() throws Exception {208System.out.println("-- test operations after group is shutdown --");209AsynchronousChannelGroup group =210AsynchronousChannelGroup.withFixedThreadPool(1, threadFactory);211212try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);213AsynchronousServerSocketChannel listener =214AsynchronousServerSocketChannel.open(group)) {215216// initiate accept217listener.bind(new InetSocketAddress(0));218Future<AsynchronousSocketChannel> result = listener.accept();219220// shutdown group221group.shutdown();222if (!group.isShutdown())223throw new RuntimeException("Group should be shutdown");224225// attempt to create another channel226try {227AsynchronousSocketChannel.open(group);228throw new RuntimeException("ShutdownChannelGroupException expected");229} catch (ShutdownChannelGroupException x) {230}231try {232AsynchronousServerSocketChannel.open(group);233throw new RuntimeException("ShutdownChannelGroupException expected");234} catch (ShutdownChannelGroupException x) {235}236237// attempt to create another channel by connecting. This should cause238// the accept operation to fail.239InetAddress lh = InetAddress.getLocalHost();240int port = ((InetSocketAddress)listener.getLocalAddress()).getPort();241InetSocketAddress isa = new InetSocketAddress(lh, port);242ch.connect(isa).get();243try {244result.get();245throw new RuntimeException("Connection was accepted");246} catch (ExecutionException x) {247Throwable cause = x.getCause();248if (!(cause instanceof IOException))249throw new RuntimeException("Cause should be IOException");250cause = cause.getCause();251if (!(cause instanceof ShutdownChannelGroupException))252throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");253}254255// initiate another accept even though channel group is shutdown.256Future<AsynchronousSocketChannel> res = listener.accept();257try {258res.get(3, TimeUnit.SECONDS);259throw new RuntimeException("TimeoutException expected");260} catch (TimeoutException x) {261}262// connect to the listener which should cause the accept to complete263AsynchronousSocketChannel.open().connect(isa);264try {265res.get();266throw new RuntimeException("Connection was accepted");267} catch (ExecutionException x) {268Throwable cause = x.getCause();269if (!(cause instanceof IOException))270throw new RuntimeException("Cause should be IOException");271cause = cause.getCause();272if (!(cause instanceof ShutdownChannelGroupException))273throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");274}275276// group should *not* terminate as channels are open277boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);278if (terminated) {279throw new RuntimeException("Group should not have terminated");280}281} finally {282group.shutdown();283}284}285286static void miscTests() throws Exception {287System.out.println("-- miscellenous tests --");288try {289AsynchronousChannelGroup.withFixedThreadPool(1, null);290throw new RuntimeException("NPE expected");291} catch (NullPointerException x) {292}293try {294AsynchronousChannelGroup.withFixedThreadPool(0, threadFactory);295throw new RuntimeException("IAE expected");296} catch (IllegalArgumentException e) {297}298try {299AsynchronousChannelGroup.withCachedThreadPool(null, 0);300throw new RuntimeException("NPE expected");301} catch (NullPointerException x) {302}303try {304AsynchronousChannelGroup.withThreadPool(null);305throw new RuntimeException("NPE expected");306} catch (NullPointerException e) {307}308}309}310311312