1/*
2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6560953
27 * @summary Test ScheduledThreadPoolExecutor.decorateTask
28 */
29
30import java.util.concurrent.CountDownLatch;
31import java.util.concurrent.Delayed;
32import java.util.concurrent.ExecutionException;
33import java.util.concurrent.RunnableScheduledFuture;
34import java.util.concurrent.ScheduledThreadPoolExecutor;
35import java.util.concurrent.TimeUnit;
36import java.util.concurrent.TimeoutException;
37import java.util.concurrent.atomic.AtomicInteger;
38
39public class DecorateTask {
40    Runnable countDownTask(final CountDownLatch latch) {
41        return new Runnable() { public void run() {
42            latch.countDown();
43            if (latch.getCount() <= 0)
44                throw new RuntimeException("done");
45        }};}
46
47    void test(String[] args) throws Throwable {
48        final int jobs = 100;
49        final AtomicInteger decoratorCount = new AtomicInteger(0);
50        final ScheduledThreadPoolExecutor pool =
51            new ScheduledThreadPoolExecutor(10) {
52                protected <V> RunnableScheduledFuture<V> decorateTask(
53                    final Runnable runnable,
54                    final RunnableScheduledFuture<V> task) {
55                    return new RunnableScheduledFuture<V>() {
56                        public void run() {
57                            decoratorCount.incrementAndGet();
58                            task.run();
59                        }
60                        public boolean isPeriodic() {
61                            return task.isPeriodic();
62                        }
63                        public boolean cancel(boolean mayInterruptIfRunning) {
64                            return task.cancel(mayInterruptIfRunning);
65                        }
66                        public boolean isCancelled() {
67                            return task.isCancelled();
68                        }
69                        public boolean isDone() {
70                            return task.isDone();
71                        }
72                        public V get()
73                            throws InterruptedException, ExecutionException {
74                            return task.get();
75                        }
76                        public V get(long timeout, TimeUnit unit)
77                            throws InterruptedException, ExecutionException, TimeoutException {
78                            return task.get(timeout, unit);
79                        }
80                        public long getDelay(TimeUnit unit) {
81                            return task.getDelay(unit);
82                        }
83                        public int compareTo(Delayed o) {
84                            return task.compareTo(o);
85                        }};}};
86        final CountDownLatch latch1 = new CountDownLatch(jobs);
87        final CountDownLatch latch2 = new CountDownLatch(jobs);
88        pool.scheduleAtFixedRate(countDownTask(latch1), 0L, 1L, TimeUnit.NANOSECONDS);
89        pool.scheduleWithFixedDelay(countDownTask(latch2), 0L, 1L, TimeUnit.NANOSECONDS);
90        latch1.await();
91        latch2.await();
92        pool.shutdown();
93        pool.awaitTermination(1L, TimeUnit.MINUTES);
94        equal(decoratorCount.get(), 2 * jobs);
95    }
96
97    //--------------------- Infrastructure ---------------------------
98    volatile int passed = 0, failed = 0;
99    void pass() {passed++;}
100    void fail() {failed++; Thread.dumpStack();}
101    void fail(String msg) {System.err.println(msg); fail();}
102    void unexpected(Throwable t) {failed++; t.printStackTrace();}
103    void check(boolean cond) {if (cond) pass(); else fail();}
104    void equal(Object x, Object y) {
105        if (x == null ? y == null : x.equals(y)) pass();
106        else fail(x + " not equal to " + y);}
107    public static void main(String[] args) throws Throwable {
108        new DecorateTask().instanceMain(args);}
109    void instanceMain(String[] args) throws Throwable {
110        try {test(args);} catch (Throwable t) {unexpected(t);}
111        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
112        if (failed > 0) throw new AssertionError("Some tests failed");}
113}
114