test/jdk/java/util/concurrent/tck/JSR166TestCase.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 54686 09f09b4e7808
child 58385 489532b89775
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     1
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     3
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     7
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    12
 * accompanied this code).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    13
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    17
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    20
 * questions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    21
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    22
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    23
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    27
 * file:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    28
 *
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    29
 * Written by Doug Lea and Martin Buchholz with assistance from
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    30
 * members of JCP JSR-166 Expert Group and released to the public
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    31
 * domain, as explained at
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
 * Other contributors include Andrew Wright, Jeffrey Hayes,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
 * Pat Fisher, Mike Judd.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    35
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
 * @test
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    39
 * @summary JSR-166 tck tests, in a number of variations.
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    40
 *          The first is the conformance testing variant,
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    41
 *          while others also test implementation details.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    42
 * @build *
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    43
 * @modules java.management
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    44
 * @run junit/othervm/timeout=1000 JSR166TestCase
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    45
 * @run junit/othervm/timeout=1000
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    46
 *      --add-opens java.base/java.util.concurrent=ALL-UNNAMED
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    47
 *      --add-opens java.base/java.lang=ALL-UNNAMED
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    48
 *      -Djsr166.testImplementationDetails=true
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    49
 *      JSR166TestCase
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    50
 * @run junit/othervm/timeout=1000
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    51
 *      --add-opens java.base/java.util.concurrent=ALL-UNNAMED
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    52
 *      --add-opens java.base/java.lang=ALL-UNNAMED
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    53
 *      -Djsr166.testImplementationDetails=true
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    54
 *      -Djava.util.concurrent.ForkJoinPool.common.parallelism=0
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    55
 *      JSR166TestCase
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    56
 * @run junit/othervm/timeout=1000
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    57
 *      --add-opens java.base/java.util.concurrent=ALL-UNNAMED
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    58
 *      --add-opens java.base/java.lang=ALL-UNNAMED
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    59
 *      -Djsr166.testImplementationDetails=true
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    60
 *      -Djava.util.concurrent.ForkJoinPool.common.parallelism=1
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    61
 *      -Djava.util.secureRandomSeed=true
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
    62
 *      JSR166TestCase
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
    63
 * @run junit/othervm/timeout=1000/policy=tck.policy
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    64
 *      --add-opens java.base/java.util.concurrent=ALL-UNNAMED
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
    65
 *      --add-opens java.base/java.lang=ALL-UNNAMED
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
    66
 *      -Djsr166.testImplementationDetails=true
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
    67
 *      JSR166TestCase
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
import static java.util.concurrent.TimeUnit.MILLISECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
import static java.util.concurrent.TimeUnit.MINUTES;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    72
import static java.util.concurrent.TimeUnit.NANOSECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
import java.io.ByteArrayInputStream;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
import java.io.ByteArrayOutputStream;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    76
import java.io.ObjectInputStream;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
import java.io.ObjectOutputStream;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
import java.lang.management.ManagementFactory;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
import java.lang.management.ThreadInfo;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    80
import java.lang.management.ThreadMXBean;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
import java.lang.reflect.Constructor;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
import java.lang.reflect.Method;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    83
import java.lang.reflect.Modifier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    84
import java.security.CodeSource;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    85
import java.security.Permission;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    86
import java.security.PermissionCollection;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
import java.security.Permissions;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    88
import java.security.Policy;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
import java.security.ProtectionDomain;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    90
import java.security.SecurityPermission;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
import java.util.ArrayList;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
import java.util.Arrays;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
    93
import java.util.Collection;
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
    94
import java.util.Collections;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
import java.util.Date;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
    96
import java.util.Deque;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    97
import java.util.Enumeration;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
    98
import java.util.HashSet;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
import java.util.Iterator;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   100
import java.util.List;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
import java.util.NoSuchElementException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
import java.util.PropertyPermission;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
   103
import java.util.Set;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
import java.util.concurrent.BlockingQueue;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
import java.util.concurrent.Callable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
import java.util.concurrent.CountDownLatch;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
import java.util.concurrent.CyclicBarrier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
import java.util.concurrent.ExecutionException;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   109
import java.util.concurrent.Executor;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
import java.util.concurrent.Executors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
import java.util.concurrent.ForkJoinPool;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
import java.util.concurrent.Future;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   114
import java.util.concurrent.FutureTask;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   115
import java.util.concurrent.RecursiveAction;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
import java.util.concurrent.RecursiveTask;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   117
import java.util.concurrent.RejectedExecutionException;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
import java.util.concurrent.RejectedExecutionHandler;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
import java.util.concurrent.Semaphore;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   120
import java.util.concurrent.ScheduledExecutorService;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   121
import java.util.concurrent.ScheduledFuture;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
   122
import java.util.concurrent.SynchronousQueue;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
import java.util.concurrent.ThreadFactory;
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
   124
