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.70 by jsr166, Sat Feb 28 19:59:23 2015 UTC vs.
Revision 1.78 by jsr166, Sun Oct 16 20:44:18 2016 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 94 | Line 94 | public class DelayQueueTest extends JSR1
94          }
95  
96          public boolean equals(Object other) {
97 <            return equals((NanoDelay)other);
98 <        }
99 <        public boolean equals(NanoDelay other) {
100 <            return other.trigger == trigger;
97 >            return (other instanceof NanoDelay) &&
98 >                this.trigger == ((NanoDelay)other).trigger;
99          }
100  
101          // suppress [overrides] javac warning
# Line 119 | Line 117 | public class DelayQueueTest extends JSR1
117  
118      /**
119       * Returns a new queue of given size containing consecutive
120 <     * PDelays 0 ... n.
120 >     * PDelays 0 ... n - 1.
121       */
122      private DelayQueue<PDelay> populatedQueue(int n) {
123          DelayQueue<PDelay> q = new DelayQueue<PDelay>();
124          assertTrue(q.isEmpty());
125 <        for (int i = n-1; i >= 0; i -= 2)
125 >        for (int i = n - 1; i >= 0; i -= 2)
126              assertTrue(q.offer(new PDelay(i)));
127          for (int i = (n & 1); i < n; i += 2)
128              assertTrue(q.offer(new PDelay(i)));
129          assertFalse(q.isEmpty());
130          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
131          assertEquals(n, q.size());
132 +        assertEquals(new PDelay(0), q.peek());
133          return q;
134      }
135  
# Line 156 | Line 155 | public class DelayQueueTest extends JSR1
155       */
156      public void testConstructor4() {
157          try {
158 <            PDelay[] ints = new PDelay[SIZE];
160 <            new DelayQueue(Arrays.asList(ints));
158 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
159              shouldThrow();
160          } catch (NullPointerException success) {}
161      }
# Line 166 | Line 164 | public class DelayQueueTest extends JSR1
164       * Initializing from Collection with some null elements throws NPE
165       */
166      public void testConstructor5() {
167 +        PDelay[] a = new PDelay[SIZE];
168 +        for (int i = 0; i < SIZE - 1; ++i)
169 +            a[i] = new PDelay(i);
170          try {
171 <            PDelay[] ints = new PDelay[SIZE];
171 <            for (int i = 0; i < SIZE-1; ++i)
172 <                ints[i] = new PDelay(i);
173 <            new DelayQueue(Arrays.asList(ints));
171 >            new DelayQueue(Arrays.asList(a));
172              shouldThrow();
173          } catch (NullPointerException success) {}
174      }
# Line 209 | Line 207 | public class DelayQueueTest extends JSR1
207          BlockingQueue q = populatedQueue(SIZE);
208          for (int i = 0; i < SIZE; ++i) {
209              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
210 <            assertEquals(SIZE-i, q.size());
210 >            assertEquals(SIZE - i, q.size());
211              assertTrue(q.remove() instanceof PDelay);
212          }
213          for (int i = 0; i < SIZE; ++i) {
# Line 243 | Line 241 | public class DelayQueueTest extends JSR1
241       * addAll(this) throws IAE
242       */
243      public void testAddAllSelf() {
244 +        DelayQueue q = populatedQueue(SIZE);
245          try {
247            DelayQueue q = populatedQueue(SIZE);
246              q.addAll(q);
247              shouldThrow();
248          } catch (IllegalArgumentException success) {}
# Line 255 | Line 253 | public class DelayQueueTest extends JSR1
253       * possibly adding some elements
254       */
255      public void testAddAll3() {
256 +        DelayQueue q = new DelayQueue();
257 +        PDelay[] a = new PDelay[SIZE];
258 +        for (int i = 0; i < SIZE - 1; ++i)
259 +            a[i] = new PDelay(i);
260          try {
261 <            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));
261 >            q.addAll(Arrays.asList(a));
262              shouldThrow();
263          } catch (NullPointerException success) {}
264      }
# Line 271 | Line 269 | public class DelayQueueTest extends JSR1
269      public void testAddAll5() {
270          PDelay[] empty = new PDelay[0];
271          PDelay[] ints = new PDelay[SIZE];
272 <        for (int i = SIZE-1; i >= 0; --i)
272 >        for (int i = SIZE - 1; i >= 0; --i)
273              ints[i] = new PDelay(i);
274          DelayQueue q = new DelayQueue();
275          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 413 | Line 411 | public class DelayQueueTest extends JSR1
411       */
412      public void testInterruptedTimedPoll() throws InterruptedException {
413          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
414 +        final DelayQueue q = populatedQueue(SIZE);
415          Thread t = newStartedThread(new CheckedRunnable() {
416              public void realRun() throws InterruptedException {
417 <                DelayQueue q = populatedQueue(SIZE);
417 >                long startTime = System.nanoTime();
418                  for (int i = 0; i < SIZE; ++i) {
419 <                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
419 >                    assertEquals(new PDelay(i),
420 >                                 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
421                  }
422  
423                  Thread.currentThread().interrupt();
# Line 433 | Line 433 | public class DelayQueueTest extends JSR1
433                      shouldThrow();
434                  } catch (InterruptedException success) {}
435                  assertFalse(Thread.interrupted());
436 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
437              }});
438  
439          await(pleaseInterrupt);
440          assertThreadStaysAlive(t);
441          t.interrupt();
442          awaitTermination(t);
443 +        checkEmpty(q);
444      }
445  
446      /**
# Line 543 | Line 545 | public class DelayQueueTest extends JSR1
545                  assertTrue(changed);
546  
547              assertTrue(q.containsAll(p));
548 <            assertEquals(SIZE-i, q.size());
548 >            assertEquals(SIZE - i, q.size());
549              p.remove();
550          }
551      }
# Line 556 | Line 558 | public class DelayQueueTest extends JSR1
558              DelayQueue q = populatedQueue(SIZE);
559              DelayQueue p = populatedQueue(i);
560              assertTrue(q.removeAll(p));
561 <            assertEquals(SIZE-i, q.size());
561 >            assertEquals(SIZE - i, q.size());
562              for (int j = 0; j < i; ++j) {
563                  PDelay x = (PDelay)(p.remove());
564                  assertFalse(q.contains(x));
# Line 654 | Line 656 | public class DelayQueueTest extends JSR1
656      public void testPollInExecutor() {
657          final DelayQueue q = new DelayQueue();
658          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
659 <        ExecutorService executor = Executors.newFixedThreadPool(2);
660 <        executor.execute(new CheckedRunnable() {
661 <            public void realRun() throws InterruptedException {
662 <                assertNull(q.poll());
663 <                threadsStarted.await();
664 <                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
665 <                checkEmpty(q);
666 <            }});
667 <
668 <        executor.execute(new CheckedRunnable() {
669 <            public void realRun() throws InterruptedException {
670 <                threadsStarted.await();
671 <                q.put(new PDelay(1));
672 <            }});
673 <
674 <        joinPool(executor);
659 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
660 >        try (PoolCleaner cleaner = cleaner(executor)) {
661 >            executor.execute(new CheckedRunnable() {
662 >                public void realRun() throws InterruptedException {
663 >                    assertNull(q.poll());
664 >                    threadsStarted.await();
665 >                    assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
666 >                    checkEmpty(q);
667 >                }});
668 >
669 >            executor.execute(new CheckedRunnable() {
670 >                public void realRun() throws InterruptedException {
671 >                    threadsStarted.await();
672 >                    q.put(new PDelay(1));
673 >                }});
674 >        }
675      }
676  
677      /**
# Line 754 | Line 756 | public class DelayQueueTest extends JSR1
756          final DelayQueue q = populatedQueue(SIZE);
757          Thread t = new Thread(new CheckedRunnable() {
758              public void realRun() {
759 <                q.put(new PDelay(SIZE+1));
759 >                q.put(new PDelay(SIZE + 1));
760              }});
761  
762          t.start();
# Line 774 | Line 776 | public class DelayQueueTest extends JSR1
776              ArrayList l = new ArrayList();
777              q.drainTo(l, i);
778              int k = (i < SIZE) ? i : SIZE;
779 <            assertEquals(SIZE-k, q.size());
779 >            assertEquals(SIZE - k, q.size());
780              assertEquals(k, l.size());
781          }
782      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines