15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
214 |
|
shouldThrow(); |
215 |
|
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
+ |
|
218 |
|
/** |
219 |
|
* addAll of a collection with any null elements throws NPE after |
220 |
|
* possibly adding some elements |
229 |
|
shouldThrow(); |
230 |
|
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
+ |
|
233 |
|
/** |
234 |
|
* addAll throws ISE if not enough room |
235 |
|
*/ |
243 |
|
shouldThrow(); |
244 |
|
} catch (IllegalStateException success) {} |
245 |
|
} |
246 |
+ |
|
247 |
|
/** |
248 |
|
* Queue contains all elements, in traversal order, of successful addAll |
249 |
|
*/ |
287 |
|
* put blocks interruptibly if full |
288 |
|
*/ |
289 |
|
public void testBlockingPut() throws InterruptedException { |
290 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
291 |
|
Thread t = new Thread(new CheckedRunnable() { |
292 |
< |
public void realRun() { |
293 |
< |
int added = 0; |
292 |
> |
public void realRun() throws InterruptedException { |
293 |
> |
for (int i = 0; i < SIZE; ++i) |
294 |
> |
q.put(i); |
295 |
> |
assertEquals(SIZE, q.size()); |
296 |
> |
assertEquals(0, q.remainingCapacity()); |
297 |
|
try { |
298 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
299 |
< |
for (int i = 0; i < SIZE; ++i) { |
300 |
< |
q.put(new Integer(i)); |
294 |
< |
++added; |
295 |
< |
} |
296 |
< |
q.put(new Integer(SIZE)); |
297 |
< |
threadShouldThrow(); |
298 |
< |
} catch (InterruptedException success) { |
299 |
< |
threadAssertEquals(added, SIZE); |
300 |
< |
} |
298 |
> |
q.put(99); |
299 |
> |
shouldThrow(); |
300 |
> |
} catch (InterruptedException success) {} |
301 |
|
}}); |
302 |
|
|
303 |
|
t.start(); |
304 |
|
Thread.sleep(SHORT_DELAY_MS); |
305 |
|
t.interrupt(); |
306 |
|
t.join(); |
307 |
+ |
assertEquals(SIZE, q.size()); |
308 |
+ |
assertEquals(0, q.remainingCapacity()); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* put blocks waiting for take when full |
313 |
|
*/ |
314 |
|
public void testPutWithTake() throws InterruptedException { |
315 |
+ |
final int capacity = 2; |
316 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
317 |
|
Thread t = new Thread(new CheckedRunnable() { |
318 |
< |
public void realRun() { |
319 |
< |
int added = 0; |
318 |
> |
public void realRun() throws InterruptedException { |
319 |
> |
for (int i = 0; i < capacity + 1; i++) |
320 |
> |
q.put(i); |
321 |
|
try { |
322 |
< |
q.put(new Object()); |
323 |
< |
++added; |
324 |
< |
q.put(new Object()); |
321 |
< |
++added; |
322 |
< |
q.put(new Object()); |
323 |
< |
++added; |
324 |
< |
q.put(new Object()); |
325 |
< |
++added; |
326 |
< |
threadShouldThrow(); |
327 |
< |
} catch (InterruptedException success) { |
328 |
< |
threadAssertTrue(added >= 2); |
329 |
< |
} |
322 |
> |
q.put(99); |
323 |
> |
shouldThrow(); |
324 |
> |
} catch (InterruptedException success) {} |
325 |
|
}}); |
326 |
|
|
327 |
|
t.start(); |
328 |
|
Thread.sleep(SHORT_DELAY_MS); |
329 |
< |
q.take(); |
329 |
> |
assertEquals(q.remainingCapacity(), 0); |
330 |
> |
assertEquals(0, q.take()); |
331 |
> |
Thread.sleep(SHORT_DELAY_MS); |
332 |
|
t.interrupt(); |
333 |
|
t.join(); |
334 |
+ |
assertEquals(q.remainingCapacity(), 0); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
339 |
|
*/ |
340 |
|
public void testTimedOffer() throws InterruptedException { |
341 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
342 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
342 |
> |
Thread t = new Thread(new CheckedRunnable() { |
343 |
|
public void realRun() throws InterruptedException { |
344 |
|
q.put(new Object()); |
345 |
|
q.put(new Object()); |
346 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
347 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
348 |
< |
}}; |
346 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
347 |
> |
try { |
348 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
349 |
> |
shouldThrow(); |
350 |
> |
} catch (InterruptedException success) {} |
351 |
> |
}}); |
352 |
|
|
353 |
|
t.start(); |
354 |
|
Thread.sleep(SMALL_DELAY_MS); |
362 |
|
public void testTake() throws InterruptedException { |
363 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
364 |
|
for (int i = 0; i < SIZE; ++i) { |
365 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
365 |
> |
assertEquals(i, q.take()); |
366 |
|
} |
367 |
|
} |
368 |
|
|
386 |
|
* Take removes existing elements until empty, then blocks interruptibly |
387 |
|
*/ |
388 |
|
public void testBlockingTake() throws InterruptedException { |
389 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
389 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
390 |
> |
Thread t = new Thread(new CheckedRunnable() { |
391 |
|
public void realRun() throws InterruptedException { |
390 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
392 |
|
for (int i = 0; i < SIZE; ++i) { |
393 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
393 |
> |
assertEquals(i, q.take()); |
394 |
|
} |
395 |
< |
q.take(); |
396 |
< |
}}; |
395 |
> |
try { |
396 |
> |
q.take(); |
397 |
> |
shouldThrow(); |
398 |
> |
} catch (InterruptedException success) {} |
399 |
> |
}}); |
400 |
|
|
401 |
|
t.start(); |
402 |
|
Thread.sleep(SHORT_DELAY_MS); |
404 |
|
t.join(); |
405 |
|
} |
406 |
|
|
403 |
– |
|
407 |
|
/** |
408 |
|
* poll succeeds unless empty |
409 |
|
*/ |
410 |
|
public void testPoll() { |
411 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
413 |
> |
assertEquals(i, q.poll()); |
414 |
|
} |
415 |
|
assertNull(q.poll()); |
416 |
|
} |
421 |
|
public void testTimedPoll0() throws InterruptedException { |
422 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
424 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
|
} |
426 |
|
assertNull(q.poll(0, MILLISECONDS)); |
427 |
|
} |
432 |
|
public void testTimedPoll() throws InterruptedException { |
433 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
434 |
|
for (int i = 0; i < SIZE; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
435 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
|
} |
437 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
442 |
|
* returning timeout status |
443 |
|
*/ |
444 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
445 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
445 |
> |
Thread t = new Thread(new CheckedRunnable() { |
446 |
|
public void realRun() throws InterruptedException { |
447 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
|
for (int i = 0; i < SIZE; ++i) { |
449 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
449 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
451 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
452 |
< |
}}; |
451 |
> |
try { |
452 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
453 |
> |
shouldThrow(); |
454 |
> |
} catch (InterruptedException success) {} |
455 |
> |
}}); |
456 |
|
|
457 |
|
t.start(); |
458 |
|
Thread.sleep(SHORT_DELAY_MS); |
466 |
|
*/ |
467 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
468 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
469 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
469 |
> |
Thread t = new Thread(new CheckedRunnable() { |
470 |
|
public void realRun() throws InterruptedException { |
471 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
472 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
473 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
474 |
< |
}}; |
471 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
472 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
473 |
> |
try { |
474 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
475 |
> |
shouldThrow(); |
476 |
> |
} catch (InterruptedException success) {} |
477 |
> |
}}); |
478 |
|
|
479 |
|
t.start(); |
480 |
|
Thread.sleep(SMALL_DELAY_MS); |
489 |
|
public void testPeek() { |
490 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
491 |
|
for (int i = 0; i < SIZE; ++i) { |
492 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
493 |
< |
q.poll(); |
492 |
> |
assertEquals(i, q.peek()); |
493 |
> |
assertEquals(i, q.poll()); |
494 |
|
assertTrue(q.peek() == null || |
495 |
< |
i != ((Integer)q.peek()).intValue()); |
495 |
> |
!q.peek().equals(i)); |
496 |
|
} |
497 |
|
assertNull(q.peek()); |
498 |
|
} |
503 |
|
public void testElement() { |
504 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
505 |
|
for (int i = 0; i < SIZE; ++i) { |
506 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
507 |
< |
q.poll(); |
506 |
> |
assertEquals(i, q.element()); |
507 |
> |
assertEquals(i, q.poll()); |
508 |
|
} |
509 |
|
try { |
510 |
|
q.element(); |
518 |
|
public void testRemove() { |
519 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
520 |
|
for (int i = 0; i < SIZE; ++i) { |
521 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
521 |
> |
assertEquals(i, q.remove()); |
522 |
|
} |
523 |
|
try { |
524 |
|
q.remove(); |
656 |
|
* toArray(null) throws NPE |
657 |
|
*/ |
658 |
|
public void testToArray_BadArg() { |
659 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
660 |
|
try { |
651 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
661 |
|
Object o[] = q.toArray(null); |
662 |
|
shouldThrow(); |
663 |
|
} catch (NullPointerException success) {} |
667 |
|
* toArray with incompatible array type throws CCE |
668 |
|
*/ |
669 |
|
public void testToArray1_BadArg() { |
670 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
671 |
|
try { |
672 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
663 |
< |
Object o[] = q.toArray(new String[10] ); |
672 |
> |
Object o[] = q.toArray(new String[10]); |
673 |
|
shouldThrow(); |
674 |
|
} catch (ArrayStoreException success) {} |
675 |
|
} |
689 |
|
/** |
690 |
|
* iterator.remove removes current element |
691 |
|
*/ |
692 |
< |
public void testIteratorRemove () { |
692 |
> |
public void testIteratorRemove() { |
693 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
694 |
|
q.add(two); |
695 |
|
q.add(one); |
700 |
|
it.remove(); |
701 |
|
|
702 |
|
it = q.iterator(); |
703 |
< |
assertEquals(it.next(), one); |
704 |
< |
assertEquals(it.next(), three); |
703 |
> |
assertSame(it.next(), one); |
704 |
> |
assertSame(it.next(), three); |
705 |
|
assertFalse(it.hasNext()); |
706 |
|
} |
707 |
|
|
717 |
|
assertEquals(0, q.remainingCapacity()); |
718 |
|
int k = 0; |
719 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
< |
int i = ((Integer)(it.next())).intValue(); |
712 |
< |
assertEquals(++k, i); |
720 |
> |
assertEquals(++k, it.next()); |
721 |
|
} |
722 |
|
assertEquals(3, k); |
723 |
|
} |
725 |
|
/** |
726 |
|
* Modifications do not cause iterators to fail |
727 |
|
*/ |
728 |
< |
public void testWeaklyConsistentIteration () { |
728 |
> |
public void testWeaklyConsistentIteration() { |
729 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
730 |
|
q.add(one); |
731 |
|
q.add(two); |
760 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
761 |
|
executor.execute(new CheckedRunnable() { |
762 |
|
public void realRun() throws InterruptedException { |
763 |
< |
threadAssertFalse(q.offer(three)); |
764 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
763 |
> |
assertFalse(q.offer(three)); |
764 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertEquals(0, q.remainingCapacity()); |
766 |
|
}}); |
767 |
|
|
768 |
|
executor.execute(new CheckedRunnable() { |
769 |
|
public void realRun() throws InterruptedException { |
770 |
|
Thread.sleep(SMALL_DELAY_MS); |
771 |
< |
threadAssertEquals(one, q.take()); |
771 |
> |
assertSame(one, q.take()); |
772 |
|
}}); |
773 |
|
|
774 |
|
joinPool(executor); |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
|
executor.execute(new CheckedRunnable() { |
784 |
|
public void realRun() throws InterruptedException { |
785 |
< |
threadAssertNull(q.poll()); |
786 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
< |
threadAssertTrue(q.isEmpty()); |
785 |
> |
assertNull(q.poll()); |
786 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(q.isEmpty()); |
788 |
|
}}); |
789 |
|
|
790 |
|
executor.execute(new CheckedRunnable() { |