ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LockSupportTest.java (file contents):
Revision 1.5 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.15 by jsr166, Mon Oct 4 20:15:16 2010 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
2 > * 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   * http://creativecommons.org/licenses/publicdomain
6 < * Other contributors include Andrew Wright, Jeffrey Hayes,
7 < * Pat Fisher, Mike Judd.
6 > * Other contributors include Andrew Wright, Jeffrey Hayes,
7 > * Pat Fisher, Mike Judd.
8   */
9  
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import java.util.concurrent.atomic.AtomicBoolean;
14   import java.util.concurrent.locks.*;
15  
16 < public class LockSupportTest extends JSR166TestCase{
16 > public class LockSupportTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run(suite());
19      }
20 +
21      public static Test suite() {
22 <        return new TestSuite(LockSupportTest.class);
22 >        return new TestSuite(LockSupportTest.class);
23      }
24  
25      /**
26 <     * park is released by unpark occuring after park
26 >     * park is released by subsequent unpark
27       */
28 <    public void testPark() {
29 <        Thread t = new Thread(new Runnable() {
30 <                public void run() {
31 <                    try {
32 <                        LockSupport.park();
33 <                    } catch(Exception e){
34 <                        threadUnexpectedException();
35 <                    }
36 <                }
37 <            });
38 <        try {
39 <            t.start();
40 <            Thread.sleep(SHORT_DELAY_MS);
38 <            LockSupport.unpark(t);
39 <            t.join();
40 <        }
41 <        catch(Exception e) {
42 <            unexpectedException();
43 <        }
28 >    public void testParkBeforeUnpark() throws InterruptedException {
29 >        final CountDownLatch threadStarted = new CountDownLatch(1);
30 >        Thread t = new Thread(new CheckedRunnable() {
31 >            public void realRun() {
32 >                threadStarted.countDown();
33 >                LockSupport.park();
34 >            }});
35 >
36 >        t.start();
37 >        threadStarted.await();
38 >        Thread.sleep(SHORT_DELAY_MS);
39 >        LockSupport.unpark(t);
40 >        joinWith(t);
41      }
42  
43      /**
44 <     * park is released by unpark occuring before park
44 >     * parkUntil is released by subsequent unpark
45       */
46 <    public void testPark2() {
47 <        Thread t = new Thread(new Runnable() {
48 <                public void run() {
49 <                    try {
50 <                        Thread.sleep(SHORT_DELAY_MS);
51 <                        LockSupport.park();
52 <                    } catch(Exception e){
53 <                        threadUnexpectedException();
54 <                    }
55 <                }
56 <            });
57 <        try {
58 <            t.start();
59 <            LockSupport.unpark(t);
60 <            t.join();
61 <        }
62 <        catch(Exception e) {
66 <            unexpectedException();
67 <        }
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 <     * park is released by interrupt
66 >     * parkNanos is released by subsequent unpark
67       */
68 <    public void testPark3() {
69 <        Thread t = new Thread(new Runnable() {
70 <                public void run() {
71 <                    try {
72 <                        LockSupport.park();
73 <                        threadAssertTrue(Thread.interrupted());
74 <                    } catch(Exception e){
75 <                        threadUnexpectedException();
76 <                    }
77 <                }
78 <            });
79 <        try {
80 <            t.start();
81 <            Thread.sleep(SHORT_DELAY_MS);
82 <            t.interrupt();
83 <            t.join();
89 <        }
90 <        catch(Exception e) {
91 <            unexpectedException();
92 <        }
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 >        Thread.sleep(SHORT_DELAY_MS);
82 >        LockSupport.unpark(t);
83 >        joinWith(t);
84      }
85  
86      /**
87 <     * park returns if interrupted before park
87 >     * park is released by preceding unpark
88       */
89 <    public void testPark4() {
90 <        final ReentrantLock lock = new ReentrantLock();
91 <        lock.lock();
92 <        Thread t = new Thread(new Runnable() {
93 <                public void run() {
94 <                    try {
95 <                        lock.lock();
96 <                        LockSupport.park();
97 <                    } catch(Exception e){
98 <                        threadUnexpectedException();
99 <                    }
100 <                }
101 <            });
102 <        try {
103 <            t.start();
104 <            t.interrupt();
105 <            lock.unlock();
106 <            t.join();
107 <        }
108 <        catch(Exception e) {
109 <            unexpectedException();
110 <        }
89 >    public void testParkAfterUnpark() throws Exception {
90 >        final CountDownLatch threadStarted = new CountDownLatch(1);
91 >        final AtomicBoolean unparked = new AtomicBoolean(false);
92 >        Thread t = new Thread(new CheckedRunnable() {
93 >            public void realRun() throws Exception {
94 >                threadStarted.countDown();
95 >                while (!unparked.get())
96 >                    Thread.yield();
97 >                LockSupport.park();
98 >            }});
99 >
100 >        t.start();
101 >        threadStarted.await();
102 >        LockSupport.unpark(t);
103 >        unparked.set(true);
104 >        joinWith(t);
105 >    }
106 >
107 >    /**
108 >     * parkUntil is released by preceding unpark
109 >     */
110 >    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 >        Thread t = new Thread(new CheckedRunnable() {
162 >            public void realRun() {
163 >                assertFalse(Thread.currentThread().isInterrupted());
164 >                threadStarted.countDown();
165 >                LockSupport.park();
166 >                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 >            }});
190 >
191 >        t.start();
192 >        threadStarted.await();
193 >        Thread.sleep(SHORT_DELAY_MS);
194 >        t.interrupt();
195 >        joinWith(t);
196 >    }
197 >
198 >    /**
199 >     * parkNanos is released by subsequent interrupt
200 >     */
201 >    public void testParkNanosBeforeInterrupt() throws InterruptedException {
202 >        final CountDownLatch threadStarted = new CountDownLatch(1);
203 >        Thread t = new Thread(new CheckedRunnable() {
204 >            public void realRun() {
205 >                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 >                LockSupport.park();
232 >                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 >            }});
283 >
284 >        t.start();
285 >        threadStarted.await();
286 >        t.interrupt();
287 >        unparked.set(true);
288 >        joinWith(t);
289      }
290  
291      /**
292       * parkNanos times out if not unparked
293       */
294 <    public void testParkNanos() {
295 <        Thread t = new Thread(new Runnable() {
296 <                public void run() {
297 <                    try {
298 <                        LockSupport.parkNanos(1000);
299 <                    } catch(Exception e){
300 <                        threadUnexpectedException();
301 <                    }
302 <                }
303 <            });
304 <        try {
136 <            t.start();
137 <            t.join();
138 <        }
139 <        catch(Exception e) {
140 <            unexpectedException();
141 <        }
294 >    public void testParkNanosTimesOut() throws InterruptedException {
295 >        Thread t = new Thread(new CheckedRunnable() {
296 >            public void realRun() {
297 >                final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
298 >                long t0 = System.nanoTime();
299 >                LockSupport.parkNanos(timeoutNanos);
300 >                assertTrue(System.nanoTime() - t0 >= timeoutNanos);
301 >            }});
302 >
303 >        t.start();
304 >        joinWith(t);
305      }
306  
307  
308      /**
309       * parkUntil times out if not unparked
310       */
311 <    public void testParkUntil() {
312 <        Thread t = new Thread(new Runnable() {
313 <                public void run() {
314 <                    try {
315 <                        long d = new Date().getTime() + 100;
316 <                        LockSupport.parkUntil(d);
317 <                    } catch(Exception e){
318 <                        threadUnexpectedException();
319 <                    }
320 <                }
321 <            });
322 <        try {
323 <            t.start();
324 <            t.join();
325 <        }
326 <        catch(Exception e) {
327 <            unexpectedException();
311 >    public void testParkUntilTimesOut() throws InterruptedException {
312 >        Thread t = new Thread(new CheckedRunnable() {
313 >            public void realRun() {
314 >                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 >                LockSupport.parkUntil(d);
319 >                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 >            }});
336 >
337 >        t.start();
338 >        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      }
348   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines