blob: b1dae53569d744c43b05be6e678377032b3f5450 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6560953
27 * @summary Test ScheduledThreadPoolExecutor.decorateTask
28 */
29
30import java.util.concurrent.*;
31import java.util.concurrent.atomic.AtomicInteger;
32
33public class DecorateTask {
34 Runnable countDownTask(final CountDownLatch latch) {
35 return new Runnable() { public void run() {
36 latch.countDown();
37 if (latch.getCount() <= 0)
38 throw new RuntimeException("done");
39 }};}
40
41 void test(String[] args) throws Throwable {
42 final int jobs = 100;
43 final AtomicInteger decoratorCount = new AtomicInteger(0);
44 final ScheduledThreadPoolExecutor pool =
45 new ScheduledThreadPoolExecutor(10) {
46 protected <V> RunnableScheduledFuture<V> decorateTask(
47 final Runnable runnable,
48 final RunnableScheduledFuture<V> task) {
49 return new RunnableScheduledFuture<V>() {
50 public void run() {
51 decoratorCount.incrementAndGet();
52 task.run();
53 }
54 public boolean isPeriodic() {
55 return task.isPeriodic();
56 }
57 public boolean cancel(boolean mayInterruptIfRunning) {
58 return task.cancel(mayInterruptIfRunning);
59 }
60 public boolean isCancelled() {
61 return task.isCancelled();
62 }
63 public boolean isDone() {
64 return task.isDone();
65 }
66 public V get()
67 throws InterruptedException, ExecutionException {
68 return task.get();
69 }
70 public V get(long timeout, TimeUnit unit)
71 throws InterruptedException, ExecutionException, TimeoutException {
72 return task.get(timeout, unit);
73 }
74 public long getDelay(TimeUnit unit) {
75 return task.getDelay(unit);
76 }
77 public int compareTo(Delayed o) {
78 return task.compareTo(o);
79 }};}};
80 final CountDownLatch latch1 = new CountDownLatch(jobs);
81 final CountDownLatch latch2 = new CountDownLatch(jobs);
82 pool.scheduleAtFixedRate(countDownTask(latch1), 0L, 1L, TimeUnit.NANOSECONDS);
83 pool.scheduleWithFixedDelay(countDownTask(latch2), 0L, 1L, TimeUnit.NANOSECONDS);
84 latch1.await();
85 latch2.await();
86 pool.shutdown();
87 pool.awaitTermination(1L, TimeUnit.MINUTES);
88 equal(decoratorCount.get(), 2 * jobs);
89 }
90
91 //--------------------- Infrastructure ---------------------------
92 volatile int passed = 0, failed = 0;
93 void pass() {passed++;}
94 void fail() {failed++; Thread.dumpStack();}
95 void fail(String msg) {System.err.println(msg); fail();}
96 void unexpected(Throwable t) {failed++; t.printStackTrace();}
97 void check(boolean cond) {if (cond) pass(); else fail();}
98 void equal(Object x, Object y) {
99 if (x == null ? y == null : x.equals(y)) pass();
100 else fail(x + " not equal to " + y);}
101 public static void main(String[] args) throws Throwable {
102 new DecorateTask().instanceMain(args);}
103 void instanceMain(String[] args) throws Throwable {
104 try {test(args);} catch (Throwable t) {unexpected(t);}
105 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
106 if (failed > 0) throw new AssertionError("Some tests failed");}
107}