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 |
|
|
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 |
|
} |
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 { |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
< |
* addAll(this) throws IAE |
251 |
> |
* addAll(this) throws IllegalArgumentException |
252 |
|
*/ |
253 |
|
public void testAddAllSelf() { |
254 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
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); |
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 |
|
} |
428 |
|
|
429 |
|
Thread.currentThread().interrupt(); |
430 |
|
try { |
431 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
431 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
432 |
|
shouldThrow(); |
433 |
|
} catch (InterruptedException success) {} |
434 |
|
assertFalse(Thread.interrupted()); |
444 |
|
}}); |
445 |
|
|
446 |
|
await(pleaseInterrupt); |
447 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
447 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
448 |
|
t.interrupt(); |
449 |
|
awaitTermination(t); |
450 |
|
} |
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 |
|
/** |
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); |