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.15 by jsr166, Mon Oct 4 20:15:16 2010 UTC vs.
Revision 1.19 by dl, Fri May 6 11:22:07 2011 UTC

# Line 2 | Line 2
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
5 > * http://creativecommons.org/publicdomain/zero/1.0/
6   * Other contributors include Andrew Wright, Jeffrey Hayes,
7   * Pat Fisher, Mike Judd.
8   */
# Line 27 | Line 27 | public class LockSupportTest extends JSR
27       */
28      public void testParkBeforeUnpark() throws InterruptedException {
29          final CountDownLatch threadStarted = new CountDownLatch(1);
30 <        Thread t = new Thread(new CheckedRunnable() {
30 >        Thread t = newStartedThread(new CheckedRunnable() {
31              public void realRun() {
32                  threadStarted.countDown();
33                  LockSupport.park();
34              }});
35  
36        t.start();
36          threadStarted.await();
37 <        Thread.sleep(SHORT_DELAY_MS);
37 >        delay(SHORT_DELAY_MS);
38          LockSupport.unpark(t);
39 <        joinWith(t);
39 >        awaitTermination(t, MEDIUM_DELAY_MS);
40      }
41  
42      /**
# Line 45 | Line 44 | public class LockSupportTest extends JSR
44       */
45      public void testParkUntilBeforeUnpark() throws InterruptedException {
46          final CountDownLatch threadStarted = new CountDownLatch(1);
47 <        Thread t = new Thread(new CheckedRunnable() {
47 >        Thread t = newStartedThread(new CheckedRunnable() {
48              public void realRun() {
49                  long d = new Date().getTime() + LONG_DELAY_MS;
50                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
# Line 55 | Line 54 | public class LockSupportTest extends JSR
54                  assertTrue(System.nanoTime() - t0 < nanos);
55              }});
56  
58        t.start();
57          threadStarted.await();
58 <        Thread.sleep(SHORT_DELAY_MS);
58 >        delay(SHORT_DELAY_MS);
59          LockSupport.unpark(t);
60 <        joinWith(t);
60 >        awaitTermination(t, MEDIUM_DELAY_MS);
61      }
62  
63      /**
# Line 67 | Line 65 | public class LockSupportTest extends JSR
65       */
66      public void testParkNanosBeforeUnpark() throws InterruptedException {
67          final CountDownLatch threadStarted = new CountDownLatch(1);
68 <        Thread t = new Thread(new CheckedRunnable() {
68 >        Thread t = newStartedThread(new CheckedRunnable() {
69              public void realRun() {
70                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
71                  long t0 = System.nanoTime();
# Line 76 | Line 74 | public class LockSupportTest extends JSR
74                  assertTrue(System.nanoTime() - t0 < nanos);
75              }});
76  
79        t.start();
77          threadStarted.await();
78 <        Thread.sleep(SHORT_DELAY_MS);
78 >        delay(SHORT_DELAY_MS);
79          LockSupport.unpark(t);
80 <        joinWith(t);
80 >        awaitTermination(t, MEDIUM_DELAY_MS);
81      }
82  
83      /**
# Line 89 | Line 86 | public class LockSupportTest extends JSR
86      public void testParkAfterUnpark() throws Exception {
87          final CountDownLatch threadStarted = new CountDownLatch(1);
88          final AtomicBoolean unparked = new AtomicBoolean(false);
89 <        Thread t = new Thread(new CheckedRunnable() {
89 >        Thread t = newStartedThread(new CheckedRunnable() {
90              public void realRun() throws Exception {
91                  threadStarted.countDown();
92                  while (!unparked.get())
# Line 97 | Line 94 | public class LockSupportTest extends JSR
94                  LockSupport.park();
95              }});
96  
100        t.start();
97          threadStarted.await();
98          LockSupport.unpark(t);
99          unparked.set(true);
100 <        joinWith(t);
100 >        awaitTermination(t, MEDIUM_DELAY_MS);
101      }
102  
103      /**
# Line 110 | Line 106 | public class LockSupportTest extends JSR
106      public void testParkUntilAfterUnpark() throws Exception {
107          final CountDownLatch threadStarted = new CountDownLatch(1);
108          final AtomicBoolean unparked = new AtomicBoolean(false);
109 <        Thread t = new Thread(new CheckedRunnable() {
109 >        Thread t = newStartedThread(new CheckedRunnable() {
110              public void realRun() throws Exception {
111                  threadStarted.countDown();
112                  while (!unparked.get())
# Line 122 | Line 118 | public class LockSupportTest extends JSR
118                  assertTrue(System.nanoTime() - t0 < nanos);
119              }});
120  
125        t.start();
121          threadStarted.await();
122          LockSupport.unpark(t);
123          unparked.set(true);
124 <        joinWith(t);
124 >        awaitTermination(t, MEDIUM_DELAY_MS);
125      }
126  
127      /**
# Line 135 | Line 130 | public class LockSupportTest extends JSR
130      public void testParkNanosAfterUnpark() throws Exception {
131          final CountDownLatch threadStarted = new CountDownLatch(1);
132          final AtomicBoolean unparked = new AtomicBoolean(false);
133 <        Thread t = new Thread(new CheckedRunnable() {
133 >        Thread t = newStartedThread(new CheckedRunnable() {
134              public void realRun() throws Exception {
135                  threadStarted.countDown();
136                  while (!unparked.get())
# Line 146 | Line 141 | public class LockSupportTest extends JSR
141                  assertTrue(System.nanoTime() - t0 < nanos);
142              }});
143  
149        t.start();
144          threadStarted.await();
145          LockSupport.unpark(t);
146          unparked.set(true);
147 <        joinWith(t);
147 >        awaitTermination(t, MEDIUM_DELAY_MS);
148      }
149  
150      /**
# Line 158 | Line 152 | public class LockSupportTest extends JSR
152       */
153      public void testParkBeforeInterrupt() throws InterruptedException {
154          final CountDownLatch threadStarted = new CountDownLatch(1);
155 <        Thread t = new Thread(new CheckedRunnable() {
155 >        Thread t = newStartedThread(new CheckedRunnable() {
156              public void realRun() {
157                  assertFalse(Thread.currentThread().isInterrupted());
158                  threadStarted.countDown();
159 <                LockSupport.park();
160 <                assertTrue(Thread.currentThread().isInterrupted());
159 >                do {
160 >                    LockSupport.park();
161 >                    // park may return spuriously
162 >                } while (! Thread.currentThread().isInterrupted());
163              }});
164  
169        t.start();
165          threadStarted.await();
166 <        Thread.sleep(SHORT_DELAY_MS);
166 >        delay(SHORT_DELAY_MS);
167          t.interrupt();
168 <        joinWith(t);
168 >        awaitTermination(t, MEDIUM_DELAY_MS);
169      }
170  
171      /**
# Line 178 | Line 173 | public class LockSupportTest extends JSR
173       */
174      public void testParkUntilBeforeInterrupt() throws InterruptedException {
175          final CountDownLatch threadStarted = new CountDownLatch(1);
176 <        Thread t = new Thread(new CheckedRunnable() {
176 >        Thread t = newStartedThread(new CheckedRunnable() {
177              public void realRun() {
178                  long d = new Date().getTime() + LONG_DELAY_MS;
179                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
180                  long t0 = System.nanoTime();
181 +                assertFalse(Thread.currentThread().isInterrupted());
182                  threadStarted.countDown();
183 <                LockSupport.parkUntil(d);
183 >                do {
184 >                    LockSupport.parkUntil(d);
185 >                    // parkUntil may return spuriously
186 >                } while (! Thread.currentThread().isInterrupted());
187                  assertTrue(System.nanoTime() - t0 < nanos);
188              }});
189  
191        t.start();
190          threadStarted.await();
191 <        Thread.sleep(SHORT_DELAY_MS);
191 >        delay(SHORT_DELAY_MS);
192          t.interrupt();
193 <        joinWith(t);
193 >        awaitTermination(t, MEDIUM_DELAY_MS);
194      }
195  
196      /**
# Line 200 | Line 198 | public class LockSupportTest extends JSR
198       */
199      public void testParkNanosBeforeInterrupt() throws InterruptedException {
200          final CountDownLatch threadStarted = new CountDownLatch(1);
201 <        Thread t = new Thread(new CheckedRunnable() {
201 >        Thread t = newStartedThread(new CheckedRunnable() {
202              public void realRun() {
203                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
204                  long t0 = System.nanoTime();
205 +                assertFalse(Thread.currentThread().isInterrupted());
206                  threadStarted.countDown();
207 <                LockSupport.parkNanos(nanos);
207 >                do {
208 >                    LockSupport.parkNanos(nanos);
209 >                    // parkNanos may return spuriously
210 >                } while (! Thread.currentThread().isInterrupted());
211                  assertTrue(System.nanoTime() - t0 < nanos);
212              }});
213  
212        t.start();
214          threadStarted.await();
215 <        Thread.sleep(SHORT_DELAY_MS);
215 >        delay(SHORT_DELAY_MS);
216          t.interrupt();
217 <        joinWith(t);
217 >        awaitTermination(t, MEDIUM_DELAY_MS);
218      }
219  
220      /**
# Line 222 | Line 223 | public class LockSupportTest extends JSR
223      public void testParkAfterInterrupt() throws Exception {
224          final CountDownLatch threadStarted = new CountDownLatch(1);
225          final AtomicBoolean unparked = new AtomicBoolean(false);
226 <        Thread t = new Thread(new CheckedRunnable() {
226 >        Thread t = newStartedThread(new CheckedRunnable() {
227              public void realRun() throws Exception {
228                  threadStarted.countDown();
229                  while (!unparked.get())
# Line 232 | Line 233 | public class LockSupportTest extends JSR
233                  assertTrue(Thread.currentThread().isInterrupted());
234              }});
235  
235        t.start();
236          threadStarted.await();
237          t.interrupt();
238          unparked.set(true);
239 <        joinWith(t);
239 >        awaitTermination(t, MEDIUM_DELAY_MS);
240      }
241  
242      /**
# Line 245 | Line 245 | public class LockSupportTest extends JSR
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() {
248 >        Thread t = newStartedThread(new CheckedRunnable() {
249              public void realRun() throws Exception {
250                  threadStarted.countDown();
251                  while (!unparked.get())
# Line 253 | Line 253 | public class LockSupportTest extends JSR
253                  long d = new Date().getTime() + LONG_DELAY_MS;
254                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
255                  long t0 = System.nanoTime();
256 +                assertTrue(Thread.currentThread().isInterrupted());
257                  LockSupport.parkUntil(d);
258                  assertTrue(System.nanoTime() - t0 < nanos);
259 +                assertTrue(Thread.currentThread().isInterrupted());
260              }});
261  
260        t.start();
262          threadStarted.await();
263          t.interrupt();
264          unparked.set(true);
265 <        joinWith(t);
265 >        awaitTermination(t, MEDIUM_DELAY_MS);
266      }
267  
268      /**
# Line 270 | Line 271 | public class LockSupportTest extends JSR
271      public void testParkNanosAfterInterrupt() throws Exception {
272          final CountDownLatch threadStarted = new CountDownLatch(1);
273          final AtomicBoolean unparked = new AtomicBoolean(false);
274 <        Thread t = new Thread(new CheckedRunnable() {
274 >        Thread t = newStartedThread(new CheckedRunnable() {
275              public void realRun() throws Exception {
276                  threadStarted.countDown();
277                  while (!unparked.get())
278                      Thread.yield();
279                  long nanos = LONG_DELAY_MS * 1000L * 1000L;
280                  long t0 = System.nanoTime();
281 +                assertTrue(Thread.currentThread().isInterrupted());
282                  LockSupport.parkNanos(nanos);
283                  assertTrue(System.nanoTime() - t0 < nanos);
284 +                assertTrue(Thread.currentThread().isInterrupted());
285              }});
286  
284        t.start();
287          threadStarted.await();
288          t.interrupt();
289          unparked.set(true);
290 <        joinWith(t);
290 >        awaitTermination(t, MEDIUM_DELAY_MS);
291      }
292  
293      /**
294       * parkNanos times out if not unparked
295       */
296      public void testParkNanosTimesOut() throws InterruptedException {
297 <        Thread t = new Thread(new CheckedRunnable() {
297 >        Thread t = newStartedThread(new CheckedRunnable() {
298              public void realRun() {
299 <                final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
300 <                long t0 = System.nanoTime();
301 <                LockSupport.parkNanos(timeoutNanos);
302 <                assertTrue(System.nanoTime() - t0 >= timeoutNanos);
299 >                for (;;) {
300 >                    long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
301 >                    long t0 = System.nanoTime();
302 >                    LockSupport.parkNanos(timeoutNanos);
303 >                    // parkNanos may return spuriously
304 >                    if (System.nanoTime() - t0 >= timeoutNanos)
305 >                        return;
306 >                }
307              }});
308  
309 <        t.start();
304 <        joinWith(t);
309 >        awaitTermination(t, MEDIUM_DELAY_MS);
310      }
311  
312  
# Line 309 | Line 314 | public class LockSupportTest extends JSR
314       * parkUntil times out if not unparked
315       */
316      public void testParkUntilTimesOut() throws InterruptedException {
317 <        Thread t = new Thread(new CheckedRunnable() {
317 >        Thread t = newStartedThread(new CheckedRunnable() {
318              public void realRun() {
319 <                long d = new Date().getTime() + SHORT_DELAY_MS;
320 <                // beware of rounding
321 <                long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L;
322 <                long t0 = System.nanoTime();
323 <                LockSupport.parkUntil(d);
324 <                assertTrue(System.nanoTime() - t0 >= timeoutNanos);
319 >                for (;;) {
320 >                    long d = new Date().getTime() + SHORT_DELAY_MS;
321 >                    // beware of rounding
322 >                    long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L;
323 >                    long t0 = System.nanoTime();
324 >                    LockSupport.parkUntil(d);
325 >                    // parkUntil may return spuriously
326 >                    if (System.nanoTime() - t0 >= timeoutNanos)
327 >                        return;
328 >                }
329              }});
330  
331 <        t.start();
323 <        joinWith(t);
331 >        awaitTermination(t, MEDIUM_DELAY_MS);
332      }
333  
334      /**
# Line 328 | Line 336 | public class LockSupportTest extends JSR
336       * Requires hotspot fix for:
337       * 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
338       */
339 <    public void XXXtestParkUntil0Returns() throws InterruptedException {
340 <        Thread t = new Thread(new CheckedRunnable() {
339 >    public void XXXXtestParkUntil0Returns() throws InterruptedException {
340 >        Thread t = newStartedThread(new CheckedRunnable() {
341              public void realRun() {
342                  LockSupport.parkUntil(0L);
343              }});
344  
345 <        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 <        }
345 >        awaitTermination(t, MEDIUM_DELAY_MS);
346      }
347   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines