test/jdk/java/util/concurrent/tck/ScheduledExecutorSubclassTest.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 52730 345266000aba
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
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
import static java.util.concurrent.TimeUnit.MILLISECONDS;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
    35
import static java.util.concurrent.TimeUnit.NANOSECONDS;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import static java.util.concurrent.TimeUnit.SECONDS;
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
import java.util.ArrayList;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
    39
import java.util.Collection;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
    40
import java.util.Collections;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
import java.util.HashSet;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
import java.util.List;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
import java.util.concurrent.BlockingQueue;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
import java.util.concurrent.Callable;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
import java.util.concurrent.CancellationException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
import java.util.concurrent.CountDownLatch;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
import java.util.concurrent.Delayed;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
import java.util.concurrent.ExecutionException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    50
import java.util.concurrent.Future;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    51
import java.util.concurrent.RejectedExecutionException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
import java.util.concurrent.RejectedExecutionHandler;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
import java.util.concurrent.RunnableScheduledFuture;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
import java.util.concurrent.ScheduledFuture;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
import java.util.concurrent.ScheduledThreadPoolExecutor;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
import java.util.concurrent.ThreadFactory;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
    57
import java.util.concurrent.ThreadLocalRandom;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
import java.util.concurrent.ThreadPoolExecutor;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
import java.util.concurrent.TimeoutException;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
import java.util.concurrent.TimeUnit;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
    61
import java.util.concurrent.atomic.AtomicBoolean;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
import java.util.concurrent.atomic.AtomicInteger;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
    63
import java.util.concurrent.atomic.AtomicLong;
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
    64
