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.77 by jsr166, Thu Sep 15 17:07:16 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 124 | Line 122 | public class DelayQueueTest extends JSR1
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)));
# Line 156 | Line 154 | public class DelayQueueTest extends JSR1
154       */
155      public void testConstructor4() {
156          try {
157 <            PDelay[] ints = new PDelay[SIZE];
160 <            new DelayQueue(Arrays.asList(ints));
157 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
158              shouldThrow();
159          } catch (NullPointerException success) {}
160      }
# Line 166 | Line 163 | public class DelayQueueTest extends JSR1
163       * Initializing from Collection with some null elements throws NPE
164       */
165      public void testConstructor5() {
166 +        PDelay[] a = new PDelay[SIZE];
167 +        for (int i = 0; i < SIZE - 1; ++i)
168 +            a[i] = new PDelay(i);
169          try {
170 <            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));
170 >            new DelayQueue(Arrays.asList(a));
171              shouldThrow();
172          } catch (NullPointerException success) {}
173      }
# Line 209 | Line 206 | public class DelayQueueTest extends JSR1
206          BlockingQueue q = populatedQueue(SIZE);
207          for (int i = 0; i < SIZE; ++i) {
208              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
209 <            assertEquals(SIZE-i, q.size());
209 >            assertEquals(SIZE - i, q.size());
210              assertTrue(q.remove() instanceof PDelay);
211          }
212          for (int i = 0; i < SIZE; ++i) {
# Line 243 | Line 240 | public class DelayQueueTest extends JSR1
240       * addAll(this) throws IAE
241       */
242      public void testAddAllSelf() {
243 +        DelayQueue q = populatedQueue(SIZE);
244          try {
247            DelayQueue q = populatedQueue(SIZE);
245              q.addAll(q);
246              shouldThrow();
247          } catch (IllegalArgumentException success) {}
# Line 255 | Line 252 | public class DelayQueueTest extends JSR1
252       * possibly adding some elements
253       */
254      public void testAddAll3() {
255 +        DelayQueue q = new DelayQueue();
256 +        PDelay[] a = new PDelay[SIZE];
257 +        for (int i = 0; i < SIZE - 1; ++i)
258 +            a[i] = new PDelay(i);
259          try {
260 <            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));
260 >            q.addAll(Arrays.asList(a));
261              shouldThrow();
262          } catch (NullPointerException success) {}
263      }
# Line 271 | Line 268 | public class DelayQueueTest extends JSR1
268      public void testAddAll5() {
269          PDelay[] empty = new PDelay[0];
270          PDelay[] ints = new PDelay[SIZE];
271 <        for (int i = SIZE-1; i >= 0; --i)
271 >        for (int i = SIZE - 1; i >= 0; --i)
272              ints[i] = new PDelay(i);
273          DelayQueue q = new DelayQueue();
274          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 413 | Line 410 | public class DelayQueueTest extends JSR1
410       */
411      public void testInterruptedTimedPoll() throws InterruptedException {
412          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
413 +        final DelayQueue q = populatedQueue(SIZE);
414          Thread t = newStartedThread(new CheckedRunnable() {
415              public void realRun() throws InterruptedException {
416 <                DelayQueue q = populatedQueue(SIZE);
416 >                long startTime = System.nanoTime();
417                  for (int i = 0; i < SIZE; ++i) {
418 <                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
418 >                    assertEquals(new PDelay(i),
419 >                                 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
420                  }
421  
422                  Thread.currentThread().interrupt();
# Line 433 | Line 432 | public class DelayQueueTest extends JSR1
432                      shouldThrow();
433                  } catch (InterruptedException success) {}
434                  assertFalse(Thread.interrupted());
435 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
436              }});
437  
438          await(pleaseInterrupt);
439          assertThreadStaysAlive(t);
440          t.interrupt();
441          awaitTermination(t);
442 +        checkEmpty(q);
443      }
444  
445      /**
# Line 543 | Line 544 | public class DelayQueueTest extends JSR1
544                  assertTrue(changed);
545  
546              assertTrue(q.containsAll(p));
547 <            assertEquals(SIZE-i, q.size());
547 >            assertEquals(SIZE - i, q.size());
548              p.remove();
549          }
550      }
# Line 556 | Line 557 | public class DelayQueueTest extends JSR1
557              DelayQueue q = populatedQueue(SIZE);
558              DelayQueue p = populatedQueue(i);
559              assertTrue(q.removeAll(p));
560 <            assertEquals(SIZE-i, q.size());
560 >            assertEquals(SIZE - i, q.size());
561              for (int j = 0; j < i; ++j) {
562                  PDelay x = (PDelay)(p.remove());
563                  assertFalse(q.contains(x));
# Line 654 | Line 655 | public class DelayQueueTest extends JSR1
655      public void testPollInExecutor() {
656          final DelayQueue q = new DelayQueue();
657          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
658 <        ExecutorService executor = Executors.newFixedThreadPool(2);
659 <        executor.execute(new CheckedRunnable() {
660 <            public void realRun() throws InterruptedException {
661 <                assertNull(q.poll());
662 <                threadsStarted.await();
663 <                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
664 <                checkEmpty(q);
665 <            }});
666 <
667 <        executor.execute(new CheckedRunnable() {
668 <            public void realRun() throws InterruptedException {
669 <                threadsStarted.await();
670 <                q.put(new PDelay(1));
671 <            }});
672 <
673 <        joinPool(executor);
658 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
659 >        try (PoolCleaner cleaner = cleaner(executor)) {
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      }
675  
676      /**
# Line 754 | Line 755 | public class DelayQueueTest extends JSR1
755          final DelayQueue q = populatedQueue(SIZE);
756          Thread t = new Thread(new CheckedRunnable() {
757              public void realRun() {
758 <                q.put(new PDelay(SIZE+1));
758 >                q.put(new PDelay(SIZE + 1));
759              }});
760  
761          t.start();
# Line 774 | Line 775 | public class DelayQueueTest extends JSR1
775              ArrayList l = new ArrayList();
776              q.drainTo(l, i);
777              int k = (i < SIZE) ? i : SIZE;
778 <            assertEquals(SIZE-k, q.size());
778 >            assertEquals(SIZE - k, q.size());
779              assertEquals(k, l.size());
780          }
781      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines