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.14 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.24 by jsr166, Wed Dec 31 19:05:43 2014 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
4 < * http://creativecommons.org/licenses/publicdomain
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/publicdomain/zero/1.0/
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.locks.*;
10 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 >
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.atomic.AtomicBoolean;
14 > import java.util.concurrent.locks.LockSupport;
15 >
16 > import junit.framework.Test;
17 > import junit.framework.TestSuite;
18  
19   public class LockSupportTest extends JSR166TestCase {
20      public static void main(String[] args) {
21          junit.textui.TestRunner.run(suite());
22      }
23 +
24      public static Test suite() {
25          return new TestSuite(LockSupportTest.class);
26      }
27  
28      /**
29 <     * park is released by unpark occurring after park
29 >     * Returns the blocker object used by tests in this file.
30 >     * Any old object will do; we'll return a convenient one.
31       */
32 <    public void testPark() throws InterruptedException {
33 <        Thread t = new Thread(new CheckedRunnable() {
34 <            public void realRun() {
32 >    static Object theBlocker() {
33 >        return LockSupportTest.class;
34 >    }
35 >
36 >    enum ParkMethod {
37 >        park() {
38 >            void park() {
39                  LockSupport.park();
40 +            }
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 +    }
84 +
85 +    /**
86 +     * park is released by subsequent unpark
87 +     */
88 +    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 +    }
106 +    public void testParkBeforeUnpark(final ParkMethod parkMethod) {
107 +        final CountDownLatch pleaseUnpark = new CountDownLatch(1);
108 +        Thread t = newStartedThread(new CheckedRunnable() {
109 +            public void realRun() {
110 +                pleaseUnpark.countDown();
111 +                parkMethod.park();
112              }});
113  
114 <        t.start();
32 <        Thread.sleep(SHORT_DELAY_MS);
114 >        await(pleaseUnpark);
115          LockSupport.unpark(t);
116 <        t.join();
116 >        awaitTermination(t);
117      }
118  
119      /**
120 <     * park is released by unpark occurring before park
120 >     * park is released by preceding unpark
121       */
122 <    public void testPark2() throws InterruptedException {
123 <        Thread t = new Thread(new CheckedRunnable() {
124 <            public void realRun() throws InterruptedException {
125 <                Thread.sleep(SHORT_DELAY_MS);
126 <                LockSupport.park();
122 >    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 >    }
137 >    public void testParkAfterUnpark_parkUntilBlocker() {
138 >        testParkAfterUnpark(ParkMethod.parkUntilBlocker);
139 >    }
140 >    public void testParkAfterUnpark(final ParkMethod parkMethod) {
141 >        final CountDownLatch pleaseUnpark = new CountDownLatch(1);
142 >        final AtomicBoolean pleasePark = new AtomicBoolean(false);
143 >        Thread t = newStartedThread(new CheckedRunnable() {
144 >            public void realRun() {
145 >                pleaseUnpark.countDown();
146 >                while (!pleasePark.get())
147 >                    Thread.yield();
148 >                parkMethod.park();
149              }});
150  
151 <        t.start();
151 >        await(pleaseUnpark);
152          LockSupport.unpark(t);
153 <        t.join();
153 >        pleasePark.set(true);
154 >        awaitTermination(t);
155      }
156  
157      /**
158 <     * park is released by interrupt
158 >     * park is released by subsequent interrupt
159       */
160 <    public void testPark3() throws InterruptedException {
161 <        Thread t = new Thread(new CheckedRunnable() {
160 >    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 >        Thread t = newStartedThread(new CheckedRunnable() {
181              public void realRun() {
182 <                LockSupport.park();
182 >                pleaseInterrupt.countDown();
183 >                do {
184 >                    parkMethod.park();
185 >                    // park may return spuriously
186 >                } while (! Thread.currentThread().isInterrupted());
187              }});
188  
189 <        t.start();
190 <        Thread.sleep(SHORT_DELAY_MS);
189 >        await(pleaseInterrupt);
190 >        assertThreadStaysAlive(t);
191          t.interrupt();
192 <        t.join();
192 >        awaitTermination(t);
193      }
194  
195      /**
196 <     * park returns if interrupted before park
196 >     * park is released by preceding interrupt
197       */
198 <    public void testPark4() throws InterruptedException {
199 <        final ReentrantLock lock = new ReentrantLock();
200 <        lock.lock();
201 <        Thread t = new Thread(new CheckedRunnable() {
202 <            public void realRun() {
203 <                lock.lock();
204 <                LockSupport.park();
198 >    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 >    }
213 >    public void testParkAfterInterrupt_parkUntilBlocker() {
214 >        testParkAfterInterrupt(ParkMethod.parkUntilBlocker);
215 >    }
216 >    public void testParkAfterInterrupt(final ParkMethod parkMethod) {
217 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
218 >        final AtomicBoolean pleasePark = new AtomicBoolean(false);
219 >        Thread t = newStartedThread(new CheckedRunnable() {
220 >            public void realRun() throws Exception {
221 >                pleaseInterrupt.countDown();
222 >                while (!pleasePark.get())
223 >                    Thread.yield();
224 >                assertTrue(Thread.currentThread().isInterrupted());
225 >                parkMethod.park();
226 >                assertTrue(Thread.currentThread().isInterrupted());
227              }});
228  
229 <        t.start();
80 <        Thread.sleep(SHORT_DELAY_MS);
229 >        await(pleaseInterrupt);
230          t.interrupt();
231 <        lock.unlock();
232 <        t.join();
231 >        pleasePark.set(true);
232 >        awaitTermination(t);
233 >    }
234 >
235 >    /**
236 >     * timed park times out if not unparked
237 >     */
238 >    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 >        Thread t = newStartedThread(new CheckedRunnable() {
252 >            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 >            }});
261 >
262 >        awaitTermination(t);
263 >    }
264 >
265 >    /**
266 >     * getBlocker(null) throws NullPointerException
267 >     */
268 >    public void testGetBlockerNull() {
269 >        try {
270 >            LockSupport.getBlocker(null);
271 >            shouldThrow();
272 >        } catch (NullPointerException success) {}
273      }
274  
275      /**
276 <     * parkNanos times out if not unparked
276 >     * getBlocker returns the blocker object passed to park
277       */
278 <    public void testParkNanos() throws InterruptedException {
279 <        Thread t = new Thread(new CheckedRunnable() {
278 >    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 >        Thread t = newStartedThread(new CheckedRunnable() {
290              public void realRun() {
291 <                LockSupport.parkNanos(1000);
291 >                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              }});
300  
301 <        t.start();
302 <        t.join();
301 >        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 >    }
318 >
319 >    /**
320 >     * 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 >     */
326 >    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 +        Thread t = newStartedThread(new CheckedRunnable() {
340 +            public void realRun() {
341 +                parkMethod.park(0L);
342 +            }});
343  
344 +        awaitTermination(t);
345 +    }
346  
347      /**
348 <     * parkUntil times out if not unparked
348 >     * timed park(Long.MIN_VALUE) returns immediately.
349       */
350 <    public void testParkUntil() throws InterruptedException {
351 <        Thread t = new Thread(new CheckedRunnable() {
350 >    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 >    }
362 >    public void testParkNeg(final ParkMethod parkMethod) {
363 >        Thread t = newStartedThread(new CheckedRunnable() {
364              public void realRun() {
365 <                long d = new Date().getTime() + 100;
107 <                LockSupport.parkUntil(d);
365 >                parkMethod.park(Long.MIN_VALUE);
366              }});
367  
368 <        t.start();
111 <        t.join();
368 >        awaitTermination(t);
369      }
370   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines