jdk/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
child 41129 e54fb9880260
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
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7976
diff changeset
     2
 * Copyright (c) 2005, 2011, 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 6233235 6268386
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test allowsCoreThreadTimeOut
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    28
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    32
import static java.util.concurrent.TimeUnit.MILLISECONDS;
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    33
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    34
import java.util.concurrent.ArrayBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    35
import java.util.concurrent.BlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    36
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    37
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    38
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    39
import java.util.concurrent.TimeUnit;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    40
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class CoreThreadTimeOut {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    43
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    44
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    45
    static class IdentifiableThreadFactory implements ThreadFactory {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    46
        static ThreadFactory defaultThreadFactory
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    47
            = Executors.defaultThreadFactory();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    49
        public Thread newThread(Runnable r) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    50
            Thread t = defaultThreadFactory.newThread(r);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    51
            t.setName("CoreThreadTimeOut-" + t.getName());
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    52
            return t;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    53
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    54
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    55
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    56
    int countExecutorThreads() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Thread[] threads = new Thread[Thread.activeCount()+100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Thread.enumerate(threads);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (Thread t : threads)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    61
            if (t != null &&
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    62
                t.getName().matches
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    63
                ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+"))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    68
    static long millisElapsedSince(long startTime) {
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    69
        return (System.nanoTime() - startTime) / (1000L * 1000L);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    70
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    71
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    72
    void test(String[] args) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        final int threadCount = 10;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    74
        final int timeoutMillis = 30;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        BlockingQueue<Runnable> q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            = new ArrayBlockingQueue<Runnable>(2*threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ThreadPoolExecutor tpe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            = new ThreadPoolExecutor(threadCount, threadCount,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    79
                                     timeoutMillis, TimeUnit.MILLISECONDS,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    80
                                     q, new IdentifiableThreadFactory());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        equal(tpe.getCorePoolSize(), threadCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        check(! tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        tpe.allowCoreThreadTimeOut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        check(tpe.allowsCoreThreadTimeOut());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        equal(countExecutorThreads(), 0);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    86
        long t0 = System.nanoTime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i = 0; i < threadCount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            tpe.submit(new Runnable() { public void run() {}});
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    89
        int count = countExecutorThreads();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    90
        if (millisElapsedSince(t0) < timeoutMillis)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    91
            equal(count, threadCount);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    92
        while (countExecutorThreads() > 0 &&
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
    93
               millisElapsedSince(t0) < 10 * 1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        equal(countExecutorThreads(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        check(tpe.allowsCoreThreadTimeOut());
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    97
        check(tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (failed > 0) throw new Exception("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   102
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   103
    //--------------------- Infrastructure ---------------------------
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   104
    volatile int passed = 0, failed = 0;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   105
    void pass() {passed++;}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   106
    void fail() {failed++; Thread.dumpStack();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   107
    void fail(String msg) {System.err.println(msg); fail();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   108
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   109
    void check(boolean cond) {if (cond) pass(); else fail();}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   110
    void equal(Object x, Object y) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   111
        if (x == null ? y == null : x.equals(y)) pass();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   112
        else fail(x + " not equal to " + y);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   113
    public static void main(String[] args) throws Throwable {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   114
        new CoreThreadTimeOut().instanceMain(args);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   115
    public void instanceMain(String[] args) throws Throwable {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   116
        try {test(args);} catch (Throwable t) {unexpected(t);}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   117
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   118
        if (failed > 0) throw new AssertionError("Some tests failed");}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}