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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.35 by jsr166, Wed Nov 3 16:46:34 2010 UTC vs.
Revision 1.43 by jsr166, Sat May 21 06:24:33 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 41 | Line 41 | public class LinkedBlockingQueueTest ext
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private LinkedBlockingQueue populatedQueue(int n) {
45 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
44 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
45 >        LinkedBlockingQueue<Integer> q =
46 >            new LinkedBlockingQueue<Integer>(n);
47          assertTrue(q.isEmpty());
48          for (int i = 0; i < n; i++)
49              assertTrue(q.offer(new Integer(i)));
# Line 276 | Line 277 | public class LinkedBlockingQueueTest ext
277      /**
278       * put(null) throws NPE
279       */
280 <     public void testPutNull() throws InterruptedException {
280 >    public void testPutNull() throws InterruptedException {
281          try {
282              LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
283              q.put(null);
284              shouldThrow();
285          } catch (NullPointerException success) {}
286 <     }
286 >    }
287  
288      /**
289       * all elements successfully put are contained
# Line 315 | Line 316 | public class LinkedBlockingQueueTest ext
316              }});
317  
318          t.start();
319 <        Thread.sleep(SHORT_DELAY_MS);
319 >        delay(SHORT_DELAY_MS);
320          t.interrupt();
321          t.join();
322          assertEquals(SIZE, q.size());
# Line 339 | Line 340 | public class LinkedBlockingQueueTest ext
340              }});
341  
342          t.start();
343 <        Thread.sleep(SHORT_DELAY_MS);
343 >        delay(SHORT_DELAY_MS);
344          assertEquals(q.remainingCapacity(), 0);
345          assertEquals(0, q.take());
346 <        Thread.sleep(SHORT_DELAY_MS);
346 >        delay(SHORT_DELAY_MS);
347          t.interrupt();
348          t.join();
349          assertEquals(q.remainingCapacity(), 0);
# Line 351 | Line 352 | public class LinkedBlockingQueueTest ext
352      /**
353       * timed offer times out if full and elements not taken
354       */
355 <    public void testTimedOffer() throws InterruptedException {
355 >    public void testTimedOffer() {
356          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
357 <        Thread t = new Thread(new CheckedRunnable() {
357 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
358 >        Thread t = newStartedThread(new CheckedRunnable() {
359              public void realRun() throws InterruptedException {
360                  q.put(new Object());
361                  q.put(new Object());
362 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
362 >                long startTime = System.nanoTime();
363 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
364 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
365 >                pleaseInterrupt.countDown();
366                  try {
367 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
367 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
368                      shouldThrow();
369                  } catch (InterruptedException success) {}
370              }});
371  
372 <        t.start();
368 <        Thread.sleep(SMALL_DELAY_MS);
372 >        await(pleaseInterrupt);
373          t.interrupt();
374 <        t.join();
374 >        awaitTermination(t);
375      }
376  
377      /**
# Line 397 | Line 401 | public class LinkedBlockingQueueTest ext
401              }});
402  
403          t.start();
404 <        Thread.sleep(SHORT_DELAY_MS);
404 >        delay(SHORT_DELAY_MS);
405          t.interrupt();
406          t.join();
407      }
# Line 440 | Line 444 | public class LinkedBlockingQueueTest ext
444       * returning timeout status
445       */
446      public void testInterruptedTimedPoll() throws InterruptedException {
447 <        Thread t = new Thread(new CheckedRunnable() {
447 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
448 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
449 >        Thread t = newStartedThread(new CheckedRunnable() {
450              public void realRun() throws InterruptedException {
445                LinkedBlockingQueue q = populatedQueue(SIZE);
451                  for (int i = 0; i < SIZE; ++i) {
452 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
452 >                    long t0 = System.nanoTime();
453 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
454 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
455                  }
456 +                long t0 = System.nanoTime();
457 +                aboutToWait.countDown();
458                  try {
459 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
459 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
460                      shouldThrow();
461 <                } catch (InterruptedException success) {}
461 >                } catch (InterruptedException success) {
462 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
463 >                }
464              }});
465  
466 <        t.start();
467 <        Thread.sleep(SHORT_DELAY_MS);
466 >        aboutToWait.await();
467 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
468          t.interrupt();
469 <        t.join();
469 >        awaitTermination(t, MEDIUM_DELAY_MS);
470 >        checkEmpty(q);
471      }
472  
473      /**
# Line 507 | Line 519 | public class LinkedBlockingQueueTest ext
519      public void testRemoveElement() {
520          LinkedBlockingQueue q = populatedQueue(SIZE);
521          for (int i = 1; i < SIZE; i+=2) {
522 <            assertTrue(q.remove(new Integer(i)));
522 >            assertTrue(q.contains(i));
523 >            assertTrue(q.remove(i));
524 >            assertFalse(q.contains(i));
525 >            assertTrue(q.contains(i-1));
526          }
527          for (int i = 0; i < SIZE; i+=2) {
528 <            assertTrue(q.remove(new Integer(i)));
529 <            assertFalse(q.remove(new Integer(i+1)));
528 >            assertTrue(q.contains(i));
529 >            assertTrue(q.remove(i));
530 >            assertFalse(q.contains(i));
531 >            assertFalse(q.remove(i+1));
532 >            assertFalse(q.contains(i+1));
533          }
534          assertTrue(q.isEmpty());
535      }
# Line 607 | Line 625 | public class LinkedBlockingQueueTest ext
625      }
626  
627      /**
628 <     * toArray contains all elements
628 >     * toArray contains all elements in FIFO order
629       */
630 <    public void testToArray() throws InterruptedException {
630 >    public void testToArray() {
631          LinkedBlockingQueue q = populatedQueue(SIZE);
632          Object[] o = q.toArray();
633          for (int i = 0; i < o.length; i++)
634 <            assertEquals(o[i], q.take());
634 >            assertSame(o[i], q.poll());
635      }
636  
637      /**
638 <     * toArray(a) contains all elements
638 >     * toArray(a) contains all elements in FIFO order
639       */
640      public void testToArray2() throws InterruptedException {
641 <        LinkedBlockingQueue q = populatedQueue(SIZE);
641 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
642          Integer[] ints = new Integer[SIZE];
643 <        ints = (Integer[])q.toArray(ints);
643 >        Integer[] array = q.toArray(ints);
644 >        assertSame(ints, array);
645          for (int i = 0; i < ints.length; i++)
646 <            assertEquals(ints[i], q.take());
646 >            assertSame(ints[i], q.poll());
647      }
648  
649      /**
# Line 742 | Line 761 | public class LinkedBlockingQueueTest ext
761  
762          executor.execute(new CheckedRunnable() {
763              public void realRun() throws InterruptedException {
764 <                Thread.sleep(SMALL_DELAY_MS);
764 >                delay(SMALL_DELAY_MS);
765                  assertSame(one, q.take());
766              }});
767  
# Line 764 | Line 783 | public class LinkedBlockingQueueTest ext
783  
784          executor.execute(new CheckedRunnable() {
785              public void realRun() throws InterruptedException {
786 <                Thread.sleep(SMALL_DELAY_MS);
786 >                delay(SMALL_DELAY_MS);
787                  q.put(one);
788              }});
789  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines