Path: blob/master/test/jdk/java/nio/channels/AsynchronousChannelGroup/Restart.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 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);69try {70testRestart(group, 100);71} finally {72group.shutdown();73}7475// group with cached thread pool76ExecutorService pool = Executors.newCachedThreadPool(factory);77group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));78try {79testRestart(group, 100);80} finally {81group.shutdown();82}8384// group with custom thread pool85group = AsynchronousChannelGroup.withThreadPool(86Executors.newFixedThreadPool(1+rand.nextInt(5), factory));87try {88testRestart(group, 100);89} finally {90group.shutdown();91}9293// give time for threads to terminate94Thread.sleep(3000);95int actual = exceptionCount.get();96if (actual != 300)97throw new RuntimeException(actual + " exceptions, expected: " + 300);98}99100static void testRestart(AsynchronousChannelGroup group, int count)101throws Exception102{103try (AsynchronousServerSocketChannel listener =104AsynchronousServerSocketChannel.open(group)) {105106listener.bind(new InetSocketAddress(0));107for (int i=0; i<count; i++) {108final CountDownLatch latch = new CountDownLatch(1);109110listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {111public void completed(AsynchronousSocketChannel ch, Void att) {112try {113ch.close();114} catch (IOException ignore) { }115116latch.countDown();117118// throw error or runtime exception119if (rand.nextBoolean()) {120throw new Error();121} else {122throw new RuntimeException();123}124}125public void failed(Throwable exc, Void att) {126}127});128129// establish loopback connection which should cause completion130// handler to be invoked.131int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();132try (AsynchronousSocketChannel ch = AsynchronousSocketChannel.open()) {133InetAddress lh = InetAddress.getLocalHost();134ch.connect(new InetSocketAddress(lh, port)).get();135}136137// wait for handler to be invoked138latch.await();139}140}141}142}143144145