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.72 by jsr166, Sat Mar 25 21:41:10 2017 UTC vs.
Revision 1.80 by jsr166, Sun May 6 22:29:24 2018 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 <        return newTestSuite(PriorityBlockingQueueTest.class,
58 <                            new Generic().testSuite(),
59 <                            new InitialCapacity().testSuite(),
60 <                            CollectionTest.testSuite(new Implementation()));
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(
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 90 | Line 109 | public class PriorityBlockingQueueTest e
109      }
110  
111      /**
112 <     * Constructor throws IAE if capacity argument nonpositive
112 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
113       */
114      public void testConstructor2() {
115          try {
# Line 229 | Line 248 | public class PriorityBlockingQueueTest e
248      }
249  
250      /**
251 <     * addAll(this) throws IAE
251 >     * addAll(this) throws IllegalArgumentException
252       */
253      public void testAddAllSelf() {
254          PriorityBlockingQueue q = populatedQueue(SIZE);
# Line 302 | Line 321 | public class PriorityBlockingQueueTest e
321      /**
322       * timed offer does not time out
323       */
324 <    public void testTimedOffer() throws InterruptedException {
324 >    public void testTimedOffer() {
325          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
326          Thread t = newStartedThread(new CheckedRunnable() {
327              public void realRun() {
# Line 333 | Line 352 | public class PriorityBlockingQueueTest e
352          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
353          Thread t = newStartedThread(new CheckedRunnable() {
354              public void realRun() throws InterruptedException {
355 <                for (int i = 0; i < SIZE; ++i) {
337 <                    assertEquals(i, q.take());
338 <                }
355 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
356  
357                  Thread.currentThread().interrupt();
358                  try {
# Line 353 | Line 370 | public class PriorityBlockingQueueTest e
370              }});
371  
372          await(pleaseInterrupt);
373 <        assertThreadStaysAlive(t);
373 >        assertThreadBlocks(t, Thread.State.WAITING);
374          t.interrupt();
375          awaitTermination(t);
376      }
# Line 402 | Line 419 | public class PriorityBlockingQueueTest e
419       */
420      public void testInterruptedTimedPoll() throws InterruptedException {
421          final BlockingQueue<Integer> q = populatedQueue(SIZE);
422 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
422 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
423          Thread t = newStartedThread(new CheckedRunnable() {
424              public void realRun() throws InterruptedException {
425                  long startTime = System.nanoTime();
426 <                for (int i = 0; i < SIZE; ++i) {
426 >                for (int i = 0; i < SIZE; i++)
427                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
428 <                }
429 <                aboutToWait.countDown();
428 >
429 >                Thread.currentThread().interrupt();
430                  try {
431                      q.poll(LONG_DELAY_MS, MILLISECONDS);
432                      shouldThrow();
433 <                } catch (InterruptedException success) {
434 <                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
435 <                }
433 >                } catch (InterruptedException success) {}
434 >                assertFalse(Thread.interrupted());
435 >
436 >                pleaseInterrupt.countDown();
437 >                try {
438 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
439 >                    shouldThrow();
440 >                } catch (InterruptedException success) {}
441 >                assertFalse(Thread.interrupted());
442 >
443 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
444              }});
445  
446 <        await(aboutToWait);
447 <        waitForThreadToEnterWaitState(t);
446 >        await(pleaseInterrupt);
447 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
448          t.interrupt();
449          awaitTermination(t);
450      }
# Line 653 | Line 678 | public class PriorityBlockingQueueTest e
678      }
679  
680      /**
681 <     * A deserialized serialized queue has same elements
681 >     * A deserialized/reserialized queue has same elements
682       */
683      public void testSerialization() throws Exception {
684          Queue x = populatedQueue(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines