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.9 by jsr166, Wed Aug 5 00:43:23 2009 UTC vs.
Revision 1.14 by jsr166, Sat Aug 15 03:20:36 2009 UTC

# Line 13 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
16 import java.util.ConcurrentModificationException;
16   import java.util.Iterator;
17 + import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
21   import junit.framework.Test;
22   import junit.framework.TestSuite;
23  
24 + @SuppressWarnings({"unchecked", "rawtypes"})
25   public class LinkedTransferQueueTest extends JSR166TestCase {
26  
27      public static void main(String[] args) {
# Line 31 | Line 32 | public class LinkedTransferQueueTest ext
32          return new TestSuite(LinkedTransferQueueTest.class);
33      }
34  
35 +    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
36 +        assertTrue(q.isEmpty());
37 +        assertEquals(0, q.size());
38 +        assertNull(q.peek());
39 +        assertNull(q.poll());
40 +        assertNull(q.poll(0, MILLISECONDS));
41 +        assertEquals(q.toString(), "[]");
42 +        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
43 +        assertFalse(q.iterator().hasNext());
44 +        try {
45 +            q.element();
46 +            shouldThrow();
47 +        } catch (NoSuchElementException success) {
48 +        }
49 +        try {
50 +            q.iterator().next();
51 +            shouldThrow();
52 +        } catch (NoSuchElementException success) {
53 +        }
54 +        try {
55 +            q.remove();
56 +            shouldThrow();
57 +        } catch (NoSuchElementException success) {
58 +        }
59 +    }
60 +
61      /**
62       * Constructor builds new queue with size being zero and empty
63       * being true
# Line 85 | Line 112 | public class LinkedTransferQueueTest ext
112       * Queue contains all elements of the collection it is initialized by
113       */
114      public void testConstructor5() {
115 <        try {
116 <            Integer[] ints = new Integer[SIZE];
117 <            for (int i = 0; i < SIZE; ++i) {
118 <                ints[i] = i;
119 <            }
120 <            LinkedTransferQueue q
121 <                = new LinkedTransferQueue(Arrays.asList(ints));
122 <            for (int i = 0; i < SIZE; ++i) {
123 <                assertEquals(ints[i], q.poll());
124 <            }
125 <        } finally {
115 >        Integer[] ints = new Integer[SIZE];
116 >        for (int i = 0; i < SIZE; ++i) {
117 >            ints[i] = i;
118 >        }
119 >        List intList = Arrays.asList(ints);
120 >        LinkedTransferQueue q
121 >            = new LinkedTransferQueue(intList);
122 >        assertEquals(q.size(), intList.size());
123 >        assertEquals(q.toString(), intList.toString());
124 >        assertTrue(Arrays.equals(q.toArray(),
125 >                                     intList.toArray()));
126 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
127 >                                 intList.toArray(new Object[0])));
128 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
129 >                                 intList.toArray(new Object[SIZE])));
130 >        for (int i = 0; i < SIZE; ++i) {
131 >            assertEquals(ints[i], q.poll());
132          }
133      }
134  
135      /**
136 <     * Remaining capacity never decrease nor increase on add or remove
136 >     * remainingCapacity() always returns Integer.MAX_VALUE
137       */
138      public void testRemainingCapacity() {
139          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 228 | Line 261 | public class LinkedTransferQueueTest ext
261      public void testPut() {
262          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
263          for (int i = 0; i < SIZE; ++i) {
264 +            assertEquals(q.size(), i);
265              q.put(i);
266              assertTrue(q.contains(i));
267          }
234        assertEquals(q.size(), SIZE);
268      }
269  
270      /**
# Line 262 | Line 295 | public class LinkedTransferQueueTest ext
295       * Take removes existing elements until empty, then blocks interruptibly
296       */
297      public void testBlockingTake() throws InterruptedException {
298 +        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
299          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
300              void realRun() throws InterruptedException {
267                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
301                  for (int i = 0; i < SIZE; ++i) {
302                      threadAssertEquals(i, (int) q.take());
303                  }
304                  q.take();
305              }});
306 <        Thread.sleep(SHORT_DELAY_MS);
306 >        Thread.sleep(SMALL_DELAY_MS);
307          t.interrupt();
308          t.join();
309 +        checkEmpty(q);
310      }
311  
312      /**
313       * poll succeeds unless empty
314       */
315 <    public void testPoll() {
315 >    public void testPoll() throws InterruptedException {
316          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
317          for (int i = 0; i < SIZE; ++i) {
318              assertEquals(i, (int) q.poll());
319          }
320          assertNull(q.poll());
321 +        checkEmpty(q);
322      }
323  
324      /**
# Line 295 | Line 330 | public class LinkedTransferQueueTest ext
330              assertEquals(i, (int) q.poll(0, MILLISECONDS));
331          }
332          assertNull(q.poll(0, MILLISECONDS));
333 +        checkEmpty(q);
334      }
335  
336      /**
# Line 303 | Line 339 | public class LinkedTransferQueueTest ext
339      public void testTimedPoll() throws InterruptedException {
340          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
341          for (int i = 0; i < SIZE; ++i) {
342 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS));
342 >            long t0 = System.nanoTime();
343 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
344 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
345 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
346          }
347          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
348 +        checkEmpty(q);
349      }
350  
351      /**
# Line 317 | Line 357 | public class LinkedTransferQueueTest ext
357          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
358              void realRun() throws InterruptedException {
359                  for (int i = 0; i < SIZE; ++i) {
360 +                    long t0 = System.nanoTime();
361                      threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
362                                                         MILLISECONDS));
363 +                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
364 +                    assertTrue(millisElapsed < SMALL_DELAY_MS);
365                  }
366                  q.poll(LONG_DELAY_MS, MILLISECONDS);
367              }});
368          Thread.sleep(SMALL_DELAY_MS);
369          t.interrupt();
370          t.join();
371 <        assertEquals(q.size(), 0);
329 <        assertNull(q.poll());
371 >        checkEmpty(q);
372      }
373  
374      /**
# Line 350 | Line 392 | public class LinkedTransferQueueTest ext
392      /**
393       * peek returns next element, or null if empty
394       */
395 <    public void testPeek() {
395 >    public void testPeek() throws InterruptedException {
396          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
397          for (int i = 0; i < SIZE; ++i) {
398              assertEquals(i, (int) q.peek());
# Line 359 | Line 401 | public class LinkedTransferQueueTest ext
401                         i != (int) q.peek());
402          }
403          assertNull(q.peek());
404 +        checkEmpty(q);
405      }
406  
407      /**
408       * element returns next element, or throws NoSuchElementException if empty
409       */
410 <    public void testElement() {
410 >    public void testElement() throws InterruptedException {
411          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
412          for (int i = 0; i < SIZE; ++i) {
413              assertEquals(i, (int) q.element());
# Line 375 | Line 418 | public class LinkedTransferQueueTest ext
418              shouldThrow();
419          } catch (NoSuchElementException success) {
420          }
421 +        checkEmpty(q);
422      }
423  
424      /**
425       * remove removes next element, or throws NoSuchElementException if empty
426       */
427 <    public void testRemove() {
427 >    public void testRemove() throws InterruptedException {
428          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
429          for (int i = 0; i < SIZE; ++i) {
430              assertEquals(i, (int) q.remove());
# Line 390 | Line 434 | public class LinkedTransferQueueTest ext
434              shouldThrow();
435          } catch (NoSuchElementException success) {
436          }
437 +        checkEmpty(q);
438      }
439  
440      /**
441       * remove(x) removes x and returns true if present
442       */
443 <    public void testRemoveElement() {
443 >    public void testRemoveElement() throws InterruptedException {
444          LinkedTransferQueue q = populatedQueue(SIZE);
445          for (int i = 1; i < SIZE; i += 2) {
446              assertTrue(q.remove(i));
# Line 404 | Line 449 | public class LinkedTransferQueueTest ext
449              assertTrue(q.remove(i));
450              assertFalse(q.remove(i + 1));
451          }
452 <        assertTrue(q.isEmpty());
452 >        checkEmpty(q);
453      }
454  
455      /**
# Line 417 | Line 462 | public class LinkedTransferQueueTest ext
462          assertTrue(q.remove(one));
463          assertTrue(q.remove(two));
464          assertTrue(q.add(three));
465 <        assertTrue(q.take() != null);
465 >        assertTrue(q.take() == three);
466      }
467  
468      /**
# Line 435 | Line 480 | public class LinkedTransferQueueTest ext
480      /**
481       * clear removes all elements
482       */
483 <    public void testClear() {
483 >    public void testClear() throws InterruptedException {
484          LinkedTransferQueue q = populatedQueue(SIZE);
485          q.clear();
486 <        assertTrue(q.isEmpty());
442 <        assertEquals(0, q.size());
486 >        checkEmpty(q);
487          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
488          q.add(one);
489          assertFalse(q.isEmpty());
490 +        assertEquals(1, q.size());
491          assertTrue(q.contains(one));
492          q.clear();
493 <        assertTrue(q.isEmpty());
493 >        checkEmpty(q);
494      }
495  
496      /**
# Line 499 | Line 544 | public class LinkedTransferQueueTest ext
544      }
545  
546      /**
547 <     * toArray contains all elements
547 >     * toArray() contains all elements
548       */
549      public void testToArray() throws InterruptedException {
550          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 534 | Line 579 | public class LinkedTransferQueueTest ext
579      }
580  
581      /**
582 <     * toArray with incompatible array type throws CCE
582 >     * toArray(incompatible array type) throws CCE
583       */
584      public void testToArray1_BadArg() {
585          try {
# Line 551 | Line 596 | public class LinkedTransferQueueTest ext
596      public void testIterator() throws InterruptedException {
597          LinkedTransferQueue q = populatedQueue(SIZE);
598          Iterator it = q.iterator();
599 +        int i = 0;
600          while (it.hasNext()) {
601 <            assertEquals(it.next(), q.take());
601 >            assertEquals(it.next(), i++);
602          }
603 +        assertEquals(i, SIZE);
604      }
605  
606      /**
607 <     * iterator.remove removes current element
607 >     * iterator.remove() removes current element
608       */
609      public void testIteratorRemove() {
610          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 644 | Line 691 | public class LinkedTransferQueueTest ext
691      }
692  
693      /**
694 <     * poll retrieves elements across Executor threads
694 >     * timed poll retrieves elements across Executor threads
695       */
696      public void testPollInExecutor() {
697          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 742 | Line 789 | public class LinkedTransferQueueTest ext
789      }
790  
791      /**
792 <     * drainTo empties full queue, unblocking a waiting put.
792 >     * drainTo(c) empties full queue, unblocking a waiting put.
793       */
794      public void testDrainToWithActivePut() throws InterruptedException {
795          final LinkedTransferQueue q = populatedQueue(SIZE);
# Line 766 | Line 813 | public class LinkedTransferQueueTest ext
813      public void testDrainToNullN() {
814          LinkedTransferQueue q = populatedQueue(SIZE);
815          try {
816 <            q.drainTo(null, 0);
816 >            q.drainTo(null, SIZE);
817              shouldThrow();
818          } catch (NullPointerException success) {
819          }
# Line 778 | Line 825 | public class LinkedTransferQueueTest ext
825      public void testDrainToSelfN() {
826          LinkedTransferQueue q = populatedQueue(SIZE);
827          try {
828 <            q.drainTo(q, 0);
828 >            q.drainTo(q, SIZE);
829              shouldThrow();
830          } catch (IllegalArgumentException success) {
831          }
# Line 807 | Line 854 | public class LinkedTransferQueueTest ext
854      }
855  
856      /**
857 <     * poll and take decrement the waiting consumer count
857 >     * timed poll() or take() increments the waiting consumer count;
858 >     * offer(e) decrements the waiting consumer count
859       */
860      public void testWaitingConsumer() throws InterruptedException {
861          final LinkedTransferQueue q = new LinkedTransferQueue();
862 <        final ConsumerObserver waiting = new ConsumerObserver();
862 >        assertEquals(q.getWaitingConsumerCount(), 0);
863 >        assertFalse(q.hasWaitingConsumer());
864  
865          Thread t = newStartedThread(new CheckedRunnable() {
866              void realRun() throws InterruptedException {
867                  Thread.sleep(SMALL_DELAY_MS);
868                  threadAssertTrue(q.hasWaitingConsumer());
869 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
869 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
870                  threadAssertTrue(q.offer(new Object()));
871 +                threadAssertFalse(q.hasWaitingConsumer());
872 +                threadAssertEquals(q.getWaitingConsumerCount(), 0);
873              }});
874  
875          assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
876 <        assertTrue(q.getWaitingConsumerCount()
877 <                   < waiting.getWaitingConsumers());
876 >        assertEquals(q.getWaitingConsumerCount(), 0);
877 >        assertFalse(q.hasWaitingConsumer());
878          t.join();
879      }
880  
# Line 926 | Line 977 | public class LinkedTransferQueueTest ext
977          Thread t = newStartedThread(new CheckedRunnable() {
978              void realRun() throws InterruptedException {
979                  q.transfer(SIZE);
980 <                threadAssertTrue(q.isEmpty());
980 >                checkEmpty(q);
981              }});
982  
983          Thread.sleep(SHORT_DELAY_MS);
984          assertEquals(SIZE, (int) q.take());
985 <        assertTrue(q.isEmpty());
985 >        checkEmpty(q);
986          t.join();
987      }
988  
# Line 951 | Line 1002 | public class LinkedTransferQueueTest ext
1002       * tryTransfer returns false and does not enqueue if there are no
1003       * consumers waiting to poll or take.
1004       */
1005 <    public void testTryTransfer2() {
1005 >    public void testTryTransfer2() throws InterruptedException {
1006          final LinkedTransferQueue q = new LinkedTransferQueue();
1007          assertFalse(q.tryTransfer(new Object()));
1008          assertFalse(q.hasWaitingConsumer());
1009 <        assertTrue(q.isEmpty());
959 <        assertEquals(0, q.size());
1009 >        checkEmpty(q);
1010      }
1011  
1012      /**
# Line 978 | Line 1028 | public class LinkedTransferQueueTest ext
1028              }});
1029  
1030          assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1031 <        assertTrue(q.isEmpty());
1031 >        checkEmpty(q);
1032          t.join();
1033      }
1034  
# Line 1001 | Line 1051 | public class LinkedTransferQueueTest ext
1051              }});
1052  
1053          assertTrue(q.take() == hotPotato);
1054 <        assertTrue(q.isEmpty());
1054 >        checkEmpty(q);
1055          t.join();
1056      }
1057  
# Line 1036 | Line 1086 | public class LinkedTransferQueueTest ext
1086              }});
1087  
1088          Thread.sleep(SMALL_DELAY_MS);
1089 <        assertTrue(q.isEmpty());
1089 >        checkEmpty(q);
1090          t.join();
1091      }
1092  
# Line 1059 | Line 1109 | public class LinkedTransferQueueTest ext
1109          assertEquals(2, q.size());
1110          assertEquals(four, q.poll());
1111          assertEquals(five, q.poll());
1112 <        assertTrue(q.isEmpty());
1112 >        checkEmpty(q);
1113          t.join();
1114      }
1115  
# Line 1074 | Line 1124 | public class LinkedTransferQueueTest ext
1124          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1125          assertEquals(1, q.size());
1126          assertEquals(four, q.poll());
1077        threadAssertTrue(q.isEmpty());
1127          assertNull(q.poll());
1128 +        checkEmpty(q);
1129      }
1130  
1131      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1089 | Line 1139 | public class LinkedTransferQueueTest ext
1139          assertFalse(q.isEmpty());
1140          return q;
1141      }
1092
1093    private static class ConsumerObserver {
1094
1095        private int waitingConsumers;
1096
1097        private ConsumerObserver() {
1098        }
1099
1100        private void setWaitingConsumer(int i) {
1101            this.waitingConsumers = i;
1102        }
1103
1104        private int getWaitingConsumers() {
1105            return waitingConsumers;
1106        }
1107    }
1142   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines