1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
|
13 |
< |
public class PriorityBlockingQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private static final int N = 10; |
16 |
< |
private static final long SHORT_DELAY_MS = 100; |
17 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static final long LONG_DELAY_MS = 10000; |
19 |
< |
private static final int NOCAP = Integer.MAX_VALUE; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Comparator; |
15 |
> |
import java.util.Iterator; |
16 |
> |
import java.util.NoSuchElementException; |
17 |
> |
import java.util.Queue; |
18 |
> |
import java.util.concurrent.BlockingQueue; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
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 |
> |
|
27 |
> |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
28 |
> |
|
29 |
> |
public static class Generic extends BlockingQueueTest { |
30 |
> |
protected BlockingQueue emptyCollection() { |
31 |
> |
return new PriorityBlockingQueue(); |
32 |
> |
} |
33 |
> |
} |
34 |
> |
|
35 |
> |
public static class InitialCapacity extends BlockingQueueTest { |
36 |
> |
protected BlockingQueue emptyCollection() { |
37 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
38 |
> |
int initialCapacity = rnd.nextInt(1, SIZE); |
39 |
> |
return new PriorityBlockingQueue(initialCapacity); |
40 |
> |
} |
41 |
> |
} |
42 |
|
|
43 |
|
public static void main(String[] args) { |
44 |
< |
junit.textui.TestRunner.run (suite()); |
44 |
> |
main(suite(), args); |
45 |
|
} |
46 |
|
|
47 |
|
public static Test suite() { |
48 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
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 JSR166TestCase.itemFor(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 |
> |
@SuppressWarnings("unchecked") |
60 |
> |
public Collection emptyCollection() { |
61 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
62 |
> |
int initialCapacity = rnd.nextInt(1, 10); |
63 |
> |
return new PriorityBlockingQueue( |
64 |
> |
initialCapacity, new MyReverseComparator()); |
65 |
> |
} |
66 |
> |
public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } |
67 |
> |
public boolean isConcurrent() { return true; } |
68 |
> |
public boolean permitsNulls() { return false; } |
69 |
> |
} |
70 |
> |
return newTestSuite( |
71 |
> |
PriorityBlockingQueueTest.class, |
72 |
> |
new Generic().testSuite(), |
73 |
> |
new InitialCapacity().testSuite(), |
74 |
> |
CollectionTest.testSuite(new Implementation()), |
75 |
> |
CollectionTest.testSuite(new ComparatorImplementation())); |
76 |
|
} |
77 |
|
|
78 |
< |
static class MyReverseComparator implements Comparator { |
78 |
> |
/** Sample Comparator */ |
79 |
> |
static class MyReverseComparator implements Comparator, java.io.Serializable { |
80 |
> |
@SuppressWarnings("unchecked") |
81 |
|
public int compare(Object x, Object y) { |
82 |
< |
int i = ((Integer)x).intValue(); |
31 |
< |
int j = ((Integer)y).intValue(); |
32 |
< |
if (i < j) return 1; |
33 |
< |
if (i > j) return -1; |
34 |
< |
return 0; |
82 |
> |
return ((Comparable)y).compareTo(x); |
83 |
|
} |
84 |
|
} |
85 |
|
|
38 |
– |
|
86 |
|
/** |
87 |
< |
* Create a queue of given size containing consecutive |
88 |
< |
* Integers 0 ... n. |
87 |
> |
* Returns a new queue of given size containing consecutive |
88 |
> |
* Items 0 ... n - 1. |
89 |
|
*/ |
90 |
< |
private PriorityBlockingQueue fullQueue(int n) { |
91 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
90 |
> |
private static PriorityBlockingQueue<Item> populatedQueue(int n) { |
91 |
> |
PriorityBlockingQueue<Item> q = |
92 |
> |
new PriorityBlockingQueue<Item>(n); |
93 |
|
assertTrue(q.isEmpty()); |
94 |
< |
for(int i = n-1; i >= 0; i-=2) |
95 |
< |
assertTrue(q.offer(new Integer(i))); |
96 |
< |
for(int i = (n & 1); i < n; i+=2) |
97 |
< |
assertTrue(q.offer(new Integer(i))); |
94 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
95 |
> |
mustOffer(q, i); |
96 |
> |
for (int i = (n & 1); i < n; i += 2) |
97 |
> |
mustOffer(q, i); |
98 |
|
assertFalse(q.isEmpty()); |
99 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
100 |
< |
assertEquals(n, q.size()); |
99 |
> |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
100 |
> |
mustEqual(n, q.size()); |
101 |
> |
mustEqual(0, q.peek()); |
102 |
|
return q; |
103 |
|
} |
104 |
< |
|
105 |
< |
public void testConstructor1(){ |
106 |
< |
assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); |
104 |
> |
|
105 |
> |
/** |
106 |
> |
* A new queue has unbounded capacity |
107 |
> |
*/ |
108 |
> |
public void testConstructor1() { |
109 |
> |
mustEqual(Integer.MAX_VALUE, |
110 |
> |
new PriorityBlockingQueue<Item>(SIZE).remainingCapacity()); |
111 |
|
} |
112 |
|
|
113 |
< |
public void testConstructor2(){ |
113 |
> |
/** |
114 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
115 |
> |
*/ |
116 |
> |
public void testConstructor2() { |
117 |
|
try { |
118 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
119 |
< |
fail("Cannot make zero-sized"); |
120 |
< |
} |
65 |
< |
catch (IllegalArgumentException success) {} |
118 |
> |
new PriorityBlockingQueue<Item>(0); |
119 |
> |
shouldThrow(); |
120 |
> |
} catch (IllegalArgumentException success) {} |
121 |
|
} |
122 |
|
|
123 |
< |
public void testConstructor3(){ |
124 |
< |
|
123 |
> |
/** |
124 |
> |
* Initializing from null Collection throws NPE |
125 |
> |
*/ |
126 |
> |
public void testConstructor3() { |
127 |
|
try { |
128 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
129 |
< |
fail("Cannot make from null collection"); |
130 |
< |
} |
74 |
< |
catch (NullPointerException success) {} |
128 |
> |
new PriorityBlockingQueue<Item>(null); |
129 |
> |
shouldThrow(); |
130 |
> |
} catch (NullPointerException success) {} |
131 |
|
} |
132 |
|
|
133 |
< |
public void testConstructor4(){ |
133 |
> |
/** |
134 |
> |
* Initializing from Collection of null elements throws NPE |
135 |
> |
*/ |
136 |
> |
public void testConstructor4() { |
137 |
> |
Collection<Item> elements = Arrays.asList(new Item[SIZE]); |
138 |
|
try { |
139 |
< |
Integer[] ints = new Integer[N]; |
140 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
141 |
< |
fail("Cannot make with null elements"); |
82 |
< |
} |
83 |
< |
catch (NullPointerException success) {} |
139 |
> |
new PriorityBlockingQueue<Item>(elements); |
140 |
> |
shouldThrow(); |
141 |
> |
} catch (NullPointerException success) {} |
142 |
|
} |
143 |
|
|
144 |
< |
public void testConstructor5(){ |
145 |
< |
try { |
146 |
< |
Integer[] ints = new Integer[N]; |
147 |
< |
for (int i = 0; i < N-1; ++i) |
148 |
< |
ints[i] = new Integer(i); |
149 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
150 |
< |
fail("Cannot make with null elements"); |
151 |
< |
} |
152 |
< |
catch (NullPointerException success) {} |
144 |
> |
/** |
145 |
> |
* Initializing from Collection with some null elements throws NPE |
146 |
> |
*/ |
147 |
> |
public void testConstructor5() { |
148 |
> |
Item[] items = new Item[2]; items[0] = zero; |
149 |
> |
Collection<Item> elements = Arrays.asList(items); |
150 |
> |
try { |
151 |
> |
new PriorityBlockingQueue<Item>(elements); |
152 |
> |
shouldThrow(); |
153 |
> |
} catch (NullPointerException success) {} |
154 |
|
} |
155 |
|
|
156 |
< |
public void testConstructor6(){ |
157 |
< |
try { |
158 |
< |
Integer[] ints = new Integer[N]; |
159 |
< |
for (int i = 0; i < N; ++i) |
160 |
< |
ints[i] = new Integer(i); |
161 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
162 |
< |
for (int i = 0; i < N; ++i) |
163 |
< |
assertEquals(ints[i], q.poll()); |
105 |
< |
} |
106 |
< |
finally {} |
156 |
> |
/** |
157 |
> |
* Queue contains all elements of collection used to initialize |
158 |
> |
*/ |
159 |
> |
public void testConstructor6() { |
160 |
> |
Item[] items = defaultItems; |
161 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(Arrays.asList(items)); |
162 |
> |
for (int i = 0; i < SIZE; ++i) |
163 |
> |
mustEqual(items[i], q.poll()); |
164 |
|
} |
165 |
|
|
166 |
< |
public void testConstructor7(){ |
167 |
< |
try { |
168 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator()); |
169 |
< |
Integer[] ints = new Integer[N]; |
170 |
< |
for (int i = 0; i < N; ++i) |
171 |
< |
ints[i] = new Integer(i); |
172 |
< |
q.addAll(Arrays.asList(ints)); |
173 |
< |
for (int i = N-1; i >= 0; --i) |
174 |
< |
assertEquals(ints[i], q.poll()); |
175 |
< |
} |
176 |
< |
finally {} |
166 |
> |
/** |
167 |
> |
* The comparator used in constructor is used |
168 |
> |
*/ |
169 |
> |
public void testConstructor7() { |
170 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
171 |
> |
@SuppressWarnings("unchecked") |
172 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE, cmp); |
173 |
> |
mustEqual(cmp, q.comparator()); |
174 |
> |
Item[] items = defaultItems; |
175 |
> |
q.addAll(Arrays.asList(items)); |
176 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
177 |
> |
mustEqual(items[i], q.poll()); |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* isEmpty is true before add, false after |
182 |
+ |
*/ |
183 |
|
public void testEmpty() { |
184 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
184 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2); |
185 |
|
assertTrue(q.isEmpty()); |
186 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
187 |
< |
q.add(new Integer(1)); |
186 |
> |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
187 |
> |
q.add(one); |
188 |
|
assertFalse(q.isEmpty()); |
189 |
< |
q.add(new Integer(2)); |
189 |
> |
q.add(two); |
190 |
|
q.remove(); |
191 |
|
q.remove(); |
192 |
|
assertTrue(q.isEmpty()); |
193 |
|
} |
194 |
|
|
195 |
< |
public void testRemainingCapacity(){ |
196 |
< |
PriorityBlockingQueue q = fullQueue(N); |
197 |
< |
for (int i = 0; i < N; ++i) { |
198 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
199 |
< |
assertEquals(N-i, q.size()); |
200 |
< |
q.remove(); |
201 |
< |
} |
202 |
< |
for (int i = 0; i < N; ++i) { |
203 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
204 |
< |
assertEquals(i, q.size()); |
205 |
< |
q.add(new Integer(i)); |
195 |
> |
/** |
196 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
197 |
> |
*/ |
198 |
> |
public void testRemainingCapacity() { |
199 |
> |
BlockingQueue<Item> q = populatedQueue(SIZE); |
200 |
> |
for (int i = 0; i < SIZE; ++i) { |
201 |
> |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
202 |
> |
mustEqual(SIZE - i, q.size()); |
203 |
> |
mustEqual(i, q.remove()); |
204 |
> |
} |
205 |
> |
for (int i = 0; i < SIZE; ++i) { |
206 |
> |
mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); |
207 |
> |
mustEqual(i, q.size()); |
208 |
> |
mustAdd(q, i); |
209 |
|
} |
210 |
|
} |
211 |
|
|
212 |
< |
public void testOfferNull(){ |
213 |
< |
try { |
214 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
151 |
< |
q.offer(null); |
152 |
< |
fail("should throw NPE"); |
153 |
< |
} catch (NullPointerException success) { } |
154 |
< |
} |
155 |
< |
|
212 |
> |
/** |
213 |
> |
* Offer of comparable element succeeds |
214 |
> |
*/ |
215 |
|
public void testOffer() { |
216 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
217 |
< |
assertTrue(q.offer(new Integer(0))); |
218 |
< |
assertTrue(q.offer(new Integer(1))); |
216 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(1); |
217 |
> |
assertTrue(q.offer(zero)); |
218 |
> |
assertTrue(q.offer(one)); |
219 |
|
} |
220 |
|
|
221 |
+ |
/** |
222 |
+ |
* Offer of non-Comparable throws CCE |
223 |
+ |
*/ |
224 |
|
public void testOfferNonComparable() { |
225 |
+ |
PriorityBlockingQueue<Object> q = new PriorityBlockingQueue<Object>(1); |
226 |
|
try { |
164 |
– |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
165 |
– |
q.offer(new Object()); |
166 |
– |
q.offer(new Object()); |
227 |
|
q.offer(new Object()); |
228 |
< |
fail("should throw CCE"); |
228 |
> |
shouldThrow(); |
229 |
> |
} catch (ClassCastException success) { |
230 |
> |
assertTrue(q.isEmpty()); |
231 |
> |
mustEqual(0, q.size()); |
232 |
> |
assertNull(q.poll()); |
233 |
|
} |
170 |
– |
catch(ClassCastException success) {} |
234 |
|
} |
235 |
|
|
236 |
< |
public void testAdd(){ |
237 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
238 |
< |
for (int i = 0; i < N; ++i) { |
239 |
< |
assertEquals(i, q.size()); |
240 |
< |
assertTrue(q.add(new Integer(i))); |
236 |
> |
/** |
237 |
> |
* add of comparable succeeds |
238 |
> |
*/ |
239 |
> |
public void testAdd() { |
240 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE); |
241 |
> |
for (int i = 0; i < SIZE; ++i) { |
242 |
> |
mustEqual(i, q.size()); |
243 |
> |
mustAdd(q, i); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
< |
public void testAddAll1(){ |
247 |
> |
/** |
248 |
> |
* addAll(this) throws IllegalArgumentException |
249 |
> |
*/ |
250 |
> |
public void testAddAllSelf() { |
251 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
252 |
|
try { |
253 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
254 |
< |
q.addAll(null); |
255 |
< |
fail("Cannot add null collection"); |
186 |
< |
} |
187 |
< |
catch (NullPointerException success) {} |
253 |
> |
q.addAll(q); |
254 |
> |
shouldThrow(); |
255 |
> |
} catch (IllegalArgumentException success) {} |
256 |
|
} |
257 |
< |
public void testAddAll2(){ |
258 |
< |
try { |
259 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
260 |
< |
Integer[] ints = new Integer[N]; |
261 |
< |
q.addAll(Arrays.asList(ints)); |
262 |
< |
fail("Cannot add null elements"); |
263 |
< |
} |
264 |
< |
catch (NullPointerException success) {} |
257 |
> |
|
258 |
> |
/** |
259 |
> |
* addAll of a collection with any null elements throws NPE after |
260 |
> |
* possibly adding some elements |
261 |
> |
*/ |
262 |
> |
public void testAddAll3() { |
263 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE); |
264 |
> |
Item[] items = new Item[2]; |
265 |
> |
items[0] = zero; |
266 |
> |
try { |
267 |
> |
q.addAll(Arrays.asList(items)); |
268 |
> |
shouldThrow(); |
269 |
> |
} catch (NullPointerException success) {} |
270 |
|
} |
271 |
< |
public void testAddAll3(){ |
272 |
< |
try { |
273 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
274 |
< |
Integer[] ints = new Integer[N]; |
275 |
< |
for (int i = 0; i < N-1; ++i) |
276 |
< |
ints[i] = new Integer(i); |
277 |
< |
q.addAll(Arrays.asList(ints)); |
278 |
< |
fail("Cannot add null elements"); |
279 |
< |
} |
280 |
< |
catch (NullPointerException success) {} |
271 |
> |
|
272 |
> |
/** |
273 |
> |
* Queue contains all elements of successful addAll |
274 |
> |
*/ |
275 |
> |
public void testAddAll5() { |
276 |
> |
Item[] empty = new Item[0]; |
277 |
> |
Item[] items = defaultItems; |
278 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE); |
279 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
280 |
> |
assertTrue(q.addAll(Arrays.asList(items))); |
281 |
> |
for (int i = 0; i < SIZE; ++i) |
282 |
> |
mustEqual(items[i], q.poll()); |
283 |
|
} |
284 |
|
|
285 |
< |
public void testAddAll5(){ |
286 |
< |
try { |
287 |
< |
Integer[] empty = new Integer[0]; |
288 |
< |
Integer[] ints = new Integer[N]; |
289 |
< |
for (int i = N-1; i >= 0; --i) |
290 |
< |
ints[i] = new Integer(i); |
291 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
292 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
293 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
219 |
< |
for (int i = 0; i < N; ++i) |
220 |
< |
assertEquals(ints[i], q.poll()); |
221 |
< |
} |
222 |
< |
finally {} |
223 |
< |
} |
224 |
< |
|
225 |
< |
public void testPutNull() { |
226 |
< |
try { |
227 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
228 |
< |
q.put(null); |
229 |
< |
fail("put should throw NPE"); |
230 |
< |
} |
231 |
< |
catch (NullPointerException success){ |
232 |
< |
} |
233 |
< |
} |
234 |
< |
|
235 |
< |
public void testPut() { |
236 |
< |
try { |
237 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
238 |
< |
for (int i = 0; i < N; ++i) { |
239 |
< |
Integer I = new Integer(i); |
240 |
< |
q.put(I); |
241 |
< |
assertTrue(q.contains(I)); |
242 |
< |
} |
243 |
< |
assertEquals(N, q.size()); |
244 |
< |
} |
245 |
< |
finally { |
285 |
> |
/** |
286 |
> |
* all elements successfully put are contained |
287 |
> |
*/ |
288 |
> |
public void testPut() { |
289 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE); |
290 |
> |
for (int i = 0; i < SIZE; ++i) { |
291 |
> |
Item x = itemFor(i); |
292 |
> |
q.put(x); |
293 |
> |
mustContain(q, x); |
294 |
|
} |
295 |
+ |
mustEqual(SIZE, q.size()); |
296 |
|
} |
297 |
|
|
298 |
< |
public void testPutWithTake() { |
299 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
300 |
< |
Thread t = new Thread(new Runnable() { |
301 |
< |
public void run(){ |
302 |
< |
int added = 0; |
303 |
< |
try { |
304 |
< |
q.put(new Integer(0)); |
305 |
< |
++added; |
306 |
< |
q.put(new Integer(0)); |
307 |
< |
++added; |
308 |
< |
q.put(new Integer(0)); |
309 |
< |
++added; |
310 |
< |
q.put(new Integer(0)); |
311 |
< |
++added; |
312 |
< |
assertTrue(added == 4); |
264 |
< |
} finally { |
265 |
< |
} |
266 |
< |
} |
267 |
< |
}); |
268 |
< |
try { |
269 |
< |
t.start(); |
270 |
< |
Thread.sleep(SHORT_DELAY_MS); |
271 |
< |
q.take(); |
272 |
< |
t.interrupt(); |
273 |
< |
t.join(); |
274 |
< |
} catch (Exception e){ |
275 |
< |
fail("Unexpected exception"); |
276 |
< |
} |
298 |
> |
/** |
299 |
> |
* put doesn't block waiting for take |
300 |
> |
*/ |
301 |
> |
public void testPutWithTake() throws InterruptedException { |
302 |
> |
final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2); |
303 |
> |
final int size = 4; |
304 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
305 |
> |
public void realRun() { |
306 |
> |
for (int i = 0; i < size; i++) |
307 |
> |
q.put(zero); |
308 |
> |
}}); |
309 |
> |
|
310 |
> |
awaitTermination(t); |
311 |
> |
mustEqual(size, q.size()); |
312 |
> |
q.take(); |
313 |
|
} |
314 |
|
|
315 |
+ |
/** |
316 |
+ |
* Queue is unbounded, so timed offer never times out |
317 |
+ |
*/ |
318 |
|
public void testTimedOffer() { |
319 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
320 |
< |
Thread t = new Thread(new Runnable() { |
321 |
< |
public void run(){ |
322 |
< |
try { |
323 |
< |
q.put(new Integer(0)); |
324 |
< |
q.put(new Integer(0)); |
325 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
326 |
< |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
288 |
< |
} finally { } |
289 |
< |
} |
290 |
< |
}); |
291 |
< |
|
292 |
< |
try { |
293 |
< |
t.start(); |
294 |
< |
Thread.sleep(SHORT_DELAY_MS); |
295 |
< |
t.interrupt(); |
296 |
< |
t.join(); |
297 |
< |
} catch (Exception e){ |
298 |
< |
fail("Unexpected exception"); |
299 |
< |
} |
300 |
< |
} |
319 |
> |
final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2); |
320 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
321 |
> |
public void realRun() { |
322 |
> |
q.put(one); |
323 |
> |
q.put(two); |
324 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
325 |
> |
assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); |
326 |
> |
}}); |
327 |
|
|
328 |
< |
public void testTake(){ |
303 |
< |
try { |
304 |
< |
PriorityBlockingQueue q = fullQueue(N); |
305 |
< |
for (int i = 0; i < N; ++i) { |
306 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
307 |
< |
} |
308 |
< |
} catch (InterruptedException e){ |
309 |
< |
fail("Unexpected exception"); |
310 |
< |
} |
328 |
> |
awaitTermination(t); |
329 |
|
} |
330 |
|
|
331 |
< |
public void testTakeFromEmpty() { |
332 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
333 |
< |
Thread t = new Thread(new Runnable() { |
334 |
< |
public void run(){ |
335 |
< |
try { |
336 |
< |
q.take(); |
337 |
< |
fail("Should block"); |
320 |
< |
} catch (InterruptedException success){ } |
321 |
< |
} |
322 |
< |
}); |
323 |
< |
try { |
324 |
< |
t.start(); |
325 |
< |
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
t.interrupt(); |
327 |
< |
t.join(); |
328 |
< |
} catch (Exception e){ |
329 |
< |
fail("Unexpected exception"); |
331 |
> |
/** |
332 |
> |
* take retrieves elements in priority order |
333 |
> |
*/ |
334 |
> |
public void testTake() throws InterruptedException { |
335 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
336 |
> |
for (int i = 0; i < SIZE; ++i) { |
337 |
> |
mustEqual(i, q.take()); |
338 |
|
} |
339 |
|
} |
340 |
|
|
341 |
< |
public void testBlockingTake(){ |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
try { |
345 |
< |
PriorityBlockingQueue q = fullQueue(N); |
346 |
< |
for (int i = 0; i < N; ++i) { |
347 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
348 |
< |
} |
349 |
< |
q.take(); |
342 |
< |
fail("take should block"); |
343 |
< |
} catch (InterruptedException success){ |
344 |
< |
} |
345 |
< |
}}); |
346 |
< |
t.start(); |
347 |
< |
try { |
348 |
< |
Thread.sleep(SHORT_DELAY_MS); |
349 |
< |
t.interrupt(); |
350 |
< |
t.join(); |
351 |
< |
} |
352 |
< |
catch (InterruptedException ie) { |
353 |
< |
fail("Unexpected exception"); |
354 |
< |
} |
355 |
< |
} |
341 |
> |
/** |
342 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
343 |
> |
*/ |
344 |
> |
public void testBlockingTake() throws InterruptedException { |
345 |
> |
final PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
346 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
347 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
348 |
> |
public void realRun() throws InterruptedException { |
349 |
> |
for (int i = 0; i < SIZE; i++) mustEqual(i, q.take()); |
350 |
|
|
351 |
+ |
Thread.currentThread().interrupt(); |
352 |
+ |
try { |
353 |
+ |
q.take(); |
354 |
+ |
shouldThrow(); |
355 |
+ |
} catch (InterruptedException success) {} |
356 |
+ |
assertFalse(Thread.interrupted()); |
357 |
|
|
358 |
< |
public void testPoll(){ |
359 |
< |
PriorityBlockingQueue q = fullQueue(N); |
360 |
< |
for (int i = 0; i < N; ++i) { |
361 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
362 |
< |
} |
363 |
< |
assertNull(q.poll()); |
358 |
> |
pleaseInterrupt.countDown(); |
359 |
> |
try { |
360 |
> |
q.take(); |
361 |
> |
shouldThrow(); |
362 |
> |
} catch (InterruptedException success) {} |
363 |
> |
assertFalse(Thread.interrupted()); |
364 |
> |
}}); |
365 |
> |
|
366 |
> |
await(pleaseInterrupt); |
367 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
368 |
> |
t.interrupt(); |
369 |
> |
awaitTermination(t); |
370 |
|
} |
371 |
|
|
372 |
< |
public void testTimedPoll0() { |
373 |
< |
try { |
374 |
< |
PriorityBlockingQueue q = fullQueue(N); |
375 |
< |
for (int i = 0; i < N; ++i) { |
376 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
377 |
< |
} |
378 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
379 |
< |
} catch (InterruptedException e){ |
380 |
< |
fail("Unexpected exception"); |
375 |
< |
} |
372 |
> |
/** |
373 |
> |
* poll succeeds unless empty |
374 |
> |
*/ |
375 |
> |
public void testPoll() { |
376 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
377 |
> |
for (int i = 0; i < SIZE; ++i) { |
378 |
> |
mustEqual(i, q.poll()); |
379 |
> |
} |
380 |
> |
assertNull(q.poll()); |
381 |
|
} |
382 |
|
|
383 |
< |
public void testTimedPoll() { |
384 |
< |
try { |
385 |
< |
PriorityBlockingQueue q = fullQueue(N); |
386 |
< |
for (int i = 0; i < N; ++i) { |
387 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
388 |
< |
} |
389 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
385 |
< |
} catch (InterruptedException e){ |
386 |
< |
fail("Unexpected exception"); |
387 |
< |
} |
388 |
< |
} |
389 |
< |
|
390 |
< |
public void testInterruptedTimedPoll(){ |
391 |
< |
Thread t = new Thread(new Runnable() { |
392 |
< |
public void run() { |
393 |
< |
try { |
394 |
< |
PriorityBlockingQueue q = fullQueue(N); |
395 |
< |
for (int i = 0; i < N; ++i) { |
396 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
397 |
< |
} |
398 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
399 |
< |
} catch (InterruptedException success){ |
400 |
< |
} |
401 |
< |
}}); |
402 |
< |
t.start(); |
403 |
< |
try { |
404 |
< |
Thread.sleep(SHORT_DELAY_MS); |
405 |
< |
t.interrupt(); |
406 |
< |
t.join(); |
407 |
< |
} |
408 |
< |
catch (InterruptedException ie) { |
409 |
< |
fail("Unexpected exception"); |
383 |
> |
/** |
384 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
385 |
> |
*/ |
386 |
> |
public void testTimedPoll0() throws InterruptedException { |
387 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
388 |
> |
for (int i = 0; i < SIZE; ++i) { |
389 |
> |
mustEqual(i, q.poll(0, MILLISECONDS)); |
390 |
|
} |
391 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
392 |
|
} |
393 |
|
|
394 |
< |
public void testTimedPollWithOffer(){ |
395 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
396 |
< |
Thread t = new Thread(new Runnable() { |
397 |
< |
public void run(){ |
398 |
< |
try { |
399 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
400 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
401 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
402 |
< |
fail("Should block"); |
403 |
< |
} catch (InterruptedException success) { } |
404 |
< |
} |
405 |
< |
}); |
406 |
< |
try { |
407 |
< |
t.start(); |
408 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
428 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
429 |
< |
t.interrupt(); |
430 |
< |
t.join(); |
431 |
< |
} catch (Exception e){ |
432 |
< |
fail("Unexpected exception"); |
433 |
< |
} |
434 |
< |
} |
394 |
> |
/** |
395 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
396 |
> |
*/ |
397 |
> |
public void testTimedPoll() throws InterruptedException { |
398 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
399 |
> |
for (int i = 0; i < SIZE; ++i) { |
400 |
> |
long startTime = System.nanoTime(); |
401 |
> |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
402 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
403 |
> |
} |
404 |
> |
long startTime = System.nanoTime(); |
405 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
406 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
407 |
> |
checkEmpty(q); |
408 |
> |
} |
409 |
|
|
410 |
+ |
/** |
411 |
+ |
* Interrupted timed poll throws InterruptedException instead of |
412 |
+ |
* returning timeout status |
413 |
+ |
*/ |
414 |
+ |
public void testInterruptedTimedPoll() throws InterruptedException { |
415 |
+ |
final BlockingQueue<Item> q = populatedQueue(SIZE); |
416 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
417 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
418 |
+ |
public void realRun() throws InterruptedException { |
419 |
+ |
for (int i = 0; i < SIZE; i++) |
420 |
+ |
mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
421 |
|
|
422 |
< |
public void testPeek(){ |
423 |
< |
PriorityBlockingQueue q = fullQueue(N); |
424 |
< |
for (int i = 0; i < N; ++i) { |
425 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
426 |
< |
q.poll(); |
422 |
> |
Thread.currentThread().interrupt(); |
423 |
> |
try { |
424 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
425 |
> |
shouldThrow(); |
426 |
> |
} catch (InterruptedException success) {} |
427 |
> |
assertFalse(Thread.interrupted()); |
428 |
> |
|
429 |
> |
pleaseInterrupt.countDown(); |
430 |
> |
try { |
431 |
> |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
432 |
> |
shouldThrow(); |
433 |
> |
} catch (InterruptedException success) {} |
434 |
> |
assertFalse(Thread.interrupted()); |
435 |
> |
}}); |
436 |
> |
|
437 |
> |
await(pleaseInterrupt); |
438 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
439 |
> |
t.interrupt(); |
440 |
> |
awaitTermination(t); |
441 |
> |
} |
442 |
> |
|
443 |
> |
/** |
444 |
> |
* peek returns next element, or null if empty |
445 |
> |
*/ |
446 |
> |
public void testPeek() { |
447 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
448 |
> |
for (int i = 0; i < SIZE; ++i) { |
449 |
> |
mustEqual(i, q.peek()); |
450 |
> |
mustEqual(i, q.poll()); |
451 |
|
assertTrue(q.peek() == null || |
452 |
< |
i != ((Integer)q.peek()).intValue()); |
452 |
> |
!q.peek().equals(i)); |
453 |
|
} |
454 |
< |
assertNull(q.peek()); |
454 |
> |
assertNull(q.peek()); |
455 |
|
} |
456 |
|
|
457 |
< |
public void testElement(){ |
458 |
< |
PriorityBlockingQueue q = fullQueue(N); |
459 |
< |
for (int i = 0; i < N; ++i) { |
460 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
461 |
< |
q.poll(); |
457 |
> |
/** |
458 |
> |
* element returns next element, or throws NSEE if empty |
459 |
> |
*/ |
460 |
> |
public void testElement() { |
461 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
462 |
> |
for (int i = 0; i < SIZE; ++i) { |
463 |
> |
mustEqual(i, q.element()); |
464 |
> |
mustEqual(i, q.poll()); |
465 |
|
} |
466 |
|
try { |
467 |
|
q.element(); |
468 |
< |
fail("no such element"); |
469 |
< |
} |
458 |
< |
catch (NoSuchElementException success) {} |
468 |
> |
shouldThrow(); |
469 |
> |
} catch (NoSuchElementException success) {} |
470 |
|
} |
471 |
|
|
472 |
< |
public void testRemove(){ |
473 |
< |
PriorityBlockingQueue q = fullQueue(N); |
474 |
< |
for (int i = 0; i < N; ++i) { |
475 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
472 |
> |
/** |
473 |
> |
* remove removes next element, or throws NSEE if empty |
474 |
> |
*/ |
475 |
> |
public void testRemove() { |
476 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
477 |
> |
for (int i = 0; i < SIZE; ++i) { |
478 |
> |
mustEqual(i, q.remove()); |
479 |
|
} |
480 |
|
try { |
481 |
|
q.remove(); |
482 |
< |
fail("remove should throw"); |
483 |
< |
} catch (NoSuchElementException success){ |
484 |
< |
} |
485 |
< |
} |
486 |
< |
|
487 |
< |
public void testRemoveElement(){ |
488 |
< |
PriorityBlockingQueue q = fullQueue(N); |
489 |
< |
for (int i = 1; i < N; i+=2) { |
490 |
< |
assertTrue(q.remove(new Integer(i))); |
491 |
< |
} |
492 |
< |
for (int i = 0; i < N; i+=2) { |
479 |
< |
assertTrue(q.remove(new Integer(i))); |
480 |
< |
assertFalse(q.remove(new Integer(i+1))); |
481 |
< |
} |
482 |
< |
assert(q.isEmpty()); |
483 |
< |
} |
484 |
< |
|
485 |
< |
public void testContains(){ |
486 |
< |
PriorityBlockingQueue q = fullQueue(N); |
487 |
< |
for (int i = 0; i < N; ++i) { |
488 |
< |
assertTrue(q.contains(new Integer(i))); |
482 |
> |
shouldThrow(); |
483 |
> |
} catch (NoSuchElementException success) {} |
484 |
> |
} |
485 |
> |
|
486 |
> |
/** |
487 |
> |
* contains(x) reports true when elements added but not yet removed |
488 |
> |
*/ |
489 |
> |
public void testContains() { |
490 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
491 |
> |
for (int i = 0; i < SIZE; ++i) { |
492 |
> |
mustContain(q, i); |
493 |
|
q.poll(); |
494 |
< |
assertFalse(q.contains(new Integer(i))); |
494 |
> |
mustNotContain(q, i); |
495 |
|
} |
496 |
|
} |
497 |
|
|
498 |
< |
public void testClear(){ |
499 |
< |
PriorityBlockingQueue q = fullQueue(N); |
498 |
> |
/** |
499 |
> |
* clear removes all elements |
500 |
> |
*/ |
501 |
> |
public void testClear() { |
502 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
503 |
|
q.clear(); |
504 |
|
assertTrue(q.isEmpty()); |
505 |
< |
assertEquals(0, q.size()); |
506 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
500 |
< |
q.add(new Integer(1)); |
505 |
> |
mustEqual(0, q.size()); |
506 |
> |
q.add(one); |
507 |
|
assertFalse(q.isEmpty()); |
508 |
+ |
mustContain(q, one); |
509 |
|
q.clear(); |
510 |
|
assertTrue(q.isEmpty()); |
511 |
|
} |
512 |
|
|
513 |
< |
public void testContainsAll(){ |
514 |
< |
PriorityBlockingQueue q = fullQueue(N); |
515 |
< |
PriorityBlockingQueue p = new PriorityBlockingQueue(N); |
516 |
< |
for (int i = 0; i < N; ++i) { |
513 |
> |
/** |
514 |
> |
* containsAll(c) is true when c contains a subset of elements |
515 |
> |
*/ |
516 |
> |
public void testContainsAll() { |
517 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
518 |
> |
PriorityBlockingQueue<Item> p = new PriorityBlockingQueue<Item>(SIZE); |
519 |
> |
for (int i = 0; i < SIZE; ++i) { |
520 |
|
assertTrue(q.containsAll(p)); |
521 |
|
assertFalse(p.containsAll(q)); |
522 |
< |
p.add(new Integer(i)); |
522 |
> |
mustAdd(p, i); |
523 |
|
} |
524 |
|
assertTrue(p.containsAll(q)); |
525 |
|
} |
526 |
|
|
527 |
< |
public void testRetainAll(){ |
528 |
< |
PriorityBlockingQueue q = fullQueue(N); |
529 |
< |
PriorityBlockingQueue p = fullQueue(N); |
530 |
< |
for (int i = 0; i < N; ++i) { |
527 |
> |
/** |
528 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
529 |
> |
*/ |
530 |
> |
public void testRetainAll() { |
531 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
532 |
> |
PriorityBlockingQueue<Item> p = populatedQueue(SIZE); |
533 |
> |
for (int i = 0; i < SIZE; ++i) { |
534 |
|
boolean changed = q.retainAll(p); |
535 |
|
if (i == 0) |
536 |
|
assertFalse(changed); |
538 |
|
assertTrue(changed); |
539 |
|
|
540 |
|
assertTrue(q.containsAll(p)); |
541 |
< |
assertEquals(N-i, q.size()); |
541 |
> |
mustEqual(SIZE - i, q.size()); |
542 |
|
p.remove(); |
543 |
|
} |
544 |
|
} |
545 |
|
|
546 |
< |
public void testRemoveAll(){ |
547 |
< |
for (int i = 1; i < N; ++i) { |
548 |
< |
PriorityBlockingQueue q = fullQueue(N); |
549 |
< |
PriorityBlockingQueue p = fullQueue(i); |
546 |
> |
/** |
547 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
548 |
> |
*/ |
549 |
> |
public void testRemoveAll() { |
550 |
> |
for (int i = 1; i < SIZE; ++i) { |
551 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
552 |
> |
PriorityBlockingQueue<Item> p = populatedQueue(i); |
553 |
|
assertTrue(q.removeAll(p)); |
554 |
< |
assertEquals(N-i, q.size()); |
554 |
> |
mustEqual(SIZE - i, q.size()); |
555 |
|
for (int j = 0; j < i; ++j) { |
556 |
< |
Integer I = (Integer)(p.remove()); |
557 |
< |
assertFalse(q.contains(I)); |
556 |
> |
Item x = p.remove(); |
557 |
> |
mustNotContain(q, x); |
558 |
|
} |
559 |
|
} |
560 |
|
} |
561 |
|
|
562 |
< |
public void testToArray(){ |
563 |
< |
PriorityBlockingQueue q = fullQueue(N); |
564 |
< |
Object[] o = q.toArray(); |
565 |
< |
Arrays.sort(o); |
566 |
< |
try { |
567 |
< |
for(int i = 0; i < o.length; i++) |
568 |
< |
assertEquals(o[i], q.take()); |
569 |
< |
} catch (InterruptedException e){ |
570 |
< |
fail("Unexpected exception"); |
571 |
< |
} |
572 |
< |
} |
573 |
< |
|
574 |
< |
public void testToArray2(){ |
575 |
< |
PriorityBlockingQueue q = fullQueue(N); |
576 |
< |
Integer[] ints = new Integer[N]; |
577 |
< |
ints = (Integer[])q.toArray(ints); |
578 |
< |
Arrays.sort(ints); |
579 |
< |
try { |
580 |
< |
for(int i = 0; i < ints.length; i++) |
581 |
< |
assertEquals(ints[i], q.take()); |
582 |
< |
} catch (InterruptedException e){ |
583 |
< |
fail("Unexpected exception"); |
584 |
< |
} |
585 |
< |
} |
586 |
< |
|
587 |
< |
public void testIterator(){ |
588 |
< |
PriorityBlockingQueue q = fullQueue(N); |
589 |
< |
int i = 0; |
590 |
< |
Iterator it = q.iterator(); |
591 |
< |
while(it.hasNext()) { |
592 |
< |
assertTrue(q.contains(it.next())); |
593 |
< |
++i; |
594 |
< |
} |
595 |
< |
assertEquals(i, N); |
596 |
< |
} |
597 |
< |
|
598 |
< |
public void testIteratorRemove () { |
599 |
< |
|
600 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
601 |
< |
|
602 |
< |
q.add(new Integer(2)); |
603 |
< |
q.add(new Integer(1)); |
604 |
< |
q.add(new Integer(3)); |
562 |
> |
/** |
563 |
> |
* toArray contains all elements |
564 |
> |
*/ |
565 |
> |
public void testToArray() throws InterruptedException { |
566 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
567 |
> |
Object[] a = q.toArray(); |
568 |
> |
assertSame(Object[].class, a.getClass()); |
569 |
> |
Arrays.sort(a); |
570 |
> |
for (Object o : a) |
571 |
> |
assertSame(o, q.take()); |
572 |
> |
assertTrue(q.isEmpty()); |
573 |
> |
} |
574 |
> |
|
575 |
> |
/** |
576 |
> |
* toArray(a) contains all elements |
577 |
> |
*/ |
578 |
> |
public void testToArray2() throws InterruptedException { |
579 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
580 |
> |
Item[] items = new Item[SIZE]; |
581 |
> |
Item[] array = q.toArray(items); |
582 |
> |
assertSame(items, array); |
583 |
> |
Arrays.sort(items); |
584 |
> |
for (Item o : items) |
585 |
> |
assertSame(o, q.take()); |
586 |
> |
assertTrue(q.isEmpty()); |
587 |
> |
} |
588 |
> |
|
589 |
> |
/** |
590 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
591 |
> |
*/ |
592 |
> |
public void testToArray1_BadArg() { |
593 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
594 |
> |
try { |
595 |
> |
q.toArray(new String[10]); |
596 |
> |
shouldThrow(); |
597 |
> |
} catch (ArrayStoreException success) {} |
598 |
> |
} |
599 |
> |
|
600 |
> |
/** |
601 |
> |
* iterator iterates through all elements |
602 |
> |
*/ |
603 |
> |
public void testIterator() { |
604 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
605 |
> |
Iterator<? extends Item> it = q.iterator(); |
606 |
> |
int i; |
607 |
> |
for (i = 0; it.hasNext(); i++) |
608 |
> |
mustContain(q, it.next()); |
609 |
> |
mustEqual(i, SIZE); |
610 |
> |
assertIteratorExhausted(it); |
611 |
> |
} |
612 |
> |
|
613 |
> |
/** |
614 |
> |
* iterator of empty collection has no elements |
615 |
> |
*/ |
616 |
> |
public void testEmptyIterator() { |
617 |
> |
assertIteratorExhausted(new PriorityBlockingQueue<Item>().iterator()); |
618 |
> |
} |
619 |
> |
|
620 |
> |
/** |
621 |
> |
* iterator.remove removes current element |
622 |
> |
*/ |
623 |
> |
public void testIteratorRemove() { |
624 |
> |
final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(3); |
625 |
> |
q.add(two); |
626 |
> |
q.add(one); |
627 |
> |
q.add(three); |
628 |
|
|
629 |
< |
Iterator it = q.iterator(); |
629 |
> |
Iterator<? extends Item> it = q.iterator(); |
630 |
|
it.next(); |
631 |
|
it.remove(); |
632 |
|
|
633 |
|
it = q.iterator(); |
634 |
< |
assertEquals(it.next(), new Integer(2)); |
635 |
< |
assertEquals(it.next(), new Integer(3)); |
634 |
> |
mustEqual(it.next(), two); |
635 |
> |
mustEqual(it.next(), three); |
636 |
|
assertFalse(it.hasNext()); |
637 |
|
} |
638 |
|
|
639 |
< |
|
640 |
< |
public void testToString(){ |
641 |
< |
PriorityBlockingQueue q = fullQueue(N); |
639 |
> |
/** |
640 |
> |
* toString contains toStrings of elements |
641 |
> |
*/ |
642 |
> |
public void testToString() { |
643 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
644 |
|
String s = q.toString(); |
645 |
< |
for (int i = 0; i < N; ++i) { |
646 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
645 |
> |
for (int i = 0; i < SIZE; ++i) { |
646 |
> |
assertTrue(s.contains(String.valueOf(i))); |
647 |
|
} |
648 |
< |
} |
648 |
> |
} |
649 |
|
|
650 |
+ |
/** |
651 |
+ |
* timed poll transfers elements across Executor tasks |
652 |
+ |
*/ |
653 |
|
public void testPollInExecutor() { |
654 |
+ |
final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2); |
655 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
656 |
+ |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
657 |
+ |
try (PoolCleaner cleaner = cleaner(executor)) { |
658 |
+ |
executor.execute(new CheckedRunnable() { |
659 |
+ |
public void realRun() throws InterruptedException { |
660 |
+ |
assertNull(q.poll()); |
661 |
+ |
threadsStarted.await(); |
662 |
+ |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
663 |
+ |
checkEmpty(q); |
664 |
+ |
}}); |
665 |
+ |
|
666 |
+ |
executor.execute(new CheckedRunnable() { |
667 |
+ |
public void realRun() throws InterruptedException { |
668 |
+ |
threadsStarted.await(); |
669 |
+ |
q.put(one); |
670 |
+ |
}}); |
671 |
+ |
} |
672 |
+ |
} |
673 |
|
|
674 |
< |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
674 |
> |
/** |
675 |
> |
* A deserialized/reserialized queue has same elements |
676 |
> |
*/ |
677 |
> |
public void testSerialization() throws Exception { |
678 |
> |
Queue<Item> x = populatedQueue(SIZE); |
679 |
> |
Queue<Item> y = serialClone(x); |
680 |
> |
|
681 |
> |
assertNotSame(x, y); |
682 |
> |
mustEqual(x.size(), y.size()); |
683 |
> |
while (!x.isEmpty()) { |
684 |
> |
assertFalse(y.isEmpty()); |
685 |
> |
mustEqual(x.remove(), y.remove()); |
686 |
> |
} |
687 |
> |
assertTrue(y.isEmpty()); |
688 |
> |
} |
689 |
|
|
690 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
690 |
> |
/** |
691 |
> |
* drainTo(c) empties queue into another collection c |
692 |
> |
*/ |
693 |
> |
public void testDrainTo() { |
694 |
> |
PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
695 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
696 |
> |
q.drainTo(l); |
697 |
> |
mustEqual(0, q.size()); |
698 |
> |
mustEqual(SIZE, l.size()); |
699 |
> |
for (int i = 0; i < SIZE; ++i) |
700 |
> |
mustEqual(l.get(i), i); |
701 |
> |
q.add(zero); |
702 |
> |
q.add(one); |
703 |
> |
assertFalse(q.isEmpty()); |
704 |
> |
mustContain(q, one); |
705 |
> |
l.clear(); |
706 |
> |
q.drainTo(l); |
707 |
> |
mustEqual(0, q.size()); |
708 |
> |
mustEqual(2, l.size()); |
709 |
> |
for (int i = 0; i < 2; ++i) |
710 |
> |
mustEqual(l.get(i), i); |
711 |
> |
} |
712 |
|
|
713 |
< |
executor.execute(new Runnable() { |
714 |
< |
public void run() { |
715 |
< |
assertNull("poll should fail", q.poll()); |
716 |
< |
try { |
717 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
718 |
< |
assertTrue(q.isEmpty()); |
719 |
< |
} |
720 |
< |
catch (InterruptedException e) { |
721 |
< |
fail("should not be interrupted"); |
624 |
< |
} |
625 |
< |
} |
626 |
< |
}); |
713 |
> |
/** |
714 |
> |
* drainTo empties queue |
715 |
> |
*/ |
716 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
717 |
> |
final PriorityBlockingQueue<Item> q = populatedQueue(SIZE); |
718 |
> |
Thread t = new Thread(new CheckedRunnable() { |
719 |
> |
public void realRun() { |
720 |
> |
q.put(new Item(SIZE + 1)); |
721 |
> |
}}); |
722 |
|
|
723 |
< |
executor.execute(new Runnable() { |
724 |
< |
public void run() { |
725 |
< |
try { |
726 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
727 |
< |
q.put(new Integer(1)); |
728 |
< |
} |
729 |
< |
catch (InterruptedException e) { |
730 |
< |
fail("should not be interrupted"); |
731 |
< |
} |
732 |
< |
} |
733 |
< |
}); |
734 |
< |
|
735 |
< |
executor.shutdown(); |
723 |
> |
t.start(); |
724 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
725 |
> |
q.drainTo(l); |
726 |
> |
assertTrue(l.size() >= SIZE); |
727 |
> |
for (int i = 0; i < SIZE; ++i) |
728 |
> |
mustEqual(l.get(i), i); |
729 |
> |
t.join(); |
730 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
731 |
> |
} |
732 |
> |
|
733 |
> |
/** |
734 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
735 |
> |
*/ |
736 |
> |
public void testDrainToN() { |
737 |
> |
PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE * 2); |
738 |
> |
for (int i = 0; i < SIZE + 2; ++i) { |
739 |
> |
for (int j = 0; j < SIZE; j++) |
740 |
> |
mustOffer(q, j); |
741 |
> |
ArrayList<Item> l = new ArrayList<Item>(); |
742 |
> |
q.drainTo(l, i); |
743 |
> |
int k = (i < SIZE) ? i : SIZE; |
744 |
> |
mustEqual(k, l.size()); |
745 |
> |
mustEqual(SIZE - k, q.size()); |
746 |
> |
for (int j = 0; j < k; ++j) |
747 |
> |
mustEqual(l.get(j), j); |
748 |
> |
do {} while (q.poll() != null); |
749 |
> |
} |
750 |
> |
} |
751 |
|
|
752 |
+ |
/** |
753 |
+ |
* remove(null), contains(null) always return false |
754 |
+ |
*/ |
755 |
+ |
public void testNeverContainsNull() { |
756 |
+ |
Collection<?>[] qs = { |
757 |
+ |
new PriorityBlockingQueue<Object>(), |
758 |
+ |
populatedQueue(2), |
759 |
+ |
}; |
760 |
+ |
|
761 |
+ |
for (Collection<?> q : qs) { |
762 |
+ |
assertFalse(q.contains(null)); |
763 |
+ |
assertFalse(q.remove(null)); |
764 |
+ |
} |
765 |
|
} |
766 |
|
|
767 |
|
} |