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.72 by jsr166, Fri May 15 18:21:19 2015 UTC vs.
Revision 1.79 by jsr166, Mon Oct 17 01:54:51 2016 UTC

# Line 39 | Line 39 | public class DelayQueueTest extends JSR1
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  
54      /**
# Line 94 | Line 102 | public class DelayQueueTest extends JSR1
102          }
103  
104          public boolean equals(Object other) {
105 <            return equals((NanoDelay)other);
106 <        }
99 <        public boolean equals(NanoDelay other) {
100 <            return other.trigger == trigger;
105 >            return (other instanceof NanoDelay) &&
106 >                this.trigger == ((NanoDelay)other).trigger;
107          }
108  
109          // suppress [overrides] javac warning
# Line 119 | 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)
136              assertTrue(q.offer(new PDelay(i)));
137          assertFalse(q.isEmpty());
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 166 | Line 173 | public class DelayQueueTest extends JSR1
173       */
174      public void testConstructor5() {
175          PDelay[] a = new PDelay[SIZE];
176 <        for (int i = 0; i < SIZE-1; ++i)
176 >        for (int i = 0; i < SIZE - 1; ++i)
177              a[i] = new PDelay(i);
178          try {
179              new DelayQueue(Arrays.asList(a));
# Line 208 | Line 215 | public class DelayQueueTest extends JSR1
215          BlockingQueue q = populatedQueue(SIZE);
216          for (int i = 0; i < SIZE; ++i) {
217              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
218 <            assertEquals(SIZE-i, q.size());
218 >            assertEquals(SIZE - i, q.size());
219              assertTrue(q.remove() instanceof PDelay);
220          }
221          for (int i = 0; i < SIZE; ++i) {
# Line 256 | Line 263 | public class DelayQueueTest extends JSR1
263      public void testAddAll3() {
264          DelayQueue q = new DelayQueue();
265          PDelay[] a = new PDelay[SIZE];
266 <        for (int i = 0; i < SIZE-1; ++i)
266 >        for (int i = 0; i < SIZE - 1; ++i)
267              a[i] = new PDelay(i);
268          try {
269              q.addAll(Arrays.asList(a));
# Line 270 | 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 412 | 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 432 | 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 542 | 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 555 | 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 x = (PDelay)(p.remove());
572                  assertFalse(q.contains(x));
# Line 653 | 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 <                assertNotNull(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 753 | 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 773 | 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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines