jdk/test/java/util/concurrent/ThreadPoolExecutor/FlakyThreadFactory.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
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:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     1
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     3
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     7
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    12
 * accompanied this code).
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    13
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    17
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    20
 * questions.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    21
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    22
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    23
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    27
 * file:
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    28
 *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    29
 * Written by Martin Buchholz and Doug Lea with assistance from
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    31
 * domain, as explained at
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    33
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    34
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    35
/*
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    36
 * @test
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    37
 * @summary Should be able to shutdown a pool when worker creation failed.
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    38
 * @library /lib/testlibrary/
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    39
 */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    40
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    41
import static java.util.concurrent.TimeUnit.MILLISECONDS;
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    42
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    43
import java.util.concurrent.LinkedBlockingQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    44
import java.util.concurrent.ThreadFactory;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    45
import java.util.concurrent.ThreadPoolExecutor;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32991
diff changeset
    46
import java.util.concurrent.TimeUnit;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    47
import jdk.testlibrary.Utils;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    48
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    49
public class FlakyThreadFactory {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    50
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    51
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    52
    void test(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    53
        test(NullPointerException.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    54
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    55
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    56
                    throw new NullPointerException();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    57
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    58
        test(OutOfMemoryError.class,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    59
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    60
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    61
                    new Thread(null, r, "a natural OOME", 1L << 60);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    62
                    // """On some platforms, the value of the stackSize
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    63
                    // parameter may have no effect whatsoever."""
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    64
                    throw new OutOfMemoryError("artificial OOME");
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    65
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    66
        test(null,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    67
             new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    68
                public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    69
                    return null;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    70
                }});
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    71
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    72
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    73
    void test(final Class<?> exceptionClass,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    74
              final ThreadFactory failingThreadFactory)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    75
            throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    76
        ThreadFactory flakyThreadFactory = new ThreadFactory() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    77
            int seq = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    78
            public Thread newThread(Runnable r) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    79
                if (seq++ < 4)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    80
                    return new Thread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    81
                else
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    82
                    return failingThreadFactory.newThread(r);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    83
            }};
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    84
        ThreadPoolExecutor pool =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    85
            new ThreadPoolExecutor(10, 10,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    86
                                   0L, TimeUnit.SECONDS,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    87
                                   new LinkedBlockingQueue(),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    88
                                   flakyThreadFactory);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    89
        try {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    90
            for (int i = 0; i < 8; i++)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    91
                pool.submit(new Runnable() { public void run() {} });
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    92
            check(exceptionClass == null);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    93
        } catch (Throwable t) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    94
            /* t.printStackTrace(); */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    95
            check(exceptionClass.isInstance(t));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    96
        }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    97
        pool.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    98
        check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
    99
    }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   100
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   101
    //--------------------- Infrastructure ---------------------------
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   102
    volatile int passed = 0, failed = 0;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   103
    void pass() {passed++;}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   104
    void fail() {failed++; Thread.dumpStack();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   105
    void fail(String msg) {System.err.println(msg); fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   106
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   107
    void check(boolean cond) {if (cond) pass(); else fail();}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   108
    void equal(Object x, Object y) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   109
        if (x == null ? y == null : x.equals(y)) pass();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   110
        else fail(x + " not equal to " + y);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   111
    public static void main(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   112
        new FlakyThreadFactory().instanceMain(args);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   113
    public void instanceMain(String[] args) throws Throwable {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   114
        try {test(args);} catch (Throwable t) {unexpected(t);}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   115
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   116
        if (failed > 0) throw new AssertionError("Some tests failed");}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents:
diff changeset
   117
}