import java.util.concurrent.ThreadLocalRandom;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
import java.util.concurrent.ThreadPoolExecutor;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   126
import java.util.concurrent.TimeUnit;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
import java.util.concurrent.TimeoutException;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   128
import java.util.concurrent.atomic.AtomicBoolean;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
import java.util.concurrent.atomic.AtomicReference;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
import java.util.regex.Pattern;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   131
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   132
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
import junit.framework.TestCase;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
import junit.framework.TestResult;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
/**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
 * Base class for JSR166 Junit TCK tests.  Defines some constants,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
 * utility methods and classes, as well as a simple framework for
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
 * helping to make sure that assertions failing in generated threads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
 * cause the associated test that generated them to itself fail (which
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
 * JUnit does not otherwise arrange).  The rules for creating such
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
 * tests are:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   144
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   145
 * <ol>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
 *
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   147
 * <li>All code not running in the main test thread (manually spawned threads
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   148
 * or the common fork join pool) must be checked for failure (and completion!).
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   149
 * Mechanisms that can be used to ensure this are:
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   150
 *   <ol>
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   151
 *   <li>Signalling via a synchronizer like AtomicInteger or CountDownLatch
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   152
 *    that the task completed normally, which is checked before returning from
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   153
 *    the test method in the main thread.
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   154
 *   <li>Using the forms {@link #threadFail}, {@link #threadAssertTrue},
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   155
 *    or {@link #threadAssertNull}, (not {@code fail}, {@code assertTrue}, etc.)
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   156
 *    Only the most typically used JUnit assertion methods are defined
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   157
 *    this way, but enough to live with.
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   158
 *   <li>Recording failure explicitly using {@link #threadUnexpectedException}
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   159
 *    or {@link #threadRecordFailure}.
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   160
 *   <li>Using a wrapper like CheckedRunnable that uses one the mechanisms above.
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
   161
 *   </ol>
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
 * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
 * to invoke {@code super.setUp} and {@code super.tearDown} within
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
 * them. These methods are used to clear and check for thread
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
 * assertion failures.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
 * <li>All delays and timeouts must use one of the constants {@code
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
 * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
 * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
 * discriminable from zero time, and always allows enough time for the
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
 * small amounts of computation (creating a thread, calling a few
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
 * methods, etc) needed to reach a timeout point. Similarly, a SMALL
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
 * is always discriminable as larger than SHORT and smaller than
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
 * MEDIUM.  And so on. These constants are set to conservative values,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
 * but even so, if there is ever any doubt, they can all be increased
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
 * in one spot to rerun tests on slower platforms.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
 * <li>All threads generated must be joined inside each test case
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
 * method (or {@code fail} to do so) before returning from the
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
 * method. The {@code joinPool} method can be used to do this when
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
 * using Executors.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
 * </ol>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
 * <p><b>Other notes</b>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
 * <ul>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
 * <li>Usually, there is one testcase method per JSR166 method
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
 * covering "normal" operation, and then as many exception-testing
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
 * methods as there are exceptions the method can throw. Sometimes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
 * there are multiple tests per JSR166 method when the different
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
 * "normal" behaviors differ significantly. And sometimes testcases
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
 * cover multiple methods when they cannot be tested in isolation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
 * <li>The documentation style for testcases is to provide as javadoc
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
 * a simple sentence or two describing the property that the testcase
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
 * method purports to test. The javadocs do not say anything about how
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
 * the property is tested. To find out, read the code.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
 * <li>These tests are "conformance tests", and do not attempt to
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
 * test throughput, latency, scalability or other performance factors
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
 * (see the separate "jtreg" tests for a set intended to check these
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
 * for the most central aspects of functionality.) So, most tests use
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
 * the smallest sensible numbers of threads, collection sizes, etc
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
 * needed to check basic conformance.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
 * <li>The test classes currently do not declare inclusion in
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
 * any particular package to simplify things for people integrating
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
 * them in TCK test suites.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
 * <li>As a convenience, the {@code main} of this class (JSR166TestCase)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
 * runs all JSR166 unit tests.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
 * </ul>
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
public class JSR166TestCase extends TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
    private static final boolean useSecurityManager =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
        Boolean.getBoolean("jsr166.useSecurityManager");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
    protected static final boolean expensiveTests =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   222
        Boolean.getBoolean("jsr166.expensiveTests");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   223
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   224
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   225
     * If true, also run tests that are not part of the official tck
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
     * because they test unspecified implementation details.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
    protected static final boolean testImplementationDetails =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
        Boolean.getBoolean("jsr166.testImplementationDetails");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   232
     * If true, report on stdout all "slow" tests, that is, ones that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   233
     * take more than profileThreshold milliseconds to execute.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   234
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   235
    private static final boolean profileTests =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   236
        Boolean.getBoolean("jsr166.profileTests");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   237
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   238
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   239
     * The number of milliseconds that tests are permitted for
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   240
     * execution without being reported, when profileTests is set.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   241
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
    private static final long profileThreshold =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
        Long.getLong("jsr166.profileThreshold", 100);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   246
     * The number of repetitions per test (for tickling rare bugs).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   248
    private static final int runsPerTest =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
        Integer.getInteger("jsr166.runsPerTest", 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   251
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   252
     * The number of repetitions of the test suite (for finding leaks?).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   254
    private static final int suiteRuns =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
        Integer.getInteger("jsr166.suiteRuns", 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   257
    /**
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   258
     * Returns the value of the system property, or NaN if not defined.
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   259
     */
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   260
    private static float systemPropertyValue(String name) {
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   261
        String floatString = System.getProperty(name);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   262
        if (floatString == null)
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   263
            return Float.NaN;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   264
        try {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   265
            return Float.parseFloat(floatString);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   266
        } catch (NumberFormatException ex) {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   267
            throw new IllegalArgumentException(
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   268
                String.format("Bad float value in system property %s=%s",
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   269
                              name, floatString));
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   270
        }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   271
    }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   272
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   274
     * The scaling factor to apply to standard delays used in tests.
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   275
     * May be initialized from any of:
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   276
     * - the "jsr166.delay.factor" system property
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   277
     * - the "test.timeout.factor" system property (as used by jtreg)
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   278
     *   See: http://openjdk.java.net/jtreg/tag-spec.html
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   279
     * - hard-coded fuzz factor when using a known slowpoke VM
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
     */
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   281
    private static final float delayFactor = delayFactor();
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   282
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   283
    private static float delayFactor() {
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   284
        float x;
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   285
        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   286
            return x;
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   287
        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   288
            return x;
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   289
        String prop = System.getProperty("java.vm.version");
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   290
        if (prop != null && prop.matches(".*debug.*"))
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   291
            return 4.0f; // How much slower is fastdebug than product?!
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   292
        return 1.0f;
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   293
    }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   294
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
    public JSR166TestCase() { super(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   296
    public JSR166TestCase(String name) { super(name); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   297
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   298
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   299
     * A filter for tests to run, matching strings of the form
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   300
     * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   301
     * Usefully combined with jsr166.runsPerTest.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
    private static final Pattern methodFilter = methodFilter();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   305
    private static Pattern methodFilter() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
        String regex = System.getProperty("jsr166.methodFilter");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   307
        return (regex == null) ? null : Pattern.compile(regex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   308
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   309
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   310
    // Instrumentation to debug very rare, but very annoying hung test runs.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
    static volatile TestCase currentTestCase;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   312
    // static volatile int currentRun = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
    static {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   314
        Runnable wedgedTestDetector = new Runnable() { public void run() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   315
            // Avoid spurious reports with enormous runsPerTest.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
            // A single test case run should never take more than 1 second.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
            // But let's cap it at the high end too ...
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   318
            final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   319
                * Math.max((int) delayFactor, 1);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   320
            final int timeoutMinutes = Math.min(15, timeoutMinutesMin);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   321
            for (TestCase lastTestCase = currentTestCase;;) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   322
                try { MINUTES.sleep(timeoutMinutes); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   323
                catch (InterruptedException unexpected) { break; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   324
                if (lastTestCase == currentTestCase) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   325
                    System.err.printf(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   326
                        "Looks like we're stuck running test: %s%n",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   327
                        lastTestCase);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   328
//                     System.err.printf(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   329
//                         "Looks like we're stuck running test: %s (%d/%d)%n",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   330
//                         lastTestCase, currentRun, runsPerTest);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   331
//                     System.err.println("availableProcessors=" +
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   332
//                         Runtime.getRuntime().availableProcessors());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   333
//                     System.err.printf("cpu model = %s%n", cpuModel());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   334
                    dumpTestThreads();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   335
                    // one stack dump is probably enough; more would be spam
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   336
                    break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   337
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   338
                lastTestCase = currentTestCase;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   339
            }}};
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   340
        Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   341
        thread.setDaemon(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   342
        thread.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   344
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   345
//     public static String cpuModel() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   346
//         try {
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   347
//             java.util.regex.Matcher matcher
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   348
//               = Pattern.compile("model name\\s*: (.*)")
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   349
//                 .matcher(new String(
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   350
//                     java.nio.file.Files.readAllBytes(
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   351
//                         java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8"));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   352
//             matcher.find();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   353
//             return matcher.group(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   354
//         } catch (Exception ex) { return null; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   355
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   356
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   357
    public void runBare() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   358
        currentTestCase = this;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   359
        if (methodFilter == null
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   360
            || methodFilter.matcher(toString()).find())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   361
            super.runBare();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   362
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   363
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   364
    protected void runTest() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   365
        for (int i = 0; i < runsPerTest; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   366
            // currentRun = i;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   367
            if (profileTests)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   368
                runTestProfiled();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   369
            else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   370
                super.runTest();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   371
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   372
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   373
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   374
    protected void runTestProfiled() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   375
        for (int i = 0; i < 2; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   376
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   377
            super.runTest();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   378
            long elapsedMillis = millisElapsedSince(startTime);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   379
            if (elapsedMillis < profileThreshold)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   380
                break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   381
            // Never report first run of any test; treat it as a
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   382
            // warmup run, notably to trigger all needed classloading,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   383
            if (i > 0)
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   384
                System.out.printf("%s: %d%n", toString(), elapsedMillis);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   386
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   387
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   388
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   389
     * Runs all JSR166 unit tests using junit.textui.TestRunner.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   390
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   391
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   392
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   393
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   395
    static class PithyResultPrinter extends junit.textui.ResultPrinter {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   397
        long runTime;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   398
        public void startTest(Test test) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   399
        protected void printHeader(long runTime) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
            this.runTime = runTime; // defer printing for later
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   401
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   402
        protected void printFooter(TestResult result) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   403
            if (result.wasSuccessful()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
                getWriter().println("OK (" + result.runCount() + " tests)"
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
                    + "  Time: " + elapsedTimeAsString(runTime));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
            } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
                getWriter().println("Time: " + elapsedTimeAsString(runTime));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
                super.printFooter(result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   409
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   411
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
     * Returns a TestRunner that doesn't bother with unnecessary
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   415
     * fluff, like printing a "." for each test case.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
    static junit.textui.TestRunner newPithyTestRunner() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
        junit.textui.TestRunner runner = new junit.textui.TestRunner();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
        runner.setPrinter(new PithyResultPrinter(System.out));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
        return runner;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   421
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   422
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   423
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   424
     * Runs all unit tests in the given test suite.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
     * Actual behavior influenced by jsr166.* system properties.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
    static void main(Test suite, String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
        if (useSecurityManager) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
            System.err.println("Setting a permissive security manager");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
            Policy.setPolicy(permissivePolicy());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
            System.setSecurityManager(new SecurityManager());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   432
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   433
        for (int i = 0; i < suiteRuns; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
            TestResult result = newPithyTestRunner().doRun(suite);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   435
            if (!result.wasSuccessful())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
                System.exit(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
            System.gc();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
            System.runFinalization();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
    public static TestSuite newTestSuite(Object... suiteOrClasses) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
        TestSuite suite = new TestSuite();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
        for (Object suiteOrClass : suiteOrClasses) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
            if (suiteOrClass instanceof TestSuite)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   446
                suite.addTest((TestSuite) suiteOrClass);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
            else if (suiteOrClass instanceof Class)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
                suite.addTest(new TestSuite((Class<?>) suiteOrClass));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
            else
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
                throw new ClassCastException("not a test suite or class");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   451
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
        return suite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   453
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
    public static void addNamedTestClasses(TestSuite suite,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
                                           String... testClassNames) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
        for (String testClassName : testClassNames) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
                Class<?> testClass = Class.forName(testClassName);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   460
                Method m = testClass.getDeclaredMethod("suite");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   461
                suite.addTest(newTestSuite((Test)m.invoke(null)));
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   462
            } catch (ReflectiveOperationException e) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   463
                throw new AssertionError("Missing test class", e);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
    public static final double JAVA_CLASS_VERSION;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   469
    public static final String JAVA_SPECIFICATION_VERSION;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
    static {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
                new java.security.PrivilegedAction<Double>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
                public Double run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
                    return Double.valueOf(System.getProperty("java.class.version"));}});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
            JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
                new java.security.PrivilegedAction<String>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
                public String run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
                    return System.getProperty("java.specification.version");}});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
        } catch (Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
            throw new Error(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   485
    public static boolean atLeastJava6()  { return JAVA_CLASS_VERSION >= 50.0; }
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   486
    public static boolean atLeastJava7()  { return JAVA_CLASS_VERSION >= 51.0; }
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   487
    public static boolean atLeastJava8()  { return JAVA_CLASS_VERSION >= 52.0; }
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   488
    public static boolean atLeastJava9()  { return JAVA_CLASS_VERSION >= 53.0; }
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47306
diff changeset
   489
    public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; }
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
   490
    public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; }
53710
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   491
    public static boolean atLeastJava12() { return JAVA_CLASS_VERSION >= 56.0; }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   492
    public static boolean atLeastJava13() { return JAVA_CLASS_VERSION >= 57.0; }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   493
    public static boolean atLeastJava14() { return JAVA_CLASS_VERSION >= 58.0; }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   494
    public static boolean atLeastJava15() { return JAVA_CLASS_VERSION >= 59.0; }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   495
    public static boolean atLeastJava16() { return JAVA_CLASS_VERSION >= 60.0; }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   496
    public static boolean atLeastJava17() { return JAVA_CLASS_VERSION >= 61.0; }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   497
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   499
     * Collects all JSR166 unit tests as one suite.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   501
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   502
        // Java7+ test classes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
        TestSuite suite = newTestSuite(
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
            ForkJoinPoolTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
            ForkJoinTaskTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
            RecursiveActionTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
            RecursiveTaskTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
            LinkedTransferQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   509
            PhaserTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
            ThreadLocalRandomTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
            AbstractExecutorServiceTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   512
            AbstractQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   513
            AbstractQueuedSynchronizerTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
            AbstractQueuedLongSynchronizerTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
            ArrayBlockingQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
            ArrayDequeTest.suite(),
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   517
            ArrayListTest.suite(),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
            AtomicBooleanTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
            AtomicIntegerArrayTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   520
            AtomicIntegerFieldUpdaterTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   521
            AtomicIntegerTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
            AtomicLongArrayTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
            AtomicLongFieldUpdaterTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
            AtomicLongTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   525
            AtomicMarkableReferenceTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
            AtomicReferenceArrayTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
            AtomicReferenceFieldUpdaterTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
            AtomicReferenceTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   529
            AtomicStampedReferenceTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
            ConcurrentHashMapTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
            ConcurrentLinkedDequeTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
            ConcurrentLinkedQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   533
            ConcurrentSkipListMapTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
            ConcurrentSkipListSubMapTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
            ConcurrentSkipListSetTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
            ConcurrentSkipListSubSetTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   537
            CopyOnWriteArrayListTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
            CopyOnWriteArraySetTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
            CountDownLatchTest.suite(),
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   540
            CountedCompleterTest.suite(),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   541
            CyclicBarrierTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
            DelayQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
            EntryTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   544
            ExchangerTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   545
            ExecutorsTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
            ExecutorCompletionServiceTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
            FutureTaskTest.suite(),
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 53710
diff changeset
   548
            HashtableTest.suite(),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   549
            LinkedBlockingDequeTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
            LinkedBlockingQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
            LinkedListTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
            LockSupportTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   553
            PriorityBlockingQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
            PriorityQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
            ReentrantLockTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   556
            ReentrantReadWriteLockTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   557
            ScheduledExecutorTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
            ScheduledExecutorSubclassTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
            SemaphoreTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
            SynchronousQueueTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
            SystemTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
            ThreadLocalTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
            ThreadPoolExecutorTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   564
            ThreadPoolExecutorSubclassTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
            ThreadTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   566
            TimeUnitTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
            TreeMapTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   568
            TreeSetTest.suite(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
            TreeSubMapTest.suite(),
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   570
            TreeSubSetTest.suite(),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   571
            VectorTest.suite());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
        // Java8+ test classes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   574
        if (atLeastJava8()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
            String[] java8TestClassNames = {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   576
                "ArrayDeque8Test",
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   577
                "Atomic8Test",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
                "CompletableFutureTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   579
                "ConcurrentHashMap8Test",
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
   580
                "CountedCompleter8Test",
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   581
                "DoubleAccumulatorTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   582
                "DoubleAdderTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   583
                "ForkJoinPool8Test",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   584
                "ForkJoinTask8Test",
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
   585
                "HashMapTest",
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
   586
                "LinkedBlockingDeque8Test",
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42339
diff changeset
   587
                "LinkedBlockingQueue8Test",
53710
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents: 52730
diff changeset
   588
                "LinkedHashMapTest",
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   589
                "LongAccumulatorTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   590
                "LongAdderTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   591
                "SplittableRandomTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   592
                "StampedLockTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   593
                "SubmissionPublisherTest",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   594
                "ThreadLocalRandom8Test",
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   595
                "TimeUnit8Test",
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   596
            };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
            addNamedTestClasses(suite, java8TestClassNames);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
        // Java9+ test classes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   601
        if (atLeastJava9()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
            String[] java9TestClassNames = {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   603
                "AtomicBoolean9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   604
                "AtomicInteger9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   605
                "AtomicIntegerArray9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   606
                "AtomicLong9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   607
                "AtomicLongArray9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   608
                "AtomicReference9Test",
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   609
                "AtomicReferenceArray9Test",
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
   610
                "ExecutorCompletionService9Test",
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
   611
                "ForkJoinPool9Test",
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   612
            };
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   613
            addNamedTestClasses(suite, java9TestClassNames);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   614
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   615
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   616
        return suite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   617
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
    /** Returns list of junit-style test method names in given class. */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
    public static ArrayList<String> testMethodNames(Class<?> testClass) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
        Method[] methods = testClass.getDeclaredMethods();
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
   622
        ArrayList<String> names = new ArrayList<>(methods.length);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
        for (Method method : methods) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
            if (method.getName().startsWith("test")
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
                && Modifier.isPublic(method.getModifiers())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
                // method.getParameterCount() requires jdk8+
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
                && method.getParameterTypes().length == 0) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
                names.add(method.getName());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   629
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   630
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
        return names;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   634
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   635
     * Returns junit-style testSuite for the given test class, but
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
     * parameterized by passing extra data to each test.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   637
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   638
    public static <ExtraData> Test parameterizedTestSuite
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
        (Class<? extends JSR166TestCase> testClass,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   640
         Class<ExtraData> dataClass,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   641
         ExtraData data) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
            TestSuite suite = new TestSuite();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   644
            Constructor c =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   645
                testClass.getDeclaredConstructor(dataClass, String.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
            for (String methodName : testMethodNames(testClass))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
                suite.addTest((Test) c.newInstance(data, methodName));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
            return suite;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   649
        } catch (ReflectiveOperationException e) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   650
            throw new AssertionError(e);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   652
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   653
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   654
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
     * Returns junit-style testSuite for the jdk8 extension of the
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
     * given test class, but parameterized by passing extra data to
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
     * each test.  Uses reflection to allow compilation in jdk7.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   659
    public static <ExtraData> Test jdk8ParameterizedTestSuite
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   660
        (Class<? extends JSR166TestCase> testClass,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
         Class<ExtraData> dataClass,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
         ExtraData data) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
        if (atLeastJava8()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   664
            String name = testClass.getName();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
            String name8 = name.replaceAll("Test$", "8Test");
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   666
            if (name.equals(name8)) throw new AssertionError(name);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
                return (Test)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   669
                    Class.forName(name8)
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   670
                    .getMethod("testSuite", dataClass)
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
                    .invoke(null, data);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   672
            } catch (ReflectiveOperationException e) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   673
                throw new AssertionError(e);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
        } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
            return new TestSuite();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   679
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
    // Delays for timing-dependent tests, in milliseconds.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   681
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   682
    public static long SHORT_DELAY_MS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   683
    public static long SMALL_DELAY_MS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   684
    public static long MEDIUM_DELAY_MS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
    public static long LONG_DELAY_MS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   687
    /**
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   688
     * A delay significantly longer than LONG_DELAY_MS.
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   689
     * Use this in a thread that is waited for via awaitTermination(Thread).
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   690
     */
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   691
    public static long LONGER_DELAY_MS;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   692
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   693
    private static final long RANDOM_TIMEOUT;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   694
    private static final long RANDOM_EXPIRED_TIMEOUT;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   695
    private static final TimeUnit RANDOM_TIMEUNIT;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   696
    static {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   697
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   698
        long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE };
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   699
        RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)];
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   700
        RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)];
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   701
        TimeUnit[] timeUnits = TimeUnit.values();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   702
        RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)];
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   703
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   704
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   705
    /**
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   706
     * Returns a timeout for use when any value at all will do.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   707
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   708
    static long randomTimeout() { return RANDOM_TIMEOUT; }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   709
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   710
    /**
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   711
     * Returns a timeout that means "no waiting", i.e. not positive.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   712
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   713
    static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   714
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   715
    /**
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   716
     * Returns a random non-null TimeUnit.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   717
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   718
    static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   719
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
    /**
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   721
     * Returns a random boolean; a "coin flip".
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   722
     */
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   723
    static boolean randomBoolean() {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   724
        return ThreadLocalRandom.current().nextBoolean();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   725
    }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   726
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   727
    /**
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   728
     * Returns a random element from given choices.
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   729
     */
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   730
    <T> T chooseRandomly(T... choices) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   731
        return choices[ThreadLocalRandom.current().nextInt(choices.length)];
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   732
    }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   733
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   734
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
     * Returns the shortest timed delay. This can be scaled up for
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   736
     * slow machines using the jsr166.delay.factor system property,
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   737
     * or via jtreg's -timeoutFactor: flag.
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
   738
     * http://openjdk.java.net/jtreg/command-help.html
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
    protected long getShortDelay() {
36957
96f46df4ac48 8151344: Improve timeout factor handling in tck/JSR166TestCase
dl
parents: 36231
diff changeset
   741
        return (long) (50 * delayFactor);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
     * Sets delays as multiples of SHORT_DELAY.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   746
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   747
    protected void setDelays() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
        SHORT_DELAY_MS = getShortDelay();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   749
        SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   750
        MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
        LONG_DELAY_MS   = SHORT_DELAY_MS * 200;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   752
        LONGER_DELAY_MS = 2 * LONG_DELAY_MS;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   755
    private static final long TIMEOUT_DELAY_MS
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   756
        = (long) (12.0 * Math.cbrt(delayFactor));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   757
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   759
     * Returns a timeout in milliseconds to be used in tests that verify
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   760
     * that operations block or time out.  We want this to be longer
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   761
     * than the OS scheduling quantum, but not too long, so don't scale
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   762
     * linearly with delayFactor; we use "crazy" cube root instead.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
     */
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   764
    static long timeoutMillis() {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
   765
        return TIMEOUT_DELAY_MS;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   768
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   769
     * Returns a new Date instance representing a time at least
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
     * delayMillis milliseconds in the future.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
    Date delayedDate(long delayMillis) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
        // Add 1 because currentTimeMillis is known to round into the past.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
        return new Date(System.currentTimeMillis() + delayMillis + 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   776
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   777
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   778
     * The first exception encountered if any threadAssertXXX method fails.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
    private final AtomicReference<Throwable> threadFailure
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
   781
        = new AtomicReference<>(null);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   782
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
     * Records an exception so that it can be rethrown later in the test
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
     * harness thread, triggering a test case failure.  Only the first
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   786
     * failure is recorded; subsequent calls to this method from within
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
     * the same test have no effect.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   788
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   789
    public void threadRecordFailure(Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
        System.err.println(t);
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   791
        if (threadFailure.compareAndSet(null, t))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
   792
            dumpTestThreads();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
    public void setUp() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   796
        setDelays();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   797
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   798
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   799
    void tearDownFail(String format, Object... args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   800
        String msg = toString() + ": " + String.format(format, args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
        System.err.println(msg);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
        dumpTestThreads();
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   803
        throw new AssertionError(msg);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   804
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   805
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   806
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   807
     * Extra checks that get done for all test cases.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   808
     *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   809
     * Triggers test case failure if any thread assertions have failed,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   810
     * by rethrowing, in the test harness thread, any exception recorded
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   811
     * earlier by threadRecordFailure.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   812
     *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   813
     * Triggers test case failure if interrupt status is set in the main thread.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   814
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   815
    public void tearDown() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
        Throwable t = threadFailure.getAndSet(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
        if (t != null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
            if (t instanceof Error)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   819
                throw (Error) t;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   820
            else if (t instanceof RuntimeException)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   821
                throw (RuntimeException) t;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   822
            else if (t instanceof Exception)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   823
                throw (Exception) t;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   824
            else
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   825
                throw new AssertionError(t.toString(), t);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   826
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   827
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
        if (Thread.interrupted())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
            tearDownFail("interrupt status set in main thread");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   831
        checkForkJoinPoolThreadLeaks();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
     * Finds missing PoolCleaners
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   836
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   837
    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   838
        Thread[] survivors = new Thread[7];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   839
        int count = Thread.enumerate(survivors);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   840
        for (int i = 0; i < count; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   841
            Thread thread = survivors[i];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   842
            String name = thread.getName();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   843
            if (name.startsWith("ForkJoinPool-")) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
                // give thread some time to terminate
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   845
                thread.join(LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   846
                if (thread.isAlive())
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
                    tearDownFail("Found leaked ForkJoinPool thread thread=%s",
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   848
                                 thread);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   849
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   850
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   851
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   852
        if (!ForkJoinPool.commonPool()
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   853
            .awaitQuiescence(LONG_DELAY_MS, MILLISECONDS))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   854
            tearDownFail("ForkJoin common pool thread stuck");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   855
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   856
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   857
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   858
     * Just like fail(reason), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   859
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   860
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   861
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   862
    public void threadFail(String reason) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   863
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   864
            fail(reason);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   865
        } catch (AssertionError fail) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   866
            threadRecordFailure(fail);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   867
            throw fail;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   868
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   869
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   870
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   871
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   872
     * Just like assertTrue(b), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   873
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   874
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   875
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   876
    public void threadAssertTrue(boolean b) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   877
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   878
            assertTrue(b);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   879
        } catch (AssertionError fail) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   880
            threadRecordFailure(fail);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   881
            throw fail;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   882
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   883
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   884
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   885
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   886
     * Just like assertFalse(b), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   887
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   888
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   889
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   890
    public void threadAssertFalse(boolean b) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   891
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   892
            assertFalse(b);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   893
        } catch (AssertionError fail) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   894
            threadRecordFailure(fail);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   895
            throw fail;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   896
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   897
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   898
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   899
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   900
     * Just like assertNull(x), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   901
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   902
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   903
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   904
    public void threadAssertNull(Object x) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   905
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   906
            assertNull(x);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   907
        } catch (AssertionError fail) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   908
            threadRecordFailure(fail);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   909
            throw fail;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   910
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   911
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   912
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   913
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   914
     * Just like assertEquals(x, y), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   915
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   916
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   917
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   918
    public void threadAssertEquals(long x, long y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   919
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   920
            assertEquals(x, y);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   921
        } catch (AssertionError fail) {
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   922
            threadRecordFailure(fail);
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   923
            throw fail;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   924
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   926
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   927
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   928
     * Just like assertEquals(x, y), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   929
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   930
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   931
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   932
    public void threadAssertEquals(Object x, Object y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   933
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   934
            assertEquals(x, y);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   935
        } catch (AssertionError fail) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   936
            threadRecordFailure(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   937
            throw fail;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   938
        } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   939
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   940
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   941
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   942
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   943
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   944
     * Just like assertSame(x, y), but additionally recording (using
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   945
     * threadRecordFailure) any AssertionError thrown, so that the
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   946
     * current testcase will fail.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   947
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   948
    public void threadAssertSame(Object x, Object y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   949
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   950
            assertSame(x, y);
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   951
        } catch (AssertionError fail) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   952
            threadRecordFailure(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   953
            throw fail;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   954
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   955
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   956
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   957
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   958
     * Calls threadFail with message "should throw exception".
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   959
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   960
    public void threadShouldThrow() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   961
        threadFail("should throw exception");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   962
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   963
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   964
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   965
     * Calls threadFail with message "should throw" + exceptionName.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   966
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   967
    public void threadShouldThrow(String exceptionName) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   968
        threadFail("should throw " + exceptionName);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   969
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   970
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   971
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   972
     * Records the given exception using {@link #threadRecordFailure},
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   973
     * then rethrows the exception, wrapping it in an AssertionError
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   974
     * if necessary.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   976
    public void threadUnexpectedException(Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
        threadRecordFailure(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   978
        t.printStackTrace();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
        if (t instanceof RuntimeException)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   980
            throw (RuntimeException) t;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   981
        else if (t instanceof Error)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   982
            throw (Error) t;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   983
        else
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
   984
            throw new AssertionError("unexpected exception: " + t, t);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   985
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   986
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   987
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   988
     * Delays, via Thread.sleep, for the given millisecond delay, but
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   989
     * if the sleep is shorter than specified, may re-sleep or yield
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   990
     * until time elapses.  Ensures that the given time, as measured
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   991
     * by System.nanoTime(), has elapsed.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   992
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   993
    static void delay(long millis) throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   994
        long nanos = millis * (1000 * 1000);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   995
        final long wakeupTime = System.nanoTime() + nanos;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   996
        do {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   997
            if (millis > 0L)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   998
                Thread.sleep(millis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   999
            else // too short to sleep
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1000
                Thread.yield();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1001
            nanos = wakeupTime - System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1002
            millis = nanos / (1000 * 1000);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1003
        } while (nanos >= 0L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1004
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1005
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1006
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1007
     * Allows use of try-with-resources with per-test thread pools.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1008
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1009
    class PoolCleaner implements AutoCloseable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1010
        private final ExecutorService pool;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1011
        public PoolCleaner(ExecutorService pool) { this.pool = pool; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1012
        public void close() { joinPool(pool); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1013
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1014
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1015
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1016
     * An extension of PoolCleaner that has an action to release the pool.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1017
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1018
    class PoolCleanerWithReleaser extends PoolCleaner {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1019
        private final Runnable releaser;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1020
        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1021
            super(pool);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1022
            this.releaser = releaser;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1023
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1024
        public void close() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1025
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1026
                releaser.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1027
            } finally {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1028
                super.close();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1029
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1030
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1031
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1032
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1033
    PoolCleaner cleaner(ExecutorService pool) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1034
        return new PoolCleaner(pool);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1035
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1036
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1037
    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1038
        return new PoolCleanerWithReleaser(pool, releaser);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1039
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1040
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1041
    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1042
        return new PoolCleanerWithReleaser(pool, releaser(latch));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1043
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1044
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1045
    Runnable releaser(final CountDownLatch latch) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1046
        return new Runnable() { public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1047
            do { latch.countDown(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1048
            while (latch.getCount() > 0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1049
        }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1050
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1051
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1052
    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1053
        return new PoolCleanerWithReleaser(pool, releaser(flag));
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1054
    }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1055
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1056
    Runnable releaser(final AtomicBoolean flag) {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1057
        return new Runnable() { public void run() { flag.set(true); }};
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1058
    }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1059
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1060
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1061
     * Waits out termination of a thread pool or fails doing so.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1062
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1063
    void joinPool(ExecutorService pool) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1064
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1065
            pool.shutdown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1066
            if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1067
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1068
                    threadFail("ExecutorService " + pool +
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1069
                               " did not terminate in a timely manner");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1070
                } finally {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1071
                    // last resort, for the benefit of subsequent tests
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1072
                    pool.shutdownNow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1073
                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1074
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1075
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1076
        } catch (SecurityException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1077
            // Allowed in case test doesn't have privs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1078
        } catch (InterruptedException fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1079
            threadFail("Unexpected InterruptedException");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1080
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1081
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1082
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1083
    /**
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1084
     * Like Runnable, but with the freedom to throw anything.
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1085
     * junit folks had the same idea:
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1086
     * http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1087
     */
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1088
    interface Action { public void run() throws Throwable; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1089
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1090
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1091
     * Runs all the given actions in parallel, failing if any fail.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1092
     * Useful for running multiple variants of tests that are
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1093
     * necessarily individually slow because they must block.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1094
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1095
    void testInParallel(Action ... actions) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1096
        ExecutorService pool = Executors.newCachedThreadPool();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1097
        try (PoolCleaner cleaner = cleaner(pool)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1098
            ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1099
            for (final Action action : actions)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1100
                futures.add(pool.submit(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1101
                    public void realRun() throws Throwable { action.run();}}));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1102
            for (Future<?> future : futures)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1103
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1104
                    assertNull(future.get(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1105
                } catch (ExecutionException ex) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1106
                    threadUnexpectedException(ex.getCause());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1107
                } catch (Exception ex) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1108
                    threadUnexpectedException(ex);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1109
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1110
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1111
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1112
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1113
    /** Returns true if thread info might be useful in a thread dump. */
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1114
    static boolean threadOfInterest(ThreadInfo info) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1115
        final String name = info.getThreadName();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1116
        String lockName;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1117
        if (name == null)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1118
            return true;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1119
        if (name.equals("Signal Dispatcher")
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1120
            || name.equals("WedgedTestDetector"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1121
            return false;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1122
        if (name.equals("Reference Handler")) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1123
            // Reference Handler stacktrace changed in JDK-8156500
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1124
            StackTraceElement[] stackTrace; String methodName;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1125
            if ((stackTrace = info.getStackTrace()) != null
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1126
                && stackTrace.length > 0
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1127
                && (methodName = stackTrace[0].getMethodName()) != null
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1128
                && methodName.equals("waitForReferencePendingList"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1129
                return false;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1130
            // jdk8 Reference Handler stacktrace
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1131
            if ((lockName = info.getLockName()) != null
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1132
                && lockName.startsWith("java.lang.ref"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1133
                return false;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1134
        }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1135
        if ((name.equals("Finalizer") || name.equals("Common-Cleaner"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1136
            && (lockName = info.getLockName()) != null
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1137
            && lockName.startsWith("java.lang.ref"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1138
            return false;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1139
        if (name.startsWith("ForkJoinPool.commonPool-worker")
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1140
            && (lockName = info.getLockName()) != null
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1141
            && lockName.startsWith("java.util.concurrent.ForkJoinPool"))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1142
            return false;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1143
        return true;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1144
    }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1145
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1146
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1147
     * A debugging tool to print stack traces of most threads, as jstack does.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1148
     * Uninteresting threads are filtered out.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1149
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1150
    static void dumpTestThreads() {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1151
        SecurityManager sm = System.getSecurityManager();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1152
        if (sm != null) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1153
            try {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1154
                System.setSecurityManager(null);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1155
            } catch (SecurityException giveUp) {
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1156
                return;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1157
            }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1158
        }
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1159
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1160
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1161
        System.err.println("------ stacktrace dump start ------");
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1162
        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1163
            if (threadOfInterest(info))
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1164
                System.err.print(info);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1165
        System.err.println("------ stacktrace dump end ------");
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1166
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  1167
        if (sm != null) System.setSecurityManager(sm);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1168
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1169
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1170
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1171
     * Checks that thread eventually enters the expected blocked thread state.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1172
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1173
    void assertThreadBlocks(Thread thread, Thread.State expected) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1174
        // always sleep at least 1 ms, with high probability avoiding
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1175
        // transitory states
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1176
        for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1177
            try { delay(1); }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1178
            catch (InterruptedException fail) {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1179
                throw new AssertionError("Unexpected InterruptedException", fail);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1180
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1181
            Thread.State s = thread.getState();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1182
            if (s == expected)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1183
                return;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1184
            else if (s == Thread.State.TERMINATED)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1185
                fail("Unexpected thread termination");
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1186
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1187
        fail("timed out waiting for thread to enter thread state " + expected);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1188
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1189
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1190
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1191
     * Checks that future.get times out, with the default timeout of
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1192
     * {@code timeoutMillis()}.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1193
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1194
    void assertFutureTimesOut(Future future) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1195
        assertFutureTimesOut(future, timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1196
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1197
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1198
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1199
     * Checks that future.get times out, with the given millisecond timeout.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1200
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1201
    void assertFutureTimesOut(Future future, long timeoutMillis) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1202
        long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1203
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1204
            future.get(timeoutMillis, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1205
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1206
        } catch (TimeoutException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1207
        } catch (Exception fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1208
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1209
        } finally { future.cancel(true); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1210
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1211
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1212
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1213
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1214
     * Fails with message "should throw exception".
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1215
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1216
    public void shouldThrow() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1217
        fail("Should throw exception");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1218
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1219
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1220
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1221
     * Fails with message "should throw " + exceptionName.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1222
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1223
    public void shouldThrow(String exceptionName) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1224
        fail("Should throw " + exceptionName);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1225
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1226
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1227
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1228
     * The maximum number of consecutive spurious wakeups we should
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1229
     * tolerate (from APIs like LockSupport.park) before failing a test.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1230
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1231
    static final int MAX_SPURIOUS_WAKEUPS = 10;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1232
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1233
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1234
     * The number of elements to place in collections, arrays, etc.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1235
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1236
    public static final int SIZE = 20;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1237
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1238
    // Some convenient Integer constants
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1239
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1240
    public static final Integer zero  = new Integer(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1241
    public static final Integer one   = new Integer(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1242
    public static final Integer two   = new Integer(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1243
    public static final Integer three = new Integer(3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1244
    public static final Integer four  = new Integer(4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1245
    public static final Integer five  = new Integer(5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1246
    public static final Integer six   = new Integer(6);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1247
    public static final Integer seven = new Integer(7);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1248
    public static final Integer eight = new Integer(8);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1249
    public static final Integer nine  = new Integer(9);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1250
    public static final Integer m1  = new Integer(-1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1251
    public static final Integer m2  = new Integer(-2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1252
    public static final Integer m3  = new Integer(-3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1253
    public static final Integer m4  = new Integer(-4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1254
    public static final Integer m5  = new Integer(-5);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1255
    public static final Integer m6  = new Integer(-6);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1256
    public static final Integer m10 = new Integer(-10);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1257
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1258
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1259
     * Runs Runnable r with a security policy that permits precisely
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1260
     * the specified permissions.  If there is no current security
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1261
     * manager, the runnable is run twice, both with and without a
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1262
     * security manager.  We require that any security manager permit
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1263
     * getPolicy/setPolicy.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1264
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1265
    public void runWithPermissions(Runnable r, Permission... permissions) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1266
        SecurityManager sm = System.getSecurityManager();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1267
        if (sm == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1268
            r.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1269
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1270
        runWithSecurityManagerWithPermissions(r, permissions);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1271
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1272
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1273
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1274
     * Runs Runnable r with a security policy that permits precisely
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1275
     * the specified permissions.  If there is no current security
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1276
     * manager, a temporary one is set for the duration of the
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1277
     * Runnable.  We require that any security manager permit
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1278
     * getPolicy/setPolicy.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1279
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1280
    public void runWithSecurityManagerWithPermissions(Runnable r,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1281
                                                      Permission... permissions) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1282
        SecurityManager sm = System.getSecurityManager();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1283
        if (sm == null) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1284
            Policy savedPolicy = Policy.getPolicy();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1285
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1286
                Policy.setPolicy(permissivePolicy());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1287
                System.setSecurityManager(new SecurityManager());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1288
                runWithSecurityManagerWithPermissions(r, permissions);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1289
            } finally {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1290
                System.setSecurityManager(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1291
                Policy.setPolicy(savedPolicy);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1292
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1293
        } else {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1294
            Policy savedPolicy = Policy.getPolicy();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1295
            AdjustablePolicy policy = new AdjustablePolicy(permissions);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1296
            Policy.setPolicy(policy);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1297
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1298
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1299
                r.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1300
            } finally {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1301
                policy.addPermission(new SecurityPermission("setPolicy"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1302
                Policy.setPolicy(savedPolicy);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1303
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1304
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1305
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1306
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1307
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1308
     * Runs a runnable without any permissions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1309
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1310
    public void runWithoutPermissions(Runnable r) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1311
        runWithPermissions(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1312
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1313
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1314
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1315
     * A security policy where new permissions can be dynamically added
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1316
     * or all cleared.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1317
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1318
    public static class AdjustablePolicy extends java.security.Policy {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1319
        Permissions perms = new Permissions();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1320
        AdjustablePolicy(Permission... permissions) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1321
            for (Permission permission : permissions)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1322
                perms.add(permission);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1323
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1324
        void addPermission(Permission perm) { perms.add(perm); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1325
        void clearPermissions() { perms = new Permissions(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1326
        public PermissionCollection getPermissions(CodeSource cs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1327
            return perms;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1328
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1329
        public PermissionCollection getPermissions(ProtectionDomain pd) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1330
            return perms;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1331
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1332
        public boolean implies(ProtectionDomain pd, Permission p) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1333
            return perms.implies(p);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1334
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1335
        public void refresh() {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1336
        public String toString() {
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42927
diff changeset
  1337
            List<Permission> ps = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1338
            for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1339
                ps.add(e.nextElement());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1340
            return "AdjustablePolicy with permissions " + ps;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1341
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1342
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1343
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1344
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1345
     * Returns a policy containing all the permissions we ever need.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1346
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1347
    public static Policy permissivePolicy() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1348
        return new AdjustablePolicy
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1349
            // Permissions j.u.c. needs directly
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1350
            (new RuntimePermission("modifyThread"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1351
             new RuntimePermission("getClassLoader"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1352
             new RuntimePermission("setContextClassLoader"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1353
             // Permissions needed to change permissions!
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1354
             new SecurityPermission("getPolicy"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1355
             new SecurityPermission("setPolicy"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1356
             new RuntimePermission("setSecurityManager"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1357
             // Permissions needed by the junit test harness
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1358
             new RuntimePermission("accessDeclaredMembers"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1359
             new PropertyPermission("*", "read"),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1360
             new java.io.FilePermission("<<ALL FILES>>", "read"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1361
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1362
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1363
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1364
     * Sleeps until the given time has elapsed.
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1365
     * Throws AssertionError if interrupted.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1366
     */
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 40278
diff changeset
  1367
    static void sleep(long millis) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1368
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1369
            delay(millis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1370
        } catch (InterruptedException fail) {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1371
            throw new AssertionError("Unexpected InterruptedException", fail);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1372
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1373
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1374
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1375
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1376
     * Spin-waits up to the specified number of milliseconds for the given
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1377
     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1378
     * @param waitingForGodot if non-null, an additional condition to satisfy
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1379
     */
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1380
    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis,
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1381
                                       Callable<Boolean> waitingForGodot) {
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1382
        for (long startTime = 0L;;) {
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1383
            switch (thread.getState()) {
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1384
            default: break;
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1385
            case BLOCKED: case WAITING: case TIMED_WAITING:
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1386
                try {
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1387
                    if (waitingForGodot == null || waitingForGodot.call())
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1388
                        return;
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1389
                } catch (Throwable fail) { threadUnexpectedException(fail); }
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1390
                break;
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1391
            case TERMINATED:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1392
                fail("Unexpected thread termination");
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1393
            }
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1394
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1395
            if (startTime == 0L)
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
  1396
                startTime = System.nanoTime();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1397
            else if (millisElapsedSince(startTime) > timeoutMillis) {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1398
                assertTrue(thread.isAlive());
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1399
                if (waitingForGodot == null
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1400
                    || thread.getState() == Thread.State.RUNNABLE)
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1401
                    fail("timed out waiting for thread to enter wait state");
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1402
                else
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1403
                    fail("timed out waiting for condition, thread state="
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1404
                         + thread.getState());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1405
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1406
            Thread.yield();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1407
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1408
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1409
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1410
    /**
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1411
     * Spin-waits up to the specified number of milliseconds for the given
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1412
     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1413
     */
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1414
    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1415
        waitForThreadToEnterWaitState(thread, timeoutMillis, null);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1416
    }
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1417
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1418
    /**
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1419
     * Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1420
     * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1421
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1422
    void waitForThreadToEnterWaitState(Thread thread) {
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1423
        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, null);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1424
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1425
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1426
    /**
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1427
     * Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1428
     * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1429
     * and additionally satisfy the given condition.
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1430
     */
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1431
    void waitForThreadToEnterWaitState(Thread thread,
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1432
                                       Callable<Boolean> waitingForGodot) {
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1433
        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot);
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1434
    }
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1435
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 43545
diff changeset
  1436
    /**
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1437
     * Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1438
     * be interrupted.  Clears the interrupt status before returning.
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1439
     */
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1440
    void awaitInterrupted() {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1441
        for (long startTime = 0L; !Thread.interrupted(); ) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1442
            if (startTime == 0L)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1443
                startTime = System.nanoTime();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1444
            else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1445
                fail("timed out waiting for thread interrupt");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1446
            Thread.yield();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1447
        }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1448
    }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1449
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1450
    /**
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1451
     * Returns the number of milliseconds since time given by
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1452
     * startNanoTime, which must have been previously returned from a
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1453
     * call to {@link System#nanoTime()}.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1454
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1455
    static long millisElapsedSince(long startNanoTime) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1456
        return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1457
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1458
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1459
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1460
     * Checks that timed f.get() returns the expected value, and does not
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1461
     * wait for the timeout to elapse before returning.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1462
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1463
    <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1464
        long startTime = System.nanoTime();
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1465
        T actual = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1466
        try {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1467
            actual = f.get(timeoutMillis, MILLISECONDS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1468
        } catch (Throwable fail) { threadUnexpectedException(fail); }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1469
        assertEquals(expectedValue, actual);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1470
        if (millisElapsedSince(startTime) > timeoutMillis/2)
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1471
            throw new AssertionError("timed get did not return promptly");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1472
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1473
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1474
    <T> void checkTimedGet(Future<T> f, T expectedValue) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1475
        checkTimedGet(f, expectedValue, LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1476
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1477
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1478
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1479
     * Returns a new started daemon Thread running the given runnable.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1480
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1481
    Thread newStartedThread(Runnable runnable) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1482
        Thread t = new Thread(runnable);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1483
        t.setDaemon(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1484
        t.start();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1485
        return t;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1486
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1487
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1488
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1489
     * Waits for the specified time (in milliseconds) for the thread
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1490
     * to terminate (using {@link Thread#join(long)}), else interrupts
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1491
     * the thread (in the hope that it may terminate later) and fails.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1492
     */
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1493
    void awaitTermination(Thread thread, long timeoutMillis) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1494
        try {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1495
            thread.join(timeoutMillis);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1496
        } catch (InterruptedException fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1497
            threadUnexpectedException(fail);
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1498
        }
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1499
        if (thread.getState() != Thread.State.TERMINATED) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1500
            String detail = String.format(
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1501
                    "timed out waiting for thread to terminate, thread=%s, state=%s" ,
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1502
                    thread, thread.getState());
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1503
            try {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1504
                threadFail(detail);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1505
            } finally {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1506
                // Interrupt thread __after__ having reported its stack trace
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 54686
diff changeset
  1507
                thread.interrupt();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1508
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1509
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1510
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1511
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1512
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1513
     * Waits for LONG_DELAY_MS milliseconds for the thread to
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1514
     * terminate (using {@link Thread#join(long)}), else interrupts
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1515
     * the thread (in the hope that it may terminate later) and fails.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1516
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1517
    void awaitTermination(Thread t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1518
        awaitTermination(t, LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1519
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1520
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1521
    // Some convenient Runnable classes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1522
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1523
    public abstract class CheckedRunnable implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1524
        protected abstract void realRun() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1525
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1526
        public final void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1527
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1528
                realRun();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1529
            } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1530
                threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1531
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1532
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1533
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1534
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1535
    public abstract class ThreadShouldThrow extends Thread {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1536
        protected abstract void realRun() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1537
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1538
        final Class<?> exceptionClass;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1539
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1540
        <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1541
            this.exceptionClass = exceptionClass;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1542
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1543
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1544
        public final void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1545
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1546
                realRun();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1547
            } catch (Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1548
                if (! exceptionClass.isInstance(t))
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1549
                    threadUnexpectedException(t);
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1550
                return;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1551
            }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1552
            threadShouldThrow(exceptionClass.getSimpleName());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1553
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1554
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1555
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1556
    public abstract class CheckedInterruptedRunnable implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1557
        protected abstract void realRun() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1558
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1559
        public final void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1560
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1561
                realRun();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1562
            } catch (InterruptedException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1563
                threadAssertFalse(Thread.interrupted());
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1564
                return;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1565
            } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1566
                threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1567
            }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1568
            threadShouldThrow("InterruptedException");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1569
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1570
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1571
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1572
    public abstract class CheckedCallable<T> implements Callable<T> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1573
        protected abstract T realCall() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1574
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1575
        public final T call() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1576
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1577
                return realCall();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1578
            } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1579
                threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1580
            }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1581
            throw new AssertionError("unreached");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1582
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1583
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1584
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1585
    public static class NoOpRunnable implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1586
        public void run() {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1587
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1588
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1589
    public static class NoOpCallable implements Callable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1590
        public Object call() { return Boolean.TRUE; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1591
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1592
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1593
    public static final String TEST_STRING = "a test string";
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1594
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1595
    public static class StringTask implements Callable<String> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1596
        final String value;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1597
        public StringTask() { this(TEST_STRING); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1598
        public StringTask(String value) { this.value = value; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1599
        public String call() { return value; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1600
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1601
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1602
    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1603
        return new CheckedCallable<String>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1604
            protected String realCall() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1605
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1606
                    latch.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1607
                } catch (InterruptedException quittingTime) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1608
                return TEST_STRING;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1609
            }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1610
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1611
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1612
    public Runnable countDowner(final CountDownLatch latch) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1613
        return new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1614
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1615
                latch.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1616
            }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1617
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1618
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1619
    class LatchAwaiter extends CheckedRunnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1620
        static final int NEW = 0;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1621
        static final int RUNNING = 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1622
        static final int DONE = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1623
        final CountDownLatch latch;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1624
        int state = NEW;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1625
        LatchAwaiter(CountDownLatch latch) { this.latch = latch; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1626
        public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1627
            state = 1;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1628
            await(latch);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1629
            state = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1630
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1631
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1632
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1633
    public LatchAwaiter awaiter(CountDownLatch latch) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1634
        return new LatchAwaiter(latch);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1635
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1636
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1637
    public void await(CountDownLatch latch, long timeoutMillis) {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1638
        boolean timedOut = false;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1639
        try {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1640
            timedOut = !latch.await(timeoutMillis, MILLISECONDS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1641
        } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1642
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1643
        }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1644
        if (timedOut)
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1645
            fail("timed out waiting for CountDownLatch for "
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1646
                 + (timeoutMillis/1000) + " sec");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1647
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1648
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1649
    public void await(CountDownLatch latch) {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1650
        await(latch, LONG_DELAY_MS);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1651
    }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35768
diff changeset
  1652
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1653
    public void await(Semaphore semaphore) {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1654
        boolean timedOut = false;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1655
        try {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1656
            timedOut = !semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1657
        } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1658
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1659
        }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1660
        if (timedOut)
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1661
            fail("timed out waiting for Semaphore for "
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1662
                 + (LONG_DELAY_MS/1000) + " sec");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1663
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1664
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1665
    public void await(CyclicBarrier barrier) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1666
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1667
            barrier.await(LONG_DELAY_MS, MILLISECONDS);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1668
        } catch (Throwable fail) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1669
            threadUnexpectedException(fail);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1670
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1671
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1672
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1673
//     /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1674
//      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1675
//      */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1676
//     public void await(AtomicBoolean flag) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1677
//         await(flag, LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1678
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1679
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1680
//     /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1681
//      * Spin-waits up to the specified timeout until flag becomes true.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1682
//      */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1683
//     public void await(AtomicBoolean flag, long timeoutMillis) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1684
//         long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1685
//         while (!flag.get()) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1686
//             if (millisElapsedSince(startTime) > timeoutMillis)
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1687
//                 throw new AssertionError("timed out");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1688
//             Thread.yield();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1689
//         }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1690
//     }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1691
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1692
    public static class NPETask implements Callable<String> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1693
        public String call() { throw new NullPointerException(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1694
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1695
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1696
    public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1697
        return new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1698
            protected void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1699
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1700
                    delay(timeoutMillis);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1701
                } catch (InterruptedException ok) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1702
            }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1703
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1704
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1705
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1706
     * For use as ThreadFactory in constructors
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1707
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1708
    public static class SimpleThreadFactory implements ThreadFactory {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1709
        public Thread newThread(Runnable r) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1710
            return new Thread(r);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1711
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1712
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1713
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1714
    public interface TrackedRunnable extends Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1715
        boolean isDone();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1716
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1717
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1718
    public static class TrackedNoOpRunnable implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1719
        public volatile boolean done = false;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1720
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1721
            done = true;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1722
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1723
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1724
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1725
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1726
     * Analog of CheckedRunnable for RecursiveAction
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1727
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1728
    public abstract class CheckedRecursiveAction extends RecursiveAction {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1729
        protected abstract void realCompute() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1730
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1731
        @Override protected final void compute() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1732
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1733
                realCompute();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1734
            } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1735
                threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1736
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1737
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1738
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1739
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1740
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1741
     * Analog of CheckedCallable for RecursiveTask
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1742
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1743
    public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1744
        protected abstract T realCompute() throws Throwable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1745
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1746
        @Override protected final T compute() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1747
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1748
                return realCompute();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1749
            } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1750
                threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1751
            }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 51951
diff changeset
  1752
            throw new AssertionError("unreached");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1753
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1754
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1755
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1756
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1757
     * For use as RejectedExecutionHandler in constructors
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1758
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1759
    public static class NoOpREHandler implements RejectedExecutionHandler {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1760
        public void rejectedExecution(Runnable r,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1761
                                      ThreadPoolExecutor executor) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1762
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1763
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1764
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1765
     * A CyclicBarrier that uses timed await and fails with
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1766
     * AssertionErrors instead of throwing checked exceptions.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1767
     */
41130
2004bf22423f 8166059: JSR166TestCase.java fails with NPE in dumpTestThreads on timeout
dl
parents: 40817
diff changeset
  1768
    public static class CheckedBarrier extends CyclicBarrier {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1769
        public CheckedBarrier(int parties) { super(parties); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1770
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1771
        public int await() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1772
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1773
                return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1774
            } catch (TimeoutException timedOut) {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1775
                throw new AssertionError("timed out");
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1776
            } catch (Exception fail) {
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1777
                throw new AssertionError("Unexpected exception: " + fail, fail);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1778
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1779
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1780
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1781
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1782
    void checkEmpty(BlockingQueue q) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1783
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1784
            assertTrue(q.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1785
            assertEquals(0, q.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1786
            assertNull(q.peek());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1787
            assertNull(q.poll());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1788
            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1789
            assertEquals(q.toString(), "[]");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1790
            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1791
            assertFalse(q.iterator().hasNext());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1792
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1793
                q.element();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1794
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1795
            } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1796
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1797
                q.iterator().next();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1798
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1799
            } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1800
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1801
                q.remove();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1802
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1803
            } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1804
        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1805
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1806
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1807
    void assertSerialEquals(Object x, Object y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1808
        assertTrue(Arrays.equals(serialBytes(x), serialBytes(y)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1809
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1810
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1811
    void assertNotSerialEquals(Object x, Object y) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1812
        assertFalse(Arrays.equals(serialBytes(x), serialBytes(y)));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1813
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1814
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1815
    byte[] serialBytes(Object o) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1816
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1817
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1818
            ObjectOutputStream oos = new ObjectOutputStream(bos);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1819
            oos.writeObject(o);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1820
            oos.flush();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1821
            oos.close();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1822
            return bos.toByteArray();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1823
        } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1824
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1825
            return new byte[0];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1826
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1827
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1828
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 53710
diff changeset
  1829
    void assertImmutable(Object o) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1830
        if (o instanceof Collection) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1831
            assertThrows(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1832
                UnsupportedOperationException.class,
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 53710
diff changeset
  1833
                () -> ((Collection) o).add(null));
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1834
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1835
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1836
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1837
    @SuppressWarnings("unchecked")
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1838
    <T> T serialClone(T o) {
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1839
        T clone = null;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1840
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1841
            ObjectInputStream ois = new ObjectInputStream
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1842
                (new ByteArrayInputStream(serialBytes(o)));
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1843
            clone = (T) ois.readObject();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1844
        } catch (Throwable fail) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1845
            threadUnexpectedException(fail);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1846
        }
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1847
        if (o == clone) assertImmutable(o);
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1848
        else assertSame(o.getClass(), clone.getClass());
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1849
        return clone;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1850
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1851
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1852
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1853
     * A version of serialClone that leaves error handling (for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1854
     * e.g. NotSerializableException) up to the caller.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1855
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1856
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1857
    <T> T serialClonePossiblyFailing(T o)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1858
        throws ReflectiveOperationException, java.io.IOException {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1859
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1860
        ObjectOutputStream oos = new ObjectOutputStream(bos);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1861
        oos.writeObject(o);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1862
        oos.flush();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1863
        oos.close();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1864
        ObjectInputStream ois = new ObjectInputStream
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1865
            (new ByteArrayInputStream(bos.toByteArray()));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1866
        T clone = (T) ois.readObject();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1867
        if (o == clone) assertImmutable(o);
51951
1239bfca87f8 8207003: Miscellaneous changes imported from jsr166 CVS 2018-09
dl
parents: 49564
diff changeset
  1868
        else assertSame(o.getClass(), clone.getClass());
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1869
        return clone;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1870
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1871
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1872
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1873
     * If o implements Cloneable and has a public clone method,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1874
     * returns a clone of o, else null.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1875
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1876
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1877
    <T> T cloneableClone(T o) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1878
        if (!(o instanceof Cloneable)) return null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1879
        final T clone;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1880
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1881
            clone = (T) o.getClass().getMethod("clone").invoke(o);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1882
        } catch (NoSuchMethodException ok) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1883
            return null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1884
        } catch (ReflectiveOperationException unexpected) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1885
            throw new Error(unexpected);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1886
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1887
        assertNotSame(o, clone); // not 100% guaranteed by spec
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1888
        assertSame(o.getClass(), clone.getClass());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1889
        return clone;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1890
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 41130
diff changeset
  1891
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1892
    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 53710
diff changeset
  1893
                             Action... throwingActions) {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 53710
diff changeset
  1894
        for (Action throwingAction : throwingActions) {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1895
            boolean threw = false;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1896
            try { throwingAction.run(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1897
            catch (Throwable t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1898
                threw = true;
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1899
                if (!expectedExceptionClass.isInstance(t))
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1900
                    throw new AssertionError(
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1901
                            "Expected " + expectedExceptionClass.getName() +
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1902
                            ", got " + t.getClass().getName(),
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 48541
diff changeset
  1903
                            t);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1904
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1905
            if (!threw)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1906
                shouldThrow(expectedExceptionClass.getName());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1907
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1908
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1909
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1910
    public void assertIteratorExhausted(Iterator<?> it) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1911
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1912
            it.next();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1913
            shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1914
        } catch (NoSuchElementException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1915
        assertFalse(it.hasNext());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1916
    }
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1917
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1918
    public <T> Callable<T> callableThrowing(final Exception ex) {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1919
        return new Callable<T>() { public T call() throws Exception { throw ex; }};
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1920
    }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1921
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1922
    public Runnable runnableThrowing(final RuntimeException ex) {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1923
        return new Runnable() { public void run() { throw ex; }};
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1924
    }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1925
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1926
    /** A reusable thread pool to be shared by tests. */
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1927
    static final ExecutorService cachedThreadPool =
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1928
        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1929
                               1000L, MILLISECONDS,
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1930
                               new SynchronousQueue<Runnable>());
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36957
diff changeset
  1931
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
  1932
    static <T> void shuffle(T[] array) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
  1933
        Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39879
diff changeset
  1934
    }
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1935
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1936
    /**
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1937
     * Returns the same String as would be returned by {@link
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1938
     * Object#toString}, whether or not the given object's class
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1939
     * overrides toString().
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1940
     *
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1941
     * @see System#identityHashCode
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1942
     */
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1943
    static String identityString(Object x) {
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1944
        return x.getClass().getName()
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1945
            + "@" + Integer.toHexString(System.identityHashCode(x));
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1946
    }
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1947
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1948
    // --- Shared assertions for Executor tests ---
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1949
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1950
    /**
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1951
     * Returns maximum number of tasks that can be submitted to given
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1952
     * pool (with bounded queue) before saturation (when submission
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1953
     * throws RejectedExecutionException).
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1954
     */
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1955
    static final int saturatedSize(ThreadPoolExecutor pool) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1956
        BlockingQueue<Runnable> q = pool.getQueue();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1957
        return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1958
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1959
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1960
    @SuppressWarnings("FutureReturnValueIgnored")
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1961
    void assertNullTaskSubmissionThrowsNullPointerException(Executor e) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1962
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1963
            e.execute((Runnable) null);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1964
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1965
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1966
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1967
        if (! (e instanceof ExecutorService)) return;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1968
        ExecutorService es = (ExecutorService) e;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1969
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1970
            es.submit((Runnable) null);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1971
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1972
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1973
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1974
            es.submit((Runnable) null, Boolean.TRUE);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1975
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1976
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1977
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1978
            es.submit((Callable) null);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1979
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1980
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1981
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1982
        if (! (e instanceof ScheduledExecutorService)) return;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1983
        ScheduledExecutorService ses = (ScheduledExecutorService) e;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1984
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1985
            ses.schedule((Runnable) null,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1986
                         randomTimeout(), randomTimeUnit());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1987
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1988
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1989
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1990
            ses.schedule((Callable) null,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1991
                         randomTimeout(), randomTimeUnit());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1992
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1993
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1994
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1995
            ses.scheduleAtFixedRate((Runnable) null,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1996
                                    randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1997
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1998
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  1999
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2000
            ses.scheduleWithFixedDelay((Runnable) null,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2001
                                       randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2002
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2003
        } catch (NullPointerException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2004
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2005
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2006
    void setRejectedExecutionHandler(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2007
        ThreadPoolExecutor p, RejectedExecutionHandler handler) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2008
        p.setRejectedExecutionHandler(handler);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2009
        assertSame(handler, p.getRejectedExecutionHandler());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2010
    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2011
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2012
    void assertTaskSubmissionsAreRejected(ThreadPoolExecutor p) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2013
        final RejectedExecutionHandler savedHandler = p.getRejectedExecutionHandler();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2014
        final long savedTaskCount = p.getTaskCount();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2015
        final long savedCompletedTaskCount = p.getCompletedTaskCount();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2016
        final int savedQueueSize = p.getQueue().size();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2017
        final boolean stock = (p.getClass().getClassLoader() == null);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2018
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2019
        Runnable r = () -> {};
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2020
        Callable<Boolean> c = () -> Boolean.TRUE;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2021
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2022
        class Recorder implements RejectedExecutionHandler {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2023
            public volatile Runnable r = null;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2024
            public volatile ThreadPoolExecutor p = null;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2025
            public void reset() { r = null; p = null; }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2026
            public void rejectedExecution(Runnable r, ThreadPoolExecutor p) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2027
                assertNull(this.r);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2028
                assertNull(this.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2029
                this.r = r;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2030
                this.p = p;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2031
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2032
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2033
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2034
        // check custom handler is invoked exactly once per task
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2035
        Recorder recorder = new Recorder();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2036
        setRejectedExecutionHandler(p, recorder);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2037
        for (int i = 2; i--> 0; ) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2038
            recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2039
            p.execute(r);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2040
            if (stock && p.getClass() == ThreadPoolExecutor.class)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2041
                assertSame(r, recorder.r);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2042
            assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2043
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2044
            recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2045
            assertFalse(p.submit(r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2046
            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2047
            assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2048
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2049
            recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2050
            assertFalse(p.submit(r, Boolean.TRUE).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2051
            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2052
            assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2053
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2054
            recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2055
            assertFalse(p.submit(c).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2056
            if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2057
            assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2058
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2059
            if (p instanceof ScheduledExecutorService) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2060
                ScheduledExecutorService s = (ScheduledExecutorService) p;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2061
                ScheduledFuture<?> future;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2062
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2063
                recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2064
                future = s.schedule(r, randomTimeout(), randomTimeUnit());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2065
                assertFalse(future.isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2066
                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2067
                assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2068
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2069
                recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2070
                future = s.schedule(c, randomTimeout(), randomTimeUnit());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2071
                assertFalse(future.isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2072
                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2073
                assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2074
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2075
                recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2076
                future = s.scheduleAtFixedRate(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2077
                assertFalse(future.isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2078
                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2079
                assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2080
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2081
                recorder.reset();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2082
                future = s.scheduleWithFixedDelay(r, randomTimeout(), LONG_DELAY_MS, MILLISECONDS);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2083
                assertFalse(future.isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2084
                if (stock) assertTrue(!((FutureTask) recorder.r).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2085
                assertSame(p, recorder.p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2086
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2087
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2088
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2089
        // Checking our custom handler above should be sufficient, but
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2090
        // we add some integration tests of standard handlers.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2091
        final AtomicReference<Thread> thread = new AtomicReference<>();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2092
        final Runnable setThread = () -> thread.set(Thread.currentThread());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2093
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2094
        setRejectedExecutionHandler(p, new ThreadPoolExecutor.AbortPolicy());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2095
        try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2096
            p.execute(setThread);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2097
            shouldThrow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2098
        } catch (RejectedExecutionException success) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2099
        assertNull(thread.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2100
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2101
        setRejectedExecutionHandler(p, new ThreadPoolExecutor.DiscardPolicy());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2102
        p.execute(setThread);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2103
        assertNull(thread.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2104
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2105
        setRejectedExecutionHandler(p, new ThreadPoolExecutor.CallerRunsPolicy());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2106
        p.execute(setThread);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2107
        if (p.isShutdown())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2108
            assertNull(thread.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2109
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2110
            assertSame(Thread.currentThread(), thread.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2111
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2112
        setRejectedExecutionHandler(p, savedHandler);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2113
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2114
        // check that pool was not perturbed by handlers
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2115
        assertEquals(savedTaskCount, p.getTaskCount());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2116
        assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2117
        assertEquals(savedQueueSize, p.getQueue().size());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 44589
diff changeset
  2118
    }
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2119
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2120
    void assertCollectionsEquals(Collection<?> x, Collection<?> y) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2121
        assertEquals(x, y);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2122
        assertEquals(y, x);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2123
        assertEquals(x.isEmpty(), y.isEmpty());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2124
        assertEquals(x.size(), y.size());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2125
        if (x instanceof List) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2126
            assertEquals(x.toString(), y.toString());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2127
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2128
        if (x instanceof List || x instanceof Set) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2129
            assertEquals(x.hashCode(), y.hashCode());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2130
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2131
        if (x instanceof List || x instanceof Deque) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2132
            assertTrue(Arrays.equals(x.toArray(), y.toArray()));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2133
            assertTrue(Arrays.equals(x.toArray(new Object[0]),
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2134
                                     y.toArray(new Object[0])));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2135
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2136
    }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2137
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2138
    /**
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2139
     * A weaker form of assertCollectionsEquals which does not insist
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2140
     * that the two collections satisfy Object#equals(Object), since
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2141
     * they may use identity semantics as Deques do.
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2142
     */
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2143
    void assertCollectionsEquivalent(Collection<?> x, Collection<?> y) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2144
        if (x instanceof List || x instanceof Set)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2145
            assertCollectionsEquals(x, y);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2146
        else {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2147
            assertEquals(x.isEmpty(), y.isEmpty());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2148
            assertEquals(x.size(), y.size());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2149
            assertEquals(new HashSet(x), new HashSet(y));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2150
            if (x instanceof Deque) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2151
                assertTrue(Arrays.equals(x.toArray(), y.toArray()));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2152
                assertTrue(Arrays.equals(x.toArray(new Object[0]),
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2153
                                         y.toArray(new Object[0])));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2154
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2155
        }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48843
diff changeset
  2156
    }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  2157
}