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.10 by jsr166, Thu Aug 6 03:55:39 2009 UTC vs.
Revision 1.18 by jsr166, Sat Nov 21 21:00:34 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;
# Line 32 | 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 +        try {
49 +            q.iterator().next();
50 +            shouldThrow();
51 +        } catch (NoSuchElementException success) {}
52 +        try {
53 +            q.remove();
54 +            shouldThrow();
55 +        } catch (NoSuchElementException success) {}
56 +    }
57 +
58      /**
59       * Constructor builds new queue with size being zero and empty
60       * being true
# Line 49 | Line 72 | public class LinkedTransferQueueTest ext
72          try {
73              new LinkedTransferQueue(null);
74              shouldThrow();
75 <        } catch (NullPointerException success) {
53 <        }
75 >        } catch (NullPointerException success) {}
76      }
77  
78      /**
# Line 62 | Line 84 | public class LinkedTransferQueueTest ext
84              Integer[] ints = new Integer[SIZE];
85              new LinkedTransferQueue(Arrays.asList(ints));
86              shouldThrow();
87 <        } catch (NullPointerException success) {
66 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 78 | Line 99 | public class LinkedTransferQueueTest ext
99              }
100              new LinkedTransferQueue(Arrays.asList(ints));
101              shouldThrow();
102 <        } catch (NullPointerException success) {
82 <        }
102 >        } catch (NullPointerException success) {}
103      }
104  
105      /**
106       * Queue contains all elements of the collection it is initialized by
107       */
108      public void testConstructor5() {
109 <        try {
110 <            Integer[] ints = new Integer[SIZE];
111 <            for (int i = 0; i < SIZE; ++i) {
112 <                ints[i] = i;
113 <            }
114 <            LinkedTransferQueue q
115 <                = new LinkedTransferQueue(Arrays.asList(ints));
116 <            for (int i = 0; i < SIZE; ++i) {
117 <                assertEquals(ints[i], q.poll());
118 <            }
119 <        } finally {
109 >        Integer[] ints = new Integer[SIZE];
110 >        for (int i = 0; i < SIZE; ++i) {
111 >            ints[i] = i;
112 >        }
113 >        List intList = Arrays.asList(ints);
114 >        LinkedTransferQueue q
115 >            = new LinkedTransferQueue(intList);
116 >        assertEquals(q.size(), intList.size());
117 >        assertEquals(q.toString(), intList.toString());
118 >        assertTrue(Arrays.equals(q.toArray(),
119 >                                     intList.toArray()));
120 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
121 >                                 intList.toArray(new Object[0])));
122 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
123 >                                 intList.toArray(new Object[SIZE])));
124 >        for (int i = 0; i < SIZE; ++i) {
125 >            assertEquals(ints[i], q.poll());
126          }
127      }
128  
129      /**
130 <     * Remaining capacity never decrease nor increase on add or remove
130 >     * remainingCapacity() always returns Integer.MAX_VALUE
131       */
132      public void testRemainingCapacity() {
133          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 125 | Line 151 | public class LinkedTransferQueueTest ext
151              LinkedTransferQueue q = new LinkedTransferQueue();
152              q.offer(null);
153              shouldThrow();
154 <        } catch (NullPointerException success) {
129 <        }
154 >        } catch (NullPointerException success) {}
155      }
156  
157      /**
# Line 137 | Line 162 | public class LinkedTransferQueueTest ext
162              LinkedTransferQueue q = new LinkedTransferQueue();
163              q.add(null);
164              shouldThrow();
165 <        } catch (NullPointerException success) {
141 <        }
165 >        } catch (NullPointerException success) {}
166      }
167  
168      /**
# Line 149 | Line 173 | public class LinkedTransferQueueTest ext
173              LinkedTransferQueue q = new LinkedTransferQueue();
174              q.addAll(null);
175              shouldThrow();
176 <        } catch (NullPointerException success) {
153 <        }
176 >        } catch (NullPointerException success) {}
177      }
178  
179      /**
# Line 161 | Line 184 | public class LinkedTransferQueueTest ext
184              LinkedTransferQueue q = populatedQueue(SIZE);
185              q.addAll(q);
186              shouldThrow();
187 <        } catch (IllegalArgumentException success) {
165 <        }
187 >        } catch (IllegalArgumentException success) {}
188      }
189  
190      /**
# Line 174 | Line 196 | public class LinkedTransferQueueTest ext
196              Integer[] ints = new Integer[SIZE];
197              q.addAll(Arrays.asList(ints));
198              shouldThrow();
199 <        } catch (NullPointerException success) {
178 <        }
199 >        } catch (NullPointerException success) {}
200      }
201  
202      /**
# Line 191 | Line 212 | public class LinkedTransferQueueTest ext
212              }
213              q.addAll(Arrays.asList(ints));
214              shouldThrow();
215 <        } catch (NullPointerException success) {
195 <        }
215 >        } catch (NullPointerException success) {}
216      }
217  
218      /**
# Line 229 | Line 249 | public class LinkedTransferQueueTest ext
249      public void testPut() {
250          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
251          for (int i = 0; i < SIZE; ++i) {
252 +            assertEquals(q.size(), i);
253              q.put(i);
254              assertTrue(q.contains(i));
255          }
235        assertEquals(q.size(), SIZE);
256      }
257  
258      /**
# Line 263 | Line 283 | public class LinkedTransferQueueTest ext
283       * Take removes existing elements until empty, then blocks interruptibly
284       */
285      public void testBlockingTake() throws InterruptedException {
286 +        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
287          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288              void realRun() throws InterruptedException {
268                LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
289                  for (int i = 0; i < SIZE; ++i) {
290                      threadAssertEquals(i, (int) q.take());
291                  }
292                  q.take();
293              }});
294 <        Thread.sleep(SHORT_DELAY_MS);
294 >        Thread.sleep(SMALL_DELAY_MS);
295          t.interrupt();
296          t.join();
297 +        checkEmpty(q);
298      }
299  
300      /**
301       * poll succeeds unless empty
302       */
303 <    public void testPoll() {
303 >    public void testPoll() throws InterruptedException {
304          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306              assertEquals(i, (int) q.poll());
307          }
308          assertNull(q.poll());
309 +        checkEmpty(q);
310      }
311  
312      /**
# Line 296 | Line 318 | public class LinkedTransferQueueTest ext
318              assertEquals(i, (int) q.poll(0, MILLISECONDS));
319          }
320          assertNull(q.poll(0, MILLISECONDS));
321 +        checkEmpty(q);
322      }
323  
324      /**
# Line 304 | Line 327 | public class LinkedTransferQueueTest ext
327      public void testTimedPoll() throws InterruptedException {
328          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
329          for (int i = 0; i < SIZE; ++i) {
330 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS));
330 >            long t0 = System.nanoTime();
331 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
332 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
333 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
334          }
335          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
336 +        checkEmpty(q);
337      }
338  
339      /**
# Line 315 | Line 342 | public class LinkedTransferQueueTest ext
342       */
343      public void testInterruptedTimedPoll() throws InterruptedException {
344          final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
345 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
345 >        Thread t = newStartedThread(new CheckedRunnable() {
346              void realRun() throws InterruptedException {
347                  for (int i = 0; i < SIZE; ++i) {
348 +                    long t0 = System.nanoTime();
349                      threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
350                                                         MILLISECONDS));
351 +                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
352 +                    assertTrue(millisElapsed < SMALL_DELAY_MS);
353                  }
354 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
354 >                try {
355 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
356 >                    shouldThrow();
357 >                } catch (InterruptedException success) {}
358              }});
359 +
360          Thread.sleep(SMALL_DELAY_MS);
361          t.interrupt();
362          t.join();
363 <        assertEquals(q.size(), 0);
330 <        assertNull(q.poll());
363 >        checkEmpty(q);
364      }
365  
366      /**
# Line 336 | Line 369 | public class LinkedTransferQueueTest ext
369       */
370      public void testTimedPollWithOffer() throws InterruptedException {
371          final LinkedTransferQueue q = new LinkedTransferQueue();
372 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
372 >        Thread t = new Thread(new CheckedRunnable() {
373              void realRun() throws InterruptedException {
374 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
376 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
374 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
376 >                try {
377 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
378 >                    shouldThrow();
379 >                } catch (InterruptedException success) {}
380              }});
381 +
382          Thread.sleep(SMALL_DELAY_MS);
383          assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
384          t.interrupt();
# Line 351 | Line 388 | public class LinkedTransferQueueTest ext
388      /**
389       * peek returns next element, or null if empty
390       */
391 <    public void testPeek() {
391 >    public void testPeek() throws InterruptedException {
392          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
393          for (int i = 0; i < SIZE; ++i) {
394              assertEquals(i, (int) q.peek());
# Line 360 | Line 397 | public class LinkedTransferQueueTest ext
397                         i != (int) q.peek());
398          }
399          assertNull(q.peek());
400 +        checkEmpty(q);
401      }
402  
403      /**
404       * element returns next element, or throws NoSuchElementException if empty
405       */
406 <    public void testElement() {
406 >    public void testElement() throws InterruptedException {
407          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
408          for (int i = 0; i < SIZE; ++i) {
409              assertEquals(i, (int) q.element());
# Line 374 | Line 412 | public class LinkedTransferQueueTest ext
412          try {
413              q.element();
414              shouldThrow();
415 <        } catch (NoSuchElementException success) {
416 <        }
415 >        } catch (NoSuchElementException success) {}
416 >        checkEmpty(q);
417      }
418  
419      /**
420       * remove removes next element, or throws NoSuchElementException if empty
421       */
422 <    public void testRemove() {
422 >    public void testRemove() throws InterruptedException {
423          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
424          for (int i = 0; i < SIZE; ++i) {
425              assertEquals(i, (int) q.remove());
# Line 389 | Line 427 | public class LinkedTransferQueueTest ext
427          try {
428              q.remove();
429              shouldThrow();
430 <        } catch (NoSuchElementException success) {
431 <        }
430 >        } catch (NoSuchElementException success) {}
431 >        checkEmpty(q);
432      }
433  
434      /**
435       * remove(x) removes x and returns true if present
436       */
437 <    public void testRemoveElement() {
437 >    public void testRemoveElement() throws InterruptedException {
438          LinkedTransferQueue q = populatedQueue(SIZE);
439          for (int i = 1; i < SIZE; i += 2) {
440              assertTrue(q.remove(i));
# Line 405 | Line 443 | public class LinkedTransferQueueTest ext
443              assertTrue(q.remove(i));
444              assertFalse(q.remove(i + 1));
445          }
446 <        assertTrue(q.isEmpty());
446 >        checkEmpty(q);
447      }
448  
449      /**
# Line 418 | Line 456 | public class LinkedTransferQueueTest ext
456          assertTrue(q.remove(one));
457          assertTrue(q.remove(two));
458          assertTrue(q.add(three));
459 <        assertTrue(q.take() != null);
459 >        assertTrue(q.take() == three);
460      }
461  
462      /**
# Line 436 | Line 474 | public class LinkedTransferQueueTest ext
474      /**
475       * clear removes all elements
476       */
477 <    public void testClear() {
477 >    public void testClear() throws InterruptedException {
478          LinkedTransferQueue q = populatedQueue(SIZE);
479          q.clear();
480 <        assertTrue(q.isEmpty());
443 <        assertEquals(0, q.size());
480 >        checkEmpty(q);
481          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
482          q.add(one);
483          assertFalse(q.isEmpty());
484 +        assertEquals(1, q.size());
485          assertTrue(q.contains(one));
486          q.clear();
487 <        assertTrue(q.isEmpty());
487 >        checkEmpty(q);
488      }
489  
490      /**
# Line 500 | Line 538 | public class LinkedTransferQueueTest ext
538      }
539  
540      /**
541 <     * toArray contains all elements
541 >     * toArray() contains all elements
542       */
543      public void testToArray() throws InterruptedException {
544          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 530 | Line 568 | public class LinkedTransferQueueTest ext
568              LinkedTransferQueue q = populatedQueue(SIZE);
569              Object o[] = q.toArray(null);
570              shouldThrow();
571 <        } catch (NullPointerException success) {
534 <        }
571 >        } catch (NullPointerException success) {}
572      }
573  
574      /**
575 <     * toArray with incompatible array type throws CCE
575 >     * toArray(incompatible array type) throws CCE
576       */
577      public void testToArray1_BadArg() {
578          try {
579              LinkedTransferQueue q = populatedQueue(SIZE);
580              Object o[] = q.toArray(new String[10]);
581              shouldThrow();
582 <        } catch (ArrayStoreException success) {
546 <        }
582 >        } catch (ArrayStoreException success) {}
583      }
584  
585      /**
# Line 560 | Line 596 | public class LinkedTransferQueueTest ext
596      }
597  
598      /**
599 <     * iterator.remove removes current element
599 >     * iterator.remove() removes current element
600       */
601      public void testIteratorRemove() {
602          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 647 | Line 683 | public class LinkedTransferQueueTest ext
683      }
684  
685      /**
686 <     * poll retrieves elements across Executor threads
686 >     * timed poll retrieves elements across Executor threads
687       */
688      public void testPollInExecutor() {
689          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 702 | Line 738 | public class LinkedTransferQueueTest ext
738          try {
739              q.drainTo(null);
740              shouldThrow();
741 <        } catch (NullPointerException success) {
706 <        }
741 >        } catch (NullPointerException success) {}
742      }
743  
744      /**
# Line 714 | Line 749 | public class LinkedTransferQueueTest ext
749          try {
750              q.drainTo(q);
751              shouldThrow();
752 <        } catch (IllegalArgumentException success) {
718 <        }
752 >        } catch (IllegalArgumentException success) {}
753      }
754  
755      /**
# Line 745 | Line 779 | public class LinkedTransferQueueTest ext
779      }
780  
781      /**
782 <     * drainTo empties full queue, unblocking a waiting put.
782 >     * drainTo(c) empties full queue, unblocking a waiting put.
783       */
784      public void testDrainToWithActivePut() throws InterruptedException {
785          final LinkedTransferQueue q = populatedQueue(SIZE);
# Line 769 | Line 803 | public class LinkedTransferQueueTest ext
803      public void testDrainToNullN() {
804          LinkedTransferQueue q = populatedQueue(SIZE);
805          try {
806 <            q.drainTo(null, 0);
806 >            q.drainTo(null, SIZE);
807              shouldThrow();
808 <        } catch (NullPointerException success) {
775 <        }
808 >        } catch (NullPointerException success) {}
809      }
810  
811      /**
# Line 781 | Line 814 | public class LinkedTransferQueueTest ext
814      public void testDrainToSelfN() {
815          LinkedTransferQueue q = populatedQueue(SIZE);
816          try {
817 <            q.drainTo(q, 0);
817 >            q.drainTo(q, SIZE);
818              shouldThrow();
819 <        } catch (IllegalArgumentException success) {
787 <        }
819 >        } catch (IllegalArgumentException success) {}
820      }
821  
822      /**
# Line 810 | Line 842 | public class LinkedTransferQueueTest ext
842      }
843  
844      /**
845 <     * poll and take decrement the waiting consumer count
845 >     * timed poll() or take() increments the waiting consumer count;
846 >     * offer(e) decrements the waiting consumer count
847       */
848      public void testWaitingConsumer() throws InterruptedException {
849          final LinkedTransferQueue q = new LinkedTransferQueue();
850 <        final ConsumerObserver waiting = new ConsumerObserver();
850 >        assertEquals(q.getWaitingConsumerCount(), 0);
851 >        assertFalse(q.hasWaitingConsumer());
852  
853          Thread t = newStartedThread(new CheckedRunnable() {
854              void realRun() throws InterruptedException {
855                  Thread.sleep(SMALL_DELAY_MS);
856                  threadAssertTrue(q.hasWaitingConsumer());
857 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
857 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
858                  threadAssertTrue(q.offer(new Object()));
859 +                threadAssertFalse(q.hasWaitingConsumer());
860 +                threadAssertEquals(q.getWaitingConsumerCount(), 0);
861              }});
862  
863          assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
864 <        assertTrue(q.getWaitingConsumerCount()
865 <                   < waiting.getWaitingConsumers());
864 >        assertEquals(q.getWaitingConsumerCount(), 0);
865 >        assertFalse(q.hasWaitingConsumer());
866          t.join();
867      }
868  
# Line 838 | Line 874 | public class LinkedTransferQueueTest ext
874              LinkedTransferQueue q = new LinkedTransferQueue();
875              q.transfer(null);
876              shouldThrow();
877 <        } catch (NullPointerException ex) {
842 <        }
877 >        } catch (NullPointerException success) {}
878      }
879  
880      /**
# Line 929 | Line 964 | public class LinkedTransferQueueTest ext
964          Thread t = newStartedThread(new CheckedRunnable() {
965              void realRun() throws InterruptedException {
966                  q.transfer(SIZE);
967 <                threadAssertTrue(q.isEmpty());
967 >                checkEmpty(q);
968              }});
969  
970          Thread.sleep(SHORT_DELAY_MS);
971          assertEquals(SIZE, (int) q.take());
972 <        assertTrue(q.isEmpty());
972 >        checkEmpty(q);
973          t.join();
974      }
975  
# Line 946 | Line 981 | public class LinkedTransferQueueTest ext
981              final LinkedTransferQueue q = new LinkedTransferQueue();
982              q.tryTransfer(null);
983              shouldThrow();
984 <        } catch (NullPointerException ex) {
950 <        }
984 >        } catch (NullPointerException success) {}
985      }
986  
987      /**
988       * tryTransfer returns false and does not enqueue if there are no
989       * consumers waiting to poll or take.
990       */
991 <    public void testTryTransfer2() {
991 >    public void testTryTransfer2() throws InterruptedException {
992          final LinkedTransferQueue q = new LinkedTransferQueue();
993          assertFalse(q.tryTransfer(new Object()));
994          assertFalse(q.hasWaitingConsumer());
995 <        assertTrue(q.isEmpty());
962 <        assertEquals(0, q.size());
995 >        checkEmpty(q);
996      }
997  
998      /**
# Line 981 | Line 1014 | public class LinkedTransferQueueTest ext
1014              }});
1015  
1016          assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1017 <        assertTrue(q.isEmpty());
1017 >        checkEmpty(q);
1018          t.join();
1019      }
1020  
# Line 1004 | Line 1037 | public class LinkedTransferQueueTest ext
1037              }});
1038  
1039          assertTrue(q.take() == hotPotato);
1040 <        assertTrue(q.isEmpty());
1040 >        checkEmpty(q);
1041          t.join();
1042      }
1043  
# Line 1039 | Line 1072 | public class LinkedTransferQueueTest ext
1072              }});
1073  
1074          Thread.sleep(SMALL_DELAY_MS);
1075 <        assertTrue(q.isEmpty());
1075 >        checkEmpty(q);
1076          t.join();
1077      }
1078  
# Line 1062 | Line 1095 | public class LinkedTransferQueueTest ext
1095          assertEquals(2, q.size());
1096          assertEquals(four, q.poll());
1097          assertEquals(five, q.poll());
1098 <        assertTrue(q.isEmpty());
1098 >        checkEmpty(q);
1099          t.join();
1100      }
1101  
# Line 1077 | Line 1110 | public class LinkedTransferQueueTest ext
1110          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1111          assertEquals(1, q.size());
1112          assertEquals(four, q.poll());
1080        threadAssertTrue(q.isEmpty());
1113          assertNull(q.poll());
1114 +        checkEmpty(q);
1115      }
1116  
1117      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1092 | Line 1125 | public class LinkedTransferQueueTest ext
1125          assertFalse(q.isEmpty());
1126          return q;
1127      }
1095
1096    private static class ConsumerObserver {
1097
1098        private int waitingConsumers;
1099
1100        private ConsumerObserver() {
1101        }
1102
1103        private void setWaitingConsumer(int i) {
1104            this.waitingConsumers = i;
1105        }
1106
1107        private int getWaitingConsumers() {
1108            return waitingConsumers;
1109        }
1110    }
1128   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines