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

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.25 by jsr166, Wed Oct 6 07:49:23 2010 UTC vs.
Revision 1.33 by dl, Fri May 6 11:22:07 2011 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 37 | Line 37 | public class SynchronousQueueTest extend
37      }
38  
39      /**
40 <     * A SynchronousQueue is both empty and full
40 >     * Any SynchronousQueue is both empty and full
41       */
42 <    public void testEmptyFull() {
43 <        SynchronousQueue q = new SynchronousQueue();
42 >    public void testEmptyFull(SynchronousQueue q) {
43          assertTrue(q.isEmpty());
44          assertEquals(0, q.size());
45          assertEquals(0, q.remainingCapacity());
# Line 48 | Line 47 | public class SynchronousQueueTest extend
47      }
48  
49      /**
50 +     * A non-fair SynchronousQueue is both empty and full
51 +     */
52 +    public void testEmptyFull() {
53 +        testEmptyFull(new SynchronousQueue());
54 +    }
55 +
56 +    /**
57       * A fair SynchronousQueue is both empty and full
58       */
59      public void testFairEmptyFull() {
60 <        SynchronousQueue q = new SynchronousQueue(true);
55 <        assertTrue(q.isEmpty());
56 <        assertEquals(0, q.size());
57 <        assertEquals(0, q.remainingCapacity());
58 <        assertFalse(q.offer(zero));
60 >        testEmptyFull(new SynchronousQueue(true));
61      }
62  
63      /**
# Line 157 | Line 159 | public class SynchronousQueueTest extend
159              q.put(null);
160              shouldThrow();
161          } catch (NullPointerException success) {}
162 <     }
162 >    }
163  
164      /**
165       * put blocks interruptibly if no active taker
# Line 170 | Line 172 | public class SynchronousQueueTest extend
172              }});
173  
174          t.start();
175 <        Thread.sleep(SHORT_DELAY_MS);
175 >        delay(SHORT_DELAY_MS);
176          t.interrupt();
177          t.join();
178      }
# Line 194 | Line 196 | public class SynchronousQueueTest extend
196              }});
197  
198          t.start();
199 <        Thread.sleep(SHORT_DELAY_MS);
199 >        delay(SHORT_DELAY_MS);
200          assertEquals(0, q.take());
201 <        Thread.sleep(SHORT_DELAY_MS);
201 >        delay(SHORT_DELAY_MS);
202          t.interrupt();
203          t.join();
204      }
# Line 204 | Line 206 | public class SynchronousQueueTest extend
206      /**
207       * timed offer times out if elements not taken
208       */
209 <    public void testTimedOffer() throws InterruptedException {
210 <        final SynchronousQueue q = new SynchronousQueue();
211 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
209 >    public void testTimedOffer(final SynchronousQueue q)
210 >            throws InterruptedException {
211 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
212 >        Thread t = newStartedThread(new CheckedRunnable() {
213              public void realRun() throws InterruptedException {
214 +                long t0 = System.nanoTime();
215                  assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
216 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
216 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
217 >                pleaseInterrupt.countDown();
218 >                t0 = System.nanoTime();
219 >                try {
220 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
221 >                    shouldThrow();
222 >                } catch (InterruptedException success) {}
223 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
224              }});
225  
226 <        t.start();
216 <        Thread.sleep(SMALL_DELAY_MS);
226 >        assertTrue(pleaseInterrupt.await(MEDIUM_DELAY_MS, MILLISECONDS));
227          t.interrupt();
228 <        t.join();
228 >        awaitTermination(t, MEDIUM_DELAY_MS);
229      }
230  
221
231      /**
232 <     * take blocks interruptibly when empty
232 >     * timed offer times out if elements not taken
233       */
234 <    public void testTakeFromEmpty() throws InterruptedException {
235 <        final SynchronousQueue q = new SynchronousQueue();
227 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
228 <            public void realRun() throws InterruptedException {
229 <                q.take();
230 <            }});
231 <
232 <        t.start();
233 <        Thread.sleep(SHORT_DELAY_MS);
234 <        t.interrupt();
235 <        t.join();
234 >    public void testTimedOffer() throws InterruptedException {
235 >        testTimedOffer(new SynchronousQueue());
236      }
237  
238 +    /**
239 +     * timed offer times out if elements not taken
240 +     */
241 +    public void testFairTimedOffer() throws InterruptedException {
242 +        testTimedOffer(new SynchronousQueue(true));
243 +    }
244  
245      /**
246       * put blocks interruptibly if no active taker
# Line 247 | Line 253 | public class SynchronousQueueTest extend
253              }});
254  
255          t.start();
256 <        Thread.sleep(SHORT_DELAY_MS);
256 >        delay(SHORT_DELAY_MS);
257          t.interrupt();
258          t.join();
259      }
# Line 271 | Line 277 | public class SynchronousQueueTest extend
277              }});
278  
279          t.start();
280 <        Thread.sleep(SHORT_DELAY_MS);
280 >        delay(SHORT_DELAY_MS);
281          assertEquals(0, q.take());
282 <        Thread.sleep(SHORT_DELAY_MS);
277 <        t.interrupt();
278 <        t.join();
279 <    }
280 <
281 <    /**
282 <     * timed offer times out if elements not taken
283 <     */
284 <    public void testFairTimedOffer() throws InterruptedException {
285 <        final SynchronousQueue q = new SynchronousQueue(true);
286 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
287 <            public void realRun() throws InterruptedException {
288 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
289 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
290 <            }});
291 <
292 <        t.start();
293 <        Thread.sleep(SMALL_DELAY_MS);
282 >        delay(SHORT_DELAY_MS);
283          t.interrupt();
284          t.join();
285      }
286  
298
287      /**
288       * take blocks interruptibly when empty
289       */
# Line 307 | Line 295 | public class SynchronousQueueTest extend
295              }});
296  
297          t.start();
298 <        Thread.sleep(SHORT_DELAY_MS);
298 >        delay(SHORT_DELAY_MS);
299          t.interrupt();
300          t.join();
301      }
302  
303      /**
304 <     * poll fails unless active taker
304 >     * poll return null if no active putter
305       */
306      public void testPoll() {
307          SynchronousQueue q = new SynchronousQueue();
# Line 321 | Line 309 | public class SynchronousQueueTest extend
309      }
310  
311      /**
312 <     * timed pool with zero timeout times out if no active taker
312 >     * timed poll with zero timeout times out if no active putter
313       */
314      public void testTimedPoll0() throws InterruptedException {
315          SynchronousQueue q = new SynchronousQueue();
# Line 329 | Line 317 | public class SynchronousQueueTest extend
317      }
318  
319      /**
320 <     * timed pool with nonzero timeout times out if no active taker
320 >     * timed poll with nonzero timeout times out if no active putter
321       */
322      public void testTimedPoll() throws InterruptedException {
323          SynchronousQueue q = new SynchronousQueue();
324 +        long t0 = System.nanoTime();
325          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
326 +        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
327      }
328  
329      /**
330       * Interrupted timed poll throws InterruptedException instead of
331       * returning timeout status
332       */
333 <    public void testInterruptedTimedPoll() throws InterruptedException {
334 <        final SynchronousQueue q = new SynchronousQueue();
335 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
333 >    public void testInterruptedTimedPoll(final SynchronousQueue q)
334 >            throws InterruptedException {
335 >        final CountDownLatch threadStarted = new CountDownLatch(1);
336 >        Thread t = newStartedThread(new CheckedRunnable() {
337              public void realRun() throws InterruptedException {
338 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
338 >                long t0 = System.nanoTime();
339 >                threadStarted.countDown();
340 >                try {
341 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
342 >                    shouldThrow();
343 >                } catch (InterruptedException success) {}
344 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
345 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
346              }});
347  
348 <        t.start();
349 <        Thread.sleep(SHORT_DELAY_MS);
348 >        threadStarted.await();
349 >        delay(SHORT_DELAY_MS);
350          t.interrupt();
351 <        t.join();
351 >        awaitTermination(t, MEDIUM_DELAY_MS);
352      }
353  
354      /**
355       * Interrupted timed poll throws InterruptedException instead of
356       * returning timeout status
357       */
358 <    public void testFairInterruptedTimedPoll() throws InterruptedException {
359 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
360 <            public void realRun() throws InterruptedException {
363 <                SynchronousQueue q = new SynchronousQueue(true);
364 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
365 <            }});
358 >    public void testInterruptedTimedPoll() throws InterruptedException {
359 >        testInterruptedTimedPoll(new SynchronousQueue());
360 >    }
361  
362 <        t.start();
363 <        Thread.sleep(SHORT_DELAY_MS);
364 <        t.interrupt();
365 <        t.join();
362 >    /**
363 >     * Interrupted timed poll throws InterruptedException instead of
364 >     * returning timeout status
365 >     */
366 >    public void testFairInterruptedTimedPoll() throws InterruptedException {
367 >        testInterruptedTimedPoll(new SynchronousQueue(true));
368      }
369  
370      /**
371 <     *  timed poll before a delayed offer fails; after offer succeeds;
372 <     *  on interruption throws
371 >     * timed poll before a delayed offer times out, returning null;
372 >     * after offer succeeds; on interruption throws
373       */
374      public void testFairTimedPollWithOffer() throws InterruptedException {
375          final SynchronousQueue q = new SynchronousQueue(true);
376 <        Thread t = new Thread(new CheckedRunnable() {
376 >        final CountDownLatch pleaseOffer = new CountDownLatch(1);
377 >        Thread t = newStartedThread(new CheckedRunnable() {
378              public void realRun() throws InterruptedException {
379 +                long t0 = System.nanoTime();
380 +                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
381 +                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
382 +
383 +                pleaseOffer.countDown();
384 +                t0 = System.nanoTime();
385 +                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
386 +                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
387 +
388 +                t0 = System.nanoTime();
389                  try {
382                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
383                    assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
390                      q.poll(LONG_DELAY_MS, MILLISECONDS);
391 <                    threadShouldThrow();
391 >                    shouldThrow();
392                  } catch (InterruptedException success) {}
393 +                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
394              }});
395  
396 <        t.start();
397 <        Thread.sleep(SMALL_DELAY_MS);
398 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
396 >        assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS));
397 >        long t0 = System.nanoTime();
398 >        assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
399 >        assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
400 >
401          t.interrupt();
402 <        t.join();
402 >        awaitTermination(t, MEDIUM_DELAY_MS);
403      }
404  
396
405      /**
406 <     * peek returns null
406 >     * peek() returns null if no active putter
407       */
408      public void testPeek() {
409          SynchronousQueue q = new SynchronousQueue();
# Line 403 | Line 411 | public class SynchronousQueueTest extend
411      }
412  
413      /**
414 <     * element throws NSEE
414 >     * element() throws NSEE if no active putter
415       */
416      public void testElement() {
417          SynchronousQueue q = new SynchronousQueue();
# Line 414 | Line 422 | public class SynchronousQueueTest extend
422      }
423  
424      /**
425 <     * remove throws NSEE if no active taker
425 >     * remove() throws NSEE if no active putter
426       */
427      public void testRemove() {
428          SynchronousQueue q = new SynchronousQueue();
# Line 565 | Line 573 | public class SynchronousQueueTest extend
573  
574          executor.execute(new CheckedRunnable() {
575              public void realRun() throws InterruptedException {
576 <                Thread.sleep(SMALL_DELAY_MS);
576 >                delay(SMALL_DELAY_MS);
577                  assertSame(one, q.take());
578              }});
579  
# Line 587 | Line 595 | public class SynchronousQueueTest extend
595  
596          executor.execute(new CheckedRunnable() {
597              public void realRun() throws InterruptedException {
598 <                Thread.sleep(SHORT_DELAY_MS);
598 >                delay(SHORT_DELAY_MS);
599                  q.put(one);
600              }});
601  
# Line 657 | Line 665 | public class SynchronousQueueTest extend
665  
666          t.start();
667          ArrayList l = new ArrayList();
668 <        Thread.sleep(SHORT_DELAY_MS);
668 >        delay(SHORT_DELAY_MS);
669          q.drainTo(l);
670          assertTrue(l.size() <= 1);
671          if (l.size() > 0)
# Line 706 | Line 714 | public class SynchronousQueueTest extend
714          t1.start();
715          t2.start();
716          ArrayList l = new ArrayList();
717 <        Thread.sleep(SHORT_DELAY_MS);
717 >        delay(SHORT_DELAY_MS);
718          q.drainTo(l, 1);
719          assertEquals(1, l.size());
720          q.drainTo(l, 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines