ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.15
Committed: Mon Oct 4 20:15:16 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +262 -27 lines
Log Message:
Add (disabled) test XXXtestParkUntil0Returns; rewrite using better infrastructure; add more tests

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 dl 1.5 * http://creativecommons.org/licenses/publicdomain
6 jsr166 1.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
7     * Pat Fisher, Mike Judd.
8 dl 1.1 */
9    
10     import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13 jsr166 1.15 import java.util.concurrent.atomic.AtomicBoolean;
14 dl 1.1 import java.util.concurrent.locks.*;
15    
16 jsr166 1.10 public class LockSupportTest extends JSR166TestCase {
17 dl 1.1 public static void main(String[] args) {
18 jsr166 1.14 junit.textui.TestRunner.run(suite());
19 dl 1.1 }
20 jsr166 1.15
21 dl 1.1 public static Test suite() {
22 jsr166 1.12 return new TestSuite(LockSupportTest.class);
23 dl 1.1 }
24    
25 dl 1.3 /**
26 jsr166 1.15 * park is released by subsequent unpark
27 dl 1.3 */
28 jsr166 1.15 public void testParkBeforeUnpark() throws InterruptedException {
29     final CountDownLatch threadStarted = new CountDownLatch(1);
30 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
31     public void realRun() {
32 jsr166 1.15 threadStarted.countDown();
33 jsr166 1.11 LockSupport.park();
34     }});
35    
36     t.start();
37 jsr166 1.15 threadStarted.await();
38     Thread.sleep(SHORT_DELAY_MS);
39     LockSupport.unpark(t);
40     joinWith(t);
41     }
42    
43     /**
44     * parkUntil is released by subsequent unpark
45     */
46     public void testParkUntilBeforeUnpark() throws InterruptedException {
47     final CountDownLatch threadStarted = new CountDownLatch(1);
48     Thread t = new Thread(new CheckedRunnable() {
49     public void realRun() {
50     long d = new Date().getTime() + LONG_DELAY_MS;
51     long nanos = LONG_DELAY_MS * 1000L * 1000L;
52     long t0 = System.nanoTime();
53     threadStarted.countDown();
54     LockSupport.parkUntil(d);
55     assertTrue(System.nanoTime() - t0 < nanos);
56     }});
57    
58     t.start();
59     threadStarted.await();
60     Thread.sleep(SHORT_DELAY_MS);
61     LockSupport.unpark(t);
62     joinWith(t);
63     }
64    
65     /**
66     * parkNanos is released by subsequent unpark
67     */
68     public void testParkNanosBeforeUnpark() throws InterruptedException {
69     final CountDownLatch threadStarted = new CountDownLatch(1);
70     Thread t = new Thread(new CheckedRunnable() {
71     public void realRun() {
72     long nanos = LONG_DELAY_MS * 1000L * 1000L;
73     long t0 = System.nanoTime();
74     threadStarted.countDown();
75     LockSupport.parkNanos(nanos);
76     assertTrue(System.nanoTime() - t0 < nanos);
77     }});
78    
79     t.start();
80     threadStarted.await();
81 jsr166 1.11 Thread.sleep(SHORT_DELAY_MS);
82     LockSupport.unpark(t);
83 jsr166 1.15 joinWith(t);
84 dl 1.1 }
85    
86 dl 1.3 /**
87 jsr166 1.15 * park is released by preceding unpark
88 dl 1.4 */
89 jsr166 1.15 public void testParkAfterUnpark() throws Exception {
90     final CountDownLatch threadStarted = new CountDownLatch(1);
91     final AtomicBoolean unparked = new AtomicBoolean(false);
92 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
93 jsr166 1.15 public void realRun() throws Exception {
94     threadStarted.countDown();
95     while (!unparked.get())
96     Thread.yield();
97 jsr166 1.11 LockSupport.park();
98     }});
99    
100     t.start();
101 jsr166 1.15 threadStarted.await();
102 jsr166 1.11 LockSupport.unpark(t);
103 jsr166 1.15 unparked.set(true);
104     joinWith(t);
105 dl 1.4 }
106    
107     /**
108 jsr166 1.15 * parkUntil is released by preceding unpark
109 dl 1.4 */
110 jsr166 1.15 public void testParkUntilAfterUnpark() throws Exception {
111     final CountDownLatch threadStarted = new CountDownLatch(1);
112     final AtomicBoolean unparked = new AtomicBoolean(false);
113     Thread t = new Thread(new CheckedRunnable() {
114     public void realRun() throws Exception {
115     threadStarted.countDown();
116     while (!unparked.get())
117     Thread.yield();
118     long d = new Date().getTime() + LONG_DELAY_MS;
119     long nanos = LONG_DELAY_MS * 1000L * 1000L;
120     long t0 = System.nanoTime();
121     LockSupport.parkUntil(d);
122     assertTrue(System.nanoTime() - t0 < nanos);
123     }});
124    
125     t.start();
126     threadStarted.await();
127     LockSupport.unpark(t);
128     unparked.set(true);
129     joinWith(t);
130     }
131    
132     /**
133     * parkNanos is released by preceding unpark
134     */
135     public void testParkNanosAfterUnpark() throws Exception {
136     final CountDownLatch threadStarted = new CountDownLatch(1);
137     final AtomicBoolean unparked = new AtomicBoolean(false);
138     Thread t = new Thread(new CheckedRunnable() {
139     public void realRun() throws Exception {
140     threadStarted.countDown();
141     while (!unparked.get())
142     Thread.yield();
143     long nanos = LONG_DELAY_MS * 1000L * 1000L;
144     long t0 = System.nanoTime();
145     LockSupport.parkNanos(nanos);
146     assertTrue(System.nanoTime() - t0 < nanos);
147     }});
148    
149     t.start();
150     threadStarted.await();
151     LockSupport.unpark(t);
152     unparked.set(true);
153     joinWith(t);
154     }
155    
156     /**
157     * park is released by subsequent interrupt
158     */
159     public void testParkBeforeInterrupt() throws InterruptedException {
160     final CountDownLatch threadStarted = new CountDownLatch(1);
161 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
162     public void realRun() {
163 jsr166 1.15 assertFalse(Thread.currentThread().isInterrupted());
164     threadStarted.countDown();
165 jsr166 1.11 LockSupport.park();
166 jsr166 1.15 assertTrue(Thread.currentThread().isInterrupted());
167     }});
168    
169     t.start();
170     threadStarted.await();
171     Thread.sleep(SHORT_DELAY_MS);
172     t.interrupt();
173     joinWith(t);
174     }
175    
176     /**
177     * parkUntil is released by subsequent interrupt
178     */
179     public void testParkUntilBeforeInterrupt() throws InterruptedException {
180     final CountDownLatch threadStarted = new CountDownLatch(1);
181     Thread t = new Thread(new CheckedRunnable() {
182     public void realRun() {
183     long d = new Date().getTime() + LONG_DELAY_MS;
184     long nanos = LONG_DELAY_MS * 1000L * 1000L;
185     long t0 = System.nanoTime();
186     threadStarted.countDown();
187     LockSupport.parkUntil(d);
188     assertTrue(System.nanoTime() - t0 < nanos);
189 jsr166 1.11 }});
190    
191     t.start();
192 jsr166 1.15 threadStarted.await();
193 jsr166 1.11 Thread.sleep(SHORT_DELAY_MS);
194     t.interrupt();
195 jsr166 1.15 joinWith(t);
196 dl 1.4 }
197    
198     /**
199 jsr166 1.15 * parkNanos is released by subsequent interrupt
200 dl 1.4 */
201 jsr166 1.15 public void testParkNanosBeforeInterrupt() throws InterruptedException {
202     final CountDownLatch threadStarted = new CountDownLatch(1);
203 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
204     public void realRun() {
205 jsr166 1.15 long nanos = LONG_DELAY_MS * 1000L * 1000L;
206     long t0 = System.nanoTime();
207     threadStarted.countDown();
208     LockSupport.parkNanos(nanos);
209     assertTrue(System.nanoTime() - t0 < nanos);
210     }});
211    
212     t.start();
213     threadStarted.await();
214     Thread.sleep(SHORT_DELAY_MS);
215     t.interrupt();
216     joinWith(t);
217     }
218    
219     /**
220     * park is released by preceding interrupt
221     */
222     public void testParkAfterInterrupt() throws Exception {
223     final CountDownLatch threadStarted = new CountDownLatch(1);
224     final AtomicBoolean unparked = new AtomicBoolean(false);
225     Thread t = new Thread(new CheckedRunnable() {
226     public void realRun() throws Exception {
227     threadStarted.countDown();
228     while (!unparked.get())
229     Thread.yield();
230     assertTrue(Thread.currentThread().isInterrupted());
231 jsr166 1.11 LockSupport.park();
232 jsr166 1.15 assertTrue(Thread.currentThread().isInterrupted());
233     }});
234    
235     t.start();
236     threadStarted.await();
237     t.interrupt();
238     unparked.set(true);
239     joinWith(t);
240     }
241    
242     /**
243     * parkUntil is released by preceding interrupt
244     */
245     public void testParkUntilAfterInterrupt() throws Exception {
246     final CountDownLatch threadStarted = new CountDownLatch(1);
247     final AtomicBoolean unparked = new AtomicBoolean(false);
248     Thread t = new Thread(new CheckedRunnable() {
249     public void realRun() throws Exception {
250     threadStarted.countDown();
251     while (!unparked.get())
252     Thread.yield();
253     long d = new Date().getTime() + LONG_DELAY_MS;
254     long nanos = LONG_DELAY_MS * 1000L * 1000L;
255     long t0 = System.nanoTime();
256     LockSupport.parkUntil(d);
257     assertTrue(System.nanoTime() - t0 < nanos);
258     }});
259    
260     t.start();
261     threadStarted.await();
262     t.interrupt();
263     unparked.set(true);
264     joinWith(t);
265     }
266    
267     /**
268     * parkNanos is released by preceding interrupt
269     */
270     public void testParkNanosAfterInterrupt() throws Exception {
271     final CountDownLatch threadStarted = new CountDownLatch(1);
272     final AtomicBoolean unparked = new AtomicBoolean(false);
273     Thread t = new Thread(new CheckedRunnable() {
274     public void realRun() throws Exception {
275     threadStarted.countDown();
276     while (!unparked.get())
277     Thread.yield();
278     long nanos = LONG_DELAY_MS * 1000L * 1000L;
279     long t0 = System.nanoTime();
280     LockSupport.parkNanos(nanos);
281     assertTrue(System.nanoTime() - t0 < nanos);
282 jsr166 1.11 }});
283    
284     t.start();
285 jsr166 1.15 threadStarted.await();
286 jsr166 1.11 t.interrupt();
287 jsr166 1.15 unparked.set(true);
288     joinWith(t);
289 dl 1.4 }
290    
291     /**
292     * parkNanos times out if not unparked
293 dl 1.3 */
294 jsr166 1.15 public void testParkNanosTimesOut() throws InterruptedException {
295 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
296     public void realRun() {
297 jsr166 1.15 final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
298     long t0 = System.nanoTime();
299     LockSupport.parkNanos(timeoutNanos);
300     assertTrue(System.nanoTime() - t0 >= timeoutNanos);
301 jsr166 1.11 }});
302    
303     t.start();
304 jsr166 1.15 joinWith(t);
305 dl 1.1 }
306    
307    
308 dl 1.3 /**
309 dl 1.4 * parkUntil times out if not unparked
310 dl 1.3 */
311 jsr166 1.15 public void testParkUntilTimesOut() throws InterruptedException {
312 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
313     public void realRun() {
314 jsr166 1.15 long d = new Date().getTime() + SHORT_DELAY_MS;
315     // beware of rounding
316     long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L;
317     long t0 = System.nanoTime();
318 jsr166 1.11 LockSupport.parkUntil(d);
319 jsr166 1.15 assertTrue(System.nanoTime() - t0 >= timeoutNanos);
320     }});
321    
322     t.start();
323     joinWith(t);
324     }
325    
326     /**
327     * parkUntil(0) returns immediately
328     * Requires hotspot fix for:
329     * 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
330     */
331     public void XXXtestParkUntil0Returns() throws InterruptedException {
332     Thread t = new Thread(new CheckedRunnable() {
333     public void realRun() {
334     LockSupport.parkUntil(0L);
335 jsr166 1.11 }});
336    
337     t.start();
338 jsr166 1.15 joinWith(t);
339     }
340    
341     private void joinWith(Thread t) throws InterruptedException {
342     t.join(MEDIUM_DELAY_MS);
343     if (t.isAlive()) {
344     fail("Test timed out");
345     t.interrupt();
346     }
347 dl 1.1 }
348     }