14 |
|
|
15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
+ |
public static class Unbounded extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new LinkedBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class Bounded extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new LinkedBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
34 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
35 |
> |
new Unbounded().testSuite(), |
36 |
> |
new Bounded().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
|
41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
45 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
44 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
LinkedBlockingQueue<Integer> q = |
46 |
> |
new LinkedBlockingQueue<Integer>(n); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
|
for (int i = 0; i < n; i++) |
49 |
|
assertTrue(q.offer(new Integer(i))); |
229 |
|
shouldThrow(); |
230 |
|
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
+ |
|
233 |
|
/** |
234 |
|
* addAll of a collection with any null elements throws NPE after |
235 |
|
* possibly adding some elements |
244 |
|
shouldThrow(); |
245 |
|
} catch (NullPointerException success) {} |
246 |
|
} |
247 |
+ |
|
248 |
|
/** |
249 |
|
* addAll throws ISE if not enough room |
250 |
|
*/ |
258 |
|
shouldThrow(); |
259 |
|
} catch (IllegalStateException success) {} |
260 |
|
} |
261 |
+ |
|
262 |
|
/** |
263 |
|
* Queue contains all elements, in traversal order, of successful addAll |
264 |
|
*/ |
302 |
|
* put blocks interruptibly if full |
303 |
|
*/ |
304 |
|
public void testBlockingPut() throws InterruptedException { |
305 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
306 |
|
Thread t = new Thread(new CheckedRunnable() { |
307 |
< |
public void realRun() { |
308 |
< |
int added = 0; |
307 |
> |
public void realRun() throws InterruptedException { |
308 |
> |
for (int i = 0; i < SIZE; ++i) |
309 |
> |
q.put(i); |
310 |
> |
assertEquals(SIZE, q.size()); |
311 |
> |
assertEquals(0, q.remainingCapacity()); |
312 |
|
try { |
313 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
314 |
< |
for (int i = 0; i < SIZE; ++i) { |
315 |
< |
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 |
< |
} |
313 |
> |
q.put(99); |
314 |
> |
shouldThrow(); |
315 |
> |
} catch (InterruptedException success) {} |
316 |
|
}}); |
317 |
|
|
318 |
|
t.start(); |
319 |
|
Thread.sleep(SHORT_DELAY_MS); |
320 |
|
t.interrupt(); |
321 |
|
t.join(); |
322 |
+ |
assertEquals(SIZE, q.size()); |
323 |
+ |
assertEquals(0, q.remainingCapacity()); |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
|
* put blocks waiting for take when full |
328 |
|
*/ |
329 |
|
public void testPutWithTake() throws InterruptedException { |
330 |
+ |
final int capacity = 2; |
331 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
332 |
|
Thread t = new Thread(new CheckedRunnable() { |
333 |
< |
public void realRun() { |
334 |
< |
int added = 0; |
333 |
> |
public void realRun() throws InterruptedException { |
334 |
> |
for (int i = 0; i < capacity + 1; i++) |
335 |
> |
q.put(i); |
336 |
|
try { |
337 |
< |
q.put(new Object()); |
338 |
< |
++added; |
339 |
< |
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 |
< |
} |
337 |
> |
q.put(99); |
338 |
> |
shouldThrow(); |
339 |
> |
} catch (InterruptedException success) {} |
340 |
|
}}); |
341 |
|
|
342 |
|
t.start(); |
343 |
|
Thread.sleep(SHORT_DELAY_MS); |
344 |
< |
q.take(); |
344 |
> |
assertEquals(q.remainingCapacity(), 0); |
345 |
> |
assertEquals(0, q.take()); |
346 |
> |
Thread.sleep(SHORT_DELAY_MS); |
347 |
|
t.interrupt(); |
348 |
|
t.join(); |
349 |
+ |
assertEquals(q.remainingCapacity(), 0); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
354 |
|
*/ |
355 |
|
public void testTimedOffer() throws InterruptedException { |
356 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
357 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
357 |
> |
Thread t = new Thread(new CheckedRunnable() { |
358 |
|
public void realRun() throws InterruptedException { |
359 |
|
q.put(new Object()); |
360 |
|
q.put(new Object()); |
361 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
362 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
363 |
< |
}}; |
361 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
362 |
> |
try { |
363 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
364 |
> |
shouldThrow(); |
365 |
> |
} catch (InterruptedException success) {} |
366 |
> |
}}); |
367 |
|
|
368 |
|
t.start(); |
369 |
|
Thread.sleep(SMALL_DELAY_MS); |
377 |
|
public void testTake() throws InterruptedException { |
378 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
379 |
|
for (int i = 0; i < SIZE; ++i) { |
380 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
380 |
> |
assertEquals(i, q.take()); |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
|
/** |
369 |
– |
* take blocks interruptibly when empty |
370 |
– |
*/ |
371 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
372 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
373 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
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 |
– |
} |
383 |
– |
|
384 |
– |
/** |
385 |
|
* Take removes existing elements until empty, then blocks interruptibly |
386 |
|
*/ |
387 |
|
public void testBlockingTake() throws InterruptedException { |
388 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
388 |
> |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
389 |
> |
Thread t = new Thread(new CheckedRunnable() { |
390 |
|
public void realRun() throws InterruptedException { |
390 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
391 |
|
for (int i = 0; i < SIZE; ++i) { |
392 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
392 |
> |
assertEquals(i, q.take()); |
393 |
|
} |
394 |
< |
q.take(); |
395 |
< |
}}; |
394 |
> |
try { |
395 |
> |
q.take(); |
396 |
> |
shouldThrow(); |
397 |
> |
} catch (InterruptedException success) {} |
398 |
> |
}}); |
399 |
|
|
400 |
|
t.start(); |
401 |
|
Thread.sleep(SHORT_DELAY_MS); |
403 |
|
t.join(); |
404 |
|
} |
405 |
|
|
403 |
– |
|
406 |
|
/** |
407 |
|
* poll succeeds unless empty |
408 |
|
*/ |
409 |
|
public void testPoll() { |
410 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
411 |
|
for (int i = 0; i < SIZE; ++i) { |
412 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
412 |
> |
assertEquals(i, q.poll()); |
413 |
|
} |
414 |
|
assertNull(q.poll()); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
418 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
419 |
|
*/ |
420 |
|
public void testTimedPoll0() throws InterruptedException { |
421 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
422 |
|
for (int i = 0; i < SIZE; ++i) { |
423 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
423 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
424 |
|
} |
425 |
|
assertNull(q.poll(0, MILLISECONDS)); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
429 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
430 |
|
*/ |
431 |
|
public void testTimedPoll() throws InterruptedException { |
432 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
433 |
|
for (int i = 0; i < SIZE; ++i) { |
434 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
434 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
435 |
|
} |
436 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
437 |
|
} |
445 |
|
public void realRun() throws InterruptedException { |
446 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
447 |
|
for (int i = 0; i < SIZE; ++i) { |
448 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
448 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
|
} |
450 |
|
try { |
451 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
461 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
462 |
– |
* on interruption throws |
463 |
– |
*/ |
464 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
465 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
466 |
– |
Thread t = new Thread(new CheckedRunnable() { |
467 |
– |
public void realRun() throws InterruptedException { |
468 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
469 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
470 |
– |
try { |
471 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
472 |
– |
shouldThrow(); |
473 |
– |
} catch (InterruptedException success) {} |
474 |
– |
}}); |
475 |
– |
|
476 |
– |
t.start(); |
477 |
– |
Thread.sleep(SMALL_DELAY_MS); |
478 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
479 |
– |
t.interrupt(); |
480 |
– |
t.join(); |
481 |
– |
} |
482 |
– |
|
483 |
– |
/** |
463 |
|
* peek returns next element, or null if empty |
464 |
|
*/ |
465 |
|
public void testPeek() { |
466 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
467 |
|
for (int i = 0; i < SIZE; ++i) { |
468 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
469 |
< |
q.poll(); |
468 |
> |
assertEquals(i, q.peek()); |
469 |
> |
assertEquals(i, q.poll()); |
470 |
|
assertTrue(q.peek() == null || |
471 |
< |
i != ((Integer)q.peek()).intValue()); |
471 |
> |
!q.peek().equals(i)); |
472 |
|
} |
473 |
|
assertNull(q.peek()); |
474 |
|
} |
479 |
|
public void testElement() { |
480 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
483 |
< |
q.poll(); |
482 |
> |
assertEquals(i, q.element()); |
483 |
> |
assertEquals(i, q.poll()); |
484 |
|
} |
485 |
|
try { |
486 |
|
q.element(); |
494 |
|
public void testRemove() { |
495 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
497 |
> |
assertEquals(i, q.remove()); |
498 |
|
} |
499 |
|
try { |
500 |
|
q.remove(); |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
< |
* toArray contains all elements |
611 |
> |
* toArray contains all elements in FIFO order |
612 |
|
*/ |
613 |
< |
public void testToArray() throws InterruptedException { |
613 |
> |
public void testToArray() { |
614 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
615 |
|
Object[] o = q.toArray(); |
616 |
|
for (int i = 0; i < o.length; i++) |
617 |
< |
assertEquals(o[i], q.take()); |
617 |
> |
assertSame(o[i], q.poll()); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* toArray(a) contains all elements |
621 |
> |
* toArray(a) contains all elements in FIFO order |
622 |
|
*/ |
623 |
|
public void testToArray2() throws InterruptedException { |
624 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
624 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
625 |
|
Integer[] ints = new Integer[SIZE]; |
626 |
< |
ints = (Integer[])q.toArray(ints); |
626 |
> |
Integer[] array = q.toArray(ints); |
627 |
> |
assertSame(ints, array); |
628 |
|
for (int i = 0; i < ints.length; i++) |
629 |
< |
assertEquals(ints[i], q.take()); |
629 |
> |
assertSame(ints[i], q.poll()); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
< |
* toArray(null) throws NPE |
633 |
> |
* toArray(null) throws NullPointerException |
634 |
|
*/ |
635 |
< |
public void testToArray_BadArg() { |
635 |
> |
public void testToArray_NullArg() { |
636 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
637 |
|
try { |
638 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
658 |
< |
Object o[] = q.toArray(null); |
638 |
> |
q.toArray(null); |
639 |
|
shouldThrow(); |
640 |
|
} catch (NullPointerException success) {} |
641 |
|
} |
642 |
|
|
643 |
|
/** |
644 |
< |
* toArray with incompatible array type throws CCE |
644 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
645 |
|
*/ |
646 |
|
public void testToArray1_BadArg() { |
647 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
648 |
|
try { |
649 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
669 |
< |
Object o[] = q.toArray(new String[10] ); |
649 |
> |
q.toArray(new String[10]); |
650 |
|
shouldThrow(); |
651 |
|
} catch (ArrayStoreException success) {} |
652 |
|
} |
666 |
|
/** |
667 |
|
* iterator.remove removes current element |
668 |
|
*/ |
669 |
< |
public void testIteratorRemove () { |
669 |
> |
public void testIteratorRemove() { |
670 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
671 |
|
q.add(two); |
672 |
|
q.add(one); |
677 |
|
it.remove(); |
678 |
|
|
679 |
|
it = q.iterator(); |
680 |
< |
assertEquals(it.next(), one); |
681 |
< |
assertEquals(it.next(), three); |
680 |
> |
assertSame(it.next(), one); |
681 |
> |
assertSame(it.next(), three); |
682 |
|
assertFalse(it.hasNext()); |
683 |
|
} |
684 |
|
|
694 |
|
assertEquals(0, q.remainingCapacity()); |
695 |
|
int k = 0; |
696 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
697 |
< |
int i = ((Integer)(it.next())).intValue(); |
718 |
< |
assertEquals(++k, i); |
697 |
> |
assertEquals(++k, it.next()); |
698 |
|
} |
699 |
|
assertEquals(3, k); |
700 |
|
} |
702 |
|
/** |
703 |
|
* Modifications do not cause iterators to fail |
704 |
|
*/ |
705 |
< |
public void testWeaklyConsistentIteration () { |
705 |
> |
public void testWeaklyConsistentIteration() { |
706 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
707 |
|
q.add(one); |
708 |
|
q.add(two); |
737 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
738 |
|
executor.execute(new CheckedRunnable() { |
739 |
|
public void realRun() throws InterruptedException { |
740 |
< |
threadAssertFalse(q.offer(three)); |
741 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
742 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
740 |
> |
assertFalse(q.offer(three)); |
741 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
742 |
> |
assertEquals(0, q.remainingCapacity()); |
743 |
|
}}); |
744 |
|
|
745 |
|
executor.execute(new CheckedRunnable() { |
746 |
|
public void realRun() throws InterruptedException { |
747 |
|
Thread.sleep(SMALL_DELAY_MS); |
748 |
< |
threadAssertEquals(one, q.take()); |
748 |
> |
assertSame(one, q.take()); |
749 |
|
}}); |
750 |
|
|
751 |
|
joinPool(executor); |
759 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
760 |
|
executor.execute(new CheckedRunnable() { |
761 |
|
public void realRun() throws InterruptedException { |
762 |
< |
threadAssertNull(q.poll()); |
763 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
764 |
< |
threadAssertTrue(q.isEmpty()); |
762 |
> |
assertNull(q.poll()); |
763 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
764 |
> |
assertTrue(q.isEmpty()); |
765 |
|
}}); |
766 |
|
|
767 |
|
executor.execute(new CheckedRunnable() { |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
884 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
885 |
|
*/ |
886 |
|
public void testDrainToN() { |
887 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
890 |
|
assertTrue(q.offer(new Integer(j))); |
891 |
|
ArrayList l = new ArrayList(); |
892 |
|
q.drainTo(l, i); |
893 |
< |
int k = (i < SIZE)? i : SIZE; |
893 |
> |
int k = (i < SIZE) ? i : SIZE; |
894 |
|
assertEquals(l.size(), k); |
895 |
|
assertEquals(q.size(), SIZE-k); |
896 |
|
for (int j = 0; j < k; ++j) |