import java.util.stream.Stream;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
import junit.framework.TestSuite;
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
public class ScheduledExecutorSubclassTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    72
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
        return new TestSuite(ScheduledExecutorSubclassTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    76
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
    static class CustomTask<V> implements RunnableScheduledFuture<V> {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40817
diff changeset
    78
        private final RunnableScheduledFuture<V> task;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
        volatile boolean ran;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 40817
diff changeset
    80
        CustomTask(RunnableScheduledFuture<V> task) { this.task = task; }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
        public boolean isPeriodic() { return task.isPeriodic(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    83
            ran = true;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    84
            task.run();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    85
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    86
        public long getDelay(TimeUnit unit) { return task.getDelay(unit); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
        public int compareTo(Delayed t) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    88
            return task.compareTo(((CustomTask)t).task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    90
        public boolean cancel(boolean mayInterruptIfRunning) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
            return task.cancel(mayInterruptIfRunning);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
        public boolean isCancelled() { return task.isCancelled(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    94
        public boolean isDone() { return task.isDone(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
        public V get() throws InterruptedException, ExecutionException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
            V v = task.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    97
            assertTrue(ran);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    98
            return v;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   100
        public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
            V v = task.get(time, unit);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
            assertTrue(ran);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
            return v;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
    public class CustomExecutor extends ScheduledThreadPoolExecutor {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
        protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
            return new CustomTask<V>(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
        protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   114
            return new CustomTask<V>(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   115
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
        CustomExecutor(int corePoolSize) { super(corePoolSize); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
        CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
            super(corePoolSize, handler);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
        CustomExecutor(int corePoolSize, ThreadFactory threadFactory) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
            super(corePoolSize, threadFactory);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
        CustomExecutor(int corePoolSize, ThreadFactory threadFactory,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
                       RejectedExecutionHandler handler) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   126
            super(corePoolSize, threadFactory, handler);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   128
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
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
     * execute successfully executes a runnable
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
    public void testExecute() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
            final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
            final Runnable task = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
                public void realRun() { done.countDown(); }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
            p.execute(task);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
            await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
     * delayed schedule of callable successfully executes after delay
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   147
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   148
    public void testSchedule1() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
            final long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
            Callable task = new CheckedCallable<Boolean>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
                public Boolean realCall() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
                    done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
                    return Boolean.TRUE;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
            assertSame(Boolean.TRUE, f.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
     * delayed schedule of runnable successfully executes after delay
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
    public void testSchedule3() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
            final long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
            final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
            Runnable task = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
                public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
                    done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
            Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
            await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
            assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
        }
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
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
     * scheduleAtFixedRate executes runnable after given initial delay
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
    public void testSchedule4() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
            final long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
            final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
            Runnable task = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
                public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
                    done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
            ScheduledFuture f =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
                p.scheduleAtFixedRate(task, timeoutMillis(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
                                      LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
            await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
            f.cancel(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
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
     * scheduleWithFixedDelay executes runnable after given initial delay
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
    public void testSchedule5() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
            final long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
            final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
            Runnable task = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
                public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
                    done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
                }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
            ScheduledFuture f =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   221
                p.scheduleWithFixedDelay(task, timeoutMillis(),
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   222
                                         LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   223
            await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   224
            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   225
            f.cancel(true);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
        }
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   229
    static class RunnableCounter implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
        AtomicInteger count = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   231
        public void run() { count.getAndIncrement(); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   232
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   233
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   234
    /**
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   235
     * scheduleAtFixedRate executes series of tasks at given rate.
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   236
     * Eventually, it must hold that:
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   237
     *   cycles - 1 <= elapsedMillis/delay < cycles
35394
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
    public void testFixedRateSequence() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   240
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   241
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   242
            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   243
                final long startTime = System.nanoTime();
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   244
                final int cycles = 8;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
                final CountDownLatch done = new CountDownLatch(cycles);
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   246
                final Runnable task = new CheckedRunnable() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
                    public void realRun() { done.countDown(); }};
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   248
                final ScheduledFuture periodicTask =
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
                    p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS);
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   250
                final int totalDelayMillis = (cycles - 1) * delay;
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   251
                await(done, totalDelayMillis + LONG_DELAY_MS);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   252
                periodicTask.cancel(true);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   253
                final long elapsedMillis = millisElapsedSince(startTime);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   254
                assertTrue(elapsedMillis >= totalDelayMillis);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   255
                if (elapsedMillis <= cycles * delay)
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
                    return;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   257
                // else retry with longer delay
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   258
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   259
            fail("unexpected execution rate");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   260
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   261
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   262
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   263
    /**
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   264
     * scheduleWithFixedDelay executes series of tasks with given period.
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   265
     * Eventually, it must hold that each task starts at least delay and at
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   266
     * most 2 * delay after the termination of the previous task.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   267
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   268
    public void testFixedDelaySequence() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   269
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   270
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   271
            for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) {
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   272
                final long startTime = System.nanoTime();
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   273
                final AtomicLong previous = new AtomicLong(startTime);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   274
                final AtomicBoolean tryLongerDelay = new AtomicBoolean(false);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   275
                final int cycles = 8;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
                final CountDownLatch done = new CountDownLatch(cycles);
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   277
                final int d = delay;
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   278
                final Runnable task = new CheckedRunnable() {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   279
                    public void realRun() {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   280
                        long now = System.nanoTime();
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   281
                        long elapsedMillis
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   282
                            = NANOSECONDS.toMillis(now - previous.get());
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   283
                        if (done.getCount() == cycles) { // first execution
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   284
                            if (elapsedMillis >= d)
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   285
                                tryLongerDelay.set(true);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   286
                        } else {
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   287
                            assertTrue(elapsedMillis >= d);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   288
                            if (elapsedMillis >= 2 * d)
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   289
                                tryLongerDelay.set(true);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   290
                        }
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   291
                        previous.set(now);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   292
                        done.countDown();
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   293
                    }};
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   294
                final ScheduledFuture periodicTask =
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   295
                    p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS);
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   296
                final int totalDelayMillis = (cycles - 1) * delay;
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   297
                await(done, totalDelayMillis + cycles * LONG_DELAY_MS);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   298
                periodicTask.cancel(true);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   299
                final long elapsedMillis = millisElapsedSince(startTime);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   300
                assertTrue(elapsedMillis >= totalDelayMillis);
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   301
                if (!tryLongerDelay.get())
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
                    return;
36231
52e0ee9847fb 8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents: 35394
diff changeset
   303
                // else retry with longer delay
35394
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
            fail("unexpected execution rate");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   307
    }
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
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   310
     * Submitting null tasks throws NullPointerException
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   311
     */
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   312
    public void testNullTaskSubmission() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   313
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   314
        try (PoolCleaner cleaner = cleaner(p)) {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   315
            assertNullTaskSubmissionThrowsNullPointerException(p);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   316
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   317
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   318
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   319
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   320
     * Submitted tasks are rejected when shutdown
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   321
     */
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   322
    public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   323
        final CustomExecutor p = new CustomExecutor(2);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   324
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   325
        final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   326
        final CountDownLatch done = new CountDownLatch(1);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   327
        final Runnable r = () -> {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   328
            threadsStarted.countDown();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   329
            for (;;) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   330
                try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   331
                    done.await();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   332
                    return;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   333
                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   334
            }};
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   335
        final Callable<Boolean> c = () -> {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   336
            threadsStarted.countDown();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   337
            for (;;) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   338
                try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   339
                    done.await();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   340
                    return Boolean.TRUE;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   341
                } catch (InterruptedException shutdownNowDeliberatelyIgnored) {}
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   342
            }};
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   343
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   344
        try (PoolCleaner cleaner = cleaner(p, done)) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   345
            for (int i = p.getCorePoolSize(); i--> 0; ) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   346
                switch (rnd.nextInt(4)) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   347
                case 0: p.execute(r); break;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   348
                case 1: assertFalse(p.submit(r).isDone()); break;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   349
                case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   350
                case 3: assertFalse(p.submit(c).isDone()); break;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   351
                }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   352
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   353
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   354
            // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   355
            await(threadsStarted);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   356
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   357
            if (rnd.nextBoolean())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   358
                p.shutdownNow();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   359
            else
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   360
                p.shutdown();
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   361
            // Pool is shutdown, but not yet terminated
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   362
            assertTaskSubmissionsAreRejected(p);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   363
            assertFalse(p.isTerminated());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   364
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   365
            done.countDown();   // release blocking tasks
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   366
            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   367
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   368
            assertTaskSubmissionsAreRejected(p);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   369
        }
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   370
        assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount());
35394
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
     * getActiveCount increases but doesn't overestimate, when a
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   375
     * thread becomes active
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   376
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   377
    public void testGetActiveCount() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   378
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   379
        final ThreadPoolExecutor p = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   380
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   381
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   382
            assertEquals(0, p.getActiveCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   383
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   384
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   385
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   386
                    assertEquals(1, p.getActiveCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   387
                    await(done);
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
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   390
            assertEquals(1, p.getActiveCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   391
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   392
    }
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
     * getCompletedTaskCount increases, but doesn't overestimate,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
     * when tasks complete
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   397
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   398
    public void testGetCompletedTaskCount() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   399
        final ThreadPoolExecutor p = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   400
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   401
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   402
            final CountDownLatch threadProceed = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   403
            final CountDownLatch threadDone = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   404
            assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   405
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
                    assertEquals(0, p.getCompletedTaskCount());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   409
                    await(threadProceed);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
                    threadDone.countDown();
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
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   413
            assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   414
            threadProceed.countDown();
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   415
            await(threadDone);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   416
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   417
            while (p.getCompletedTaskCount() != 1) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   418
                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   419
                    fail("timed out");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   420
                Thread.yield();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   425
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   426
     * getCorePoolSize returns size given in constructor if not otherwise set
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   427
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   428
    public void testGetCorePoolSize() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   429
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   430
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   431
            assertEquals(1, p.getCorePoolSize());
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   434
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   435
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
     * getLargestPoolSize increases, but doesn't overestimate, when
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
     * multiple threads active
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
    public void testGetLargestPoolSize() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
        final int THREADS = 3;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   441
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
        final ThreadPoolExecutor p = new CustomExecutor(THREADS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
            assertEquals(0, p.getLargestPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   446
            for (int i = 0; i < THREADS; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
                p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
                    public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
                        threadsStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
                        await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   451
                        assertEquals(THREADS, p.getLargestPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
                    }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   453
            await(threadsStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
            assertEquals(THREADS, p.getLargestPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
        assertEquals(THREADS, p.getLargestPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   458
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
     * getPoolSize increases, but doesn't overestimate, when threads
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   461
     * become active
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
    public void testGetPoolSize() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
        final ThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
            assertEquals(0, p.getPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   469
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
                    assertEquals(1, p.getPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
                    await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   474
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
            assertEquals(1, p.getPoolSize());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   477
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
     * getTaskCount increases, but doesn't overestimate, when tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
     * submitted
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
    public void testGetTaskCount() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
        final int TASKS = 3;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
        final ThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   489
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   490
            assertEquals(0, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   491
            assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   494
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   495
                    await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   496
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   497
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
            assertEquals(1, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   499
            assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   500
            for (int i = 0; i < TASKS; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   501
                assertEquals(1 + i, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   502
                p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   503
                    public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   504
                        threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
                        assertEquals(1 + TASKS, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
                        await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
                    }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   509
            assertEquals(1 + TASKS, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
            assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   512
        assertEquals(1 + TASKS, p.getTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   513
        assertEquals(1 + TASKS, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   515
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   517
     * getThreadFactory returns factory in constructor if not set
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
    public void testGetThreadFactory() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   520
        final ThreadFactory threadFactory = new SimpleThreadFactory();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   521
        final CustomExecutor p = new CustomExecutor(1, threadFactory);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   522
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   523
            assertSame(threadFactory, p.getThreadFactory());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   525
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   527
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   528
     * setThreadFactory sets the thread factory returned by getThreadFactory
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   529
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   530
    public void testSetThreadFactory() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
        final ThreadFactory threadFactory = new SimpleThreadFactory();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   533
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
            p.setThreadFactory(threadFactory);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
            assertSame(threadFactory, p.getThreadFactory());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   537
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
     * setThreadFactory(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   541
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
    public void testSetThreadFactoryNull() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   544
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   545
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
                p.setThreadFactory(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   548
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   549
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   553
     * isShutdown is false before shutdown, true after
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
    public void testIsShutdown() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   556
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   557
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
            assertFalse(p.isShutdown());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
            try { p.shutdown(); } catch (SecurityException ok) { return; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   560
            assertTrue(p.isShutdown());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   564
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
     * isTerminated is false before termination, true after
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   566
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
    public void testIsTerminated() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   568
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
        final ThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   570
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   572
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   574
                    assertFalse(p.isTerminated());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   575
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   576
                    await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   577
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   578
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   579
            assertFalse(p.isTerminated());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
            assertFalse(p.isTerminating());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   581
            done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   582
            try { p.shutdown(); } catch (SecurityException ok) { return; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   583
            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   584
            assertTrue(p.isTerminated());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   585
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   586
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   587
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   588
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   589
     * isTerminating is not true when running or when terminated
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   590
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   591
    public void testIsTerminating() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   592
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   593
        final ThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   594
        try (PoolCleaner cleaner = cleaner(p)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   595
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   596
            assertFalse(p.isTerminating());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
            p.execute(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
                public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
                    assertFalse(p.isTerminating());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
                    threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   601
                    await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
                }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
            assertFalse(p.isTerminating());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
            done.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   606
            try { p.shutdown(); } catch (SecurityException ok) { return; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   607
            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   608
            assertTrue(p.isTerminated());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   609
            assertFalse(p.isTerminating());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   610
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   614
     * getQueue returns the work queue, which contains queued tasks
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
    public void testGetQueue() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   617
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
            ScheduledFuture[] tasks = new ScheduledFuture[5];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   622
            for (int i = 0; i < tasks.length; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
                Runnable r = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
                    public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   625
                        threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
                        await(done);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   627
                    }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   628
                tasks[i] = p.schedule(r, 1, MILLISECONDS);
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
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
            BlockingQueue<Runnable> q = p.getQueue();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   632
            assertTrue(q.contains(tasks[tasks.length - 1]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   633
            assertFalse(q.contains(tasks[0]));
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   636
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
     * remove(task) removes queued task, and fails to remove active task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   639
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   640
    public void testRemove() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   641
        final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   642
        final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   643
        try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   644
            ScheduledFuture[] tasks = new ScheduledFuture[5];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   645
            final CountDownLatch threadStarted = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   646
            for (int i = 0; i < tasks.length; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   647
                Runnable r = new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   648
                    public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   649
                        threadStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   650
                        await(done);
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
                tasks[i] = p.schedule(r, 1, MILLISECONDS);
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
            await(threadStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   655
            BlockingQueue<Runnable> q = p.getQueue();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   656
            assertFalse(p.remove((Runnable)tasks[0]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   657
            assertTrue(q.contains((Runnable)tasks[4]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   658
            assertTrue(q.contains((Runnable)tasks[3]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   659
            assertTrue(p.remove((Runnable)tasks[4]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   660
            assertFalse(p.remove((Runnable)tasks[4]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   661
            assertFalse(q.contains((Runnable)tasks[4]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   662
            assertTrue(q.contains((Runnable)tasks[3]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
            assertTrue(p.remove((Runnable)tasks[3]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   664
            assertFalse(q.contains((Runnable)tasks[3]));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   665
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   666
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   667
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   668
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   669
     * purge removes cancelled tasks from the queue
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   670
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   671
    public void testPurge() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   672
        final ScheduledFuture[] tasks = new ScheduledFuture[5];
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   673
        final Runnable releaser = new Runnable() { public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   674
            for (ScheduledFuture task : tasks)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   675
                if (task != null) task.cancel(true); }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   676
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   677
        try (PoolCleaner cleaner = cleaner(p, releaser)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   678
            for (int i = 0; i < tasks.length; i++)
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 48541
diff changeset
   679
                tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS),
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   680
                                      LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   681
            int max = tasks.length;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   682
            if (tasks[4].cancel(true)) --max;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   683
            if (tasks[3].cancel(true)) --max;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   684
            // There must eventually be an interference-free point at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
            // which purge will not fail. (At worst, when queue is empty.)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
            long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
            do {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
                p.purge();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   689
                long count = p.getTaskCount();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
                if (count == max)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
                    return;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
            } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   693
            fail("Purge failed to remove cancelled tasks");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   694
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   695
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   698
     * shutdownNow returns a list containing tasks that were not run,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   699
     * and those tasks are drained from the queue
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   700
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   701
    public void testShutdownNow() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
        final int poolSize = 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
        final int count = 5;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
        final AtomicInteger ran = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
        final CustomExecutor p = new CustomExecutor(poolSize);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
        final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
        Runnable waiter = new CheckedRunnable() { public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
            threadsStarted.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
            try {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52730
diff changeset
   710
                MILLISECONDS.sleep(LONGER_DELAY_MS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   711
            } catch (InterruptedException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
            ran.getAndIncrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
        }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   714
        for (int i = 0; i < count; i++)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   715
            p.execute(waiter);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
        await(threadsStarted);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
        assertEquals(poolSize, p.getActiveCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
        assertEquals(0, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
        final List<Runnable> queuedTasks;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   721
            queuedTasks = p.shutdownNow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   722
        } catch (SecurityException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   723
            return; // Allowed in case test doesn't have privs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   724
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   725
        assertTrue(p.isShutdown());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   726
        assertTrue(p.getQueue().isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   727
        assertEquals(count - poolSize, queuedTasks.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   728
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   729
        assertTrue(p.isTerminated());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
        assertEquals(poolSize, ran.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
        assertEquals(poolSize, p.getCompletedTaskCount());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   732
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   733
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
     * shutdownNow returns a list containing tasks that were not run,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
     * and those tasks are drained from the queue
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   737
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   738
    public void testShutdownNow_delayedTasks() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   739
        final CustomExecutor p = new CustomExecutor(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   740
        List<ScheduledFuture> tasks = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   741
        for (int i = 0; i < 3; i++) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   742
            Runnable r = new NoOpRunnable();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   743
            tasks.add(p.schedule(r, 9, SECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   744
            tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   745
            tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS));
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
        if (testImplementationDetails)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   748
            assertEquals(new HashSet(tasks), new HashSet(p.getQueue()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   749
        final List<Runnable> queuedTasks;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   750
        try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   751
            queuedTasks = p.shutdownNow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   752
        } catch (SecurityException ok) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   753
            return; // Allowed in case test doesn't have privs
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
        assertTrue(p.isShutdown());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
        assertTrue(p.getQueue().isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
        if (testImplementationDetails)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
            assertEquals(new HashSet(tasks), new HashSet(queuedTasks));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
        assertEquals(tasks.size(), queuedTasks.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   760
        for (ScheduledFuture task : tasks) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   761
            assertFalse(((CustomTask)task).ran);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   762
            assertFalse(task.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
            assertFalse(task.isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   764
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
        assertTrue(p.isTerminated());
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
     * By default, periodic tasks are cancelled at shutdown.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
     * By default, delayed tasks keep running after shutdown.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
     * Check that changing the default values work:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
     * - setExecuteExistingDelayedTasksAfterShutdownPolicy
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
     * - setContinueExistingPeriodicTasksAfterShutdownPolicy
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   775
     */
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   776
    @SuppressWarnings("FutureReturnValueIgnored")
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   777
    public void testShutdown_cancellation() throws Exception {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   778
        final int poolSize = 4;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
        final CustomExecutor p = new CustomExecutor(poolSize);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   780
        final BlockingQueue<Runnable> q = p.getQueue();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   781
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   782
        final long delay = rnd.nextInt(2);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   783
        final int rounds = rnd.nextInt(1, 3);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   784
        final boolean effectiveDelayedPolicy;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   785
        final boolean effectivePeriodicPolicy;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   786
        final boolean effectiveRemovePolicy;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   787
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   788
        if (rnd.nextBoolean())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   789
            p.setExecuteExistingDelayedTasksAfterShutdownPolicy(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   790
                effectiveDelayedPolicy = rnd.nextBoolean());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   791
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   792
            effectiveDelayedPolicy = true;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
        assertEquals(effectiveDelayedPolicy,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
                     p.getExecuteExistingDelayedTasksAfterShutdownPolicy());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   795
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   796
        if (rnd.nextBoolean())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   797
            p.setContinueExistingPeriodicTasksAfterShutdownPolicy(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   798
                effectivePeriodicPolicy = rnd.nextBoolean());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   799
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   800
            effectivePeriodicPolicy = false;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   801
        assertEquals(effectivePeriodicPolicy,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   802
                     p.getContinueExistingPeriodicTasksAfterShutdownPolicy());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   803
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   804
        if (rnd.nextBoolean())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   805
            p.setRemoveOnCancelPolicy(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   806
                effectiveRemovePolicy = rnd.nextBoolean());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   807
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   808
            effectiveRemovePolicy = false;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   809
        assertEquals(effectiveRemovePolicy,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   810
                     p.getRemoveOnCancelPolicy());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   811
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   812
        final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   813
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   814
        // Strategy: Wedge the pool with one wave of "blocker" tasks,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   815
        // then add a second wave that waits in the queue until unblocked.
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   816
        final AtomicInteger ran = new AtomicInteger(0);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   817
        final CountDownLatch poolBlocked = new CountDownLatch(poolSize);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   818
        final CountDownLatch unblock = new CountDownLatch(1);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   819
        final RuntimeException exception = new RuntimeException();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   820
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   821
        class Task implements Runnable {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   822
            public void run() {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   823
                try {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   824
                    ran.getAndIncrement();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   825
                    poolBlocked.countDown();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   826
                    await(unblock);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   827
                } catch (Throwable fail) { threadUnexpectedException(fail); }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   828
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   829
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   830
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   831
        class PeriodicTask extends Task {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   832
            PeriodicTask(int rounds) { this.rounds = rounds; }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   833
            int rounds;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   834
            public void run() {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   835
                if (--rounds == 0) super.run();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   836
                // throw exception to surely terminate this periodic task,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   837
                // but in a separate execution and in a detectable way.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   838
                if (rounds == -1) throw exception;
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   839
            }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   840
        }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   841
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   842
        Runnable task = new Task();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   843
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   844
        List<Future<?>> immediates = new ArrayList<>();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   845
        List<Future<?>> delayeds   = new ArrayList<>();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   846
        List<Future<?>> periodics  = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   847
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   848
        immediates.add(p.submit(task));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   849
        delayeds.add(p.schedule(task, delay, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   850
        periodics.add(p.scheduleAtFixedRate(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   851
                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   852
        periodics.add(p.scheduleWithFixedDelay(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   853
                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   854
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   855
        await(poolBlocked);
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   856
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   857
        assertEquals(poolSize, ran.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   858
        assertEquals(poolSize, p.getActiveCount());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   859
        assertTrue(q.isEmpty());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   860
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   861
        // Add second wave of tasks.
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   862
        immediates.add(p.submit(task));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   863
        delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   864
        periodics.add(p.scheduleAtFixedRate(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   865
                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   866
        periodics.add(p.scheduleWithFixedDelay(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   867
                          new PeriodicTask(rounds), delay, 1, MILLISECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   868
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   869
        assertEquals(poolSize, q.size());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   870
        assertEquals(poolSize, ran.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   871
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   872
        immediates.forEach(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   873
            f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   874
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
   875
        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   876
            .forEach(f -> assertFalse(f.isDone()));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   877
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   878
        try { p.shutdown(); } catch (SecurityException ok) { return; }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   879
        assertTrue(p.isShutdown());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   880
        assertTrue(p.isTerminating());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   881
        assertFalse(p.isTerminated());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   882
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   883
        if (rnd.nextBoolean())
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   884
            assertThrows(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   885
                RejectedExecutionException.class,
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   886
                () -> p.submit(task),
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   887
                () -> p.schedule(task, 1, SECONDS),
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   888
                () -> p.scheduleAtFixedRate(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   889
                    new PeriodicTask(1), 1, 1, SECONDS),
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   890
                () -> p.scheduleWithFixedDelay(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   891
                    new PeriodicTask(2), 1, 1, SECONDS));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   892
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   893
        assertTrue(q.contains(immediates.get(1)));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   894
        assertTrue(!effectiveDelayedPolicy
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   895
                   ^ q.contains(delayeds.get(1)));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   896
        assertTrue(!effectivePeriodicPolicy
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   897
                   ^ q.containsAll(periodics.subList(2, 4)));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   898
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   899
        immediates.forEach(f -> assertFalse(f.isDone()));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   900
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   901
        assertFalse(delayeds.get(0).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   902
        if (effectiveDelayedPolicy)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   903
            assertFalse(delayeds.get(1).isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   904
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   905
            assertTrue(delayeds.get(1).isCancelled());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   906
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   907
        if (effectivePeriodicPolicy)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   908
            periodics.forEach(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   909
                f -> {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   910
                    assertFalse(f.isDone());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   911
                    if (!periodicTasksContinue) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   912
                        assertTrue(f.cancel(false));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   913
                        assertTrue(f.isCancelled());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   914
                    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   915
                });
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   916
        else {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   917
            periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone()));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   918
            periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled()));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   919
        }
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   920
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   921
        unblock.countDown();    // Release all pool threads
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   922
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   923
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   924
        assertFalse(p.isTerminating());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
        assertTrue(p.isTerminated());
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   926
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   927
        assertTrue(q.isEmpty());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   928
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
   929
        Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream)
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   930
            .forEach(f -> assertTrue(f.isDone()));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   931
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   932
        for (Future<?> f : immediates) assertNull(f.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   933
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   934
        assertNull(delayeds.get(0).get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   935
        if (effectiveDelayedPolicy)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   936
            assertNull(delayeds.get(1).get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   937
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   938
            assertTrue(delayeds.get(1).isCancelled());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   939
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   940
        if (periodicTasksContinue)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   941
            periodics.forEach(
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   942
                f -> {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   943
                    try { f.get(); }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   944
                    catch (ExecutionException success) {
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   945
                        assertSame(exception, success.getCause());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   946
                    }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   947
                    catch (Throwable fail) { threadUnexpectedException(fail); }
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   948
                });
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   949
        else
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   950
            periodics.forEach(f -> assertTrue(f.isCancelled()));
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   951
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   952
        assertEquals(poolSize + 1
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   953
                     + (effectiveDelayedPolicy ? 1 : 0)
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   954
                     + (periodicTasksContinue ? 2 : 0),
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   955
                     ran.get());
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
   956
    }
35394
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   959
     * completed submit of callable returns result
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   960
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   961
    public void testSubmitCallable() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   962
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   963
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   964
            Future<String> future = e.submit(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   965
            String result = future.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   966
            assertSame(TEST_STRING, result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   967
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   968
    }
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
     * completed submit of runnable returns successfully
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   972
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   973
    public void testSubmitRunnable() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   974
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   976
            Future<?> future = e.submit(new NoOpRunnable());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
            future.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   978
            assertTrue(future.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   980
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   981
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   982
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   983
     * completed submit of (runnable, result) returns result
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   984
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   985
    public void testSubmitRunnable2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   986
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   987
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   988
            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   989
            String result = future.get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   990
            assertSame(TEST_STRING, result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   991
        }
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   994
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   995
     * invokeAny(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   996
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   997
    public void testInvokeAny1() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   998
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   999
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1000
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1001
                e.invokeAny(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1002
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1003
            } catch (NullPointerException success) {}
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
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1008
     * invokeAny(empty collection) throws IllegalArgumentException
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1009
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1010
    public void testInvokeAny2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1011
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1012
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1013
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1014
                e.invokeAny(new ArrayList<Callable<String>>());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1015
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1016
            } catch (IllegalArgumentException success) {}
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1019
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1020
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1021
     * invokeAny(c) throws NPE if c has null elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1022
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1023
    public void testInvokeAny3() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1024
        final CountDownLatch latch = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1025
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1026
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1027
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1028
            l.add(latchAwaitingStringTask(latch));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1029
            l.add(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1030
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1031
                e.invokeAny(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1032
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1033
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1034
            latch.countDown();
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
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1038
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1039
     * invokeAny(c) throws ExecutionException if no task completes
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
    public void testInvokeAny4() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1042
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1043
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1044
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1045
            l.add(new NPETask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1046
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1047
                e.invokeAny(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1048
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1049
            } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1050
                assertTrue(success.getCause() instanceof NullPointerException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1051
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1052
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1053
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1054
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1055
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1056
     * invokeAny(c) returns result of some task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1057
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1058
    public void testInvokeAny5() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1059
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1060
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1061
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1062
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1063
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1064
            String result = e.invokeAny(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1065
            assertSame(TEST_STRING, result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1066
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1067
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1068
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1069
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1070
     * invokeAll(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1071
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1072
    public void testInvokeAll1() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1073
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1074
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1075
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1076
                e.invokeAll(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1077
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1078
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1079
        }
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
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1083
     * invokeAll(empty collection) returns empty list
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1084
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1085
    public void testInvokeAll2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1086
        final ExecutorService e = new CustomExecutor(2);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1087
        final Collection<Callable<String>> emptyCollection
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1088
            = Collections.emptyList();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1089
        try (PoolCleaner cleaner = cleaner(e)) {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1090
            List<Future<String>> r = e.invokeAll(emptyCollection);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1091
            assertTrue(r.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1092
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1093
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1096
     * invokeAll(c) throws NPE if c has null elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1097
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1098
    public void testInvokeAll3() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1099
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1100
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1101
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1102
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1103
            l.add(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1104
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1105
                e.invokeAll(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1106
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1107
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1108
        }
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
     * get of invokeAll(c) throws exception on failed task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1113
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1114
    public void testInvokeAll4() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1115
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1116
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1117
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1118
            l.add(new NPETask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1119
            List<Future<String>> futures = e.invokeAll(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1120
            assertEquals(1, futures.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1121
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1122
                futures.get(0).get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1123
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1124
            } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1125
                assertTrue(success.getCause() instanceof NullPointerException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1126
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1127
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1128
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1129
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1130
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1131
     * invokeAll(c) returns results of all completed tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1132
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1133
    public void testInvokeAll5() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1134
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1135
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1136
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1137
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1138
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1139
            List<Future<String>> futures = e.invokeAll(l);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1140
            assertEquals(2, futures.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1141
            for (Future<String> future : futures)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1142
                assertSame(TEST_STRING, future.get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1143
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1144
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1145
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
     * timed invokeAny(null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1148
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1149
    public void testTimedInvokeAny1() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1150
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1151
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1152
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1153
                e.invokeAny(null, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1154
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1155
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1156
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1157
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1158
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1159
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1160
     * timed invokeAny(,,null) throws NPE
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1161
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1162
    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1163
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1164
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1165
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1166
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1167
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1168
                e.invokeAny(l, randomTimeout(), null);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1169
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1170
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1171
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1172
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1173
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1174
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1175
     * timed invokeAny(empty collection) throws IllegalArgumentException
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1176
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1177
    public void testTimedInvokeAny2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1178
        final ExecutorService e = new CustomExecutor(2);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1179
        final Collection<Callable<String>> emptyCollection
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1180
            = Collections.emptyList();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1181
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1182
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1183
                e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1184
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1185
            } catch (IllegalArgumentException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1186
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1187
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1188
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1189
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1190
     * timed invokeAny(c) throws NPE if c has null elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1191
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1192
    public void testTimedInvokeAny3() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1193
        CountDownLatch latch = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1194
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1195
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1196
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1197
            l.add(latchAwaitingStringTask(latch));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1198
            l.add(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1199
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1200
                e.invokeAny(l, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1201
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1202
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1203
            latch.countDown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1204
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1205
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1206
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1207
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1208
     * timed invokeAny(c) throws ExecutionException if no task completes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1209
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1210
    public void testTimedInvokeAny4() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1211
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1212
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1213
            long startTime = System.nanoTime();
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1214
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1215
            l.add(new NPETask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1216
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1217
                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1218
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1219
            } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1220
                assertTrue(success.getCause() instanceof NullPointerException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1221
            }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1222
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1223
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1224
    }
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
     * timed invokeAny(c) returns result of some task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1228
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1229
    public void testTimedInvokeAny5() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1230
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1231
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1232
            long startTime = System.nanoTime();
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1233
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1234
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1235
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1236
            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1237
            assertSame(TEST_STRING, result);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1238
            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1241
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1242
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1243
     * timed invokeAll(null) throws NullPointerException
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1244
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1245
    public void testTimedInvokeAll1() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1246
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1247
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1248
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1249
                e.invokeAll(null, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1250
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1251
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1252
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1253
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1254
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1255
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1256
     * timed invokeAll(,,null) throws NullPointerException
35394
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
    public void testTimedInvokeAllNullTimeUnit() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1259
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1260
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1261
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1262
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1263
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1264
                e.invokeAll(l, randomTimeout(), null);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1265
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1266
            } catch (NullPointerException success) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1267
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1268
    }
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
    /**
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1271
     * timed invokeAll(empty collection) returns empty list
35394
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
    public void testTimedInvokeAll2() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1274
        final ExecutorService e = new CustomExecutor(2);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1275
        final Collection<Callable<String>> emptyCollection
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1276
            = Collections.emptyList();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1277
        try (PoolCleaner cleaner = cleaner(e)) {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1278
            List<Future<String>> r =
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1279
                e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1280
            assertTrue(r.isEmpty());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1281
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1282
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1283
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1284
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1285
     * timed invokeAll(c) throws NPE if c has null elements
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1286
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1287
    public void testTimedInvokeAll3() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1288
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1289
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1290
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1291
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1292
            l.add(null);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1293
            try {
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1294
                e.invokeAll(l, randomTimeout(), randomTimeUnit());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1295
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1296
            } catch (NullPointerException success) {}
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1299
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1300
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1301
     * get of element of invokeAll(c) throws exception on failed task
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1302
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1303
    public void testTimedInvokeAll4() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1304
        final ExecutorService e = new CustomExecutor(2);
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1305
        final Collection<Callable<String>> c = new ArrayList<>();
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1306
        c.add(new NPETask());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1307
        try (PoolCleaner cleaner = cleaner(e)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1308
            List<Future<String>> futures =
45936
d564ef4bed8f 8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents: 43522
diff changeset
  1309
                e.invokeAll(c, LONG_DELAY_MS, MILLISECONDS);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1310
            assertEquals(1, futures.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1311
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1312
                futures.get(0).get();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1313
                shouldThrow();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1314
            } catch (ExecutionException success) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1315
                assertTrue(success.getCause() instanceof NullPointerException);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1316
            }
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1319
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1320
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1321
     * timed invokeAll(c) returns results of all completed tasks
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1322
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1323
    public void testTimedInvokeAll5() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1324
        final ExecutorService e = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1325
        try (PoolCleaner cleaner = cleaner(e)) {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42927
diff changeset
  1326
            List<Callable<String>> l = new ArrayList<>();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1327
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1328
            l.add(new StringTask());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1329
            List<Future<String>> futures =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1330
                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1331
            assertEquals(2, futures.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1332
            for (Future<String> future : futures)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1333
                assertSame(TEST_STRING, future.get());
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
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1336
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1337
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1338
     * timed invokeAll(c) cancels tasks not completed by timeout
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1339
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1340
    public void testTimedInvokeAll6() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1341
        for (long timeout = timeoutMillis();;) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1342
            final CountDownLatch done = new CountDownLatch(1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1343
            final Callable<String> waiter = new CheckedCallable<String>() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1344
                public String realCall() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1345
                    try { done.await(LONG_DELAY_MS, MILLISECONDS); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1346
                    catch (InterruptedException ok) {}
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1347
                    return "1"; }};
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1348
            final ExecutorService p = new CustomExecutor(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1349
            try (PoolCleaner cleaner = cleaner(p, done)) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1350
                List<Callable<String>> tasks = new ArrayList<>();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1351
                tasks.add(new StringTask("0"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1352
                tasks.add(waiter);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1353
                tasks.add(new StringTask("2"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1354
                long startTime = System.nanoTime();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1355
                List<Future<String>> futures =
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1356
                    p.invokeAll(tasks, timeout, MILLISECONDS);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1357
                assertEquals(tasks.size(), futures.size());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1358
                assertTrue(millisElapsedSince(startTime) >= timeout);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1359
                for (Future future : futures)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1360
                    assertTrue(future.isDone());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1361
                assertTrue(futures.get(1).isCancelled());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1362
                try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1363
                    assertEquals("0", futures.get(0).get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1364
                    assertEquals("2", futures.get(2).get());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1365
                    break;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1366
                } catch (CancellationException retryWithLongerTimeout) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1367
                    timeout *= 2;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1368
                    if (timeout >= LONG_DELAY_MS / 2)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1369
                        fail("expected exactly one task to be cancelled");
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1370
                }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1371
            }
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
}