J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 6464365 |
| 27 | * @summary Test state transitions; check protected methods are called |
| 28 | * @author Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | import java.util.concurrent.atomic.*; |
| 34 | |
| 35 | public class Customized { |
| 36 | static final AtomicLong doneCount = new AtomicLong(0); |
| 37 | static final AtomicLong setCount = new AtomicLong(0); |
| 38 | static final AtomicLong setExceptionCount = new AtomicLong(0); |
| 39 | |
| 40 | static void equal(long expected, AtomicLong actual) { |
| 41 | equal(expected, actual.get()); |
| 42 | } |
| 43 | |
| 44 | static void equalCounts(long x, long y, long z) { |
| 45 | equal(x, doneCount); |
| 46 | equal(y, setCount); |
| 47 | equal(z, setExceptionCount); |
| 48 | } |
| 49 | |
| 50 | static class MyFutureTask<V> extends FutureTask<V> { |
| 51 | MyFutureTask(Runnable r, V result) { super(r, result); } |
| 52 | protected void done() { |
| 53 | doneCount.getAndIncrement(); |
| 54 | super.done(); |
| 55 | } |
| 56 | protected void set(V v) { |
| 57 | setCount.getAndIncrement(); |
| 58 | super.set(v); |
| 59 | } |
| 60 | protected void setException(Throwable t) { |
| 61 | setExceptionCount.getAndIncrement(); |
| 62 | super.setException(t); |
| 63 | } |
| 64 | public boolean runAndReset() { |
| 65 | return super.runAndReset(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static <V> void checkReady(final FutureTask<V> task) { |
| 70 | check(! task.isDone()); |
| 71 | check(! task.isCancelled()); |
| 72 | THROWS(TimeoutException.class, |
| 73 | new Fun(){void f() throws Throwable { |
| 74 | task.get(0L, TimeUnit.SECONDS); }}); |
| 75 | } |
| 76 | |
| 77 | static <V> void checkDone(final FutureTask<V> task) { |
| 78 | try { |
| 79 | check(task.isDone()); |
| 80 | check(! task.isCancelled()); |
| 81 | check(task.get() != null); |
| 82 | } catch (Throwable t) { unexpected(t); } |
| 83 | } |
| 84 | |
| 85 | static <V> void checkCancelled(final FutureTask<V> task) { |
| 86 | check(task.isDone()); |
| 87 | check(task.isCancelled()); |
| 88 | THROWS(CancellationException.class, |
| 89 | new Fun(){void f() throws Throwable { |
| 90 | task.get(0L, TimeUnit.SECONDS); }}, |
| 91 | new Fun(){void f() throws Throwable { |
| 92 | task.get(); }}); |
| 93 | } |
| 94 | |
| 95 | static <V> void checkThrew(final FutureTask<V> task) { |
| 96 | check(task.isDone()); |
| 97 | check(! task.isCancelled()); |
| 98 | THROWS(ExecutionException.class, |
| 99 | new Fun(){void f() throws Throwable { |
| 100 | task.get(0L, TimeUnit.SECONDS); }}, |
| 101 | new Fun(){void f() throws Throwable { |
| 102 | task.get(); }}); |
| 103 | } |
| 104 | |
| 105 | static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) { |
| 106 | task.cancel(mayInterruptIfRunning); |
| 107 | checkCancelled(task); |
| 108 | } |
| 109 | |
| 110 | static <V> void run(FutureTask<V> task) { |
| 111 | boolean isCancelled = task.isCancelled(); |
| 112 | task.run(); |
| 113 | check(task.isDone()); |
| 114 | equal(isCancelled, task.isCancelled()); |
| 115 | } |
| 116 | |
| 117 | static void realMain(String[] args) throws Throwable { |
| 118 | final Runnable nop = new Runnable() { |
| 119 | public void run() {}}; |
| 120 | final Runnable bad = new Runnable() { |
| 121 | public void run() { throw new Error(); }}; |
| 122 | |
| 123 | try { |
| 124 | final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
| 125 | checkReady(task); |
| 126 | equalCounts(0,0,0); |
| 127 | check(task.runAndReset()); |
| 128 | checkReady(task); |
| 129 | equalCounts(0,0,0); |
| 130 | run(task); |
| 131 | checkDone(task); |
| 132 | equalCounts(1,1,0); |
| 133 | equal(42L, task.get()); |
| 134 | run(task); |
| 135 | checkDone(task); |
| 136 | equalCounts(1,1,0); |
| 137 | equal(42L, task.get()); |
| 138 | } catch (Throwable t) { unexpected(t); } |
| 139 | |
| 140 | try { |
| 141 | final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
| 142 | cancel(task, false); |
| 143 | equalCounts(2,1,0); |
| 144 | cancel(task, false); |
| 145 | equalCounts(2,1,0); |
| 146 | run(task); |
| 147 | equalCounts(2,1,0); |
| 148 | check(! task.runAndReset()); |
| 149 | } catch (Throwable t) { unexpected(t); } |
| 150 | |
| 151 | try { |
| 152 | final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L); |
| 153 | checkReady(task); |
| 154 | run(task); |
| 155 | checkThrew(task); |
| 156 | equalCounts(3,1,1); |
| 157 | run(task); |
| 158 | equalCounts(3,1,1); |
| 159 | } catch (Throwable t) { unexpected(t); } |
| 160 | |
| 161 | try { |
| 162 | final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
| 163 | checkReady(task); |
| 164 | task.set(99L); |
| 165 | checkDone(task); |
| 166 | equalCounts(4,2,1); |
| 167 | run(task); |
| 168 | equalCounts(4,2,1); |
| 169 | task.setException(new Throwable()); |
| 170 | checkDone(task); |
| 171 | equalCounts(4,2,2); |
| 172 | } catch (Throwable t) { unexpected(t); } |
| 173 | |
| 174 | try { |
| 175 | final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
| 176 | checkReady(task); |
| 177 | task.setException(new Throwable()); |
| 178 | checkThrew(task); |
| 179 | equalCounts(5,2,3); |
| 180 | run(task); |
| 181 | equalCounts(5,2,3); |
| 182 | task.set(99L); |
| 183 | checkThrew(task); |
| 184 | equalCounts(5,3,3); |
| 185 | } catch (Throwable t) { unexpected(t); } |
| 186 | |
| 187 | System.out.printf("doneCount=%d%n", doneCount.get()); |
| 188 | System.out.printf("setCount=%d%n", setCount.get()); |
| 189 | System.out.printf("setExceptionCount=%d%n", setExceptionCount.get()); |
| 190 | } |
| 191 | |
| 192 | //--------------------- Infrastructure --------------------------- |
| 193 | static volatile int passed = 0, failed = 0; |
| 194 | static void pass() {passed++;} |
| 195 | static void fail() {failed++; Thread.dumpStack();} |
| 196 | static void fail(String msg) {System.out.println(msg); fail();} |
| 197 | static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 198 | static void check(boolean cond) {if (cond) pass(); else fail();} |
| 199 | static void equal(Object x, Object y) { |
| 200 | if (x == null ? y == null : x.equals(y)) pass(); |
| 201 | else fail(x + " not equal to " + y);} |
| 202 | public static void main(String[] args) throws Throwable { |
| 203 | try {realMain(args);} catch (Throwable t) {unexpected(t);} |
| 204 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 205 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 206 | private static abstract class Fun {abstract void f() throws Throwable;} |
| 207 | static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
| 208 | for (Fun f : fs) |
| 209 | try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
| 210 | catch (Throwable t) { |
| 211 | if (k.isAssignableFrom(t.getClass())) pass(); |
| 212 | else unexpected(t);}} |
| 213 | } |