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.17 by jsr166, Sat Nov 21 19:45:16 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 351 | Line 384 | public class LinkedTransferQueueTest ext
384      /**
385       * peek returns next element, or null if empty
386       */
387 <    public void testPeek() {
387 >    public void testPeek() throws InterruptedException {
388          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
389          for (int i = 0; i < SIZE; ++i) {
390              assertEquals(i, (int) q.peek());
# Line 360 | Line 393 | public class LinkedTransferQueueTest ext
393                         i != (int) q.peek());
394          }
395          assertNull(q.peek());
396 +        checkEmpty(q);
397      }
398  
399      /**
400       * element returns next element, or throws NoSuchElementException if empty
401       */
402 <    public void testElement() {
402 >    public void testElement() throws InterruptedException {
403          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
404          for (int i = 0; i < SIZE; ++i) {
405              assertEquals(i, (int) q.element());
# Line 374 | Line 408 | public class LinkedTransferQueueTest ext
408          try {
409              q.element();
410              shouldThrow();
411 <        } catch (NoSuchElementException success) {
412 <        }
411 >        } catch (NoSuchElementException success) {}
412 >        checkEmpty(q);
413      }
414  
415      /**
416       * remove removes next element, or throws NoSuchElementException if empty
417       */
418 <    public void testRemove() {
418 >    public void testRemove() throws InterruptedException {
419          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421              assertEquals(i, (int) q.remove());
# Line 389 | Line 423 | public class LinkedTransferQueueTest ext
423          try {
424              q.remove();
425              shouldThrow();
426 <        } catch (NoSuchElementException success) {
427 <        }
426 >        } catch (NoSuchElementException success) {}
427 >        checkEmpty(q);
428      }
429  
430      /**
431       * remove(x) removes x and returns true if present
432       */
433 <    public void testRemoveElement() {
433 >    public void testRemoveElement() throws InterruptedException {
434          LinkedTransferQueue q = populatedQueue(SIZE);
435          for (int i = 1; i < SIZE; i += 2) {
436              assertTrue(q.remove(i));
# Line 405 | Line 439 | public class LinkedTransferQueueTest ext
439              assertTrue(q.remove(i));
440              assertFalse(q.remove(i + 1));
441          }
442 <        assertTrue(q.isEmpty());
442 >        checkEmpty(q);
443      }
444  
445      /**
# Line 418 | Line 452 | public class LinkedTransferQueueTest ext
452          assertTrue(q.remove(one));
453          assertTrue(q.remove(two));
454          assertTrue(q.add(three));
455 <        assertTrue(q.take() != null);
455 >        assertTrue(q.take() == three);
456      }
457  
458      /**
# Line 436 | Line 470 | public class LinkedTransferQueueTest ext
470      /**
471       * clear removes all elements
472       */
473 <    public void testClear() {
473 >    public void testClear() throws InterruptedException {
474          LinkedTransferQueue q = populatedQueue(SIZE);
475          q.clear();
476 <        assertTrue(q.isEmpty());
443 <        assertEquals(0, q.size());
476 >        checkEmpty(q);
477          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
478          q.add(one);
479          assertFalse(q.isEmpty());
480 +        assertEquals(1, q.size());
481          assertTrue(q.contains(one));
482          q.clear();
483 <        assertTrue(q.isEmpty());
483 >        checkEmpty(q);
484      }
485  
486      /**
# Line 500 | Line 534 | public class LinkedTransferQueueTest ext
534      }
535  
536      /**
537 <     * toArray contains all elements
537 >     * toArray() contains all elements
538       */
539      public void testToArray() throws InterruptedException {
540          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 530 | Line 564 | public class LinkedTransferQueueTest ext
564              LinkedTransferQueue q = populatedQueue(SIZE);
565              Object o[] = q.toArray(null);
566              shouldThrow();
567 <        } catch (NullPointerException success) {
534 <        }
567 >        } catch (NullPointerException success) {}
568      }
569  
570      /**
571 <     * toArray with incompatible array type throws CCE
571 >     * toArray(incompatible array type) throws CCE
572       */
573      public void testToArray1_BadArg() {
574          try {
575              LinkedTransferQueue q = populatedQueue(SIZE);
576              Object o[] = q.toArray(new String[10]);
577              shouldThrow();
578 <        } catch (ArrayStoreException success) {
546 <        }
578 >        } catch (ArrayStoreException success) {}
579      }
580  
581      /**
# Line 560 | Line 592 | public class LinkedTransferQueueTest ext
592      }
593  
594      /**
595 <     * iterator.remove removes current element
595 >     * iterator.remove() removes current element
596       */
597      public void testIteratorRemove() {
598          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 647 | Line 679 | public class LinkedTransferQueueTest ext
679      }
680  
681      /**
682 <     * poll retrieves elements across Executor threads
682 >     * timed poll retrieves elements across Executor threads
683       */
684      public void testPollInExecutor() {
685          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 702 | Line 734 | public class LinkedTransferQueueTest ext
734          try {
735              q.drainTo(null);
736              shouldThrow();
737 <        } catch (NullPointerException success) {
706 <        }
737 >        } catch (NullPointerException success) {}
738      }
739  
740      /**
# Line 714 | Line 745 | public class LinkedTransferQueueTest ext
745          try {
746              q.drainTo(q);
747              shouldThrow();
748 <        } catch (IllegalArgumentException success) {
718 <        }
748 >        } catch (IllegalArgumentException success) {}
749      }
750  
751      /**
# Line 745 | Line 775 | public class LinkedTransferQueueTest ext
775      }
776  
777      /**
778 <     * drainTo empties full queue, unblocking a waiting put.
778 >     * drainTo(c) empties full queue, unblocking a waiting put.
779       */
780      public void testDrainToWithActivePut() throws InterruptedException {
781          final LinkedTransferQueue q = populatedQueue(SIZE);
# Line 769 | Line 799 | public class LinkedTransferQueueTest ext
799      public void testDrainToNullN() {
800          LinkedTransferQueue q = populatedQueue(SIZE);
801          try {
802 <            q.drainTo(null, 0);
802 >            q.drainTo(null, SIZE);
803              shouldThrow();
804 <        } catch (NullPointerException success) {
775 <        }
804 >        } catch (NullPointerException success) {}
805      }
806  
807      /**
# Line 781 | Line 810 | public class LinkedTransferQueueTest ext
810      public void testDrainToSelfN() {
811          LinkedTransferQueue q = populatedQueue(SIZE);
812          try {
813 <            q.drainTo(q, 0);
813 >            q.drainTo(q, SIZE);
814              shouldThrow();
815 <        } catch (IllegalArgumentException success) {
787 <        }
815 >        } catch (IllegalArgumentException success) {}
816      }
817  
818      /**
# Line 810 | Line 838 | public class LinkedTransferQueueTest ext
838      }
839  
840      /**
841 <     * poll and take decrement the waiting consumer count
841 >     * timed poll() or take() increments the waiting consumer count;
842 >     * offer(e) decrements the waiting consumer count
843       */
844      public void testWaitingConsumer() throws InterruptedException {
845          final LinkedTransferQueue q = new LinkedTransferQueue();
846 <        final ConsumerObserver waiting = new ConsumerObserver();
846 >        assertEquals(q.getWaitingConsumerCount(), 0);
847 >        assertFalse(q.hasWaitingConsumer());
848  
849          Thread t = newStartedThread(new CheckedRunnable() {
850              void realRun() throws InterruptedException {
851                  Thread.sleep(SMALL_DELAY_MS);
852                  threadAssertTrue(q.hasWaitingConsumer());
853 <                waiting.setWaitingConsumer(q.getWaitingConsumerCount());
853 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
854                  threadAssertTrue(q.offer(new Object()));
855 +                threadAssertFalse(q.hasWaitingConsumer());
856 +                threadAssertEquals(q.getWaitingConsumerCount(), 0);
857              }});
858  
859          assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
860 <        assertTrue(q.getWaitingConsumerCount()
861 <                   < waiting.getWaitingConsumers());
860 >        assertEquals(q.getWaitingConsumerCount(), 0);
861 >        assertFalse(q.hasWaitingConsumer());
862          t.join();
863      }
864  
# Line 838 | Line 870 | public class LinkedTransferQueueTest ext
870              LinkedTransferQueue q = new LinkedTransferQueue();
871              q.transfer(null);
872              shouldThrow();
873 <        } catch (NullPointerException ex) {
842 <        }
873 >        } catch (NullPointerException success) {}
874      }
875  
876      /**
# Line 929 | Line 960 | public class LinkedTransferQueueTest ext
960          Thread t = newStartedThread(new CheckedRunnable() {
961              void realRun() throws InterruptedException {
962                  q.transfer(SIZE);
963 <                threadAssertTrue(q.isEmpty());
963 >                checkEmpty(q);
964              }});
965  
966          Thread.sleep(SHORT_DELAY_MS);
967          assertEquals(SIZE, (int) q.take());
968 <        assertTrue(q.isEmpty());
968 >        checkEmpty(q);
969          t.join();
970      }
971  
# Line 946 | Line 977 | public class LinkedTransferQueueTest ext
977              final LinkedTransferQueue q = new LinkedTransferQueue();
978              q.tryTransfer(null);
979              shouldThrow();
980 <        } catch (NullPointerException ex) {
950 <        }
980 >        } catch (NullPointerException success) {}
981      }
982  
983      /**
984       * tryTransfer returns false and does not enqueue if there are no
985       * consumers waiting to poll or take.
986       */
987 <    public void testTryTransfer2() {
987 >    public void testTryTransfer2() throws InterruptedException {
988          final LinkedTransferQueue q = new LinkedTransferQueue();
989          assertFalse(q.tryTransfer(new Object()));
990          assertFalse(q.hasWaitingConsumer());
991 <        assertTrue(q.isEmpty());
962 <        assertEquals(0, q.size());
991 >        checkEmpty(q);
992      }
993  
994      /**
# Line 981 | Line 1010 | public class LinkedTransferQueueTest ext
1010              }});
1011  
1012          assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1013 <        assertTrue(q.isEmpty());
1013 >        checkEmpty(q);
1014          t.join();
1015      }
1016  
# Line 1004 | Line 1033 | public class LinkedTransferQueueTest ext
1033              }});
1034  
1035          assertTrue(q.take() == hotPotato);
1036 <        assertTrue(q.isEmpty());
1036 >        checkEmpty(q);
1037          t.join();
1038      }
1039  
# Line 1039 | Line 1068 | public class LinkedTransferQueueTest ext
1068              }});
1069  
1070          Thread.sleep(SMALL_DELAY_MS);
1071 <        assertTrue(q.isEmpty());
1071 >        checkEmpty(q);
1072          t.join();
1073      }
1074  
# Line 1062 | Line 1091 | public class LinkedTransferQueueTest ext
1091          assertEquals(2, q.size());
1092          assertEquals(four, q.poll());
1093          assertEquals(five, q.poll());
1094 <        assertTrue(q.isEmpty());
1094 >        checkEmpty(q);
1095          t.join();
1096      }
1097  
# Line 1077 | Line 1106 | public class LinkedTransferQueueTest ext
1106          assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1107          assertEquals(1, q.size());
1108          assertEquals(four, q.poll());
1080        threadAssertTrue(q.isEmpty());
1109          assertNull(q.poll());
1110 +        checkEmpty(q);
1111      }
1112  
1113      private LinkedTransferQueue<Integer> populatedQueue(int n) {
# Line 1092 | Line 1121 | public class LinkedTransferQueueTest ext
1121          assertFalse(q.isEmpty());
1122          return q;
1123      }
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    }
1124   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines