Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/FutureTask/Customized.java
+++ new/test/java/util/concurrent/FutureTask/Customized.java
1 1 /*
2 2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6464365
27 27 * @summary Test state transitions; check protected methods are called
28 28 * @author Martin Buchholz
29 29 */
30 30
31 31 import java.util.*;
32 32 import java.util.concurrent.*;
33 33 import java.util.concurrent.atomic.*;
34 34
35 35 public class Customized {
36 36 static final AtomicLong doneCount = new AtomicLong(0);
37 37 static final AtomicLong setCount = new AtomicLong(0);
38 38 static final AtomicLong setExceptionCount = new AtomicLong(0);
39 39
40 40 static void equal(long expected, AtomicLong actual) {
41 41 equal(expected, actual.get());
42 42 }
43 43
44 44 static void equalCounts(long x, long y, long z) {
45 45 equal(x, doneCount);
46 46 equal(y, setCount);
47 47 equal(z, setExceptionCount);
48 48 }
49 49
50 50 static class MyFutureTask<V> extends FutureTask<V> {
51 51 MyFutureTask(Runnable r, V result) { super(r, result); }
52 52 protected void done() {
53 53 doneCount.getAndIncrement();
54 54 super.done();
55 55 }
56 56 protected void set(V v) {
57 57 setCount.getAndIncrement();
58 58 super.set(v);
59 59 }
60 60 protected void setException(Throwable t) {
61 61 setExceptionCount.getAndIncrement();
62 62 super.setException(t);
63 63 }
64 64 public boolean runAndReset() {
65 65 return super.runAndReset();
66 66 }
67 67 }
68 68
69 69 static <V> void checkReady(final FutureTask<V> task) {
70 70 check(! task.isDone());
71 71 check(! task.isCancelled());
72 72 THROWS(TimeoutException.class,
73 73 new Fun(){void f() throws Throwable {
74 74 task.get(0L, TimeUnit.SECONDS); }});
75 75 }
76 76
77 77 static <V> void checkDone(final FutureTask<V> task) {
78 78 try {
79 79 check(task.isDone());
80 80 check(! task.isCancelled());
81 81 check(task.get() != null);
82 82 } catch (Throwable t) { unexpected(t); }
83 83 }
84 84
85 85 static <V> void checkCancelled(final FutureTask<V> task) {
86 86 check(task.isDone());
87 87 check(task.isCancelled());
88 88 THROWS(CancellationException.class,
89 89 new Fun(){void f() throws Throwable {
90 90 task.get(0L, TimeUnit.SECONDS); }},
91 91 new Fun(){void f() throws Throwable {
92 92 task.get(); }});
93 93 }
94 94
95 95 static <V> void checkThrew(final FutureTask<V> task) {
96 96 check(task.isDone());
97 97 check(! task.isCancelled());
98 98 THROWS(ExecutionException.class,
99 99 new Fun(){void f() throws Throwable {
100 100 task.get(0L, TimeUnit.SECONDS); }},
101 101 new Fun(){void f() throws Throwable {
102 102 task.get(); }});
103 103 }
104 104
105 105 static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
106 106 task.cancel(mayInterruptIfRunning);
107 107 checkCancelled(task);
108 108 }
109 109
110 110 static <V> void run(FutureTask<V> task) {
111 111 boolean isCancelled = task.isCancelled();
112 112 task.run();
113 113 check(task.isDone());
114 114 equal(isCancelled, task.isCancelled());
115 115 }
116 116
117 117 static void realMain(String[] args) throws Throwable {
118 118 final Runnable nop = new Runnable() {
119 119 public void run() {}};
120 120 final Runnable bad = new Runnable() {
121 121 public void run() { throw new Error(); }};
122 122
123 123 try {
124 124 final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
125 125 checkReady(task);
126 126 equalCounts(0,0,0);
127 127 check(task.runAndReset());
128 128 checkReady(task);
129 129 equalCounts(0,0,0);
130 130 run(task);
131 131 checkDone(task);
132 132 equalCounts(1,1,0);
133 133 equal(42L, task.get());
134 134 run(task);
135 135 checkDone(task);
136 136 equalCounts(1,1,0);
137 137 equal(42L, task.get());
138 138 } catch (Throwable t) { unexpected(t); }
139 139
140 140 try {
141 141 final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
142 142 cancel(task, false);
143 143 equalCounts(2,1,0);
144 144 cancel(task, false);
145 145 equalCounts(2,1,0);
146 146 run(task);
147 147 equalCounts(2,1,0);
148 148 check(! task.runAndReset());
149 149 } catch (Throwable t) { unexpected(t); }
150 150
151 151 try {
152 152 final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L);
153 153 checkReady(task);
154 154 run(task);
155 155 checkThrew(task);
156 156 equalCounts(3,1,1);
157 157 run(task);
158 158 equalCounts(3,1,1);
159 159 } catch (Throwable t) { unexpected(t); }
160 160
161 161 try {
162 162 final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
163 163 checkReady(task);
164 164 task.set(99L);
165 165 checkDone(task);
166 166 equalCounts(4,2,1);
167 167 run(task);
168 168 equalCounts(4,2,1);
169 169 task.setException(new Throwable());
170 170 checkDone(task);
171 171 equalCounts(4,2,2);
172 172 } catch (Throwable t) { unexpected(t); }
173 173
174 174 try {
175 175 final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L);
176 176 checkReady(task);
177 177 task.setException(new Throwable());
178 178 checkThrew(task);
179 179 equalCounts(5,2,3);
180 180 run(task);
181 181 equalCounts(5,2,3);
182 182 task.set(99L);
183 183 checkThrew(task);
184 184 equalCounts(5,3,3);
185 185 } catch (Throwable t) { unexpected(t); }
186 186
187 187 System.out.printf("doneCount=%d%n", doneCount.get());
188 188 System.out.printf("setCount=%d%n", setCount.get());
189 189 System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
190 190 }
191 191
192 192 //--------------------- Infrastructure ---------------------------
193 193 static volatile int passed = 0, failed = 0;
194 194 static void pass() {passed++;}
195 195 static void fail() {failed++; Thread.dumpStack();}
↓ open down ↓ |
195 lines elided |
↑ open up ↑ |
196 196 static void fail(String msg) {System.out.println(msg); fail();}
197 197 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
198 198 static void check(boolean cond) {if (cond) pass(); else fail();}
199 199 static void equal(Object x, Object y) {
200 200 if (x == null ? y == null : x.equals(y)) pass();
201 201 else fail(x + " not equal to " + y);}
202 202 public static void main(String[] args) throws Throwable {
203 203 try {realMain(args);} catch (Throwable t) {unexpected(t);}
204 204 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
205 205 if (failed > 0) throw new AssertionError("Some tests failed");}
206 - private static abstract class Fun {abstract void f() throws Throwable;}
206 + private abstract static class Fun {abstract void f() throws Throwable;}
207 207 static void THROWS(Class<? extends Throwable> k, Fun... fs) {
208 208 for (Fun f : fs)
209 209 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
210 210 catch (Throwable t) {
211 211 if (k.isAssignableFrom(t.getClass())) pass();
212 212 else unexpected(t);}}
213 213 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX