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 |
|
|
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 |
|
|
45 |
|
} |
46 |
|
|
47 |
|
public static Test suite() { |
48 |
< |
return newTestSuite(PriorityBlockingQueueTest.class, |
49 |
< |
new Generic().testSuite(), |
50 |
< |
new InitialCapacity().testSuite()); |
48 |
> |
class Implementation implements CollectionImplementation { |
49 |
> |
public Class<?> klazz() { return PriorityBlockingQueue.class; } |
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( |
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 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
|
* Returns a new queue of given size containing consecutive |
86 |
< |
* Integers 0 ... n. |
86 |
> |
* Integers 0 ... n - 1. |
87 |
|
*/ |
88 |
< |
private PriorityBlockingQueue<Integer> populatedQueue(int n) { |
88 |
> |
private static PriorityBlockingQueue<Integer> populatedQueue(int n) { |
89 |
|
PriorityBlockingQueue<Integer> q = |
90 |
|
new PriorityBlockingQueue<Integer>(n); |
91 |
|
assertTrue(q.isEmpty()); |
92 |
< |
for (int i = n-1; i >= 0; i -= 2) |
92 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
93 |
|
assertTrue(q.offer(new Integer(i))); |
94 |
|
for (int i = (n & 1); i < n; i += 2) |
95 |
|
assertTrue(q.offer(new Integer(i))); |
96 |
|
assertFalse(q.isEmpty()); |
97 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
98 |
|
assertEquals(n, q.size()); |
99 |
+ |
assertEquals((Integer) 0, q.peek()); |
100 |
|
return q; |
101 |
|
} |
102 |
|
|
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 { |
144 |
|
*/ |
145 |
|
public void testConstructor5() { |
146 |
|
Integer[] ints = new Integer[SIZE]; |
147 |
< |
for (int i = 0; i < SIZE-1; ++i) |
147 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
148 |
|
ints[i] = i; |
149 |
|
Collection<Integer> elements = Arrays.asList(ints); |
150 |
|
try { |
176 |
|
for (int i = 0; i < SIZE; ++i) |
177 |
|
ints[i] = new Integer(i); |
178 |
|
q.addAll(Arrays.asList(ints)); |
179 |
< |
for (int i = SIZE-1; i >= 0; --i) |
179 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
180 |
|
assertEquals(ints[i], q.poll()); |
181 |
|
} |
182 |
|
|
228 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
229 |
|
try { |
230 |
|
q.offer(new Object()); |
203 |
– |
q.offer(new Object()); |
231 |
|
shouldThrow(); |
232 |
< |
} catch (ClassCastException success) {} |
232 |
> |
} catch (ClassCastException success) { |
233 |
> |
assertTrue(q.isEmpty()); |
234 |
> |
assertEquals(0, q.size()); |
235 |
> |
assertNull(q.poll()); |
236 |
> |
} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
< |
* addAll(this) throws IAE |
251 |
> |
* addAll(this) throws IllegalArgumentException |
252 |
|
*/ |
253 |
|
public void testAddAllSelf() { |
254 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
265 |
|
public void testAddAll3() { |
266 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
267 |
|
Integer[] ints = new Integer[SIZE]; |
268 |
< |
for (int i = 0; i < SIZE-1; ++i) |
268 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
269 |
|
ints[i] = new Integer(i); |
270 |
|
try { |
271 |
|
q.addAll(Arrays.asList(ints)); |
279 |
|
public void testAddAll5() { |
280 |
|
Integer[] empty = new Integer[0]; |
281 |
|
Integer[] ints = new Integer[SIZE]; |
282 |
< |
for (int i = SIZE-1; i >= 0; --i) |
282 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
283 |
|
ints[i] = new Integer(i); |
284 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
285 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
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() { |
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) { |
325 |
< |
assertEquals(i, q.take()); |
326 |
< |
} |
355 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
356 |
|
|
357 |
|
Thread.currentThread().interrupt(); |
358 |
|
try { |
370 |
|
}}); |
371 |
|
|
372 |
|
await(pleaseInterrupt); |
373 |
< |
assertThreadStaysAlive(t); |
373 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
374 |
|
t.interrupt(); |
375 |
|
awaitTermination(t); |
376 |
|
} |
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 |
< |
for (int i = 0; i < SIZE; ++i) { |
426 |
< |
long t0 = System.nanoTime(); |
425 |
> |
long startTime = System.nanoTime(); |
426 |
> |
for (int i = 0; i < SIZE; i++) |
427 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
428 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
429 |
< |
} |
401 |
< |
long t0 = System.nanoTime(); |
402 |
< |
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(t0) < MEDIUM_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 |
< |
aboutToWait.await(); |
447 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
446 |
> |
await(pleaseInterrupt); |
447 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
448 |
|
t.interrupt(); |
449 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
449 |
> |
awaitTermination(t); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
547 |
|
assertTrue(changed); |
548 |
|
|
549 |
|
assertTrue(q.containsAll(p)); |
550 |
< |
assertEquals(SIZE-i, q.size()); |
550 |
> |
assertEquals(SIZE - i, q.size()); |
551 |
|
p.remove(); |
552 |
|
} |
553 |
|
} |
560 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
561 |
|
PriorityBlockingQueue p = populatedQueue(i); |
562 |
|
assertTrue(q.removeAll(p)); |
563 |
< |
assertEquals(SIZE-i, q.size()); |
563 |
> |
assertEquals(SIZE - i, q.size()); |
564 |
|
for (int j = 0; j < i; ++j) { |
565 |
|
Integer x = (Integer)(p.remove()); |
566 |
|
assertFalse(q.contains(x)); |
573 |
|
*/ |
574 |
|
public void testToArray() throws InterruptedException { |
575 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
576 |
< |
Object[] o = q.toArray(); |
577 |
< |
Arrays.sort(o); |
578 |
< |
for (int i = 0; i < o.length; i++) |
579 |
< |
assertSame(o[i], q.take()); |
576 |
> |
Object[] a = q.toArray(); |
577 |
> |
assertSame(Object[].class, a.getClass()); |
578 |
> |
Arrays.sort(a); |
579 |
> |
for (Object o : a) |
580 |
> |
assertSame(o, q.take()); |
581 |
> |
assertTrue(q.isEmpty()); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
590 |
|
Integer[] array = q.toArray(ints); |
591 |
|
assertSame(ints, array); |
592 |
|
Arrays.sort(ints); |
593 |
< |
for (int i = 0; i < ints.length; i++) |
594 |
< |
assertSame(ints[i], q.take()); |
593 |
> |
for (Integer o : ints) |
594 |
> |
assertSame(o, q.take()); |
595 |
> |
assertTrue(q.isEmpty()); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
662 |
|
public void testPollInExecutor() { |
663 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
664 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
665 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
666 |
< |
executor.execute(new CheckedRunnable() { |
667 |
< |
public void realRun() throws InterruptedException { |
668 |
< |
assertNull(q.poll()); |
669 |
< |
threadsStarted.await(); |
670 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
671 |
< |
checkEmpty(q); |
672 |
< |
}}); |
673 |
< |
|
674 |
< |
executor.execute(new CheckedRunnable() { |
675 |
< |
public void realRun() throws InterruptedException { |
676 |
< |
threadsStarted.await(); |
677 |
< |
q.put(one); |
678 |
< |
}}); |
679 |
< |
|
680 |
< |
joinPool(executor); |
665 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
666 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
667 |
> |
executor.execute(new CheckedRunnable() { |
668 |
> |
public void realRun() throws InterruptedException { |
669 |
> |
assertNull(q.poll()); |
670 |
> |
threadsStarted.await(); |
671 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
672 |
> |
checkEmpty(q); |
673 |
> |
}}); |
674 |
> |
|
675 |
> |
executor.execute(new CheckedRunnable() { |
676 |
> |
public void realRun() throws InterruptedException { |
677 |
> |
threadsStarted.await(); |
678 |
> |
q.put(one); |
679 |
> |
}}); |
680 |
> |
} |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
< |
* A deserialized serialized queue has same elements |
684 |
> |
* A deserialized/reserialized queue has same elements |
685 |
|
*/ |
686 |
|
public void testSerialization() throws Exception { |
687 |
|
Queue x = populatedQueue(SIZE); |
727 |
|
final PriorityBlockingQueue q = populatedQueue(SIZE); |
728 |
|
Thread t = new Thread(new CheckedRunnable() { |
729 |
|
public void realRun() { |
730 |
< |
q.put(new Integer(SIZE+1)); |
730 |
> |
q.put(new Integer(SIZE + 1)); |
731 |
|
}}); |
732 |
|
|
733 |
|
t.start(); |
744 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
745 |
|
*/ |
746 |
|
public void testDrainToN() { |
747 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
747 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2); |
748 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
749 |
|
for (int j = 0; j < SIZE; j++) |
750 |
|
assertTrue(q.offer(new Integer(j))); |
752 |
|
q.drainTo(l, i); |
753 |
|
int k = (i < SIZE) ? i : SIZE; |
754 |
|
assertEquals(k, l.size()); |
755 |
< |
assertEquals(SIZE-k, q.size()); |
755 |
> |
assertEquals(SIZE - k, q.size()); |
756 |
|
for (int j = 0; j < k; ++j) |
757 |
|
assertEquals(l.get(j), new Integer(j)); |
758 |
|
do {} while (q.poll() != null); |