Restart.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4607272 6842687
26 * @summary Unit test for AsynchronousChannelGroup
27 * @build Restart
28 * @run main/othervm -XX:-UseVMInterruptibleIO Restart
29 */
30
31import java.nio.channels.*;
32import java.net.*;
33import java.util.*;
34import java.util.concurrent.*;
35import java.util.concurrent.atomic.*;
36import java.io.IOException;
37
38/**
39 * Exercise replacement of threads in the thread pool when completion handlers
40 * terminate due to errors or runtime exceptions.
41 */
42
43public class Restart {
44    static final Random rand = new Random();
45
46    public static void main(String[] args) throws Exception {
47        // thread group for thread pools
48        final ThreadGroup tg = new ThreadGroup("test");
49
50        // keep track of the number of threads that terminate
51        final AtomicInteger exceptionCount = new AtomicInteger(0);
52        final Thread.UncaughtExceptionHandler ueh =
53            new Thread.UncaughtExceptionHandler() {
54                public void uncaughtException(Thread t, Throwable e) {
55                    exceptionCount.incrementAndGet();
56                }
57            };
58        ThreadFactory factory = new ThreadFactory() {
59            @Override
60            public Thread newThread(Runnable r) {
61                Thread t = new Thread(tg, r);
62                t.setUncaughtExceptionHandler(ueh);
63                return t;
64            }
65        };
66
67        // group with fixed thread pool
68        int nThreads = 1 + rand.nextInt(4);
69        AsynchronousChannelGroup group =
70            AsynchronousChannelGroup.withFixedThreadPool(nThreads, factory);
71        testRestart(group, 100);
72        group.shutdown();
73
74        // group with cached thread pool
75        ExecutorService pool = Executors.newCachedThreadPool(factory);
76        group = AsynchronousChannelGroup.withCachedThreadPool(pool, rand.nextInt(5));
77        testRestart(group, 100);
78        group.shutdown();
79
80        // group with custom thread pool
81        group = AsynchronousChannelGroup
82                .withThreadPool(Executors.newFixedThreadPool(1+rand.nextInt(5), factory));
83        testRestart(group, 100);
84        group.shutdown();
85
86        // give time for threads to terminate
87        Thread.sleep(3000);
88        int actual = exceptionCount.get();
89        if (actual != 300)
90            throw new RuntimeException(actual + " exceptions, expected: " + 300);
91    }
92
93    static void testRestart(AsynchronousChannelGroup group, int count)
94        throws Exception
95    {
96        AsynchronousServerSocketChannel listener =
97            AsynchronousServerSocketChannel.open(group)
98                .bind(new InetSocketAddress(0));
99
100        for (int i=0; i<count; i++) {
101            final CountDownLatch latch = new CountDownLatch(1);
102
103            listener.accept((Void)null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
104                public void completed(AsynchronousSocketChannel ch, Void att) {
105                    try {
106                        ch.close();
107                    } catch (IOException ignore) { }
108
109                    latch.countDown();
110
111                    // throw error or runtime exception
112                    if (rand.nextBoolean()) {
113                        throw new Error();
114                    } else {
115                        throw new RuntimeException();
116                    }
117                }
118                public void failed(Throwable exc, Void att) {
119                }
120            });
121
122            // establish loopback connection which should cause completion
123            // handler to be invoked.
124            int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
125            AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
126            InetAddress lh = InetAddress.getLocalHost();
127            ch.connect(new InetSocketAddress(lh, port)).get();
128            ch.close();
129
130            // wait for handler to be invoked
131            latch.await();
132        }
133
134        // clean-up
135        listener.close();
136    }
137}
138