ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.25
Committed: Sat Apr 25 04:55:31 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.24: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

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