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.67 by jsr166, Fri Jan 2 03:52:53 2015 UTC vs.
Revision 1.75 by jsr166, Sun Oct 4 18:49:02 2015 UTC

# Line 35 | 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() {
# Line 43 | Line 43 | public class DelayQueueTest extends JSR1
43                              new Generic().testSuite());
44      }
45  
46    private static final int NOCAP = Integer.MAX_VALUE;
47
46      /**
47       * A delayed implementation for testing.
48       * Most tests use Pseudodelays, where delays are all elapsed
# Line 126 | Line 124 | public class DelayQueueTest extends JSR1
124      private DelayQueue<PDelay> populatedQueue(int n) {
125          DelayQueue<PDelay> q = new DelayQueue<PDelay>();
126          assertTrue(q.isEmpty());
127 <        for (int i = n-1; i >= 0; i -= 2)
127 >        for (int i = n - 1; i >= 0; i -= 2)
128              assertTrue(q.offer(new PDelay(i)));
129          for (int i = (n & 1); i < n; i += 2)
130              assertTrue(q.offer(new PDelay(i)));
131          assertFalse(q.isEmpty());
132 <        assertEquals(NOCAP, q.remainingCapacity());
132 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
133          assertEquals(n, q.size());
134          return q;
135      }
# Line 140 | Line 138 | public class DelayQueueTest extends JSR1
138       * A new queue has unbounded capacity
139       */
140      public void testConstructor1() {
141 <        assertEquals(NOCAP, new DelayQueue().remainingCapacity());
141 >        assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity());
142      }
143  
144      /**
# Line 148 | Line 146 | public class DelayQueueTest extends JSR1
146       */
147      public void testConstructor3() {
148          try {
149 <            DelayQueue q = new DelayQueue(null);
149 >            new DelayQueue(null);
150              shouldThrow();
151          } catch (NullPointerException success) {}
152      }
# Line 158 | Line 156 | public class DelayQueueTest extends JSR1
156       */
157      public void testConstructor4() {
158          try {
159 <            PDelay[] ints = new PDelay[SIZE];
162 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
159 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
160              shouldThrow();
161          } catch (NullPointerException success) {}
162      }
# Line 168 | Line 165 | public class DelayQueueTest extends JSR1
165       * Initializing from Collection with some null elements throws NPE
166       */
167      public void testConstructor5() {
168 +        PDelay[] a = new PDelay[SIZE];
169 +        for (int i = 0; i < SIZE - 1; ++i)
170 +            a[i] = new PDelay(i);
171          try {
172 <            PDelay[] ints = new PDelay[SIZE];
173 <            for (int i = 0; i < SIZE-1; ++i)
174 <                ints[i] = new PDelay(i);
175 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
172 >            new DelayQueue(Arrays.asList(a));
173              shouldThrow();
174          } catch (NullPointerException success) {}
175      }
# Line 195 | Line 192 | public class DelayQueueTest extends JSR1
192      public void testEmpty() {
193          DelayQueue q = new DelayQueue();
194          assertTrue(q.isEmpty());
195 <        assertEquals(NOCAP, q.remainingCapacity());
195 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
196          q.add(new PDelay(1));
197          assertFalse(q.isEmpty());
198          q.add(new PDelay(2));
# Line 205 | Line 202 | public class DelayQueueTest extends JSR1
202      }
203  
204      /**
205 <     * remainingCapacity does not change when elements added or removed,
209 <     * but size does
205 >     * remainingCapacity() always returns Integer.MAX_VALUE
206       */
207      public void testRemainingCapacity() {
208 <        DelayQueue q = populatedQueue(SIZE);
208 >        BlockingQueue q = populatedQueue(SIZE);
209          for (int i = 0; i < SIZE; ++i) {
210 <            assertEquals(NOCAP, q.remainingCapacity());
211 <            assertEquals(SIZE-i, q.size());
212 <            q.remove();
210 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
211 >            assertEquals(SIZE - i, q.size());
212 >            assertTrue(q.remove() instanceof PDelay);
213          }
214          for (int i = 0; i < SIZE; ++i) {
215 <            assertEquals(NOCAP, q.remainingCapacity());
215 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
216              assertEquals(i, q.size());
217 <            q.add(new PDelay(i));
217 >            assertTrue(q.add(new PDelay(i)));
218          }
219      }
220  
# Line 246 | Line 242 | public class DelayQueueTest extends JSR1
242       * addAll(this) throws IAE
243       */
244      public void testAddAllSelf() {
245 +        DelayQueue q = populatedQueue(SIZE);
246          try {
250            DelayQueue q = populatedQueue(SIZE);
247              q.addAll(q);
248              shouldThrow();
249          } catch (IllegalArgumentException success) {}
# Line 258 | Line 254 | public class DelayQueueTest extends JSR1
254       * possibly adding some elements
255       */
256      public void testAddAll3() {
257 +        DelayQueue q = new DelayQueue();
258 +        PDelay[] a = new PDelay[SIZE];
259 +        for (int i = 0; i < SIZE - 1; ++i)
260 +            a[i] = new PDelay(i);
261          try {
262 <            DelayQueue q = new DelayQueue();
263 <            PDelay[] ints = new PDelay[SIZE];
264 <            for (int i = 0; i < SIZE-1; ++i)
265 <                ints[i] = new PDelay(i);
266 <            q.addAll(Arrays.asList(ints));
262 >            q.addAll(Arrays.asList(a));
263              shouldThrow();
264          } catch (NullPointerException success) {}
265      }
# Line 274 | Line 270 | public class DelayQueueTest extends JSR1
270      public void testAddAll5() {
271          PDelay[] empty = new PDelay[0];
272          PDelay[] ints = new PDelay[SIZE];
273 <        for (int i = SIZE-1; i >= 0; --i)
273 >        for (int i = SIZE - 1; i >= 0; --i)
274              ints[i] = new PDelay(i);
275          DelayQueue q = new DelayQueue();
276          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 509 | Line 505 | public class DelayQueueTest extends JSR1
505          q.clear();
506          assertTrue(q.isEmpty());
507          assertEquals(0, q.size());
508 <        assertEquals(NOCAP, q.remainingCapacity());
508 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
509          PDelay x = new PDelay(1);
510          q.add(x);
511          assertFalse(q.isEmpty());
# Line 546 | Line 542 | public class DelayQueueTest extends JSR1
542                  assertTrue(changed);
543  
544              assertTrue(q.containsAll(p));
545 <            assertEquals(SIZE-i, q.size());
545 >            assertEquals(SIZE - i, q.size());
546              p.remove();
547          }
548      }
# Line 559 | Line 555 | public class DelayQueueTest extends JSR1
555              DelayQueue q = populatedQueue(SIZE);
556              DelayQueue p = populatedQueue(i);
557              assertTrue(q.removeAll(p));
558 <            assertEquals(SIZE-i, q.size());
558 >            assertEquals(SIZE - i, q.size());
559              for (int j = 0; j < i; ++j) {
560                  PDelay x = (PDelay)(p.remove());
561                  assertFalse(q.contains(x));
# Line 614 | Line 610 | public class DelayQueueTest extends JSR1
610              ++i;
611          }
612          assertEquals(i, SIZE);
613 +        assertIteratorExhausted(it);
614 +    }
615 +
616 +    /**
617 +     * iterator of empty collection has no elements
618 +     */
619 +    public void testEmptyIterator() {
620 +        assertIteratorExhausted(new DelayQueue().iterator());
621      }
622  
623      /**
# Line 649 | Line 653 | public class DelayQueueTest extends JSR1
653      public void testPollInExecutor() {
654          final DelayQueue q = new DelayQueue();
655          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
656 <        ExecutorService executor = Executors.newFixedThreadPool(2);
657 <        executor.execute(new CheckedRunnable() {
658 <            public void realRun() throws InterruptedException {
659 <                assertNull(q.poll());
660 <                threadsStarted.await();
661 <                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
662 <                checkEmpty(q);
663 <            }});
664 <
665 <        executor.execute(new CheckedRunnable() {
666 <            public void realRun() throws InterruptedException {
667 <                threadsStarted.await();
668 <                q.put(new PDelay(1));
669 <            }});
670 <
671 <        joinPool(executor);
656 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
657 >        try (PoolCleaner cleaner = cleaner(executor)) {
658 >            executor.execute(new CheckedRunnable() {
659 >                public void realRun() throws InterruptedException {
660 >                    assertNull(q.poll());
661 >                    threadsStarted.await();
662 >                    assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
663 >                    checkEmpty(q);
664 >                }});
665 >
666 >            executor.execute(new CheckedRunnable() {
667 >                public void realRun() throws InterruptedException {
668 >                    threadsStarted.await();
669 >                    q.put(new PDelay(1));
670 >                }});
671 >        }
672      }
673  
674      /**
# Line 749 | Line 753 | public class DelayQueueTest extends JSR1
753          final DelayQueue q = populatedQueue(SIZE);
754          Thread t = new Thread(new CheckedRunnable() {
755              public void realRun() {
756 <                q.put(new PDelay(SIZE+1));
756 >                q.put(new PDelay(SIZE + 1));
757              }});
758  
759          t.start();
# Line 769 | Line 773 | public class DelayQueueTest extends JSR1
773              ArrayList l = new ArrayList();
774              q.drainTo(l, i);
775              int k = (i < SIZE) ? i : SIZE;
776 <            assertEquals(SIZE-k, q.size());
776 >            assertEquals(SIZE - k, q.size());
777              assertEquals(k, l.size());
778          }
779      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines