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