author | igerasim |
Mon, 02 Jun 2014 19:49:57 +0400 | |
changeset 24692 | 268fbc344d53 |
parent 7668 | d4a77089c587 |
child 34347 | 4a17f9e90a0f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
73 |
() -> task.get(0L, TimeUnit.SECONDS)); |
2 | 74 |
} |
75 |
||
76 |
static <V> void checkDone(final FutureTask<V> task) { |
|
77 |
try { |
|
78 |
check(task.isDone()); |
|
79 |
check(! task.isCancelled()); |
|
80 |
check(task.get() != null); |
|
81 |
} catch (Throwable t) { unexpected(t); } |
|
82 |
} |
|
83 |
||
84 |
static <V> void checkCancelled(final FutureTask<V> task) { |
|
85 |
check(task.isDone()); |
|
86 |
check(task.isCancelled()); |
|
87 |
THROWS(CancellationException.class, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
88 |
() -> task.get(0L, TimeUnit.SECONDS), |
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
89 |
() -> task.get()); |
2 | 90 |
} |
91 |
||
92 |
static <V> void checkThrew(final FutureTask<V> task) { |
|
93 |
check(task.isDone()); |
|
94 |
check(! task.isCancelled()); |
|
95 |
THROWS(ExecutionException.class, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
96 |
() -> task.get(0L, TimeUnit.SECONDS), |
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
97 |
() -> task.get()); |
2 | 98 |
} |
99 |
||
100 |
static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) { |
|
101 |
task.cancel(mayInterruptIfRunning); |
|
102 |
checkCancelled(task); |
|
103 |
} |
|
104 |
||
105 |
static <V> void run(FutureTask<V> task) { |
|
106 |
boolean isCancelled = task.isCancelled(); |
|
107 |
task.run(); |
|
108 |
check(task.isDone()); |
|
109 |
equal(isCancelled, task.isCancelled()); |
|
110 |
} |
|
111 |
||
112 |
static void realMain(String[] args) throws Throwable { |
|
113 |
final Runnable nop = new Runnable() { |
|
114 |
public void run() {}}; |
|
115 |
final Runnable bad = new Runnable() { |
|
116 |
public void run() { throw new Error(); }}; |
|
117 |
||
118 |
try { |
|
119 |
final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
|
120 |
checkReady(task); |
|
121 |
equalCounts(0,0,0); |
|
122 |
check(task.runAndReset()); |
|
123 |
checkReady(task); |
|
124 |
equalCounts(0,0,0); |
|
125 |
run(task); |
|
126 |
checkDone(task); |
|
127 |
equalCounts(1,1,0); |
|
128 |
equal(42L, task.get()); |
|
129 |
run(task); |
|
130 |
checkDone(task); |
|
131 |
equalCounts(1,1,0); |
|
132 |
equal(42L, task.get()); |
|
133 |
} catch (Throwable t) { unexpected(t); } |
|
134 |
||
135 |
try { |
|
136 |
final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
|
137 |
cancel(task, false); |
|
138 |
equalCounts(2,1,0); |
|
139 |
cancel(task, false); |
|
140 |
equalCounts(2,1,0); |
|
141 |
run(task); |
|
142 |
equalCounts(2,1,0); |
|
143 |
check(! task.runAndReset()); |
|
144 |
} catch (Throwable t) { unexpected(t); } |
|
145 |
||
146 |
try { |
|
147 |
final MyFutureTask<Long> task = new MyFutureTask<Long>(bad, 42L); |
|
148 |
checkReady(task); |
|
149 |
run(task); |
|
150 |
checkThrew(task); |
|
151 |
equalCounts(3,1,1); |
|
152 |
run(task); |
|
153 |
equalCounts(3,1,1); |
|
154 |
} catch (Throwable t) { unexpected(t); } |
|
155 |
||
156 |
try { |
|
157 |
final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
|
158 |
checkReady(task); |
|
159 |
task.set(99L); |
|
160 |
checkDone(task); |
|
161 |
equalCounts(4,2,1); |
|
162 |
run(task); |
|
163 |
equalCounts(4,2,1); |
|
164 |
task.setException(new Throwable()); |
|
165 |
checkDone(task); |
|
166 |
equalCounts(4,2,2); |
|
167 |
} catch (Throwable t) { unexpected(t); } |
|
168 |
||
169 |
try { |
|
170 |
final MyFutureTask<Long> task = new MyFutureTask<Long>(nop, 42L); |
|
171 |
checkReady(task); |
|
172 |
task.setException(new Throwable()); |
|
173 |
checkThrew(task); |
|
174 |
equalCounts(5,2,3); |
|
175 |
run(task); |
|
176 |
equalCounts(5,2,3); |
|
177 |
task.set(99L); |
|
178 |
checkThrew(task); |
|
179 |
equalCounts(5,3,3); |
|
180 |
} catch (Throwable t) { unexpected(t); } |
|
181 |
||
182 |
System.out.printf("doneCount=%d%n", doneCount.get()); |
|
183 |
System.out.printf("setCount=%d%n", setCount.get()); |
|
184 |
System.out.printf("setExceptionCount=%d%n", setExceptionCount.get()); |
|
185 |
} |
|
186 |
||
187 |
//--------------------- Infrastructure --------------------------- |
|
188 |
static volatile int passed = 0, failed = 0; |
|
189 |
static void pass() {passed++;} |
|
190 |
static void fail() {failed++; Thread.dumpStack();} |
|
191 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
192 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
193 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
194 |
static void equal(Object x, Object y) { |
|
195 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
196 |
else fail(x + " not equal to " + y);} |
|
197 |
public static void main(String[] args) throws Throwable { |
|
198 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
199 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
200 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
201 |
interface Fun {void f() throws Throwable;} |
2 | 202 |
static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
203 |
for (Fun f : fs) |
|
204 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
205 |
catch (Throwable t) { |
|
206 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
207 |
else unexpected(t);}} |
|
208 |
} |