13 |
|
|
14 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
19 |
> |
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
39 |
|
private PriorityBlockingQueue populatedQueue(int n) { |
40 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
< |
for (int i = n-1; i >= 0; i-=2) |
43 |
< |
assertTrue(q.offer(new Integer(i))); |
44 |
< |
for (int i = (n & 1); i < n; i+=2) |
45 |
< |
assertTrue(q.offer(new Integer(i))); |
42 |
> |
for (int i = n-1; i >= 0; i-=2) |
43 |
> |
assertTrue(q.offer(new Integer(i))); |
44 |
> |
for (int i = (n & 1); i < n; i+=2) |
45 |
> |
assertTrue(q.offer(new Integer(i))); |
46 |
|
assertFalse(q.isEmpty()); |
47 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
48 |
< |
assertEquals(n, q.size()); |
48 |
> |
assertEquals(n, q.size()); |
49 |
|
return q; |
50 |
|
} |
51 |
|
|
63 |
|
try { |
64 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
65 |
|
shouldThrow(); |
66 |
< |
} |
67 |
< |
catch (IllegalArgumentException success) {} |
66 |
> |
} catch (IllegalArgumentException success) {} |
67 |
|
} |
68 |
|
|
69 |
|
/** |
73 |
|
try { |
74 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
75 |
|
shouldThrow(); |
76 |
< |
} |
78 |
< |
catch (NullPointerException success) {} |
76 |
> |
} catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
|
/** |
84 |
|
Integer[] ints = new Integer[SIZE]; |
85 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
< |
} |
90 |
< |
catch (NullPointerException success) {} |
87 |
> |
} catch (NullPointerException success) {} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
97 |
|
ints[i] = new Integer(i); |
98 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
99 |
|
shouldThrow(); |
100 |
< |
} |
104 |
< |
catch (NullPointerException success) {} |
100 |
> |
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* Queue contains all elements of collection used to initialize |
105 |
|
*/ |
106 |
|
public void testConstructor6() { |
107 |
< |
try { |
108 |
< |
Integer[] ints = new Integer[SIZE]; |
109 |
< |
for (int i = 0; i < SIZE; ++i) |
110 |
< |
ints[i] = new Integer(i); |
111 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
112 |
< |
for (int i = 0; i < SIZE; ++i) |
117 |
< |
assertEquals(ints[i], q.poll()); |
118 |
< |
} |
119 |
< |
finally {} |
107 |
> |
Integer[] ints = new Integer[SIZE]; |
108 |
> |
for (int i = 0; i < SIZE; ++i) |
109 |
> |
ints[i] = new Integer(i); |
110 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
111 |
> |
for (int i = 0; i < SIZE; ++i) |
112 |
> |
assertEquals(ints[i], q.poll()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
|
* The comparator used in constructor is used |
117 |
|
*/ |
118 |
|
public void testConstructor7() { |
119 |
< |
try { |
120 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
121 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
122 |
< |
assertEquals(cmp, q.comparator()); |
123 |
< |
Integer[] ints = new Integer[SIZE]; |
124 |
< |
for (int i = 0; i < SIZE; ++i) |
125 |
< |
ints[i] = new Integer(i); |
126 |
< |
q.addAll(Arrays.asList(ints)); |
127 |
< |
for (int i = SIZE-1; i >= 0; --i) |
135 |
< |
assertEquals(ints[i], q.poll()); |
136 |
< |
} |
137 |
< |
finally {} |
119 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
120 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
121 |
> |
assertEquals(cmp, q.comparator()); |
122 |
> |
Integer[] ints = new Integer[SIZE]; |
123 |
> |
for (int i = 0; i < SIZE; ++i) |
124 |
> |
ints[i] = new Integer(i); |
125 |
> |
q.addAll(Arrays.asList(ints)); |
126 |
> |
for (int i = SIZE-1; i >= 0; --i) |
127 |
> |
assertEquals(ints[i], q.poll()); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
164 |
|
* offer(null) throws NPE |
165 |
|
*/ |
166 |
|
public void testOfferNull() { |
167 |
< |
try { |
167 |
> |
try { |
168 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
169 |
|
q.offer(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* add(null) throws NPE |
176 |
|
*/ |
177 |
|
public void testAddNull() { |
178 |
< |
try { |
178 |
> |
try { |
179 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
180 |
|
q.add(null); |
181 |
|
shouldThrow(); |
182 |
< |
} catch (NullPointerException success) { } |
182 |
> |
} catch (NullPointerException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
201 |
|
q.offer(new Object()); |
202 |
|
q.offer(new Object()); |
203 |
|
shouldThrow(); |
204 |
< |
} |
215 |
< |
catch (ClassCastException success) {} |
204 |
> |
} catch (ClassCastException success) {} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
223 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
224 |
|
q.addAll(null); |
225 |
|
shouldThrow(); |
226 |
< |
} |
238 |
< |
catch (NullPointerException success) {} |
226 |
> |
} catch (NullPointerException success) {} |
227 |
|
} |
228 |
|
|
229 |
|
/** |
234 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
235 |
|
q.addAll(q); |
236 |
|
shouldThrow(); |
237 |
< |
} |
250 |
< |
catch (IllegalArgumentException success) {} |
237 |
> |
} catch (IllegalArgumentException success) {} |
238 |
|
} |
239 |
|
|
240 |
|
/** |
246 |
|
Integer[] ints = new Integer[SIZE]; |
247 |
|
q.addAll(Arrays.asList(ints)); |
248 |
|
shouldThrow(); |
249 |
< |
} |
263 |
< |
catch (NullPointerException success) {} |
249 |
> |
} catch (NullPointerException success) {} |
250 |
|
} |
251 |
|
/** |
252 |
|
* addAll of a collection with any null elements throws NPE after |
260 |
|
ints[i] = new Integer(i); |
261 |
|
q.addAll(Arrays.asList(ints)); |
262 |
|
shouldThrow(); |
263 |
< |
} |
278 |
< |
catch (NullPointerException success) {} |
263 |
> |
} catch (NullPointerException success) {} |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
|
* Queue contains all elements of successful addAll |
268 |
|
*/ |
269 |
|
public void testAddAll5() { |
270 |
< |
try { |
271 |
< |
Integer[] empty = new Integer[0]; |
272 |
< |
Integer[] ints = new Integer[SIZE]; |
273 |
< |
for (int i = SIZE-1; i >= 0; --i) |
274 |
< |
ints[i] = new Integer(i); |
275 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
276 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
277 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
278 |
< |
for (int i = 0; i < SIZE; ++i) |
294 |
< |
assertEquals(ints[i], q.poll()); |
295 |
< |
} |
296 |
< |
finally {} |
270 |
> |
Integer[] empty = new Integer[0]; |
271 |
> |
Integer[] ints = new Integer[SIZE]; |
272 |
> |
for (int i = SIZE-1; i >= 0; --i) |
273 |
> |
ints[i] = new Integer(i); |
274 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
275 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
276 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
277 |
> |
for (int i = 0; i < SIZE; ++i) |
278 |
> |
assertEquals(ints[i], q.poll()); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
|
* put(null) throws NPE |
283 |
|
*/ |
284 |
|
public void testPutNull() { |
285 |
< |
try { |
285 |
> |
try { |
286 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
287 |
|
q.put(null); |
288 |
|
shouldThrow(); |
289 |
< |
} |
308 |
< |
catch (NullPointerException success){ |
309 |
< |
} |
289 |
> |
} catch (NullPointerException success) {} |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
|
* all elements successfully put are contained |
294 |
|
*/ |
295 |
|
public void testPut() { |
296 |
< |
try { |
297 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
298 |
< |
for (int i = 0; i < SIZE; ++i) { |
299 |
< |
Integer I = new Integer(i); |
300 |
< |
q.put(I); |
321 |
< |
assertTrue(q.contains(I)); |
322 |
< |
} |
323 |
< |
assertEquals(SIZE, q.size()); |
296 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
297 |
> |
for (int i = 0; i < SIZE; ++i) { |
298 |
> |
Integer I = new Integer(i); |
299 |
> |
q.put(I); |
300 |
> |
assertTrue(q.contains(I)); |
301 |
|
} |
302 |
< |
finally { |
326 |
< |
} |
302 |
> |
assertEquals(SIZE, q.size()); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* put doesn't block waiting for take |
307 |
|
*/ |
308 |
< |
public void testPutWithTake() { |
308 |
> |
public void testPutWithTake() throws InterruptedException { |
309 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
310 |
< |
Thread t = new Thread(new Runnable() { |
311 |
< |
public void run() { |
312 |
< |
int added = 0; |
313 |
< |
try { |
314 |
< |
q.put(new Integer(0)); |
315 |
< |
++added; |
316 |
< |
q.put(new Integer(0)); |
317 |
< |
++added; |
318 |
< |
q.put(new Integer(0)); |
319 |
< |
++added; |
320 |
< |
q.put(new Integer(0)); |
321 |
< |
++added; |
322 |
< |
threadAssertTrue(added == 4); |
347 |
< |
} finally { |
348 |
< |
} |
349 |
< |
} |
350 |
< |
}); |
351 |
< |
try { |
352 |
< |
t.start(); |
353 |
< |
Thread.sleep(SHORT_DELAY_MS); |
354 |
< |
q.take(); |
355 |
< |
t.interrupt(); |
356 |
< |
t.join(); |
357 |
< |
} catch (Exception e){ |
358 |
< |
unexpectedException(); |
359 |
< |
} |
310 |
> |
final int size = 4; |
311 |
> |
Thread t = new Thread(new CheckedRunnable() { |
312 |
> |
public void realRun() { |
313 |
> |
for (int i = 0; i < size; i++) |
314 |
> |
q.put(new Integer(0)); |
315 |
> |
}}); |
316 |
> |
|
317 |
> |
t.start(); |
318 |
> |
Thread.sleep(SHORT_DELAY_MS); |
319 |
> |
assertEquals(q.size(), size); |
320 |
> |
q.take(); |
321 |
> |
t.interrupt(); |
322 |
> |
t.join(); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
|
* timed offer does not time out |
327 |
|
*/ |
328 |
< |
public void testTimedOffer() { |
328 |
> |
public void testTimedOffer() throws InterruptedException { |
329 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
330 |
|
Thread t = new Thread(new Runnable() { |
331 |
|
public void run() { |
338 |
|
} |
339 |
|
}); |
340 |
|
|
341 |
< |
try { |
342 |
< |
t.start(); |
343 |
< |
Thread.sleep(SMALL_DELAY_MS); |
344 |
< |
t.interrupt(); |
382 |
< |
t.join(); |
383 |
< |
} catch (Exception e){ |
384 |
< |
unexpectedException(); |
385 |
< |
} |
341 |
> |
t.start(); |
342 |
> |
Thread.sleep(SMALL_DELAY_MS); |
343 |
> |
t.interrupt(); |
344 |
> |
t.join(); |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
|
* take retrieves elements in priority order |
349 |
|
*/ |
350 |
< |
public void testTake() { |
351 |
< |
try { |
352 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
353 |
< |
for (int i = 0; i < SIZE; ++i) { |
354 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
396 |
< |
} |
397 |
< |
} catch (InterruptedException e){ |
398 |
< |
unexpectedException(); |
399 |
< |
} |
350 |
> |
public void testTake() throws InterruptedException { |
351 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
352 |
> |
for (int i = 0; i < SIZE; ++i) { |
353 |
> |
assertEquals(i, ((Integer)q.take()).intValue()); |
354 |
> |
} |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
|
* take blocks interruptibly when empty |
359 |
|
*/ |
360 |
< |
public void testTakeFromEmpty() { |
360 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
361 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
362 |
< |
Thread t = new Thread(new Runnable() { |
363 |
< |
public void run() { |
364 |
< |
try { |
365 |
< |
q.take(); |
366 |
< |
threadShouldThrow(); |
367 |
< |
} catch (InterruptedException success){ } |
368 |
< |
} |
369 |
< |
}); |
370 |
< |
try { |
416 |
< |
t.start(); |
417 |
< |
Thread.sleep(SHORT_DELAY_MS); |
418 |
< |
t.interrupt(); |
419 |
< |
t.join(); |
420 |
< |
} catch (Exception e){ |
421 |
< |
unexpectedException(); |
422 |
< |
} |
362 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
363 |
> |
public void realRun() throws InterruptedException { |
364 |
> |
q.take(); |
365 |
> |
}}); |
366 |
> |
|
367 |
> |
t.start(); |
368 |
> |
Thread.sleep(SHORT_DELAY_MS); |
369 |
> |
t.interrupt(); |
370 |
> |
t.join(); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
|
* Take removes existing elements until empty, then blocks interruptibly |
375 |
|
*/ |
376 |
< |
public void testBlockingTake() { |
377 |
< |
Thread t = new Thread(new Runnable() { |
378 |
< |
public void run() { |
379 |
< |
try { |
380 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
381 |
< |
for (int i = 0; i < SIZE; ++i) { |
382 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
383 |
< |
} |
384 |
< |
q.take(); |
385 |
< |
threadShouldThrow(); |
438 |
< |
} catch (InterruptedException success){ |
439 |
< |
} |
440 |
< |
}}); |
376 |
> |
public void testBlockingTake() throws InterruptedException { |
377 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
378 |
> |
public void realRun() throws InterruptedException { |
379 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
380 |
> |
for (int i = 0; i < SIZE; ++i) { |
381 |
> |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
382 |
> |
} |
383 |
> |
q.take(); |
384 |
> |
}}); |
385 |
> |
|
386 |
|
t.start(); |
387 |
< |
try { |
388 |
< |
Thread.sleep(SHORT_DELAY_MS); |
389 |
< |
t.interrupt(); |
445 |
< |
t.join(); |
446 |
< |
} |
447 |
< |
catch (InterruptedException ie) { |
448 |
< |
unexpectedException(); |
449 |
< |
} |
387 |
> |
Thread.sleep(SHORT_DELAY_MS); |
388 |
> |
t.interrupt(); |
389 |
> |
t.join(); |
390 |
|
} |
391 |
|
|
392 |
|
|
398 |
|
for (int i = 0; i < SIZE; ++i) { |
399 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
400 |
|
} |
401 |
< |
assertNull(q.poll()); |
401 |
> |
assertNull(q.poll()); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* timed pool with zero timeout succeeds when non-empty, else times out |
406 |
|
*/ |
407 |
< |
public void testTimedPoll0() { |
408 |
< |
try { |
409 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
410 |
< |
for (int i = 0; i < SIZE; ++i) { |
411 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
412 |
< |
} |
473 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
474 |
< |
} catch (InterruptedException e){ |
475 |
< |
unexpectedException(); |
476 |
< |
} |
407 |
> |
public void testTimedPoll0() throws InterruptedException { |
408 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
409 |
> |
for (int i = 0; i < SIZE; ++i) { |
410 |
> |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
411 |
> |
} |
412 |
> |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
|
* timed pool with nonzero timeout succeeds when non-empty, else times out |
417 |
|
*/ |
418 |
< |
public void testTimedPoll() { |
419 |
< |
try { |
420 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
421 |
< |
for (int i = 0; i < SIZE; ++i) { |
422 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
423 |
< |
} |
488 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
489 |
< |
} catch (InterruptedException e){ |
490 |
< |
unexpectedException(); |
491 |
< |
} |
418 |
> |
public void testTimedPoll() throws InterruptedException { |
419 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
420 |
> |
for (int i = 0; i < SIZE; ++i) { |
421 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
422 |
> |
} |
423 |
> |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* Interrupted timed poll throws InterruptedException instead of |
428 |
|
* returning timeout status |
429 |
|
*/ |
430 |
< |
public void testInterruptedTimedPoll() { |
431 |
< |
Thread t = new Thread(new Runnable() { |
432 |
< |
public void run() { |
433 |
< |
try { |
434 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
435 |
< |
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
437 |
< |
} |
438 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
439 |
< |
} catch (InterruptedException success){ |
440 |
< |
} |
441 |
< |
}}); |
430 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
431 |
> |
Thread t = new Thread(new CheckedRunnable() { |
432 |
> |
public void realRun() throws InterruptedException { |
433 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
434 |
> |
for (int i = 0; i < SIZE; ++i) { |
435 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
436 |
> |
} |
437 |
> |
try { |
438 |
> |
q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
439 |
> |
threadShouldThrow(); |
440 |
> |
} catch (InterruptedException success) {} |
441 |
> |
}}); |
442 |
> |
|
443 |
|
t.start(); |
444 |
< |
try { |
445 |
< |
Thread.sleep(SHORT_DELAY_MS); |
446 |
< |
t.interrupt(); |
514 |
< |
t.join(); |
515 |
< |
} |
516 |
< |
catch (InterruptedException ie) { |
517 |
< |
unexpectedException(); |
518 |
< |
} |
444 |
> |
Thread.sleep(SHORT_DELAY_MS); |
445 |
> |
t.interrupt(); |
446 |
> |
t.join(); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
451 |
|
* on interruption throws |
452 |
|
*/ |
453 |
< |
public void testTimedPollWithOffer() { |
453 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
454 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
455 |
< |
Thread t = new Thread(new Runnable() { |
456 |
< |
public void run() { |
457 |
< |
try { |
458 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
459 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
460 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
461 |
< |
threadShouldThrow(); |
462 |
< |
} catch (InterruptedException success) { } |
463 |
< |
} |
464 |
< |
}); |
465 |
< |
try { |
466 |
< |
t.start(); |
467 |
< |
Thread.sleep(SMALL_DELAY_MS); |
468 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
469 |
< |
t.interrupt(); |
542 |
< |
t.join(); |
543 |
< |
} catch (Exception e){ |
544 |
< |
unexpectedException(); |
545 |
< |
} |
455 |
> |
Thread t = new Thread(new CheckedRunnable() { |
456 |
> |
public void realRun() throws InterruptedException { |
457 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
458 |
> |
threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
459 |
> |
try { |
460 |
> |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
461 |
> |
threadShouldThrow(); |
462 |
> |
} catch (InterruptedException success) {} |
463 |
> |
}}); |
464 |
> |
|
465 |
> |
t.start(); |
466 |
> |
Thread.sleep(SMALL_DELAY_MS); |
467 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
468 |
> |
t.interrupt(); |
469 |
> |
t.join(); |
470 |
|
} |
471 |
|
|
472 |
|
|
481 |
|
assertTrue(q.peek() == null || |
482 |
|
i != ((Integer)q.peek()).intValue()); |
483 |
|
} |
484 |
< |
assertNull(q.peek()); |
484 |
> |
assertNull(q.peek()); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
496 |
|
try { |
497 |
|
q.element(); |
498 |
|
shouldThrow(); |
499 |
< |
} |
576 |
< |
catch (NoSuchElementException success) {} |
499 |
> |
} catch (NoSuchElementException success) {} |
500 |
|
} |
501 |
|
|
502 |
|
/** |
510 |
|
try { |
511 |
|
q.remove(); |
512 |
|
shouldThrow(); |
513 |
< |
} catch (NoSuchElementException success){ |
591 |
< |
} |
513 |
> |
} catch (NoSuchElementException success) {} |
514 |
|
} |
515 |
|
|
516 |
|
/** |
607 |
|
/** |
608 |
|
* toArray contains all elements |
609 |
|
*/ |
610 |
< |
public void testToArray() { |
610 |
> |
public void testToArray() throws InterruptedException { |
611 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
612 |
< |
Object[] o = q.toArray(); |
612 |
> |
Object[] o = q.toArray(); |
613 |
|
Arrays.sort(o); |
614 |
< |
try { |
615 |
< |
for (int i = 0; i < o.length; i++) |
694 |
< |
assertEquals(o[i], q.take()); |
695 |
< |
} catch (InterruptedException e){ |
696 |
< |
unexpectedException(); |
697 |
< |
} |
614 |
> |
for (int i = 0; i < o.length; i++) |
615 |
> |
assertEquals(o[i], q.take()); |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
|
* toArray(a) contains all elements |
620 |
|
*/ |
621 |
< |
public void testToArray2() { |
621 |
> |
public void testToArray2() throws InterruptedException { |
622 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
623 |
< |
Integer[] ints = new Integer[SIZE]; |
624 |
< |
ints = (Integer[])q.toArray(ints); |
623 |
> |
Integer[] ints = new Integer[SIZE]; |
624 |
> |
ints = (Integer[])q.toArray(ints); |
625 |
|
Arrays.sort(ints); |
626 |
< |
try { |
627 |
< |
for (int i = 0; i < ints.length; i++) |
710 |
< |
assertEquals(ints[i], q.take()); |
711 |
< |
} catch (InterruptedException e){ |
712 |
< |
unexpectedException(); |
713 |
< |
} |
626 |
> |
for (int i = 0; i < ints.length; i++) |
627 |
> |
assertEquals(ints[i], q.take()); |
628 |
|
} |
629 |
|
|
630 |
|
/** |
631 |
|
* toArray(null) throws NPE |
632 |
|
*/ |
633 |
|
public void testToArray_BadArg() { |
634 |
< |
try { |
634 |
> |
try { |
635 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
636 |
< |
Object o[] = q.toArray(null); |
637 |
< |
shouldThrow(); |
638 |
< |
} catch (NullPointerException success){} |
636 |
> |
Object o[] = q.toArray(null); |
637 |
> |
shouldThrow(); |
638 |
> |
} catch (NullPointerException success) {} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
|
* toArray with incompatible array type throws CCE |
643 |
|
*/ |
644 |
|
public void testToArray1_BadArg() { |
645 |
< |
try { |
645 |
> |
try { |
646 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
< |
Object o[] = q.toArray(new String[10] ); |
648 |
< |
shouldThrow(); |
649 |
< |
} catch (ArrayStoreException success){} |
647 |
> |
Object o[] = q.toArray(new String[10] ); |
648 |
> |
shouldThrow(); |
649 |
> |
} catch (ArrayStoreException success) {} |
650 |
|
} |
651 |
|
|
652 |
|
/** |
655 |
|
public void testIterator() { |
656 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
657 |
|
int i = 0; |
658 |
< |
Iterator it = q.iterator(); |
658 |
> |
Iterator it = q.iterator(); |
659 |
|
while (it.hasNext()) { |
660 |
|
assertTrue(q.contains(it.next())); |
661 |
|
++i; |
700 |
|
public void testPollInExecutor() { |
701 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
702 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
703 |
< |
executor.execute(new Runnable() { |
704 |
< |
public void run() { |
703 |
> |
executor.execute(new CheckedRunnable() { |
704 |
> |
public void realRun() throws InterruptedException { |
705 |
|
threadAssertNull(q.poll()); |
706 |
< |
try { |
707 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
708 |
< |
threadAssertTrue(q.isEmpty()); |
709 |
< |
} |
710 |
< |
catch (InterruptedException e) { |
711 |
< |
threadUnexpectedException(); |
712 |
< |
} |
713 |
< |
} |
714 |
< |
}); |
801 |
< |
|
802 |
< |
executor.execute(new Runnable() { |
803 |
< |
public void run() { |
804 |
< |
try { |
805 |
< |
Thread.sleep(SMALL_DELAY_MS); |
806 |
< |
q.put(new Integer(1)); |
807 |
< |
} |
808 |
< |
catch (InterruptedException e) { |
809 |
< |
threadUnexpectedException(); |
810 |
< |
} |
811 |
< |
} |
812 |
< |
}); |
706 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
707 |
> |
threadAssertTrue(q.isEmpty()); |
708 |
> |
}}); |
709 |
> |
|
710 |
> |
executor.execute(new CheckedRunnable() { |
711 |
> |
public void realRun() throws InterruptedException { |
712 |
> |
Thread.sleep(SMALL_DELAY_MS); |
713 |
> |
q.put(new Integer(1)); |
714 |
> |
}}); |
715 |
|
|
716 |
|
joinPool(executor); |
717 |
|
} |
719 |
|
/** |
720 |
|
* A deserialized serialized queue has same elements |
721 |
|
*/ |
722 |
< |
public void testSerialization() { |
722 |
> |
public void testSerialization() throws Exception { |
723 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
724 |
< |
try { |
725 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
726 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
727 |
< |
out.writeObject(q); |
728 |
< |
out.close(); |
729 |
< |
|
730 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
731 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
732 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
733 |
< |
assertEquals(q.size(), r.size()); |
734 |
< |
while (!q.isEmpty()) |
833 |
< |
assertEquals(q.remove(), r.remove()); |
834 |
< |
} catch (Exception e){ |
835 |
< |
unexpectedException(); |
836 |
< |
} |
724 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
725 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
726 |
> |
out.writeObject(q); |
727 |
> |
out.close(); |
728 |
> |
|
729 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
730 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
731 |
> |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
732 |
> |
assertEquals(q.size(), r.size()); |
733 |
> |
while (!q.isEmpty()) |
734 |
> |
assertEquals(q.remove(), r.remove()); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
742 |
|
try { |
743 |
|
q.drainTo(null); |
744 |
|
shouldThrow(); |
745 |
< |
} catch (NullPointerException success) { |
848 |
< |
} |
745 |
> |
} catch (NullPointerException success) {} |
746 |
|
} |
747 |
|
|
748 |
|
/** |
753 |
|
try { |
754 |
|
q.drainTo(q); |
755 |
|
shouldThrow(); |
756 |
< |
} catch (IllegalArgumentException success) { |
860 |
< |
} |
756 |
> |
} catch (IllegalArgumentException success) {} |
757 |
|
} |
758 |
|
|
759 |
|
/** |
783 |
|
/** |
784 |
|
* drainTo empties queue |
785 |
|
*/ |
786 |
< |
public void testDrainToWithActivePut() { |
786 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
787 |
|
final PriorityBlockingQueue q = populatedQueue(SIZE); |
788 |
< |
Thread t = new Thread(new Runnable() { |
789 |
< |
public void run() { |
790 |
< |
q.put(new Integer(SIZE+1)); |
791 |
< |
} |
792 |
< |
}); |
793 |
< |
try { |
794 |
< |
t.start(); |
795 |
< |
ArrayList l = new ArrayList(); |
796 |
< |
q.drainTo(l); |
797 |
< |
assertTrue(l.size() >= SIZE); |
798 |
< |
for (int i = 0; i < SIZE; ++i) |
799 |
< |
assertEquals(l.get(i), new Integer(i)); |
800 |
< |
t.join(); |
905 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
906 |
< |
} catch (Exception e){ |
907 |
< |
unexpectedException(); |
908 |
< |
} |
788 |
> |
Thread t = new Thread(new CheckedRunnable() { |
789 |
> |
public void realRun() { |
790 |
> |
q.put(new Integer(SIZE+1)); |
791 |
> |
}}); |
792 |
> |
|
793 |
> |
t.start(); |
794 |
> |
ArrayList l = new ArrayList(); |
795 |
> |
q.drainTo(l); |
796 |
> |
assertTrue(l.size() >= SIZE); |
797 |
> |
for (int i = 0; i < SIZE; ++i) |
798 |
> |
assertEquals(l.get(i), new Integer(i)); |
799 |
> |
t.join(); |
800 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
808 |
|
try { |
809 |
|
q.drainTo(null, 0); |
810 |
|
shouldThrow(); |
811 |
< |
} catch (NullPointerException success) { |
920 |
< |
} |
811 |
> |
} catch (NullPointerException success) {} |
812 |
|
} |
813 |
|
|
814 |
|
/** |
819 |
|
try { |
820 |
|
q.drainTo(q, 0); |
821 |
|
shouldThrow(); |
822 |
< |
} catch (IllegalArgumentException success) { |
932 |
< |
} |
822 |
> |
} catch (IllegalArgumentException success) {} |
823 |
|
} |
824 |
|
|
825 |
|
/** |
841 |
|
} |
842 |
|
} |
843 |
|
|
954 |
– |
|
844 |
|
} |