jdk/test/java/util/concurrent/ThreadPoolExecutor/ConfigChanges.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34516 3bcd03288484
child 40816 9756dfcba32e
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
/*
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
     2
 * Copyright (c) 2007, 2015, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test proper handling of pool state changes
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    28
 * @library /lib/testlibrary/
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    29
 * @build jdk.testlibrary.RandomFactory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main/othervm ConfigChanges
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    31
 * @key randomness intermittent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    35
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    36
import static java.util.concurrent.TimeUnit.MINUTES;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    37
import static java.util.concurrent.TimeUnit.SECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    38
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    39
import java.security.Permission;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    40
import java.util.Random;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    41
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    42
import java.util.concurrent.CyclicBarrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    43
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    44
import java.util.concurrent.RejectedExecutionException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    45
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    46
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
    47
import java.util.concurrent.atomic.AtomicInteger;
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    48
import jdk.testlibrary.RandomFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class ConfigChanges {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    51
    static final ThreadGroup tg = new ThreadGroup("pool");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
34516
3bcd03288484 8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents: 34347
diff changeset
    53
    static final Random rnd = RandomFactory.getRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void report(ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.printf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                "active=%d submitted=%d completed=%d queued=%d sizes=%d/%d/%d%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                tg.activeCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                tpe.getTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                tpe.getCompletedTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                tpe.getQueue().size(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                tpe.getPoolSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                tpe.getCorePoolSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void report(String label, ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.printf("%10s ", label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        report(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static class PermissiveSecurityManger extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static void checkShutdown(final ExecutorService es) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        final Runnable nop = new Runnable() {public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (new Random().nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                check(es.isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (es instanceof ThreadPoolExecutor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    check(((ThreadPoolExecutor) es).isTerminating()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                          || es.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                THROWS(RejectedExecutionException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
    87
                       () -> es.execute(nop));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void checkTerminated(final ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            checkShutdown(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            check(tpe.getQueue().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            check(tpe.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            check(! tpe.isTerminating());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            equal(tpe.getActiveCount(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            equal(tpe.getPoolSize(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 24692
diff changeset
   101
            check(tpe.awaitTermination(0L, SECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static Runnable waiter(final CyclicBarrier barrier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new Runnable() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try { barrier.await(); barrier.await(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            catch (Throwable t) { unexpected(t); }}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static volatile Runnable runnableDuJour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.setSecurityManager(new PermissiveSecurityManger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        final boolean prestart = rnd.nextBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        final Thread.UncaughtExceptionHandler handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            = new Thread.UncaughtExceptionHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    public void uncaughtException(Thread t, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        check(! Thread.currentThread().isInterrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        unexpected(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        final int n = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        final ThreadPoolExecutor tpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            = new ThreadPoolExecutor(n, 3*n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                     3L, MINUTES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                     new ArrayBlockingQueue<Runnable>(3*n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        tpe.setThreadFactory(new ThreadFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    Thread t = new Thread(tg, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    t.setUncaughtExceptionHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (prestart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            tpe.prestartAllCoreThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            equal(tg.activeCount(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            equal(tg.activeCount(), tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        final Runnable runRunnableDuJour =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            new Runnable() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // Delay choice of action till last possible moment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                runnableDuJour.run(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        final CyclicBarrier pumpedUp = new CyclicBarrier(3*n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        runnableDuJour = waiter(pumpedUp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (prestart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            for (int i = 0; i < 1*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // Wait for prestarted threads to dequeue their initial tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            while (! tpe.getQueue().isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            for (int i = 0; i < 5*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            for (int i = 0; i < 6*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //report("submitted", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        pumpedUp.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        equal(tg.activeCount(), 3*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        equal(tg.activeCount(), tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        equal(tpe.getCorePoolSize(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //report("pumped up", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        equal(tpe.getMaximumPoolSize(), 3*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        tpe.setMaximumPoolSize(4*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        equal(tpe.getMaximumPoolSize(), 4*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //report("pumped up2", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        final CyclicBarrier pumpedUp2 = new CyclicBarrier(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        runnableDuJour = waiter(pumpedUp2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < 1*n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            tpe.execute(runRunnableDuJour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        pumpedUp2.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        equal(tg.activeCount(), 4*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        equal(tg.activeCount(), tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        equal(tpe.getCompletedTaskCount(), 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        //report("pumped up2", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        runnableDuJour = new Runnable() { public void run() {}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        tpe.setMaximumPoolSize(2*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        //report("after set", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        pumpedUp2.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        pumpedUp.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
//      while (tg.activeCount() != n &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
//             tg.activeCount() != n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
//          Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
//      equal(tg.activeCount(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
//      equal(tg.activeCount(), tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        while (tg.activeCount() != 2*n &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
               tg.activeCount() != 2*n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        equal(tg.activeCount(), 2*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        equal(tg.activeCount(), tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
//report("draining", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        while (tpe.getCompletedTaskCount() < 7*n &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
               tpe.getCompletedTaskCount() < 7*n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        //equal(tg.activeCount(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        //equal(tg.activeCount(), tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        equal(tg.activeCount(), 2*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        equal(tg.activeCount(), tpe.getMaximumPoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        equal(tpe.getTaskCount(), 7L*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        equal(tpe.getCompletedTaskCount(), 7L*n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        equal(tpe.getKeepAliveTime(MINUTES), 3L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        tpe.setKeepAliveTime(7L, MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        equal(tpe.getKeepAliveTime(MILLISECONDS), 7L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        while (tg.activeCount() > n &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
               tg.activeCount() > n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        equal(tg.activeCount(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        //report("idle", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        check(! tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        tpe.allowCoreThreadTimeOut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        check(tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (tg.activeCount() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
               tg.activeCount() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        equal(tg.activeCount(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        //report("idle", tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        checkShutdown(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        check(tpe.awaitTermination(3L, MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   256
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}