ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.31
Committed: Sat May 13 23:50:00 2017 UTC (6 years, 11 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.30: +7 -8 lines
Log Message:
claw back some millis using assertThreadBlocks

File Contents

# User Rev Content
1 dl 1.1 /*
2 jsr166 1.15 * Written by Doug Lea and Martin Buchholz with assistance from
3     * members of JCP JSR-166 Expert Group and released to the public
4     * domain, as explained at
5 jsr166 1.18 * http://creativecommons.org/publicdomain/zero/1.0/
6 jsr166 1.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
7     * Pat Fisher, Mike Judd.
8 dl 1.1 */
9    
10 jsr166 1.24 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11    
12 jsr166 1.23 import java.util.concurrent.CountDownLatch;
13 jsr166 1.15 import java.util.concurrent.atomic.AtomicBoolean;
14 jsr166 1.23 import java.util.concurrent.locks.LockSupport;
15 jsr166 1.24
16     import junit.framework.Test;
17     import junit.framework.TestSuite;
18 dl 1.1
19 jsr166 1.10 public class LockSupportTest extends JSR166TestCase {
20 dl 1.1 public static void main(String[] args) {
21 jsr166 1.25 main(suite(), args);
22 dl 1.1 }
23 jsr166 1.15
24 dl 1.1 public static Test suite() {
25 jsr166 1.12 return new TestSuite(LockSupportTest.class);
26 dl 1.1 }
27    
28 jsr166 1.26 static {
29     // Reduce the risk of rare disastrous classloading in first call to
30     // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
31     Class<?> ensureLoaded = LockSupport.class;
32     }
33    
34 dl 1.3 /**
35 jsr166 1.23 * Returns the blocker object used by tests in this file.
36     * Any old object will do; we'll return a convenient one.
37 dl 1.3 */
38 jsr166 1.23 static Object theBlocker() {
39     return LockSupportTest.class;
40     }
41    
42     enum ParkMethod {
43     park() {
44     void park() {
45 jsr166 1.11 LockSupport.park();
46 jsr166 1.23 }
47 jsr166 1.31 Thread.State parkedState() { return Thread.State.WAITING; }
48 jsr166 1.23 },
49     parkUntil() {
50     void park(long millis) {
51     LockSupport.parkUntil(deadline(millis));
52     }
53     },
54     parkNanos() {
55     void park(long millis) {
56     LockSupport.parkNanos(MILLISECONDS.toNanos(millis));
57     }
58     },
59     parkBlocker() {
60     void park() {
61     LockSupport.park(theBlocker());
62     }
63 jsr166 1.31 Thread.State parkedState() { return Thread.State.WAITING; }
64 jsr166 1.23 },
65     parkUntilBlocker() {
66     void park(long millis) {
67     LockSupport.parkUntil(theBlocker(), deadline(millis));
68     }
69     },
70     parkNanosBlocker() {
71     void park(long millis) {
72     LockSupport.parkNanos(theBlocker(),
73     MILLISECONDS.toNanos(millis));
74     }
75     };
76    
77     void park() { park(2 * LONG_DELAY_MS); }
78 jsr166 1.31 void park(long millis) {
79     throw new UnsupportedOperationException();
80     }
81     Thread.State parkedState() { return Thread.State.TIMED_WAITING; }
82 jsr166 1.23
83     /** Returns a deadline to use with parkUntil. */
84     long deadline(long millis) {
85     // beware of rounding
86     return System.currentTimeMillis() + millis + 1;
87     }
88 jsr166 1.15 }
89    
90     /**
91 jsr166 1.23 * park is released by subsequent unpark
92 jsr166 1.15 */
93 jsr166 1.23 public void testParkBeforeUnpark_park() {
94     testParkBeforeUnpark(ParkMethod.park);
95     }
96     public void testParkBeforeUnpark_parkNanos() {
97     testParkBeforeUnpark(ParkMethod.parkNanos);
98     }
99     public void testParkBeforeUnpark_parkUntil() {
100     testParkBeforeUnpark(ParkMethod.parkUntil);
101     }
102     public void testParkBeforeUnpark_parkBlocker() {
103     testParkBeforeUnpark(ParkMethod.parkBlocker);
104     }
105     public void testParkBeforeUnpark_parkNanosBlocker() {
106     testParkBeforeUnpark(ParkMethod.parkNanosBlocker);
107     }
108     public void testParkBeforeUnpark_parkUntilBlocker() {
109     testParkBeforeUnpark(ParkMethod.parkUntilBlocker);
110 jsr166 1.15 }
111 jsr166 1.23 public void testParkBeforeUnpark(final ParkMethod parkMethod) {
112     final CountDownLatch pleaseUnpark = new CountDownLatch(1);
113 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
114 jsr166 1.15 public void realRun() {
115 jsr166 1.23 pleaseUnpark.countDown();
116     parkMethod.park();
117 jsr166 1.15 }});
118    
119 jsr166 1.23 await(pleaseUnpark);
120 jsr166 1.11 LockSupport.unpark(t);
121 jsr166 1.23 awaitTermination(t);
122 dl 1.1 }
123    
124 dl 1.3 /**
125 jsr166 1.15 * park is released by preceding unpark
126 dl 1.4 */
127 jsr166 1.23 public void testParkAfterUnpark_park() {
128     testParkAfterUnpark(ParkMethod.park);
129     }
130     public void testParkAfterUnpark_parkNanos() {
131     testParkAfterUnpark(ParkMethod.parkNanos);
132     }
133     public void testParkAfterUnpark_parkUntil() {
134     testParkAfterUnpark(ParkMethod.parkUntil);
135     }
136     public void testParkAfterUnpark_parkBlocker() {
137     testParkAfterUnpark(ParkMethod.parkBlocker);
138     }
139     public void testParkAfterUnpark_parkNanosBlocker() {
140     testParkAfterUnpark(ParkMethod.parkNanosBlocker);
141 dl 1.4 }
142 jsr166 1.23 public void testParkAfterUnpark_parkUntilBlocker() {
143     testParkAfterUnpark(ParkMethod.parkUntilBlocker);
144 jsr166 1.15 }
145 jsr166 1.23 public void testParkAfterUnpark(final ParkMethod parkMethod) {
146     final CountDownLatch pleaseUnpark = new CountDownLatch(1);
147     final AtomicBoolean pleasePark = new AtomicBoolean(false);
148 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
149 jsr166 1.23 public void realRun() {
150     pleaseUnpark.countDown();
151     while (!pleasePark.get())
152 jsr166 1.15 Thread.yield();
153 jsr166 1.23 parkMethod.park();
154 jsr166 1.15 }});
155    
156 jsr166 1.23 await(pleaseUnpark);
157 jsr166 1.15 LockSupport.unpark(t);
158 jsr166 1.23 pleasePark.set(true);
159     awaitTermination(t);
160 jsr166 1.15 }
161    
162     /**
163     * park is released by subsequent interrupt
164     */
165 jsr166 1.23 public void testParkBeforeInterrupt_park() {
166     testParkBeforeInterrupt(ParkMethod.park);
167     }
168     public void testParkBeforeInterrupt_parkNanos() {
169     testParkBeforeInterrupt(ParkMethod.parkNanos);
170     }
171     public void testParkBeforeInterrupt_parkUntil() {
172     testParkBeforeInterrupt(ParkMethod.parkUntil);
173     }
174     public void testParkBeforeInterrupt_parkBlocker() {
175     testParkBeforeInterrupt(ParkMethod.parkBlocker);
176     }
177     public void testParkBeforeInterrupt_parkNanosBlocker() {
178     testParkBeforeInterrupt(ParkMethod.parkNanosBlocker);
179     }
180     public void testParkBeforeInterrupt_parkUntilBlocker() {
181     testParkBeforeInterrupt(ParkMethod.parkUntilBlocker);
182     }
183     public void testParkBeforeInterrupt(final ParkMethod parkMethod) {
184     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
185 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
186 jsr166 1.12 public void realRun() {
187 jsr166 1.23 pleaseInterrupt.countDown();
188 jsr166 1.29 for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) {
189 jsr166 1.23 parkMethod.park();
190 jsr166 1.29 if (Thread.interrupted())
191     return;
192     }
193     fail("too many consecutive spurious wakeups?");
194 jsr166 1.15 }});
195    
196 jsr166 1.23 await(pleaseInterrupt);
197 jsr166 1.31 assertThreadBlocks(t, parkMethod.parkedState());
198 jsr166 1.15 t.interrupt();
199 jsr166 1.23 awaitTermination(t);
200 jsr166 1.15 }
201    
202     /**
203 jsr166 1.23 * park is released by preceding interrupt
204 jsr166 1.15 */
205 jsr166 1.23 public void testParkAfterInterrupt_park() {
206     testParkAfterInterrupt(ParkMethod.park);
207     }
208     public void testParkAfterInterrupt_parkNanos() {
209     testParkAfterInterrupt(ParkMethod.parkNanos);
210     }
211     public void testParkAfterInterrupt_parkUntil() {
212     testParkAfterInterrupt(ParkMethod.parkUntil);
213     }
214     public void testParkAfterInterrupt_parkBlocker() {
215     testParkAfterInterrupt(ParkMethod.parkBlocker);
216     }
217     public void testParkAfterInterrupt_parkNanosBlocker() {
218     testParkAfterInterrupt(ParkMethod.parkNanosBlocker);
219 dl 1.4 }
220 jsr166 1.23 public void testParkAfterInterrupt_parkUntilBlocker() {
221     testParkAfterInterrupt(ParkMethod.parkUntilBlocker);
222 jsr166 1.15 }
223 jsr166 1.23 public void testParkAfterInterrupt(final ParkMethod parkMethod) {
224     final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
225 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
226 jsr166 1.15 public void realRun() throws Exception {
227 jsr166 1.23 pleaseInterrupt.countDown();
228 jsr166 1.30 while (!Thread.currentThread().isInterrupted())
229 jsr166 1.15 Thread.yield();
230 jsr166 1.23 parkMethod.park();
231 jsr166 1.30 assertTrue(Thread.interrupted());
232 jsr166 1.15 }});
233    
234 jsr166 1.23 await(pleaseInterrupt);
235 jsr166 1.15 t.interrupt();
236 jsr166 1.23 awaitTermination(t);
237 jsr166 1.15 }
238    
239     /**
240 jsr166 1.23 * timed park times out if not unparked
241 jsr166 1.15 */
242 jsr166 1.23 public void testParkTimesOut_parkNanos() {
243     testParkTimesOut(ParkMethod.parkNanos);
244     }
245     public void testParkTimesOut_parkUntil() {
246     testParkTimesOut(ParkMethod.parkUntil);
247     }
248     public void testParkTimesOut_parkNanosBlocker() {
249     testParkTimesOut(ParkMethod.parkNanosBlocker);
250     }
251     public void testParkTimesOut_parkUntilBlocker() {
252     testParkTimesOut(ParkMethod.parkUntilBlocker);
253     }
254     public void testParkTimesOut(final ParkMethod parkMethod) {
255 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
256 jsr166 1.23 public void realRun() {
257 jsr166 1.27 for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) {
258 jsr166 1.23 long startTime = System.nanoTime();
259     parkMethod.park(timeoutMillis());
260     if (millisElapsedSince(startTime) >= timeoutMillis())
261     return;
262     }
263 jsr166 1.27 fail("too many consecutive spurious wakeups?");
264 jsr166 1.15 }});
265    
266 jsr166 1.23 awaitTermination(t);
267 jsr166 1.15 }
268    
269     /**
270 jsr166 1.23 * getBlocker(null) throws NullPointerException
271 jsr166 1.15 */
272 jsr166 1.23 public void testGetBlockerNull() {
273     try {
274     LockSupport.getBlocker(null);
275     shouldThrow();
276     } catch (NullPointerException success) {}
277 dl 1.4 }
278    
279     /**
280 jsr166 1.23 * getBlocker returns the blocker object passed to park
281 dl 1.3 */
282 jsr166 1.23 public void testGetBlocker_parkBlocker() {
283     testGetBlocker(ParkMethod.parkBlocker);
284     }
285     public void testGetBlocker_parkNanosBlocker() {
286     testGetBlocker(ParkMethod.parkNanosBlocker);
287     }
288     public void testGetBlocker_parkUntilBlocker() {
289     testGetBlocker(ParkMethod.parkUntilBlocker);
290     }
291     public void testGetBlocker(final ParkMethod parkMethod) {
292     final CountDownLatch started = new CountDownLatch(1);
293 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
294 jsr166 1.12 public void realRun() {
295 jsr166 1.23 Thread t = Thread.currentThread();
296     started.countDown();
297 jsr166 1.29 for (int tries = MAX_SPURIOUS_WAKEUPS; tries-->0; ) {
298 jsr166 1.23 assertNull(LockSupport.getBlocker(t));
299     parkMethod.park();
300     assertNull(LockSupport.getBlocker(t));
301 jsr166 1.29 if (Thread.interrupted())
302     return;
303     }
304     fail("too many consecutive spurious wakeups?");
305 jsr166 1.11 }});
306    
307 jsr166 1.23 long startTime = System.nanoTime();
308     await(started);
309     for (;;) {
310     Object x = LockSupport.getBlocker(t);
311     if (x == theBlocker()) { // success
312     t.interrupt();
313     awaitTermination(t);
314     assertNull(LockSupport.getBlocker(t));
315     return;
316     } else {
317     assertNull(x); // ok
318     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
319     fail("timed out");
320 jsr166 1.29 if (t.getState() == Thread.State.TERMINATED)
321     break;
322 jsr166 1.23 Thread.yield();
323     }
324     }
325 dl 1.1 }
326    
327 dl 1.3 /**
328 jsr166 1.23 * timed park(0) returns immediately.
329     *
330     * Requires hotspot fix for:
331     * 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
332     * which is in jdk7-b118 and 6u25.
333 dl 1.3 */
334 jsr166 1.23 public void testPark0_parkNanos() {
335     testPark0(ParkMethod.parkNanos);
336     }
337     public void testPark0_parkUntil() {
338     testPark0(ParkMethod.parkUntil);
339     }
340     public void testPark0_parkNanosBlocker() {
341     testPark0(ParkMethod.parkNanosBlocker);
342     }
343     public void testPark0_parkUntilBlocker() {
344     testPark0(ParkMethod.parkUntilBlocker);
345     }
346     public void testPark0(final ParkMethod parkMethod) {
347 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
348 jsr166 1.12 public void realRun() {
349 jsr166 1.23 parkMethod.park(0L);
350 jsr166 1.15 }});
351    
352 jsr166 1.23 awaitTermination(t);
353 jsr166 1.15 }
354    
355     /**
356 jsr166 1.23 * timed park(Long.MIN_VALUE) returns immediately.
357 jsr166 1.20 */
358 jsr166 1.23 public void testParkNeg_parkNanos() {
359     testParkNeg(ParkMethod.parkNanos);
360     }
361     public void testParkNeg_parkUntil() {
362     testParkNeg(ParkMethod.parkUntil);
363     }
364     public void testParkNeg_parkNanosBlocker() {
365     testParkNeg(ParkMethod.parkNanosBlocker);
366     }
367     public void testParkNeg_parkUntilBlocker() {
368     testParkNeg(ParkMethod.parkUntilBlocker);
369 jsr166 1.20 }
370 jsr166 1.23 public void testParkNeg(final ParkMethod parkMethod) {
371 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
372 jsr166 1.15 public void realRun() {
373 jsr166 1.23 parkMethod.park(Long.MIN_VALUE);
374 jsr166 1.11 }});
375    
376 jsr166 1.21 awaitTermination(t);
377 dl 1.1 }
378     }