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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.79 by jsr166, Fri Aug 4 03:30:21 2017 UTC vs.
Revision 1.84 by jsr166, Thu Sep 5 21:11:13 2019 UTC

# Line 20 | Line 20 | import java.util.concurrent.CountDownLat
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.PriorityBlockingQueue;
23 + import java.util.concurrent.ThreadLocalRandom;
24  
25   import junit.framework.Test;
26  
# Line 33 | Line 34 | public class PriorityBlockingQueueTest e
34  
35      public static class InitialCapacity extends BlockingQueueTest {
36          protected BlockingQueue emptyCollection() {
37 <            return new PriorityBlockingQueue(SIZE);
37 >            ThreadLocalRandom rnd = ThreadLocalRandom.current();
38 >            int initialCapacity = rnd.nextInt(1, SIZE);
39 >            return new PriorityBlockingQueue(initialCapacity);
40          }
41      }
42  
# Line 44 | Line 47 | public class PriorityBlockingQueueTest e
47      public static Test suite() {
48          class Implementation implements CollectionImplementation {
49              public Class<?> klazz() { return PriorityBlockingQueue.class; }
50 <            public Collection emptyCollection() { return new PriorityBlockingQueue(); }
50 >            public Collection emptyCollection() {
51 >                return new PriorityBlockingQueue();
52 >            }
53 >            public Object makeElement(int i) { return i; }
54 >            public boolean isConcurrent() { return true; }
55 >            public boolean permitsNulls() { return false; }
56 >        }
57 >        class ComparatorImplementation implements CollectionImplementation {
58 >            public Class<?> klazz() { return PriorityBlockingQueue.class; }
59 >            public Collection emptyCollection() {
60 >                ThreadLocalRandom rnd = ThreadLocalRandom.current();
61 >                int initialCapacity = rnd.nextInt(1, 10);
62 >                return new PriorityBlockingQueue(
63 >                    initialCapacity, new MyReverseComparator());
64 >            }
65              public Object makeElement(int i) { return i; }
66              public boolean isConcurrent() { return true; }
67              public boolean permitsNulls() { return false; }
68          }
69 <        return newTestSuite(PriorityBlockingQueueTest.class,
70 <                            new Generic().testSuite(),
71 <                            new InitialCapacity().testSuite(),
72 <                            CollectionTest.testSuite(new Implementation()));
69 >        return newTestSuite(
70 >            PriorityBlockingQueueTest.class,
71 >            new Generic().testSuite(),
72 >            new InitialCapacity().testSuite(),
73 >            CollectionTest.testSuite(new Implementation()),
74 >            CollectionTest.testSuite(new ComparatorImplementation()));
75      }
76  
77      /** Sample Comparator */
78 <    static class MyReverseComparator implements Comparator {
78 >    static class MyReverseComparator implements Comparator, java.io.Serializable {
79          public int compare(Object x, Object y) {
80              return ((Comparable)y).compareTo(x);
81          }
# Line 300 | Line 319 | public class PriorityBlockingQueueTest e
319      }
320  
321      /**
322 <     * timed offer does not time out
322 >     * Queue is unbounded, so timed offer never times out
323       */
324      public void testTimedOffer() {
325          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
# Line 351 | Line 370 | public class PriorityBlockingQueueTest e
370              }});
371  
372          await(pleaseInterrupt);
373 <        assertThreadBlocks(t, Thread.State.WAITING);
373 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
374          t.interrupt();
375          awaitTermination(t);
376      }
# Line 403 | Line 422 | public class PriorityBlockingQueueTest e
422          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
423          Thread t = newStartedThread(new CheckedRunnable() {
424              public void realRun() throws InterruptedException {
406                long startTime = System.nanoTime();
425                  for (int i = 0; i < SIZE; i++)
426                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
427  
428                  Thread.currentThread().interrupt();
429                  try {
430 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
430 >                    q.poll(randomTimeout(), randomTimeUnit());
431                      shouldThrow();
432                  } catch (InterruptedException success) {}
433                  assertFalse(Thread.interrupted());
434  
435                  pleaseInterrupt.countDown();
436                  try {
437 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
437 >                    q.poll(LONGER_DELAY_MS, MILLISECONDS);
438                      shouldThrow();
439                  } catch (InterruptedException success) {}
440                  assertFalse(Thread.interrupted());
423
424                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
441              }});
442  
443          await(pleaseInterrupt);
444 <        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
444 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
445          t.interrupt();
446          awaitTermination(t);
447      }
# Line 554 | Line 570 | public class PriorityBlockingQueueTest e
570       */
571      public void testToArray() throws InterruptedException {
572          PriorityBlockingQueue q = populatedQueue(SIZE);
573 <        Object[] o = q.toArray();
574 <        Arrays.sort(o);
575 <        for (int i = 0; i < o.length; i++)
576 <            assertSame(o[i], q.take());
573 >        Object[] a = q.toArray();
574 >        assertSame(Object[].class, a.getClass());
575 >        Arrays.sort(a);
576 >        for (Object o : a)
577 >            assertSame(o, q.take());
578 >        assertTrue(q.isEmpty());
579      }
580  
581      /**
# Line 569 | Line 587 | public class PriorityBlockingQueueTest e
587          Integer[] array = q.toArray(ints);
588          assertSame(ints, array);
589          Arrays.sort(ints);
590 <        for (int i = 0; i < ints.length; i++)
591 <            assertSame(ints[i], q.take());
590 >        for (Integer o : ints)
591 >            assertSame(o, q.take());
592 >        assertTrue(q.isEmpty());
593      }
594  
595      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines