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