Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/AsynchronousChannelGroup/Restart.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 4607272 684268725* @summary Unit test for AsynchronousChannelGroup26* @key randomness27*/2829import java.nio.channels.*;30import java.net.*;31import java.util.*;32import java.util.concurrent.*;33import java.util.concurrent.atomic.*;34import java.io.IOException;3536/**37* Exercise replacement of threads in the thread pool when completion handlers38* terminate due to errors or runtime exceptions.39*/4041public class Restart {42static final Random rand = new Random();4344public static void main(String[] args) throws Exception {45// thread group for thread pools46final ThreadGroup tg = new ThreadGroup("test");4748// keep track of the number of threads that terminate49final AtomicInteger exceptionCount = new AtomicInteger(0);50final Thread.UncaughtExceptionHandler ueh =51new Thread.UncaughtExceptionHandler() {52public void uncaughtException(Thread t, Throwable e) {53exceptionCount.incrementAndGet();54}55};56ThreadFactory factory = new ThreadFactory() {57@Override58public Thread newThread(Runnable r) {59Thread t = new Thread(tg, r);60t.setUncaughtExceptionHandler(ueh);61return t;62}63};6465// group with fixed thread pool66int nThreads = 1 + rand.nextInt(4);67AsynchronousChannelGroup group =68AsynchronousChannelGroup.withFixedThreadPool(nThreads, factory);69testRestart(group, 100);70group.shutdown();7172// group with cached thread pool73ExecutorService pool = Executors.newCachedThreadPool(factory);74group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));75testRestart(group, 100);76group.shutdown();7778// group with custom thread pool79group = AsynchronousChannelGroup80.withThreadPool(Executors.newFixedThreadPool(1+rand.nextInt(5), factory));81testRestart(group, 100);82group.shutdown();8384// give time for threads to terminate85Thread.sleep(3000);86int actual = exceptionCount.get();87if (actual != 300)88throw new RuntimeException(actual + " exceptions, expected: " + 300);89}9091static void testRestart(AsynchronousChannelGroup group, int count)92throws Exception93{94AsynchronousServerSocketChannel listener =95AsynchronousServerSocketChannel.open(group)96.bind(new InetSocketAddress(0));9798for (int i=0; i<count; i++) {99final CountDownLatch latch = new CountDownLatch(1);100101listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {102public void completed(AsynchronousSocketChannel ch, Void att) {103try {104ch.close();105} catch (IOException ignore) { }106107latch.countDown();108109// throw error or runtime exception110if (rand.nextBoolean()) {111throw new Error();112} else {113throw new RuntimeException();114}115}116public void failed(Throwable exc, Void att) {117}118});119120// establish loopback connection which should cause completion121// handler to be invoked.122int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();123AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();124InetAddress lh = InetAddress.getLocalHost();125ch.connect(new InetSocketAddress(lh, port)).get();126ch.close();127128// wait for handler to be invoked129latch.await();130}131132// clean-up133listener.close();134}135}136137138