jdk/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
child 42322 c3474fef4fe4
permissions -rw-r--r--
8150523: improve jtreg test timeout handling, especially -timeout: Reviewed-by: martin, psandoz, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22952
diff changeset
     2
 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6450200 6450205 6450207 6450211
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test proper handling of tasks that terminate abruptly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    31
import java.security.Permission;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    32
import java.util.Arrays;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    33
import java.util.ArrayList;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    34
import java.util.Collections;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    35
import java.util.Hashtable;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    36
import java.util.List;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    37
import java.util.Map;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    38
import java.util.Random;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    39
import java.util.concurrent.ConcurrentHashMap;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    40
import java.util.concurrent.CountDownLatch;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    41
import java.util.concurrent.LinkedBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    42
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    43
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    44
import java.util.concurrent.TimeUnit;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    45
import java.util.concurrent.atomic.AtomicInteger;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    46
import java.util.concurrent.atomic.AtomicLong;
21605
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
    47
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ThrowingTasks {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    50
    static final Random rnd = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static class UncaughtExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        extends ConcurrentHashMap<Class<?>, Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        void inc(Class<?> key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                Integer i = get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (i == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    if (putIfAbsent(key, 1) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    if (replace(key, i, i + 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static class UncaughtExceptionsTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        extends Hashtable<Class<?>, Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        synchronized void inc(Class<?> key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Integer i = get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            put(key, (i == null) ? 1 : i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    80
    static final UncaughtExceptions uncaughtExceptions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        = new UncaughtExceptions();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    82
    static final UncaughtExceptionsTable uncaughtExceptionsTable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        = new UncaughtExceptionsTable();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    84
    static final AtomicLong totalUncaughtExceptions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        = new AtomicLong(0);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    86
    static final CountDownLatch uncaughtExceptionsLatch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        = new CountDownLatch(24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    89
    static final Thread.UncaughtExceptionHandler handler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        = new Thread.UncaughtExceptionHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                public void uncaughtException(Thread t, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    check(! Thread.currentThread().isInterrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    totalUncaughtExceptions.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    uncaughtExceptions.inc(e.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    uncaughtExceptionsTable.inc(e.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    uncaughtExceptionsLatch.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    99
    static final ThreadGroup tg = new ThreadGroup("Flaky");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   101
    static final ThreadFactory tf = new ThreadFactory() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                Thread t = new Thread(tg, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                t.setUncaughtExceptionHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   108
    static final RuntimeException rte = new RuntimeException();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   109
    static final Error error = new Error();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   110
    static final Throwable weird = new Throwable();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   111
    static final Exception checkedException = new Exception();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static class Thrower implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        Throwable t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Thrower(Throwable t) { this.t = t; }
18160
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   116
        public void run() {
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   117
            if (t != null)
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   118
                ThrowingTasks.<RuntimeException>uncheckedThrow(t);
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   119
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   122
    static final Thrower noThrower      = new Thrower(null);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   123
    static final Thrower rteThrower     = new Thrower(rte);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   124
    static final Thrower errorThrower   = new Thrower(error);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   125
    static final Thrower weirdThrower   = new Thrower(weird);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   126
    static final Thrower checkedThrower = new Thrower(checkedException);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   128
    static final List<Thrower> throwers = Arrays.asList(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static class Flaky implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        final Runnable beforeExecute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        final Runnable execute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Flaky(Runnable beforeExecute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
              Runnable execute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            this.beforeExecute = beforeExecute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            this.execute = execute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public void run() { execute.run(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final List<Flaky> flakes = new ArrayList<Flaky>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (Thrower x : throwers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            for (Thrower y : throwers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                flakes.add(new Flaky(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Collections.shuffle(flakes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static final CountDownLatch allStarted = new CountDownLatch(flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static final CountDownLatch allContinue = new CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static class PermissiveSecurityManger extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static void checkTerminated(ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            check(tpe.getQueue().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            check(tpe.isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            check(tpe.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            check(! tpe.isTerminating());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            equal(tpe.getActiveCount(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            equal(tpe.getPoolSize(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
   166
            check(tpe.awaitTermination(0L, TimeUnit.SECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static class CheckingExecutor extends ThreadPoolExecutor {
21605
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   171
        private final ReentrantLock lock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        CheckingExecutor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            super(10, 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                  1L, TimeUnit.HOURS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                  new LinkedBlockingQueue<Runnable>(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                  tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        @Override protected void beforeExecute(Thread t, Runnable r) {
21605
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   179
            final boolean lessThanCorePoolSize;
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   180
            // Add a lock to sync allStarted.countDown() and
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   181
            // allStarted.getCount() < getCorePoolSize()
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   182
            lock.lock();
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   183
            try {
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   184
                allStarted.countDown();
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   185
                lessThanCorePoolSize = allStarted.getCount() < getCorePoolSize();
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   186
            } finally {
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   187
                lock.unlock();
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   188
            }
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   189
            if (lessThanCorePoolSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                try { allContinue.await(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                catch (InterruptedException x) { unexpected(x); }
21605
d32edf24ffcb 8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents: 18160
diff changeset
   192
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            beforeExecuteCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ((Flaky)r).beforeExecute.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        @Override protected void afterExecute(Runnable r, Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            //System.out.println(tg.activeCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            afterExecuteCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            check(((Thrower)((Flaky)r).execute).t == t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        @Override protected void terminated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                terminatedCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (rnd.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    check(isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    check(isTerminating());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    check(! awaitTermination(0L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static final AtomicInteger beforeExecuteCount = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    static final AtomicInteger afterExecuteCount  = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static final AtomicInteger terminatedCount    = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            System.setSecurityManager(new PermissiveSecurityManger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        CheckingExecutor tpe = new CheckingExecutor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (Runnable task : flakes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            tpe.execute(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (rnd.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            allStarted.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            equal(tpe.getTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                  (long) flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            equal(tpe.getCompletedTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                  (long) flakes.size() - tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        allContinue.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        //System.out.printf("thread count = %d%n", tg.activeCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        uncaughtExceptionsLatch.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        while (tg.activeCount() != tpe.getCorePoolSize() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
               tg.activeCount() != tpe.getCorePoolSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        equal(tg.activeCount(), tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //while (tg.activeCount() > 0) Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        //System.out.println(uncaughtExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        List<Map<Class<?>, Integer>> maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            = new ArrayList<Map<Class<?>, Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        maps.add(uncaughtExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        maps.add(uncaughtExceptionsTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (Map<Class<?>, Integer> map : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            equal(map.get(Exception.class), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            equal(map.get(weird.getClass()), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            equal(map.get(Error.class), throwers.size() + 1 + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            equal(map.get(RuntimeException.class), throwers.size() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            equal(map.size(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        equal(totalUncaughtExceptions.get(), 4L*throwers.size() + 4L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        equal(beforeExecuteCount.get(), flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        equal(afterExecuteCount.get(), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        equal(tpe.getCompletedTaskCount(), (long) flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        equal(terminatedCount.get(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // check for termination operation idempotence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        tpe.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        equal(terminatedCount.get(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (failed > 0) throw new AssertionError("Some tests failed");}
18160
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   293
    @SuppressWarnings("unchecked") static <T extends Throwable>
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   294
        void uncheckedThrow(Throwable t) throws T {
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   295
        throw (T)t; // rely on vacuous cast
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 7668
diff changeset
   296
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}