71 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
72 |
|
q.offerFirst(null); |
73 |
|
shouldThrow(); |
74 |
< |
} catch (NullPointerException success) { |
75 |
< |
} |
74 |
> |
} catch (NullPointerException success) {} |
75 |
|
} |
76 |
|
|
77 |
|
/** |
168 |
|
try { |
169 |
|
q.getFirst(); |
170 |
|
shouldThrow(); |
171 |
< |
} |
173 |
< |
catch (NoSuchElementException success) {} |
171 |
> |
} catch (NoSuchElementException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
183 |
|
try { |
184 |
|
q.getLast(); |
185 |
|
shouldThrow(); |
186 |
< |
} |
189 |
< |
catch (NoSuchElementException success) {} |
186 |
> |
} catch (NoSuchElementException success) {} |
187 |
|
assertNull(q.peekLast()); |
188 |
|
} |
189 |
|
|
198 |
|
try { |
199 |
|
q.removeFirst(); |
200 |
|
shouldThrow(); |
201 |
< |
} catch (NoSuchElementException success) { |
205 |
< |
} |
201 |
> |
} catch (NoSuchElementException success) {} |
202 |
|
} |
203 |
|
|
204 |
|
/** |
212 |
|
try { |
213 |
|
q.remove(); |
214 |
|
shouldThrow(); |
215 |
< |
} catch (NoSuchElementException success) { |
220 |
< |
} |
215 |
> |
} catch (NoSuchElementException success) {} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
< |
* Constructor throws IAE if capacity argument nonpositive |
279 |
> |
* Constructor throws IAE if capacity argument nonpositive |
280 |
|
*/ |
281 |
|
public void testConstructor2() { |
282 |
|
try { |
283 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
284 |
|
shouldThrow(); |
285 |
< |
} |
291 |
< |
catch (IllegalArgumentException success) {} |
285 |
> |
} catch (IllegalArgumentException success) {} |
286 |
|
} |
287 |
|
|
288 |
|
/** |
292 |
|
try { |
293 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
294 |
|
shouldThrow(); |
295 |
< |
} |
302 |
< |
catch (NullPointerException success) {} |
295 |
> |
} catch (NullPointerException success) {} |
296 |
|
} |
297 |
|
|
298 |
|
/** |
303 |
|
Integer[] ints = new Integer[SIZE]; |
304 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
305 |
|
shouldThrow(); |
306 |
< |
} |
314 |
< |
catch (NullPointerException success) {} |
306 |
> |
} catch (NullPointerException success) {} |
307 |
|
} |
308 |
|
|
309 |
|
/** |
316 |
|
ints[i] = new Integer(i); |
317 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
318 |
|
shouldThrow(); |
319 |
< |
} |
328 |
< |
catch (NullPointerException success) {} |
319 |
> |
} catch (NullPointerException success) {} |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
|
* Deque contains all elements of collection used to initialize |
324 |
|
*/ |
325 |
|
public void testConstructor6() { |
326 |
< |
try { |
327 |
< |
Integer[] ints = new Integer[SIZE]; |
328 |
< |
for (int i = 0; i < SIZE; ++i) |
329 |
< |
ints[i] = new Integer(i); |
330 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
331 |
< |
for (int i = 0; i < SIZE; ++i) |
341 |
< |
assertEquals(ints[i], q.poll()); |
342 |
< |
} |
343 |
< |
finally {} |
326 |
> |
Integer[] ints = new Integer[SIZE]; |
327 |
> |
for (int i = 0; i < SIZE; ++i) |
328 |
> |
ints[i] = new Integer(i); |
329 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
330 |
> |
for (int i = 0; i < SIZE; ++i) |
331 |
> |
assertEquals(ints[i], q.poll()); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
371 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
372 |
|
q.offer(null); |
373 |
|
shouldThrow(); |
374 |
< |
} catch (NullPointerException success) { } |
374 |
> |
} catch (NullPointerException success) {} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
382 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
383 |
|
q.add(null); |
384 |
|
shouldThrow(); |
385 |
< |
} catch (NullPointerException success) { } |
385 |
> |
} catch (NullPointerException success) {} |
386 |
|
} |
387 |
|
|
388 |
|
/** |
393 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
394 |
|
q.push(null); |
395 |
|
shouldThrow(); |
396 |
< |
} catch (NullPointerException success) { } |
396 |
> |
} catch (NullPointerException success) {} |
397 |
|
} |
398 |
|
|
399 |
|
/** |
409 |
|
} |
410 |
|
assertEquals(0, q.remainingCapacity()); |
411 |
|
q.push(new Integer(SIZE)); |
412 |
< |
} catch (IllegalStateException success) { |
413 |
< |
} |
412 |
> |
shouldThrow(); |
413 |
> |
} catch (IllegalStateException success) {} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
435 |
|
try { |
436 |
|
q.pop(); |
437 |
|
shouldThrow(); |
438 |
< |
} catch (NoSuchElementException success) { |
451 |
< |
} |
438 |
> |
} catch (NoSuchElementException success) {} |
439 |
|
} |
440 |
|
|
441 |
|
|
459 |
|
} |
460 |
|
assertEquals(0, q.remainingCapacity()); |
461 |
|
q.add(new Integer(SIZE)); |
462 |
< |
} catch (IllegalStateException success) { |
463 |
< |
} |
462 |
> |
shouldThrow(); |
463 |
> |
} catch (IllegalStateException success) {} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
471 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
472 |
|
q.addAll(null); |
473 |
|
shouldThrow(); |
474 |
< |
} |
488 |
< |
catch (NullPointerException success) {} |
474 |
> |
} catch (NullPointerException success) {} |
475 |
|
} |
476 |
|
|
477 |
|
/** |
482 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
483 |
|
q.addAll(q); |
484 |
|
shouldThrow(); |
485 |
< |
} |
500 |
< |
catch (IllegalArgumentException success) {} |
485 |
> |
} catch (IllegalArgumentException success) {} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
494 |
|
Integer[] ints = new Integer[SIZE]; |
495 |
|
q.addAll(Arrays.asList(ints)); |
496 |
|
shouldThrow(); |
497 |
< |
} |
513 |
< |
catch (NullPointerException success) {} |
497 |
> |
} catch (NullPointerException success) {} |
498 |
|
} |
499 |
|
/** |
500 |
|
* addAll of a collection with any null elements throws NPE after |
508 |
|
ints[i] = new Integer(i); |
509 |
|
q.addAll(Arrays.asList(ints)); |
510 |
|
shouldThrow(); |
511 |
< |
} |
528 |
< |
catch (NullPointerException success) {} |
511 |
> |
} catch (NullPointerException success) {} |
512 |
|
} |
513 |
|
/** |
514 |
|
* addAll throws ISE if not enough room |
521 |
|
ints[i] = new Integer(i); |
522 |
|
q.addAll(Arrays.asList(ints)); |
523 |
|
shouldThrow(); |
524 |
< |
} |
542 |
< |
catch (IllegalStateException success) {} |
524 |
> |
} catch (IllegalStateException success) {} |
525 |
|
} |
526 |
|
/** |
527 |
|
* Deque contains all elements, in traversal order, of successful addAll |
528 |
|
*/ |
529 |
|
public void testAddAll5() { |
530 |
< |
try { |
531 |
< |
Integer[] empty = new Integer[0]; |
532 |
< |
Integer[] ints = new Integer[SIZE]; |
533 |
< |
for (int i = 0; i < SIZE; ++i) |
534 |
< |
ints[i] = new Integer(i); |
535 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
536 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
537 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
538 |
< |
for (int i = 0; i < SIZE; ++i) |
557 |
< |
assertEquals(ints[i], q.poll()); |
558 |
< |
} |
559 |
< |
finally {} |
530 |
> |
Integer[] empty = new Integer[0]; |
531 |
> |
Integer[] ints = new Integer[SIZE]; |
532 |
> |
for (int i = 0; i < SIZE; ++i) |
533 |
> |
ints[i] = new Integer(i); |
534 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
535 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
536 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
537 |
> |
for (int i = 0; i < SIZE; ++i) |
538 |
> |
assertEquals(ints[i], q.poll()); |
539 |
|
} |
540 |
|
|
541 |
|
|
542 |
|
/** |
543 |
|
* put(null) throws NPE |
544 |
|
*/ |
545 |
< |
public void testPutNull() { |
545 |
> |
public void testPutNull() throws InterruptedException { |
546 |
|
try { |
547 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
548 |
|
q.put(null); |
549 |
|
shouldThrow(); |
550 |
< |
} |
551 |
< |
catch (NullPointerException success) { |
573 |
< |
} |
574 |
< |
catch (InterruptedException ie) { |
575 |
< |
unexpectedException(); |
576 |
< |
} |
577 |
< |
} |
550 |
> |
} catch (NullPointerException success) {} |
551 |
> |
} |
552 |
|
|
553 |
|
/** |
554 |
|
* all elements successfully put are contained |
555 |
|
*/ |
556 |
< |
public void testPut() { |
557 |
< |
try { |
558 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
559 |
< |
for (int i = 0; i < SIZE; ++i) { |
560 |
< |
Integer I = new Integer(i); |
561 |
< |
q.put(I); |
588 |
< |
assertTrue(q.contains(I)); |
589 |
< |
} |
590 |
< |
assertEquals(0, q.remainingCapacity()); |
591 |
< |
} |
592 |
< |
catch (InterruptedException ie) { |
593 |
< |
unexpectedException(); |
556 |
> |
public void testPut() throws InterruptedException { |
557 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
558 |
> |
for (int i = 0; i < SIZE; ++i) { |
559 |
> |
Integer I = new Integer(i); |
560 |
> |
q.put(I); |
561 |
> |
assertTrue(q.contains(I)); |
562 |
|
} |
563 |
+ |
assertEquals(0, q.remainingCapacity()); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
|
* put blocks interruptibly if full |
568 |
|
*/ |
569 |
< |
public void testBlockingPut() { |
570 |
< |
Thread t = new Thread(new Runnable() { |
571 |
< |
public void run() { |
572 |
< |
int added = 0; |
573 |
< |
try { |
574 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
575 |
< |
for (int i = 0; i < SIZE; ++i) { |
576 |
< |
q.put(new Integer(i)); |
577 |
< |
++added; |
609 |
< |
} |
610 |
< |
q.put(new Integer(SIZE)); |
611 |
< |
threadShouldThrow(); |
612 |
< |
} catch (InterruptedException ie) { |
613 |
< |
threadAssertEquals(added, SIZE); |
569 |
> |
public void testBlockingPut() throws InterruptedException { |
570 |
> |
Thread t = new Thread(new CheckedRunnable() { |
571 |
> |
public void realRun() { |
572 |
> |
int added = 0; |
573 |
> |
try { |
574 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
575 |
> |
for (int i = 0; i < SIZE; ++i) { |
576 |
> |
q.put(new Integer(i)); |
577 |
> |
++added; |
578 |
|
} |
579 |
< |
}}); |
579 |
> |
q.put(new Integer(SIZE)); |
580 |
> |
threadShouldThrow(); |
581 |
> |
} catch (InterruptedException success) { |
582 |
> |
threadAssertEquals(added, SIZE); |
583 |
> |
} |
584 |
> |
}}); |
585 |
> |
|
586 |
|
t.start(); |
587 |
< |
try { |
588 |
< |
Thread.sleep(SHORT_DELAY_MS); |
589 |
< |
t.interrupt(); |
620 |
< |
t.join(); |
621 |
< |
} |
622 |
< |
catch (InterruptedException ie) { |
623 |
< |
unexpectedException(); |
624 |
< |
} |
587 |
> |
Thread.sleep(SHORT_DELAY_MS); |
588 |
> |
t.interrupt(); |
589 |
> |
t.join(); |
590 |
|
} |
591 |
|
|
592 |
|
/** |
593 |
|
* put blocks waiting for take when full |
594 |
|
*/ |
595 |
< |
public void testPutWithTake() { |
595 |
> |
public void testPutWithTake() throws InterruptedException { |
596 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
597 |
< |
Thread t = new Thread(new Runnable() { |
598 |
< |
public void run() { |
599 |
< |
int added = 0; |
600 |
< |
try { |
601 |
< |
q.put(new Object()); |
602 |
< |
++added; |
603 |
< |
q.put(new Object()); |
604 |
< |
++added; |
605 |
< |
q.put(new Object()); |
606 |
< |
++added; |
607 |
< |
q.put(new Object()); |
608 |
< |
++added; |
609 |
< |
threadShouldThrow(); |
610 |
< |
} catch (InterruptedException e) { |
611 |
< |
threadAssertTrue(added >= 2); |
647 |
< |
} |
597 |
> |
Thread t = new Thread(new CheckedRunnable() { |
598 |
> |
public void realRun() { |
599 |
> |
int added = 0; |
600 |
> |
try { |
601 |
> |
q.put(new Object()); |
602 |
> |
++added; |
603 |
> |
q.put(new Object()); |
604 |
> |
++added; |
605 |
> |
q.put(new Object()); |
606 |
> |
++added; |
607 |
> |
q.put(new Object()); |
608 |
> |
++added; |
609 |
> |
threadShouldThrow(); |
610 |
> |
} catch (InterruptedException success) { |
611 |
> |
threadAssertTrue(added >= 2); |
612 |
|
} |
613 |
< |
}); |
614 |
< |
try { |
615 |
< |
t.start(); |
616 |
< |
Thread.sleep(SHORT_DELAY_MS); |
617 |
< |
q.take(); |
618 |
< |
t.interrupt(); |
619 |
< |
t.join(); |
656 |
< |
} catch (Exception e) { |
657 |
< |
unexpectedException(); |
658 |
< |
} |
613 |
> |
}}); |
614 |
> |
|
615 |
> |
t.start(); |
616 |
> |
Thread.sleep(SHORT_DELAY_MS); |
617 |
> |
q.take(); |
618 |
> |
t.interrupt(); |
619 |
> |
t.join(); |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
|
* timed offer times out if full and elements not taken |
624 |
|
*/ |
625 |
< |
public void testTimedOffer() { |
625 |
> |
public void testTimedOffer() throws InterruptedException { |
626 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
627 |
< |
Thread t = new Thread(new Runnable() { |
628 |
< |
public void run() { |
629 |
< |
try { |
630 |
< |
q.put(new Object()); |
631 |
< |
q.put(new Object()); |
632 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
633 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
673 |
< |
threadShouldThrow(); |
674 |
< |
} catch (InterruptedException success) {} |
675 |
< |
} |
676 |
< |
}); |
627 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
628 |
> |
public void realRun() throws InterruptedException { |
629 |
> |
q.put(new Object()); |
630 |
> |
q.put(new Object()); |
631 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
632 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
633 |
> |
}}; |
634 |
|
|
635 |
< |
try { |
636 |
< |
t.start(); |
637 |
< |
Thread.sleep(SMALL_DELAY_MS); |
638 |
< |
t.interrupt(); |
682 |
< |
t.join(); |
683 |
< |
} catch (Exception e) { |
684 |
< |
unexpectedException(); |
685 |
< |
} |
635 |
> |
t.start(); |
636 |
> |
Thread.sleep(SMALL_DELAY_MS); |
637 |
> |
t.interrupt(); |
638 |
> |
t.join(); |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
|
* take retrieves elements in FIFO order |
643 |
|
*/ |
644 |
< |
public void testTake() { |
645 |
< |
try { |
646 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
647 |
< |
for (int i = 0; i < SIZE; ++i) { |
695 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
696 |
< |
} |
697 |
< |
} catch (InterruptedException e) { |
698 |
< |
unexpectedException(); |
644 |
> |
public void testTake() throws InterruptedException { |
645 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
646 |
> |
for (int i = 0; i < SIZE; ++i) { |
647 |
> |
assertEquals(i, ((Integer)q.take()).intValue()); |
648 |
|
} |
649 |
|
} |
650 |
|
|
651 |
|
/** |
652 |
|
* take blocks interruptibly when empty |
653 |
|
*/ |
654 |
< |
public void testTakeFromEmpty() { |
654 |
> |
public void testTakeFromEmpty() throws InterruptedException { |
655 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
656 |
< |
Thread t = new Thread(new Runnable() { |
657 |
< |
public void run() { |
658 |
< |
try { |
659 |
< |
q.take(); |
660 |
< |
threadShouldThrow(); |
661 |
< |
} catch (InterruptedException success) { } |
662 |
< |
} |
663 |
< |
}); |
664 |
< |
try { |
716 |
< |
t.start(); |
717 |
< |
Thread.sleep(SHORT_DELAY_MS); |
718 |
< |
t.interrupt(); |
719 |
< |
t.join(); |
720 |
< |
} catch (Exception e) { |
721 |
< |
unexpectedException(); |
722 |
< |
} |
656 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
657 |
> |
public void realRun() throws InterruptedException { |
658 |
> |
q.take(); |
659 |
> |
}}; |
660 |
> |
|
661 |
> |
t.start(); |
662 |
> |
Thread.sleep(SHORT_DELAY_MS); |
663 |
> |
t.interrupt(); |
664 |
> |
t.join(); |
665 |
|
} |
666 |
|
|
667 |
|
/** |
668 |
|
* Take removes existing elements until empty, then blocks interruptibly |
669 |
|
*/ |
670 |
< |
public void testBlockingTake() { |
671 |
< |
Thread t = new Thread(new Runnable() { |
672 |
< |
public void run() { |
673 |
< |
try { |
674 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
675 |
< |
for (int i = 0; i < SIZE; ++i) { |
676 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
677 |
< |
} |
678 |
< |
q.take(); |
679 |
< |
threadShouldThrow(); |
738 |
< |
} catch (InterruptedException success) { |
739 |
< |
} |
740 |
< |
}}); |
670 |
> |
public void testBlockingTake() throws InterruptedException { |
671 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
672 |
> |
public void realRun() throws InterruptedException { |
673 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
674 |
> |
for (int i = 0; i < SIZE; ++i) { |
675 |
> |
assertEquals(i, ((Integer)q.take()).intValue()); |
676 |
> |
} |
677 |
> |
q.take(); |
678 |
> |
}}; |
679 |
> |
|
680 |
|
t.start(); |
681 |
< |
try { |
682 |
< |
Thread.sleep(SHORT_DELAY_MS); |
683 |
< |
t.interrupt(); |
745 |
< |
t.join(); |
746 |
< |
} |
747 |
< |
catch (InterruptedException ie) { |
748 |
< |
unexpectedException(); |
749 |
< |
} |
681 |
> |
Thread.sleep(SHORT_DELAY_MS); |
682 |
> |
t.interrupt(); |
683 |
> |
t.join(); |
684 |
|
} |
685 |
|
|
686 |
|
|
698 |
|
/** |
699 |
|
* timed poll with zero timeout succeeds when non-empty, else times out |
700 |
|
*/ |
701 |
< |
public void testTimedPoll0() { |
702 |
< |
try { |
703 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
704 |
< |
for (int i = 0; i < SIZE; ++i) { |
771 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
772 |
< |
} |
773 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
774 |
< |
} catch (InterruptedException e) { |
775 |
< |
unexpectedException(); |
701 |
> |
public void testTimedPoll0() throws InterruptedException { |
702 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
703 |
> |
for (int i = 0; i < SIZE; ++i) { |
704 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
705 |
|
} |
706 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
711 |
|
*/ |
712 |
< |
public void testTimedPoll() { |
713 |
< |
try { |
714 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
715 |
< |
for (int i = 0; i < SIZE; ++i) { |
786 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
787 |
< |
} |
788 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
789 |
< |
} catch (InterruptedException e) { |
790 |
< |
unexpectedException(); |
712 |
> |
public void testTimedPoll() throws InterruptedException { |
713 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
714 |
> |
for (int i = 0; i < SIZE; ++i) { |
715 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
716 |
|
} |
717 |
+ |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
718 |
|
} |
719 |
|
|
720 |
|
/** |
721 |
|
* Interrupted timed poll throws InterruptedException instead of |
722 |
|
* returning timeout status |
723 |
|
*/ |
724 |
< |
public void testInterruptedTimedPoll() { |
725 |
< |
Thread t = new Thread(new Runnable() { |
726 |
< |
public void run() { |
727 |
< |
try { |
728 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
729 |
< |
for (int i = 0; i < SIZE; ++i) { |
730 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
731 |
< |
} |
732 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
733 |
< |
} catch (InterruptedException success) { |
808 |
< |
} |
809 |
< |
}}); |
724 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
725 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
726 |
> |
public void realRun() throws InterruptedException { |
727 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
728 |
> |
for (int i = 0; i < SIZE; ++i) { |
729 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
730 |
> |
} |
731 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
732 |
> |
}}; |
733 |
> |
|
734 |
|
t.start(); |
735 |
< |
try { |
736 |
< |
Thread.sleep(SHORT_DELAY_MS); |
737 |
< |
t.interrupt(); |
814 |
< |
t.join(); |
815 |
< |
} |
816 |
< |
catch (InterruptedException ie) { |
817 |
< |
unexpectedException(); |
818 |
< |
} |
735 |
> |
Thread.sleep(SHORT_DELAY_MS); |
736 |
> |
t.interrupt(); |
737 |
> |
t.join(); |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
|
* timed poll before a delayed offer fails; after offer succeeds; |
742 |
|
* on interruption throws |
743 |
|
*/ |
744 |
< |
public void testTimedPollWithOffer() { |
744 |
> |
public void testTimedPollWithOffer() throws InterruptedException { |
745 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
746 |
< |
Thread t = new Thread(new Runnable() { |
747 |
< |
public void run() { |
748 |
< |
try { |
749 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
750 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
751 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
752 |
< |
threadShouldThrow(); |
753 |
< |
} catch (InterruptedException success) { } |
754 |
< |
} |
755 |
< |
}); |
756 |
< |
try { |
757 |
< |
t.start(); |
839 |
< |
Thread.sleep(SMALL_DELAY_MS); |
840 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
841 |
< |
t.interrupt(); |
842 |
< |
t.join(); |
843 |
< |
} catch (Exception e) { |
844 |
< |
unexpectedException(); |
845 |
< |
} |
746 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
747 |
> |
public void realRun() throws InterruptedException { |
748 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
749 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
750 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
751 |
> |
}}; |
752 |
> |
|
753 |
> |
t.start(); |
754 |
> |
Thread.sleep(SMALL_DELAY_MS); |
755 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
756 |
> |
t.interrupt(); |
757 |
> |
t.join(); |
758 |
|
} |
759 |
|
|
760 |
|
|
761 |
|
/** |
762 |
|
* putFirst(null) throws NPE |
763 |
|
*/ |
764 |
< |
public void testPutFirstNull() { |
764 |
> |
public void testPutFirstNull() throws InterruptedException { |
765 |
|
try { |
766 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
767 |
|
q.putFirst(null); |
768 |
|
shouldThrow(); |
769 |
< |
} |
858 |
< |
catch (NullPointerException success) { |
859 |
< |
} |
860 |
< |
catch (InterruptedException ie) { |
861 |
< |
unexpectedException(); |
862 |
< |
} |
769 |
> |
} catch (NullPointerException success) {} |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
|
* all elements successfully putFirst are contained |
774 |
|
*/ |
775 |
< |
public void testPutFirst() { |
776 |
< |
try { |
777 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
< |
for (int i = 0; i < SIZE; ++i) { |
779 |
< |
Integer I = new Integer(i); |
780 |
< |
q.putFirst(I); |
874 |
< |
assertTrue(q.contains(I)); |
875 |
< |
} |
876 |
< |
assertEquals(0, q.remainingCapacity()); |
775 |
> |
public void testPutFirst() throws InterruptedException { |
776 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
777 |
> |
for (int i = 0; i < SIZE; ++i) { |
778 |
> |
Integer I = new Integer(i); |
779 |
> |
q.putFirst(I); |
780 |
> |
assertTrue(q.contains(I)); |
781 |
|
} |
782 |
< |
catch (InterruptedException ie) { |
879 |
< |
unexpectedException(); |
880 |
< |
} |
782 |
> |
assertEquals(0, q.remainingCapacity()); |
783 |
|
} |
784 |
|
|
785 |
|
/** |
786 |
|
* putFirst blocks interruptibly if full |
787 |
|
*/ |
788 |
< |
public void testBlockingPutFirst() { |
789 |
< |
Thread t = new Thread(new Runnable() { |
790 |
< |
public void run() { |
791 |
< |
int added = 0; |
792 |
< |
try { |
793 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
794 |
< |
for (int i = 0; i < SIZE; ++i) { |
795 |
< |
q.putFirst(new Integer(i)); |
796 |
< |
++added; |
895 |
< |
} |
896 |
< |
q.putFirst(new Integer(SIZE)); |
897 |
< |
threadShouldThrow(); |
898 |
< |
} catch (InterruptedException ie) { |
899 |
< |
threadAssertEquals(added, SIZE); |
788 |
> |
public void testBlockingPutFirst() throws InterruptedException { |
789 |
> |
Thread t = new Thread(new CheckedRunnable() { |
790 |
> |
public void realRun() { |
791 |
> |
int added = 0; |
792 |
> |
try { |
793 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
794 |
> |
for (int i = 0; i < SIZE; ++i) { |
795 |
> |
q.putFirst(new Integer(i)); |
796 |
> |
++added; |
797 |
|
} |
798 |
< |
}}); |
798 |
> |
q.putFirst(new Integer(SIZE)); |
799 |
> |
threadShouldThrow(); |
800 |
> |
} catch (InterruptedException success) { |
801 |
> |
threadAssertEquals(added, SIZE); |
802 |
> |
} |
803 |
> |
}}); |
804 |
> |
|
805 |
|
t.start(); |
806 |
< |
try { |
807 |
< |
Thread.sleep(SHORT_DELAY_MS); |
808 |
< |
t.interrupt(); |
906 |
< |
t.join(); |
907 |
< |
} |
908 |
< |
catch (InterruptedException ie) { |
909 |
< |
unexpectedException(); |
910 |
< |
} |
806 |
> |
Thread.sleep(SHORT_DELAY_MS); |
807 |
> |
t.interrupt(); |
808 |
> |
t.join(); |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
|
* putFirst blocks waiting for take when full |
813 |
|
*/ |
814 |
< |
public void testPutFirstWithTake() { |
814 |
> |
public void testPutFirstWithTake() throws InterruptedException { |
815 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
816 |
< |
Thread t = new Thread(new Runnable() { |
817 |
< |
public void run() { |
818 |
< |
int added = 0; |
819 |
< |
try { |
820 |
< |
q.putFirst(new Object()); |
821 |
< |
++added; |
822 |
< |
q.putFirst(new Object()); |
823 |
< |
++added; |
824 |
< |
q.putFirst(new Object()); |
825 |
< |
++added; |
826 |
< |
q.putFirst(new Object()); |
827 |
< |
++added; |
828 |
< |
threadShouldThrow(); |
829 |
< |
} catch (InterruptedException e) { |
830 |
< |
threadAssertTrue(added >= 2); |
933 |
< |
} |
816 |
> |
Thread t = new Thread(new CheckedRunnable() { |
817 |
> |
public void realRun() { |
818 |
> |
int added = 0; |
819 |
> |
try { |
820 |
> |
q.putFirst(new Object()); |
821 |
> |
++added; |
822 |
> |
q.putFirst(new Object()); |
823 |
> |
++added; |
824 |
> |
q.putFirst(new Object()); |
825 |
> |
++added; |
826 |
> |
q.putFirst(new Object()); |
827 |
> |
++added; |
828 |
> |
threadShouldThrow(); |
829 |
> |
} catch (InterruptedException success) { |
830 |
> |
threadAssertTrue(added >= 2); |
831 |
|
} |
832 |
< |
}); |
833 |
< |
try { |
834 |
< |
t.start(); |
835 |
< |
Thread.sleep(SHORT_DELAY_MS); |
836 |
< |
q.take(); |
837 |
< |
t.interrupt(); |
838 |
< |
t.join(); |
942 |
< |
} catch (Exception e) { |
943 |
< |
unexpectedException(); |
944 |
< |
} |
832 |
> |
}}); |
833 |
> |
|
834 |
> |
t.start(); |
835 |
> |
Thread.sleep(SHORT_DELAY_MS); |
836 |
> |
q.take(); |
837 |
> |
t.interrupt(); |
838 |
> |
t.join(); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
842 |
|
* timed offerFirst times out if full and elements not taken |
843 |
|
*/ |
844 |
< |
public void testTimedOfferFirst() { |
844 |
> |
public void testTimedOfferFirst() throws InterruptedException { |
845 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
846 |
< |
Thread t = new Thread(new Runnable() { |
847 |
< |
public void run() { |
848 |
< |
try { |
849 |
< |
q.putFirst(new Object()); |
850 |
< |
q.putFirst(new Object()); |
851 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
852 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
959 |
< |
threadShouldThrow(); |
960 |
< |
} catch (InterruptedException success) {} |
961 |
< |
} |
962 |
< |
}); |
846 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
847 |
> |
public void realRun() throws InterruptedException { |
848 |
> |
q.putFirst(new Object()); |
849 |
> |
q.putFirst(new Object()); |
850 |
> |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
851 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
852 |
> |
}}; |
853 |
|
|
854 |
< |
try { |
855 |
< |
t.start(); |
856 |
< |
Thread.sleep(SMALL_DELAY_MS); |
857 |
< |
t.interrupt(); |
968 |
< |
t.join(); |
969 |
< |
} catch (Exception e) { |
970 |
< |
unexpectedException(); |
971 |
< |
} |
854 |
> |
t.start(); |
855 |
> |
Thread.sleep(SMALL_DELAY_MS); |
856 |
> |
t.interrupt(); |
857 |
> |
t.join(); |
858 |
|
} |
859 |
|
|
860 |
|
/** |
861 |
|
* take retrieves elements in FIFO order |
862 |
|
*/ |
863 |
< |
public void testTakeFirst() { |
864 |
< |
try { |
865 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
866 |
< |
for (int i = 0; i < SIZE; ++i) { |
981 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
982 |
< |
} |
983 |
< |
} catch (InterruptedException e) { |
984 |
< |
unexpectedException(); |
863 |
> |
public void testTakeFirst() throws InterruptedException { |
864 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
865 |
> |
for (int i = 0; i < SIZE; ++i) { |
866 |
> |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
867 |
|
} |
868 |
|
} |
869 |
|
|
870 |
|
/** |
871 |
|
* takeFirst blocks interruptibly when empty |
872 |
|
*/ |
873 |
< |
public void testTakeFirstFromEmpty() { |
873 |
> |
public void testTakeFirstFromEmpty() throws InterruptedException { |
874 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
875 |
< |
Thread t = new Thread(new Runnable() { |
876 |
< |
public void run() { |
877 |
< |
try { |
878 |
< |
q.takeFirst(); |
879 |
< |
threadShouldThrow(); |
880 |
< |
} catch (InterruptedException success) { } |
881 |
< |
} |
882 |
< |
}); |
883 |
< |
try { |
1002 |
< |
t.start(); |
1003 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1004 |
< |
t.interrupt(); |
1005 |
< |
t.join(); |
1006 |
< |
} catch (Exception e) { |
1007 |
< |
unexpectedException(); |
1008 |
< |
} |
875 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
876 |
> |
public void realRun() throws InterruptedException { |
877 |
> |
q.takeFirst(); |
878 |
> |
}}; |
879 |
> |
|
880 |
> |
t.start(); |
881 |
> |
Thread.sleep(SHORT_DELAY_MS); |
882 |
> |
t.interrupt(); |
883 |
> |
t.join(); |
884 |
|
} |
885 |
|
|
886 |
|
/** |
887 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
888 |
|
*/ |
889 |
< |
public void testBlockingTakeFirst() { |
890 |
< |
Thread t = new Thread(new Runnable() { |
891 |
< |
public void run() { |
892 |
< |
try { |
893 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
894 |
< |
for (int i = 0; i < SIZE; ++i) { |
895 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
896 |
< |
} |
897 |
< |
q.takeFirst(); |
898 |
< |
threadShouldThrow(); |
1024 |
< |
} catch (InterruptedException success) { |
1025 |
< |
} |
1026 |
< |
}}); |
889 |
> |
public void testBlockingTakeFirst() throws InterruptedException { |
890 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
891 |
> |
public void realRun() throws InterruptedException { |
892 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
893 |
> |
for (int i = 0; i < SIZE; ++i) { |
894 |
> |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
895 |
> |
} |
896 |
> |
q.takeFirst(); |
897 |
> |
}}; |
898 |
> |
|
899 |
|
t.start(); |
900 |
< |
try { |
901 |
< |
Thread.sleep(SHORT_DELAY_MS); |
902 |
< |
t.interrupt(); |
1031 |
< |
t.join(); |
1032 |
< |
} |
1033 |
< |
catch (InterruptedException ie) { |
1034 |
< |
unexpectedException(); |
1035 |
< |
} |
900 |
> |
Thread.sleep(SHORT_DELAY_MS); |
901 |
> |
t.interrupt(); |
902 |
> |
t.join(); |
903 |
|
} |
904 |
|
|
905 |
|
|
906 |
|
/** |
907 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
908 |
|
*/ |
909 |
< |
public void testTimedPollFirst0() { |
910 |
< |
try { |
911 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
912 |
< |
for (int i = 0; i < SIZE; ++i) { |
1046 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
1047 |
< |
} |
1048 |
< |
assertNull(q.pollFirst(0, MILLISECONDS)); |
1049 |
< |
} catch (InterruptedException e) { |
1050 |
< |
unexpectedException(); |
909 |
> |
public void testTimedPollFirst0() throws InterruptedException { |
910 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
911 |
> |
for (int i = 0; i < SIZE; ++i) { |
912 |
> |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
913 |
|
} |
914 |
+ |
assertNull(q.pollFirst(0, MILLISECONDS)); |
915 |
|
} |
916 |
|
|
917 |
|
/** |
918 |
|
* timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
919 |
|
*/ |
920 |
< |
public void testTimedPollFirst() { |
921 |
< |
try { |
922 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
923 |
< |
for (int i = 0; i < SIZE; ++i) { |
1061 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1062 |
< |
} |
1063 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1064 |
< |
} catch (InterruptedException e) { |
1065 |
< |
unexpectedException(); |
920 |
> |
public void testTimedPollFirst() throws InterruptedException { |
921 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
922 |
> |
for (int i = 0; i < SIZE; ++i) { |
923 |
> |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
924 |
|
} |
925 |
+ |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
929 |
|
* Interrupted timed pollFirst throws InterruptedException instead of |
930 |
|
* returning timeout status |
931 |
|
*/ |
932 |
< |
public void testInterruptedTimedPollFirst() { |
933 |
< |
Thread t = new Thread(new Runnable() { |
934 |
< |
public void run() { |
935 |
< |
try { |
936 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
937 |
< |
for (int i = 0; i < SIZE; ++i) { |
938 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
939 |
< |
} |
940 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
941 |
< |
} catch (InterruptedException success) { |
1083 |
< |
} |
1084 |
< |
}}); |
932 |
> |
public void testInterruptedTimedPollFirst() throws InterruptedException { |
933 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
934 |
> |
public void realRun() throws InterruptedException { |
935 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
936 |
> |
for (int i = 0; i < SIZE; ++i) { |
937 |
> |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
938 |
> |
} |
939 |
> |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
940 |
> |
}}; |
941 |
> |
|
942 |
|
t.start(); |
943 |
< |
try { |
944 |
< |
Thread.sleep(SHORT_DELAY_MS); |
945 |
< |
t.interrupt(); |
1089 |
< |
t.join(); |
1090 |
< |
} |
1091 |
< |
catch (InterruptedException ie) { |
1092 |
< |
unexpectedException(); |
1093 |
< |
} |
943 |
> |
Thread.sleep(SHORT_DELAY_MS); |
944 |
> |
t.interrupt(); |
945 |
> |
t.join(); |
946 |
|
} |
947 |
|
|
948 |
|
/** |
949 |
|
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
950 |
|
* on interruption throws |
951 |
|
*/ |
952 |
< |
public void testTimedPollFirstWithOfferFirst() { |
952 |
> |
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
953 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
954 |
< |
Thread t = new Thread(new Runnable() { |
955 |
< |
public void run() { |
956 |
< |
try { |
957 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
958 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
959 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
960 |
< |
threadShouldThrow(); |
961 |
< |
} catch (InterruptedException success) { } |
962 |
< |
} |
963 |
< |
}); |
964 |
< |
try { |
965 |
< |
t.start(); |
1114 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1115 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1116 |
< |
t.interrupt(); |
1117 |
< |
t.join(); |
1118 |
< |
} catch (Exception e) { |
1119 |
< |
unexpectedException(); |
1120 |
< |
} |
954 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
955 |
> |
public void realRun() throws InterruptedException { |
956 |
> |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
957 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
958 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
959 |
> |
}}; |
960 |
> |
|
961 |
> |
t.start(); |
962 |
> |
Thread.sleep(SMALL_DELAY_MS); |
963 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
964 |
> |
t.interrupt(); |
965 |
> |
t.join(); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
|
* putLast(null) throws NPE |
970 |
|
*/ |
971 |
< |
public void testPutLastNull() { |
971 |
> |
public void testPutLastNull() throws InterruptedException { |
972 |
|
try { |
973 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
974 |
|
q.putLast(null); |
975 |
|
shouldThrow(); |
976 |
< |
} |
1132 |
< |
catch (NullPointerException success) { |
1133 |
< |
} |
1134 |
< |
catch (InterruptedException ie) { |
1135 |
< |
unexpectedException(); |
1136 |
< |
} |
976 |
> |
} catch (NullPointerException success) {} |
977 |
|
} |
978 |
|
|
979 |
|
/** |
980 |
|
* all elements successfully putLast are contained |
981 |
|
*/ |
982 |
< |
public void testPutLast() { |
983 |
< |
try { |
984 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
985 |
< |
for (int i = 0; i < SIZE; ++i) { |
986 |
< |
Integer I = new Integer(i); |
987 |
< |
q.putLast(I); |
1148 |
< |
assertTrue(q.contains(I)); |
1149 |
< |
} |
1150 |
< |
assertEquals(0, q.remainingCapacity()); |
982 |
> |
public void testPutLast() throws InterruptedException { |
983 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
984 |
> |
for (int i = 0; i < SIZE; ++i) { |
985 |
> |
Integer I = new Integer(i); |
986 |
> |
q.putLast(I); |
987 |
> |
assertTrue(q.contains(I)); |
988 |
|
} |
989 |
< |
catch (InterruptedException ie) { |
1153 |
< |
unexpectedException(); |
1154 |
< |
} |
989 |
> |
assertEquals(0, q.remainingCapacity()); |
990 |
|
} |
991 |
|
|
992 |
|
/** |
993 |
|
* putLast blocks interruptibly if full |
994 |
|
*/ |
995 |
< |
public void testBlockingPutLast() { |
996 |
< |
Thread t = new Thread(new Runnable() { |
997 |
< |
public void run() { |
998 |
< |
int added = 0; |
999 |
< |
try { |
1000 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
< |
for (int i = 0; i < SIZE; ++i) { |
1002 |
< |
q.putLast(new Integer(i)); |
1003 |
< |
++added; |
1169 |
< |
} |
1170 |
< |
q.putLast(new Integer(SIZE)); |
1171 |
< |
threadShouldThrow(); |
1172 |
< |
} catch (InterruptedException ie) { |
1173 |
< |
threadAssertEquals(added, SIZE); |
995 |
> |
public void testBlockingPutLast() throws InterruptedException { |
996 |
> |
Thread t = new Thread(new CheckedRunnable() { |
997 |
> |
public void realRun() { |
998 |
> |
int added = 0; |
999 |
> |
try { |
1000 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
> |
for (int i = 0; i < SIZE; ++i) { |
1002 |
> |
q.putLast(new Integer(i)); |
1003 |
> |
++added; |
1004 |
|
} |
1005 |
< |
}}); |
1005 |
> |
q.putLast(new Integer(SIZE)); |
1006 |
> |
threadShouldThrow(); |
1007 |
> |
} catch (InterruptedException success) { |
1008 |
> |
threadAssertEquals(added, SIZE); |
1009 |
> |
} |
1010 |
> |
}}); |
1011 |
> |
|
1012 |
|
t.start(); |
1013 |
< |
try { |
1014 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1015 |
< |
t.interrupt(); |
1180 |
< |
t.join(); |
1181 |
< |
} |
1182 |
< |
catch (InterruptedException ie) { |
1183 |
< |
unexpectedException(); |
1184 |
< |
} |
1013 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1014 |
> |
t.interrupt(); |
1015 |
> |
t.join(); |
1016 |
|
} |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* putLast blocks waiting for take when full |
1020 |
|
*/ |
1021 |
< |
public void testPutLastWithTake() { |
1021 |
> |
public void testPutLastWithTake() throws InterruptedException { |
1022 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1023 |
< |
Thread t = new Thread(new Runnable() { |
1024 |
< |
public void run() { |
1025 |
< |
int added = 0; |
1026 |
< |
try { |
1027 |
< |
q.putLast(new Object()); |
1028 |
< |
++added; |
1029 |
< |
q.putLast(new Object()); |
1030 |
< |
++added; |
1031 |
< |
q.putLast(new Object()); |
1032 |
< |
++added; |
1033 |
< |
q.putLast(new Object()); |
1034 |
< |
++added; |
1035 |
< |
threadShouldThrow(); |
1036 |
< |
} catch (InterruptedException e) { |
1037 |
< |
threadAssertTrue(added >= 2); |
1207 |
< |
} |
1023 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1024 |
> |
public void realRun() { |
1025 |
> |
int added = 0; |
1026 |
> |
try { |
1027 |
> |
q.putLast(new Object()); |
1028 |
> |
++added; |
1029 |
> |
q.putLast(new Object()); |
1030 |
> |
++added; |
1031 |
> |
q.putLast(new Object()); |
1032 |
> |
++added; |
1033 |
> |
q.putLast(new Object()); |
1034 |
> |
++added; |
1035 |
> |
threadShouldThrow(); |
1036 |
> |
} catch (InterruptedException success) { |
1037 |
> |
threadAssertTrue(added >= 2); |
1038 |
|
} |
1039 |
< |
}); |
1040 |
< |
try { |
1041 |
< |
t.start(); |
1042 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1043 |
< |
q.take(); |
1044 |
< |
t.interrupt(); |
1045 |
< |
t.join(); |
1216 |
< |
} catch (Exception e) { |
1217 |
< |
unexpectedException(); |
1218 |
< |
} |
1039 |
> |
}}); |
1040 |
> |
|
1041 |
> |
t.start(); |
1042 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1043 |
> |
q.take(); |
1044 |
> |
t.interrupt(); |
1045 |
> |
t.join(); |
1046 |
|
} |
1047 |
|
|
1048 |
|
/** |
1049 |
|
* timed offerLast times out if full and elements not taken |
1050 |
|
*/ |
1051 |
< |
public void testTimedOfferLast() { |
1051 |
> |
public void testTimedOfferLast() throws InterruptedException { |
1052 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1053 |
< |
Thread t = new Thread(new Runnable() { |
1054 |
< |
public void run() { |
1055 |
< |
try { |
1056 |
< |
q.putLast(new Object()); |
1057 |
< |
q.putLast(new Object()); |
1058 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1059 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1233 |
< |
threadShouldThrow(); |
1234 |
< |
} catch (InterruptedException success) {} |
1235 |
< |
} |
1236 |
< |
}); |
1053 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1054 |
> |
public void realRun() throws InterruptedException { |
1055 |
> |
q.putLast(new Object()); |
1056 |
> |
q.putLast(new Object()); |
1057 |
> |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1058 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1059 |
> |
}}; |
1060 |
|
|
1061 |
< |
try { |
1062 |
< |
t.start(); |
1063 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1064 |
< |
t.interrupt(); |
1242 |
< |
t.join(); |
1243 |
< |
} catch (Exception e) { |
1244 |
< |
unexpectedException(); |
1245 |
< |
} |
1061 |
> |
t.start(); |
1062 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1063 |
> |
t.interrupt(); |
1064 |
> |
t.join(); |
1065 |
|
} |
1066 |
|
|
1067 |
|
/** |
1068 |
|
* takeLast retrieves elements in FIFO order |
1069 |
|
*/ |
1070 |
< |
public void testTakeLast() { |
1071 |
< |
try { |
1072 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1073 |
< |
for (int i = 0; i < SIZE; ++i) { |
1255 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1256 |
< |
} |
1257 |
< |
} catch (InterruptedException e) { |
1258 |
< |
unexpectedException(); |
1070 |
> |
public void testTakeLast() throws InterruptedException { |
1071 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1072 |
> |
for (int i = 0; i < SIZE; ++i) { |
1073 |
> |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1074 |
|
} |
1075 |
|
} |
1076 |
|
|
1077 |
|
/** |
1078 |
|
* takeLast blocks interruptibly when empty |
1079 |
|
*/ |
1080 |
< |
public void testTakeLastFromEmpty() { |
1080 |
> |
public void testTakeLastFromEmpty() throws InterruptedException { |
1081 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1082 |
< |
Thread t = new Thread(new Runnable() { |
1083 |
< |
public void run() { |
1084 |
< |
try { |
1085 |
< |
q.takeLast(); |
1086 |
< |
threadShouldThrow(); |
1087 |
< |
} catch (InterruptedException success) { } |
1088 |
< |
} |
1089 |
< |
}); |
1090 |
< |
try { |
1276 |
< |
t.start(); |
1277 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1278 |
< |
t.interrupt(); |
1279 |
< |
t.join(); |
1280 |
< |
} catch (Exception e) { |
1281 |
< |
unexpectedException(); |
1282 |
< |
} |
1082 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1083 |
> |
public void realRun() throws InterruptedException { |
1084 |
> |
q.takeLast(); |
1085 |
> |
}}; |
1086 |
> |
|
1087 |
> |
t.start(); |
1088 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1089 |
> |
t.interrupt(); |
1090 |
> |
t.join(); |
1091 |
|
} |
1092 |
|
|
1093 |
|
/** |
1094 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1095 |
|
*/ |
1096 |
< |
public void testBlockingTakeLast() { |
1097 |
< |
Thread t = new Thread(new Runnable() { |
1098 |
< |
public void run() { |
1099 |
< |
try { |
1100 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1101 |
< |
for (int i = 0; i < SIZE; ++i) { |
1102 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1103 |
< |
} |
1104 |
< |
q.takeLast(); |
1105 |
< |
threadShouldThrow(); |
1298 |
< |
} catch (InterruptedException success) { |
1299 |
< |
} |
1300 |
< |
}}); |
1096 |
> |
public void testBlockingTakeLast() throws InterruptedException { |
1097 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1098 |
> |
public void realRun() throws InterruptedException { |
1099 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1100 |
> |
for (int i = 0; i < SIZE; ++i) { |
1101 |
> |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1102 |
> |
} |
1103 |
> |
q.takeLast(); |
1104 |
> |
}}; |
1105 |
> |
|
1106 |
|
t.start(); |
1107 |
< |
try { |
1108 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1109 |
< |
t.interrupt(); |
1305 |
< |
t.join(); |
1306 |
< |
} |
1307 |
< |
catch (InterruptedException ie) { |
1308 |
< |
unexpectedException(); |
1309 |
< |
} |
1107 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1108 |
> |
t.interrupt(); |
1109 |
> |
t.join(); |
1110 |
|
} |
1111 |
|
|
1112 |
|
|
1113 |
|
/** |
1114 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1115 |
|
*/ |
1116 |
< |
public void testTimedPollLast0() { |
1117 |
< |
try { |
1118 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1119 |
< |
for (int i = 0; i < SIZE; ++i) { |
1320 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1321 |
< |
} |
1322 |
< |
assertNull(q.pollLast(0, MILLISECONDS)); |
1323 |
< |
} catch (InterruptedException e) { |
1324 |
< |
unexpectedException(); |
1116 |
> |
public void testTimedPollLast0() throws InterruptedException { |
1117 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1118 |
> |
for (int i = 0; i < SIZE; ++i) { |
1119 |
> |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1120 |
|
} |
1121 |
+ |
assertNull(q.pollLast(0, MILLISECONDS)); |
1122 |
|
} |
1123 |
|
|
1124 |
|
/** |
1125 |
|
* timed pollLast with nonzero timeout succeeds when non-empty, else times out |
1126 |
|
*/ |
1127 |
< |
public void testTimedPollLast() { |
1128 |
< |
try { |
1129 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1130 |
< |
for (int i = 0; i < SIZE; ++i) { |
1335 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1336 |
< |
} |
1337 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1338 |
< |
} catch (InterruptedException e) { |
1339 |
< |
unexpectedException(); |
1127 |
> |
public void testTimedPollLast() throws InterruptedException { |
1128 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1129 |
> |
for (int i = 0; i < SIZE; ++i) { |
1130 |
> |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1131 |
|
} |
1132 |
+ |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
|
* Interrupted timed pollLast throws InterruptedException instead of |
1137 |
|
* returning timeout status |
1138 |
|
*/ |
1139 |
< |
public void testInterruptedTimedPollLast() { |
1140 |
< |
Thread t = new Thread(new Runnable() { |
1141 |
< |
public void run() { |
1142 |
< |
try { |
1143 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1144 |
< |
for (int i = 0; i < SIZE; ++i) { |
1145 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1146 |
< |
} |
1147 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1148 |
< |
} catch (InterruptedException success) { |
1357 |
< |
} |
1358 |
< |
}}); |
1139 |
> |
public void testInterruptedTimedPollLast() throws InterruptedException { |
1140 |
> |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1141 |
> |
public void realRun() throws InterruptedException { |
1142 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1143 |
> |
for (int i = 0; i < SIZE; ++i) { |
1144 |
> |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1145 |
> |
} |
1146 |
> |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1147 |
> |
}}; |
1148 |
> |
|
1149 |
|
t.start(); |
1150 |
< |
try { |
1151 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1152 |
< |
t.interrupt(); |
1363 |
< |
t.join(); |
1364 |
< |
} |
1365 |
< |
catch (InterruptedException ie) { |
1366 |
< |
unexpectedException(); |
1367 |
< |
} |
1150 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1151 |
> |
t.interrupt(); |
1152 |
> |
t.join(); |
1153 |
|
} |
1154 |
|
|
1155 |
|
/** |
1156 |
|
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1157 |
|
* on interruption throws |
1158 |
|
*/ |
1159 |
< |
public void testTimedPollWithOfferLast() { |
1159 |
> |
public void testTimedPollWithOfferLast() throws InterruptedException { |
1160 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1161 |
< |
Thread t = new Thread(new Runnable() { |
1162 |
< |
public void run() { |
1163 |
< |
try { |
1164 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1165 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1166 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1167 |
< |
threadShouldThrow(); |
1168 |
< |
} catch (InterruptedException success) { } |
1169 |
< |
} |
1170 |
< |
}); |
1171 |
< |
try { |
1172 |
< |
t.start(); |
1173 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1174 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1175 |
< |
t.interrupt(); |
1391 |
< |
t.join(); |
1392 |
< |
} catch (Exception e) { |
1393 |
< |
unexpectedException(); |
1394 |
< |
} |
1161 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1162 |
> |
public void realRun() throws InterruptedException { |
1163 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1164 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1165 |
> |
try { |
1166 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1167 |
> |
shouldThrow(); |
1168 |
> |
} catch (InterruptedException success) {} |
1169 |
> |
}}); |
1170 |
> |
|
1171 |
> |
t.start(); |
1172 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1173 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1174 |
> |
t.interrupt(); |
1175 |
> |
t.join(); |
1176 |
|
} |
1177 |
|
|
1178 |
|
|
1188 |
|
try { |
1189 |
|
q.element(); |
1190 |
|
shouldThrow(); |
1191 |
< |
} |
1411 |
< |
catch (NoSuchElementException success) {} |
1191 |
> |
} catch (NoSuchElementException success) {} |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1286 |
|
/** |
1287 |
|
* toArray contains all elements |
1288 |
|
*/ |
1289 |
< |
public void testToArray() { |
1289 |
> |
public void testToArray() throws InterruptedException{ |
1290 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1291 |
|
Object[] o = q.toArray(); |
1512 |
– |
try { |
1292 |
|
for (int i = 0; i < o.length; i++) |
1293 |
|
assertEquals(o[i], q.take()); |
1515 |
– |
} catch (InterruptedException e) { |
1516 |
– |
unexpectedException(); |
1517 |
– |
} |
1294 |
|
} |
1295 |
|
|
1296 |
|
/** |
1297 |
|
* toArray(a) contains all elements |
1298 |
|
*/ |
1299 |
< |
public void testToArray2() { |
1299 |
> |
public void testToArray2() throws InterruptedException { |
1300 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1301 |
|
Integer[] ints = new Integer[SIZE]; |
1302 |
|
ints = (Integer[])q.toArray(ints); |
1303 |
< |
try { |
1304 |
< |
for (int i = 0; i < ints.length; i++) |
1529 |
< |
assertEquals(ints[i], q.take()); |
1530 |
< |
} catch (InterruptedException e) { |
1531 |
< |
unexpectedException(); |
1532 |
< |
} |
1303 |
> |
for (int i = 0; i < ints.length; i++) |
1304 |
> |
assertEquals(ints[i], q.take()); |
1305 |
|
} |
1306 |
|
|
1307 |
|
/** |
1323 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
|
Object o[] = q.toArray(new String[10] ); |
1325 |
|
shouldThrow(); |
1326 |
< |
} catch (ArrayStoreException success) {} |
1326 |
> |
} catch (ArrayStoreException success) {} |
1327 |
|
} |
1328 |
|
|
1329 |
|
|
1330 |
|
/** |
1331 |
|
* iterator iterates through all elements |
1332 |
|
*/ |
1333 |
< |
public void testIterator() { |
1333 |
> |
public void testIterator() throws InterruptedException { |
1334 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1335 |
|
Iterator it = q.iterator(); |
1336 |
< |
try { |
1337 |
< |
while (it.hasNext()) { |
1566 |
< |
assertEquals(it.next(), q.take()); |
1567 |
< |
} |
1568 |
< |
} catch (InterruptedException e) { |
1569 |
< |
unexpectedException(); |
1336 |
> |
while (it.hasNext()) { |
1337 |
> |
assertEquals(it.next(), q.take()); |
1338 |
|
} |
1339 |
|
} |
1340 |
|
|
1383 |
|
q.add(one); |
1384 |
|
q.add(two); |
1385 |
|
q.add(three); |
1386 |
< |
try { |
1387 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
1388 |
< |
q.remove(); |
1621 |
< |
it.next(); |
1622 |
< |
} |
1623 |
< |
} |
1624 |
< |
catch (ConcurrentModificationException e) { |
1625 |
< |
unexpectedException(); |
1386 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
1387 |
> |
q.remove(); |
1388 |
> |
it.next(); |
1389 |
|
} |
1390 |
|
assertEquals(0, q.size()); |
1391 |
|
} |
1406 |
|
assertFalse(it.hasNext()); |
1407 |
|
try { |
1408 |
|
it.next(); |
1409 |
< |
} catch (NoSuchElementException success) { |
1410 |
< |
} |
1409 |
> |
shouldThrow(); |
1410 |
> |
} catch (NoSuchElementException success) {} |
1411 |
|
} |
1412 |
|
|
1413 |
|
/** |
1475 |
|
q.add(one); |
1476 |
|
q.add(two); |
1477 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1478 |
< |
executor.execute(new Runnable() { |
1479 |
< |
public void run() { |
1478 |
> |
executor.execute(new CheckedRunnable() { |
1479 |
> |
public void realRun() throws InterruptedException { |
1480 |
|
threadAssertFalse(q.offer(three)); |
1481 |
< |
try { |
1482 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1483 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1484 |
< |
} |
1485 |
< |
catch (InterruptedException e) { |
1486 |
< |
threadUnexpectedException(); |
1487 |
< |
} |
1488 |
< |
} |
1489 |
< |
}); |
1727 |
< |
|
1728 |
< |
executor.execute(new Runnable() { |
1729 |
< |
public void run() { |
1730 |
< |
try { |
1731 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1732 |
< |
threadAssertEquals(one, q.take()); |
1733 |
< |
} |
1734 |
< |
catch (InterruptedException e) { |
1735 |
< |
threadUnexpectedException(); |
1736 |
< |
} |
1737 |
< |
} |
1738 |
< |
}); |
1481 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1482 |
> |
threadAssertEquals(0, q.remainingCapacity()); |
1483 |
> |
}}); |
1484 |
> |
|
1485 |
> |
executor.execute(new CheckedRunnable() { |
1486 |
> |
public void realRun() throws InterruptedException { |
1487 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1488 |
> |
threadAssertEquals(one, q.take()); |
1489 |
> |
}}); |
1490 |
|
|
1491 |
|
joinPool(executor); |
1492 |
|
} |
1497 |
|
public void testPollInExecutor() { |
1498 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1499 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1500 |
< |
executor.execute(new Runnable() { |
1501 |
< |
public void run() { |
1500 |
> |
executor.execute(new CheckedRunnable() { |
1501 |
> |
public void realRun() throws InterruptedException { |
1502 |
|
threadAssertNull(q.poll()); |
1503 |
< |
try { |
1504 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1505 |
< |
threadAssertTrue(q.isEmpty()); |
1506 |
< |
} |
1507 |
< |
catch (InterruptedException e) { |
1508 |
< |
threadUnexpectedException(); |
1509 |
< |
} |
1510 |
< |
} |
1511 |
< |
}); |
1761 |
< |
|
1762 |
< |
executor.execute(new Runnable() { |
1763 |
< |
public void run() { |
1764 |
< |
try { |
1765 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1766 |
< |
q.put(one); |
1767 |
< |
} |
1768 |
< |
catch (InterruptedException e) { |
1769 |
< |
threadUnexpectedException(); |
1770 |
< |
} |
1771 |
< |
} |
1772 |
< |
}); |
1503 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1504 |
> |
threadAssertTrue(q.isEmpty()); |
1505 |
> |
}}); |
1506 |
> |
|
1507 |
> |
executor.execute(new CheckedRunnable() { |
1508 |
> |
public void realRun() throws InterruptedException { |
1509 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1510 |
> |
q.put(one); |
1511 |
> |
}}); |
1512 |
|
|
1513 |
|
joinPool(executor); |
1514 |
|
} |
1516 |
|
/** |
1517 |
|
* A deserialized serialized deque has same elements in same order |
1518 |
|
*/ |
1519 |
< |
public void testSerialization() { |
1519 |
> |
public void testSerialization() throws Exception { |
1520 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1521 |
|
|
1522 |
< |
try { |
1523 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1524 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1525 |
< |
out.writeObject(q); |
1526 |
< |
out.close(); |
1527 |
< |
|
1528 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1529 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1530 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1531 |
< |
assertEquals(q.size(), r.size()); |
1532 |
< |
while (!q.isEmpty()) |
1794 |
< |
assertEquals(q.remove(), r.remove()); |
1795 |
< |
} catch (Exception e) { |
1796 |
< |
unexpectedException(); |
1797 |
< |
} |
1522 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1523 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1524 |
> |
out.writeObject(q); |
1525 |
> |
out.close(); |
1526 |
> |
|
1527 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1528 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1529 |
> |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1530 |
> |
assertEquals(q.size(), r.size()); |
1531 |
> |
while (!q.isEmpty()) |
1532 |
> |
assertEquals(q.remove(), r.remove()); |
1533 |
|
} |
1534 |
|
|
1535 |
|
/** |
1540 |
|
try { |
1541 |
|
q.drainTo(null); |
1542 |
|
shouldThrow(); |
1543 |
< |
} catch (NullPointerException success) { |
1809 |
< |
} |
1543 |
> |
} catch (NullPointerException success) {} |
1544 |
|
} |
1545 |
|
|
1546 |
|
/** |
1551 |
|
try { |
1552 |
|
q.drainTo(q); |
1553 |
|
shouldThrow(); |
1554 |
< |
} catch (IllegalArgumentException success) { |
1821 |
< |
} |
1554 |
> |
} catch (IllegalArgumentException success) {} |
1555 |
|
} |
1556 |
|
|
1557 |
|
/** |
1581 |
|
/** |
1582 |
|
* drainTo empties full deque, unblocking a waiting put. |
1583 |
|
*/ |
1584 |
< |
public void testDrainToWithActivePut() { |
1584 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
1585 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1586 |
< |
Thread t = new Thread(new Runnable() { |
1587 |
< |
public void run() { |
1588 |
< |
try { |
1589 |
< |
q.put(new Integer(SIZE+1)); |
1590 |
< |
} catch (InterruptedException ie) { |
1591 |
< |
threadUnexpectedException(); |
1592 |
< |
} |
1593 |
< |
} |
1594 |
< |
}); |
1595 |
< |
try { |
1596 |
< |
t.start(); |
1597 |
< |
ArrayList l = new ArrayList(); |
1598 |
< |
q.drainTo(l); |
1866 |
< |
assertTrue(l.size() >= SIZE); |
1867 |
< |
for (int i = 0; i < SIZE; ++i) |
1868 |
< |
assertEquals(l.get(i), new Integer(i)); |
1869 |
< |
t.join(); |
1870 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1871 |
< |
} catch (Exception e) { |
1872 |
< |
unexpectedException(); |
1873 |
< |
} |
1586 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1587 |
> |
public void realRun() throws InterruptedException { |
1588 |
> |
q.put(new Integer(SIZE+1)); |
1589 |
> |
}}); |
1590 |
> |
|
1591 |
> |
t.start(); |
1592 |
> |
ArrayList l = new ArrayList(); |
1593 |
> |
q.drainTo(l); |
1594 |
> |
assertTrue(l.size() >= SIZE); |
1595 |
> |
for (int i = 0; i < SIZE; ++i) |
1596 |
> |
assertEquals(l.get(i), new Integer(i)); |
1597 |
> |
t.join(); |
1598 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1599 |
|
} |
1600 |
|
|
1601 |
|
/** |
1606 |
|
try { |
1607 |
|
q.drainTo(null, 0); |
1608 |
|
shouldThrow(); |
1609 |
< |
} catch (NullPointerException success) { |
1885 |
< |
} |
1609 |
> |
} catch (NullPointerException success) {} |
1610 |
|
} |
1611 |
|
|
1612 |
|
/** |
1617 |
|
try { |
1618 |
|
q.drainTo(q, 0); |
1619 |
|
shouldThrow(); |
1620 |
< |
} catch (IllegalArgumentException success) { |
1897 |
< |
} |
1620 |
> |
} catch (IllegalArgumentException success) {} |
1621 |
|
} |
1622 |
|
|
1623 |
|
/** |