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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.32 by jsr166, Fri Oct 29 06:21:16 2010 UTC vs.
Revision 1.45 by dl, Fri May 6 16:43:45 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 John Vint
6   */
7  
# Line 40 | Line 40 | public class LinkedTransferQueueTest ext
40                              new Generic().testSuite());
41      }
42  
43    void checkEmpty(BlockingQueue q) {
44        try {
45            assertTrue(q.isEmpty());
46            assertEquals(0, q.size());
47            assertNull(q.peek());
48            assertNull(q.poll());
49            assertNull(q.poll(0, MILLISECONDS));
50            assertEquals(q.toString(), "[]");
51            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52            assertFalse(q.iterator().hasNext());
53            try {
54                q.element();
55                shouldThrow();
56            } catch (NoSuchElementException success) {}
57            try {
58                q.iterator().next();
59                shouldThrow();
60            } catch (NoSuchElementException success) {}
61            try {
62                q.remove();
63                shouldThrow();
64            } catch (NoSuchElementException success) {}
65        } catch (InterruptedException ie) {
66            threadUnexpectedException(ie);
67        }
68    }
69
43      /**
44       * Constructor builds new queue with size being zero and empty
45       * being true
# Line 354 | Line 327 | public class LinkedTransferQueueTest ext
327                  for (int i = 0; i < SIZE; ++i) {
328                      long t0 = System.nanoTime();
329                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
330 <                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
330 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
331                  }
332 +                long t0 = System.nanoTime();
333                  aboutToWait.countDown();
334                  try {
335                      q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
336                      shouldThrow();
337 <                } catch (InterruptedException success) {}
337 >                } catch (InterruptedException success) {
338 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
339 >                }
340              }});
341  
342          aboutToWait.await();
# Line 382 | Line 358 | public class LinkedTransferQueueTest ext
358                  for (int i = 0; i < SIZE; ++i) {
359                      long t0 = System.nanoTime();
360                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
361 <                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
361 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
362                  }
363                  try {
364                      q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
# Line 445 | Line 421 | public class LinkedTransferQueueTest ext
421       */
422      public void testRemoveElement() throws InterruptedException {
423          LinkedTransferQueue q = populatedQueue(SIZE);
424 <        for (int i = 1; i < SIZE; i += 2) {
424 >        for (int i = 1; i < SIZE; i+=2) {
425 >            assertTrue(q.contains(i));
426              assertTrue(q.remove(i));
427 +            assertFalse(q.contains(i));
428 +            assertTrue(q.contains(i-1));
429          }
430 <        for (int i = 0; i < SIZE; i += 2) {
430 >        for (int i = 0; i < SIZE; i+=2) {
431 >            assertTrue(q.contains(i));
432              assertTrue(q.remove(i));
433 <            assertFalse(q.remove(i + 1));
433 >            assertFalse(q.contains(i));
434 >            assertFalse(q.remove(i+1));
435 >            assertFalse(q.contains(i+1));
436          }
437          checkEmpty(q);
438      }
# Line 547 | Line 529 | public class LinkedTransferQueueTest ext
529      }
530  
531      /**
532 <     * toArray() contains all elements
532 >     * toArray() contains all elements in FIFO order
533       */
534 <    public void testToArray() throws InterruptedException {
534 >    public void testToArray() {
535          LinkedTransferQueue q = populatedQueue(SIZE);
536          Object[] o = q.toArray();
537          for (int i = 0; i < o.length; i++) {
538 <            assertEquals(o[i], q.take());
538 >            assertSame(o[i], q.poll());
539          }
540      }
541  
542      /**
543 <     * toArray(a) contains all elements
543 >     * toArray(a) contains all elements in FIFO order
544       */
545 <    public void testToArray2() throws InterruptedException {
545 >    public void testToArray2() {
546          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
547          Integer[] ints = new Integer[SIZE];
548 <        ints = q.toArray(ints);
548 >        Integer[] array = q.toArray(ints);
549 >        assertSame(ints, array);
550          for (int i = 0; i < ints.length; i++) {
551 <            assertEquals(ints[i], q.take());
551 >            assertSame(ints[i], q.poll());
552          }
553      }
554  
555      /**
556       * toArray(null) throws NullPointerException
557       */
558 <    public void testToArray_BadArg() {
558 >    public void testToArray_NullArg() {
559          LinkedTransferQueue q = populatedQueue(SIZE);
560          try {
561 <            Object o[] = q.toArray(null);
561 >            q.toArray(null);
562              shouldThrow();
563          } catch (NullPointerException success) {}
564      }
565  
566      /**
567 <     * toArray(incompatible array type) throws CCE
567 >     * toArray(incompatible array type) throws ArrayStoreException
568       */
569      public void testToArray1_BadArg() {
570          LinkedTransferQueue q = populatedQueue(SIZE);
571          try {
572 <            Object o[] = q.toArray(new String[10]);
572 >            q.toArray(new String[10]);
573              shouldThrow();
574          } catch (ArrayStoreException success) {}
575      }
# Line 910 | Line 893 | public class LinkedTransferQueueTest ext
893          Thread t = newStartedThread(new CheckedRunnable() {
894              public void realRun() throws InterruptedException {
895                  threadStarted.countDown();
896 <                q.transfer(SIZE);
896 >                q.transfer(five);
897                  checkEmpty(q);
898              }});
899  
900          threadStarted.await();
901          waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
919        assertTrue(t.isAlive());
902          assertEquals(1, q.size());
903 <        assertEquals(SIZE, (int) q.poll());
903 >        assertSame(five, q.poll());
904          checkEmpty(q);
905          awaitTermination(t, MEDIUM_DELAY_MS);
906      }
# Line 1069 | Line 1051 | public class LinkedTransferQueueTest ext
1051      }
1052  
1053      /**
1054 <     * tryTransfer waits the amount given if interrupted, and
1055 <     * throws interrupted exception
1054 >     * tryTransfer waits the amount given, and throws
1055 >     * InterruptedException when interrupted.
1056       */
1057      public void testTryTransfer5() throws InterruptedException {
1058          final LinkedTransferQueue q = new LinkedTransferQueue();
1059          final CountDownLatch threadStarted = new CountDownLatch(1);
1060 +        assertTrue(q.isEmpty());
1061  
1062          Thread t = newStartedThread(new CheckedRunnable() {
1063              public void realRun() throws InterruptedException {
# Line 1085 | Line 1068 | public class LinkedTransferQueueTest ext
1068                      shouldThrow();
1069                  } catch (InterruptedException success) {}
1070                  assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1071 +                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
1072              }});
1073  
1074          threadStarted.await();
1075 <        Thread.sleep(SHORT_DELAY_MS);
1075 >        while (q.isEmpty())
1076 >            Thread.yield();
1077 >        delay(SHORT_DELAY_MS);
1078          t.interrupt();
1079          awaitTermination(t, MEDIUM_DELAY_MS);
1080          checkEmpty(q);
1081      }
1082  
1083      /**
1084 <     * tryTransfer gives up after the timeout and return false
1084 >     * tryTransfer gives up after the timeout and returns false
1085       */
1086      public void testTryTransfer6() throws InterruptedException {
1087          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 1137 | Line 1123 | public class LinkedTransferQueueTest ext
1123      }
1124  
1125      /**
1126 <     * tryTransfer attempts to enqueue into the q and fails returning
1127 <     * false not enqueueing and the successive poll is null
1126 >     * tryTransfer attempts to enqueue into the queue and fails
1127 >     * returning false not enqueueing and the successive poll is null
1128       */
1129      public void testTryTransfer8() throws InterruptedException {
1130          final LinkedTransferQueue q = new LinkedTransferQueue();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines