Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousChannelGroup/Basic.java
38821 views
/*1* Copyright (c) 2008, 2012, 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));121testShutdownWithChannels(pool, group);122}123for (int i = 0; i < 100; i++) {124int nThreads = 1 + rand.nextInt(8);125AsynchronousChannelGroup group = AsynchronousChannelGroup126.withFixedThreadPool(nThreads, threadFactory);127testShutdownWithChannels(null, group);128}129for (int i = 0; i < 100; i++) {130ExecutorService pool = Executors.newCachedThreadPool();131AsynchronousChannelGroup group = AsynchronousChannelGroup132.withThreadPool(pool);133testShutdownWithChannels(pool, group);134}135}136137static void testShutdownNow(ExecutorService pool,138AsynchronousChannelGroup group)139throws Exception140{141// I/O in progress142AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel143.open(group).bind(new InetSocketAddress(0));144ch.accept();145146// forceful shutdown147group.shutdownNow();148149// shutdownNow is required to close all channels150if (ch.isOpen())151throw new RuntimeException("Channel should be closed");152153awaitTermination(group);154155if (pool != null && !pool.isTerminated())156throw new RuntimeException("Executor should have terminated");157}158159static void shutdownNowTests() throws Exception {160System.out.println("-- test shutdownNow --");161162for (int i = 0; i < 10; i++) {163ExecutorService pool = pool = Executors.newCachedThreadPool();164AsynchronousChannelGroup group = AsynchronousChannelGroup165.withCachedThreadPool(pool, rand.nextInt(5));166testShutdownNow(pool, group);167}168for (int i = 0; i < 10; i++) {169int nThreads = 1 + rand.nextInt(8);170AsynchronousChannelGroup group = AsynchronousChannelGroup171.withFixedThreadPool(nThreads, threadFactory);172testShutdownNow(null, group);173}174for (int i = 0; i < 10; i++) {175ExecutorService pool = Executors.newCachedThreadPool();176AsynchronousChannelGroup group = AsynchronousChannelGroup177.withThreadPool(pool);178testShutdownNow(pool, group);179}180}181182// test creating channels in group after group is shutdown183static void afterShutdownTests() throws Exception {184System.out.println("-- test operations after group is shutdown --");185AsynchronousChannelGroup group =186AsynchronousChannelGroup.withFixedThreadPool(1, threadFactory);187188AsynchronousSocketChannel ch = AsynchronousSocketChannel.open(group);189AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open(group);190191// initiate accept192listener.bind(new InetSocketAddress(0));193Future<AsynchronousSocketChannel> result = listener.accept();194195// shutdown group196group.shutdown();197if (!group.isShutdown())198throw new RuntimeException("Group should be shutdown");199200// attempt to create another channel201try {202AsynchronousSocketChannel.open(group);203throw new RuntimeException("ShutdownChannelGroupException expected");204} catch (ShutdownChannelGroupException x) {205}206try {207AsynchronousServerSocketChannel.open(group);208throw new RuntimeException("ShutdownChannelGroupException expected");209} catch (ShutdownChannelGroupException x) {210}211212// attempt to create another channel by connecting. This should cause213// the accept operation to fail.214InetAddress lh = InetAddress.getLocalHost();215int port = ((InetSocketAddress)listener.getLocalAddress()).getPort();216InetSocketAddress isa = new InetSocketAddress(lh, port);217ch.connect(isa).get();218try {219result.get();220throw new RuntimeException("Connection was accepted");221} catch (ExecutionException x) {222Throwable cause = x.getCause();223if (!(cause instanceof IOException))224throw new RuntimeException("Cause should be IOException");225cause = cause.getCause();226if (!(cause instanceof ShutdownChannelGroupException))227throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");228}229230// initiate another accept even though channel group is shutdown.231Future<AsynchronousSocketChannel> res = listener.accept();232try {233res.get(3, TimeUnit.SECONDS);234throw new RuntimeException("TimeoutException expected");235} catch (TimeoutException x) {236}237// connect to the listener which should cause the accept to complete238AsynchronousSocketChannel.open().connect(isa);239try {240res.get();241throw new RuntimeException("Connection was accepted");242} catch (ExecutionException x) {243Throwable cause = x.getCause();244if (!(cause instanceof IOException))245throw new RuntimeException("Cause should be IOException");246cause = cause.getCause();247if (!(cause instanceof ShutdownChannelGroupException))248throw new RuntimeException("IOException cause should be ShutdownChannelGroupException");249}250251// group should *not* terminate as channels are open252boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);253if (terminated)254throw new RuntimeException("Group should not have terminated");255256// close channel; group should terminate quickly257ch.close();258listener.close();259awaitTermination(group);260}261262static void miscTests() throws Exception {263System.out.println("-- miscellenous tests --");264try {265AsynchronousChannelGroup.withFixedThreadPool(1, null);266throw new RuntimeException("NPE expected");267} catch (NullPointerException x) {268}269try {270AsynchronousChannelGroup.withFixedThreadPool(0, threadFactory);271throw new RuntimeException("IAE expected");272} catch (IllegalArgumentException e) {273}274try {275AsynchronousChannelGroup.withCachedThreadPool(null, 0);276throw new RuntimeException("NPE expected");277} catch (NullPointerException x) {278}279try {280AsynchronousChannelGroup.withThreadPool(null);281throw new RuntimeException("NPE expected");282} catch (NullPointerException e) {283}284}285}286287288