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

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.61 by jsr166, Fri Apr 5 19:27:23 2013 UTC vs.
Revision 1.79 by jsr166, Mon Oct 17 01:54:51 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.Arrays;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13 + import java.util.Collection;
14   import java.util.Iterator;
15   import java.util.NoSuchElementException;
16   import java.util.concurrent.BlockingQueue;
# Line 18 | Line 20 | import java.util.concurrent.DelayQueue;
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.TimeUnit;
23 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 >
24 > import junit.framework.Test;
25  
26   public class DelayQueueTest extends JSR166TestCase {
27  
# Line 32 | Line 35 | public class DelayQueueTest extends JSR1
35      }
36  
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run(suite());
38 >        main(suite(), args);
39      }
40  
41      public static Test suite() {
42 +        class Implementation implements CollectionImplementation {
43 +            public Class<?> klazz() { return DelayQueue.class; }
44 +            public Collection emptyCollection() { return new DelayQueue(); }
45 +            public Object makeElement(int i) { return new PDelay(i); }
46 +            public boolean isConcurrent() { return true; }
47 +            public boolean permitsNulls() { return false; }
48 +        }
49          return newTestSuite(DelayQueueTest.class,
50 <                            new Generic().testSuite());
50 >                            new Generic().testSuite(),
51 >                            CollectionTest.testSuite(new Implementation()));
52      }
53  
43    private static final int NOCAP = Integer.MAX_VALUE;
44
54      /**
55       * A delayed implementation for testing.
56       * Most tests use Pseudodelays, where delays are all elapsed
# Line 93 | Line 102 | public class DelayQueueTest extends JSR1
102          }
103  
104          public boolean equals(Object other) {
105 <            return equals((NanoDelay)other);
106 <        }
98 <        public boolean equals(NanoDelay other) {
99 <            return other.trigger == trigger;
105 >            return (other instanceof NanoDelay) &&
106 >                this.trigger == ((NanoDelay)other).trigger;
107          }
108  
109          // suppress [overrides] javac warning
# Line 118 | Line 125 | public class DelayQueueTest extends JSR1
125  
126      /**
127       * Returns a new queue of given size containing consecutive
128 <     * PDelays 0 ... n.
128 >     * PDelays 0 ... n - 1.
129       */
130      private DelayQueue<PDelay> populatedQueue(int n) {
131          DelayQueue<PDelay> q = new DelayQueue<PDelay>();
132          assertTrue(q.isEmpty());
133 <        for (int i = n-1; i >= 0; i-=2)
133 >        for (int i = n - 1; i >= 0; i -= 2)
134              assertTrue(q.offer(new PDelay(i)));
135 <        for (int i = (n & 1); i < n; i+=2)
135 >        for (int i = (n & 1); i < n; i += 2)
136              assertTrue(q.offer(new PDelay(i)));
137          assertFalse(q.isEmpty());
138 <        assertEquals(NOCAP, q.remainingCapacity());
138 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
139          assertEquals(n, q.size());
140 +        assertEquals(new PDelay(0), q.peek());
141          return q;
142      }
143  
# Line 137 | Line 145 | public class DelayQueueTest extends JSR1
145       * A new queue has unbounded capacity
146       */
147      public void testConstructor1() {
148 <        assertEquals(NOCAP, new DelayQueue().remainingCapacity());
148 >        assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity());
149      }
150  
151      /**
# Line 145 | Line 153 | public class DelayQueueTest extends JSR1
153       */
154      public void testConstructor3() {
155          try {
156 <            DelayQueue q = new DelayQueue(null);
156 >            new DelayQueue(null);
157              shouldThrow();
158          } catch (NullPointerException success) {}
159      }
# Line 155 | Line 163 | public class DelayQueueTest extends JSR1
163       */
164      public void testConstructor4() {
165          try {
166 <            PDelay[] ints = new PDelay[SIZE];
159 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
166 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
167              shouldThrow();
168          } catch (NullPointerException success) {}
169      }
# Line 165 | Line 172 | public class DelayQueueTest extends JSR1
172       * Initializing from Collection with some null elements throws NPE
173       */
174      public void testConstructor5() {
175 +        PDelay[] a = new PDelay[SIZE];
176 +        for (int i = 0; i < SIZE - 1; ++i)
177 +            a[i] = new PDelay(i);
178          try {
179 <            PDelay[] ints = new PDelay[SIZE];
170 <            for (int i = 0; i < SIZE-1; ++i)
171 <                ints[i] = new PDelay(i);
172 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
179 >            new DelayQueue(Arrays.asList(a));
180              shouldThrow();
181          } catch (NullPointerException success) {}
182      }
# Line 192 | Line 199 | public class DelayQueueTest extends JSR1
199      public void testEmpty() {
200          DelayQueue q = new DelayQueue();
201          assertTrue(q.isEmpty());
202 <        assertEquals(NOCAP, q.remainingCapacity());
202 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
203          q.add(new PDelay(1));
204          assertFalse(q.isEmpty());
205          q.add(new PDelay(2));
# Line 202 | Line 209 | public class DelayQueueTest extends JSR1
209      }
210  
211      /**
212 <     * remainingCapacity does not change when elements added or removed,
206 <     * but size does
212 >     * remainingCapacity() always returns Integer.MAX_VALUE
213       */
214      public void testRemainingCapacity() {
215 <        DelayQueue q = populatedQueue(SIZE);
215 >        BlockingQueue q = populatedQueue(SIZE);
216          for (int i = 0; i < SIZE; ++i) {
217 <            assertEquals(NOCAP, q.remainingCapacity());
218 <            assertEquals(SIZE-i, q.size());
219 <            q.remove();
217 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
218 >            assertEquals(SIZE - i, q.size());
219 >            assertTrue(q.remove() instanceof PDelay);
220          }
221          for (int i = 0; i < SIZE; ++i) {
222 <            assertEquals(NOCAP, q.remainingCapacity());
222 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
223              assertEquals(i, q.size());
224 <            q.add(new PDelay(i));
224 >            assertTrue(q.add(new PDelay(i)));
225          }
226      }
227  
# Line 243 | Line 249 | public class DelayQueueTest extends JSR1
249       * addAll(this) throws IAE
250       */
251      public void testAddAllSelf() {
252 +        DelayQueue q = populatedQueue(SIZE);
253          try {
247            DelayQueue q = populatedQueue(SIZE);
254              q.addAll(q);
255              shouldThrow();
256          } catch (IllegalArgumentException success) {}
# Line 255 | Line 261 | public class DelayQueueTest extends JSR1
261       * possibly adding some elements
262       */
263      public void testAddAll3() {
264 +        DelayQueue q = new DelayQueue();
265 +        PDelay[] a = new PDelay[SIZE];
266 +        for (int i = 0; i < SIZE - 1; ++i)
267 +            a[i] = new PDelay(i);
268          try {
269 <            DelayQueue q = new DelayQueue();
260 <            PDelay[] ints = new PDelay[SIZE];
261 <            for (int i = 0; i < SIZE-1; ++i)
262 <                ints[i] = new PDelay(i);
263 <            q.addAll(Arrays.asList(ints));
269 >            q.addAll(Arrays.asList(a));
270              shouldThrow();
271          } catch (NullPointerException success) {}
272      }
# Line 271 | Line 277 | public class DelayQueueTest extends JSR1
277      public void testAddAll5() {
278          PDelay[] empty = new PDelay[0];
279          PDelay[] ints = new PDelay[SIZE];
280 <        for (int i = SIZE-1; i >= 0; --i)
280 >        for (int i = SIZE - 1; i >= 0; --i)
281              ints[i] = new PDelay(i);
282          DelayQueue q = new DelayQueue();
283          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 286 | Line 292 | public class DelayQueueTest extends JSR1
292      public void testPut() {
293          DelayQueue q = new DelayQueue();
294          for (int i = 0; i < SIZE; ++i) {
295 <            PDelay I = new PDelay(i);
296 <            q.put(I);
297 <            assertTrue(q.contains(I));
295 >            PDelay x = new PDelay(i);
296 >            q.put(x);
297 >            assertTrue(q.contains(x));
298          }
299          assertEquals(SIZE, q.size());
300      }
# Line 332 | Line 338 | public class DelayQueueTest extends JSR1
338      public void testTake() throws InterruptedException {
339          DelayQueue q = populatedQueue(SIZE);
340          for (int i = 0; i < SIZE; ++i) {
341 <            assertEquals(new PDelay(i), ((PDelay)q.take()));
341 >            assertEquals(new PDelay(i), q.take());
342          }
343      }
344  
# Line 375 | Line 381 | public class DelayQueueTest extends JSR1
381      public void testPoll() {
382          DelayQueue q = populatedQueue(SIZE);
383          for (int i = 0; i < SIZE; ++i) {
384 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
384 >            assertEquals(new PDelay(i), q.poll());
385          }
386          assertNull(q.poll());
387      }
# Line 386 | Line 392 | public class DelayQueueTest extends JSR1
392      public void testTimedPoll0() throws InterruptedException {
393          DelayQueue q = populatedQueue(SIZE);
394          for (int i = 0; i < SIZE; ++i) {
395 <            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
395 >            assertEquals(new PDelay(i), q.poll(0, MILLISECONDS));
396          }
397          assertNull(q.poll(0, MILLISECONDS));
398      }
# Line 398 | Line 404 | public class DelayQueueTest extends JSR1
404          DelayQueue q = populatedQueue(SIZE);
405          for (int i = 0; i < SIZE; ++i) {
406              long startTime = System.nanoTime();
407 <            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
407 >            assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS));
408              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
409          }
410          long startTime = System.nanoTime();
# Line 413 | Line 419 | public class DelayQueueTest extends JSR1
419       */
420      public void testInterruptedTimedPoll() throws InterruptedException {
421          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
422 +        final DelayQueue q = populatedQueue(SIZE);
423          Thread t = newStartedThread(new CheckedRunnable() {
424              public void realRun() throws InterruptedException {
425 <                DelayQueue q = populatedQueue(SIZE);
425 >                long startTime = System.nanoTime();
426                  for (int i = 0; i < SIZE; ++i) {
427 <                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
427 >                    assertEquals(new PDelay(i),
428 >                                 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
429                  }
430  
431                  Thread.currentThread().interrupt();
# Line 433 | Line 441 | public class DelayQueueTest extends JSR1
441                      shouldThrow();
442                  } catch (InterruptedException success) {}
443                  assertFalse(Thread.interrupted());
444 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
445              }});
446  
447          await(pleaseInterrupt);
448          assertThreadStaysAlive(t);
449          t.interrupt();
450          awaitTermination(t);
451 +        checkEmpty(q);
452      }
453  
454      /**
# Line 447 | Line 457 | public class DelayQueueTest extends JSR1
457      public void testPeek() {
458          DelayQueue q = populatedQueue(SIZE);
459          for (int i = 0; i < SIZE; ++i) {
460 <            assertEquals(new PDelay(i), ((PDelay)q.peek()));
461 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
460 >            assertEquals(new PDelay(i), q.peek());
461 >            assertEquals(new PDelay(i), q.poll());
462              if (q.isEmpty())
463                  assertNull(q.peek());
464              else
# Line 463 | Line 473 | public class DelayQueueTest extends JSR1
473      public void testElement() {
474          DelayQueue q = populatedQueue(SIZE);
475          for (int i = 0; i < SIZE; ++i) {
476 <            assertEquals(new PDelay(i), ((PDelay)q.element()));
476 >            assertEquals(new PDelay(i), q.element());
477              q.poll();
478          }
479          try {
# Line 478 | Line 488 | public class DelayQueueTest extends JSR1
488      public void testRemove() {
489          DelayQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(new PDelay(i), ((PDelay)q.remove()));
491 >            assertEquals(new PDelay(i), q.remove());
492          }
493          try {
494              q.remove();
# Line 506 | Line 516 | public class DelayQueueTest extends JSR1
516          q.clear();
517          assertTrue(q.isEmpty());
518          assertEquals(0, q.size());
519 <        assertEquals(NOCAP, q.remainingCapacity());
519 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
520          PDelay x = new PDelay(1);
521          q.add(x);
522          assertFalse(q.isEmpty());
# Line 543 | Line 553 | public class DelayQueueTest extends JSR1
553                  assertTrue(changed);
554  
555              assertTrue(q.containsAll(p));
556 <            assertEquals(SIZE-i, q.size());
556 >            assertEquals(SIZE - i, q.size());
557              p.remove();
558          }
559      }
# Line 556 | Line 566 | public class DelayQueueTest extends JSR1
566              DelayQueue q = populatedQueue(SIZE);
567              DelayQueue p = populatedQueue(i);
568              assertTrue(q.removeAll(p));
569 <            assertEquals(SIZE-i, q.size());
569 >            assertEquals(SIZE - i, q.size());
570              for (int j = 0; j < i; ++j) {
571 <                PDelay I = (PDelay)(p.remove());
572 <                assertFalse(q.contains(I));
571 >                PDelay x = (PDelay)(p.remove());
572 >                assertFalse(q.contains(x));
573              }
574          }
575      }
# Line 611 | Line 621 | public class DelayQueueTest extends JSR1
621              ++i;
622          }
623          assertEquals(i, SIZE);
624 +        assertIteratorExhausted(it);
625 +    }
626 +
627 +    /**
628 +     * iterator of empty collection has no elements
629 +     */
630 +    public void testEmptyIterator() {
631 +        assertIteratorExhausted(new DelayQueue().iterator());
632      }
633  
634      /**
# Line 646 | Line 664 | public class DelayQueueTest extends JSR1
664      public void testPollInExecutor() {
665          final DelayQueue q = new DelayQueue();
666          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
667 <        ExecutorService executor = Executors.newFixedThreadPool(2);
668 <        executor.execute(new CheckedRunnable() {
669 <            public void realRun() throws InterruptedException {
670 <                assertNull(q.poll());
671 <                threadsStarted.await();
672 <                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
673 <                checkEmpty(q);
674 <            }});
675 <
676 <        executor.execute(new CheckedRunnable() {
677 <            public void realRun() throws InterruptedException {
678 <                threadsStarted.await();
679 <                q.put(new PDelay(1));
680 <            }});
681 <
682 <        joinPool(executor);
667 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
668 >        try (PoolCleaner cleaner = cleaner(executor)) {
669 >            executor.execute(new CheckedRunnable() {
670 >                public void realRun() throws InterruptedException {
671 >                    assertNull(q.poll());
672 >                    threadsStarted.await();
673 >                    assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
674 >                    checkEmpty(q);
675 >                }});
676 >
677 >            executor.execute(new CheckedRunnable() {
678 >                public void realRun() throws InterruptedException {
679 >                    threadsStarted.await();
680 >                    q.put(new PDelay(1));
681 >                }});
682 >        }
683      }
684  
685      /**
# Line 746 | Line 764 | public class DelayQueueTest extends JSR1
764          final DelayQueue q = populatedQueue(SIZE);
765          Thread t = new Thread(new CheckedRunnable() {
766              public void realRun() {
767 <                q.put(new PDelay(SIZE+1));
767 >                q.put(new PDelay(SIZE + 1));
768              }});
769  
770          t.start();
# Line 766 | Line 784 | public class DelayQueueTest extends JSR1
784              ArrayList l = new ArrayList();
785              q.drainTo(l, i);
786              int k = (i < SIZE) ? i : SIZE;
787 <            assertEquals(SIZE-k, q.size());
787 >            assertEquals(SIZE - k, q.size());
788              assertEquals(k, l.size());
789          }
790      }
791  
792 +    /**
793 +     * remove(null), contains(null) always return false
794 +     */
795 +    public void testNeverContainsNull() {
796 +        Collection<?> q = populatedQueue(SIZE);
797 +        assertFalse(q.contains(null));
798 +        assertFalse(q.remove(null));
799 +    }
800   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines