37 |
|
} |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return LinkedBlockingQueue.class; } |
46 |
+ |
public Collection emptyCollection() { return new LinkedBlockingQueue(); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(LinkedBlockingQueueTest.class, |
52 |
|
new Unbounded().testSuite(), |
53 |
< |
new Bounded().testSuite()); |
53 |
> |
new Bounded().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Returns a new queue of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
< |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
< |
LinkedBlockingQueue<Integer> q = |
55 |
< |
new LinkedBlockingQueue<Integer>(n); |
61 |
> |
private static LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
> |
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(n); |
63 |
|
assertTrue(q.isEmpty()); |
64 |
|
for (int i = 0; i < n; i++) |
65 |
|
assertTrue(q.offer(new Integer(i))); |
66 |
|
assertFalse(q.isEmpty()); |
67 |
|
assertEquals(0, q.remainingCapacity()); |
68 |
|
assertEquals(n, q.size()); |
69 |
+ |
assertEquals((Integer) 0, q.peek()); |
70 |
|
return q; |
71 |
|
} |
72 |
|
|
116 |
|
*/ |
117 |
|
public void testConstructor5() { |
118 |
|
Integer[] ints = new Integer[SIZE]; |
119 |
< |
for (int i = 0; i < SIZE-1; ++i) |
119 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
120 |
|
ints[i] = new Integer(i); |
121 |
|
Collection<Integer> elements = Arrays.asList(ints); |
122 |
|
try { |
156 |
|
* remainingCapacity decreases on add, increases on remove |
157 |
|
*/ |
158 |
|
public void testRemainingCapacity() { |
159 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
159 |
> |
BlockingQueue q = populatedQueue(SIZE); |
160 |
|
for (int i = 0; i < SIZE; ++i) { |
161 |
|
assertEquals(i, q.remainingCapacity()); |
162 |
< |
assertEquals(SIZE-i, q.size()); |
163 |
< |
q.remove(); |
162 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
163 |
> |
assertEquals(i, q.remove()); |
164 |
|
} |
165 |
|
for (int i = 0; i < SIZE; ++i) { |
166 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
167 |
< |
assertEquals(i, q.size()); |
168 |
< |
q.add(new Integer(i)); |
166 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
167 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
168 |
> |
assertTrue(q.add(i)); |
169 |
|
} |
170 |
|
} |
171 |
|
|
210 |
|
public void testAddAll3() { |
211 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
212 |
|
Integer[] ints = new Integer[SIZE]; |
213 |
< |
for (int i = 0; i < SIZE-1; ++i) |
213 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
214 |
|
ints[i] = new Integer(i); |
215 |
|
Collection<Integer> elements = Arrays.asList(ints); |
216 |
|
try { |
255 |
|
public void testPut() throws InterruptedException { |
256 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
257 |
|
for (int i = 0; i < SIZE; ++i) { |
258 |
< |
Integer I = new Integer(i); |
259 |
< |
q.put(I); |
260 |
< |
assertTrue(q.contains(I)); |
258 |
> |
Integer x = new Integer(i); |
259 |
> |
q.put(x); |
260 |
> |
assertTrue(q.contains(x)); |
261 |
|
} |
262 |
|
assertEquals(0, q.remainingCapacity()); |
263 |
|
} |
291 |
|
}}); |
292 |
|
|
293 |
|
await(pleaseInterrupt); |
294 |
< |
assertThreadStaysAlive(t); |
294 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
295 |
|
t.interrupt(); |
296 |
|
awaitTermination(t); |
297 |
|
assertEquals(SIZE, q.size()); |
313 |
|
pleaseTake.countDown(); |
314 |
|
q.put(86); |
315 |
|
|
316 |
+ |
Thread.currentThread().interrupt(); |
317 |
+ |
try { |
318 |
+ |
q.put(99); |
319 |
+ |
shouldThrow(); |
320 |
+ |
} catch (InterruptedException success) {} |
321 |
+ |
assertFalse(Thread.interrupted()); |
322 |
+ |
|
323 |
|
pleaseInterrupt.countDown(); |
324 |
|
try { |
325 |
|
q.put(99); |
333 |
|
assertEquals(0, q.take()); |
334 |
|
|
335 |
|
await(pleaseInterrupt); |
336 |
< |
assertThreadStaysAlive(t); |
336 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
337 |
|
t.interrupt(); |
338 |
|
awaitTermination(t); |
339 |
|
assertEquals(0, q.remainingCapacity()); |
349 |
|
public void realRun() throws InterruptedException { |
350 |
|
q.put(new Object()); |
351 |
|
q.put(new Object()); |
352 |
+ |
|
353 |
|
long startTime = System.nanoTime(); |
354 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
355 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
356 |
+ |
|
357 |
+ |
Thread.currentThread().interrupt(); |
358 |
+ |
try { |
359 |
+ |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
360 |
+ |
shouldThrow(); |
361 |
+ |
} catch (InterruptedException success) {} |
362 |
+ |
assertFalse(Thread.interrupted()); |
363 |
+ |
|
364 |
|
pleaseInterrupt.countDown(); |
365 |
|
try { |
366 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
367 |
|
shouldThrow(); |
368 |
|
} catch (InterruptedException success) {} |
369 |
+ |
assertFalse(Thread.interrupted()); |
370 |
|
}}); |
371 |
|
|
372 |
|
await(pleaseInterrupt); |
373 |
< |
assertThreadStaysAlive(t); |
373 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
374 |
|
t.interrupt(); |
375 |
|
awaitTermination(t); |
376 |
|
} |
393 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
394 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
395 |
|
public void realRun() throws InterruptedException { |
396 |
< |
for (int i = 0; i < SIZE; ++i) { |
372 |
< |
assertEquals(i, q.take()); |
373 |
< |
} |
396 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
397 |
|
|
398 |
|
Thread.currentThread().interrupt(); |
399 |
|
try { |
411 |
|
}}); |
412 |
|
|
413 |
|
await(pleaseInterrupt); |
414 |
< |
assertThreadStaysAlive(t); |
414 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
415 |
|
t.interrupt(); |
416 |
|
awaitTermination(t); |
417 |
|
} |
460 |
|
*/ |
461 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
462 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
463 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
463 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
464 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
465 |
|
public void realRun() throws InterruptedException { |
466 |
< |
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
long t0 = System.nanoTime(); |
466 |
> |
long startTime = System.nanoTime(); |
467 |
> |
for (int i = 0; i < SIZE; i++) |
468 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
469 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
470 |
< |
} |
471 |
< |
long t0 = System.nanoTime(); |
472 |
< |
aboutToWait.countDown(); |
469 |
> |
|
470 |
> |
Thread.currentThread().interrupt(); |
471 |
> |
try { |
472 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
473 |
> |
shouldThrow(); |
474 |
> |
} catch (InterruptedException success) {} |
475 |
> |
assertFalse(Thread.interrupted()); |
476 |
> |
|
477 |
> |
pleaseInterrupt.countDown(); |
478 |
|
try { |
479 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
479 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
480 |
|
shouldThrow(); |
481 |
< |
} catch (InterruptedException success) { |
482 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
483 |
< |
} |
481 |
> |
} catch (InterruptedException success) {} |
482 |
> |
assertFalse(Thread.interrupted()); |
483 |
> |
|
484 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
485 |
|
}}); |
486 |
|
|
487 |
< |
aboutToWait.await(); |
488 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
487 |
> |
await(pleaseInterrupt); |
488 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
489 |
|
t.interrupt(); |
490 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
490 |
> |
awaitTermination(t); |
491 |
|
checkEmpty(q); |
492 |
|
} |
493 |
|
|
603 |
|
assertTrue(changed); |
604 |
|
|
605 |
|
assertTrue(q.containsAll(p)); |
606 |
< |
assertEquals(SIZE-i, q.size()); |
606 |
> |
assertEquals(SIZE - i, q.size()); |
607 |
|
p.remove(); |
608 |
|
} |
609 |
|
} |
616 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
617 |
|
LinkedBlockingQueue p = populatedQueue(i); |
618 |
|
assertTrue(q.removeAll(p)); |
619 |
< |
assertEquals(SIZE-i, q.size()); |
619 |
> |
assertEquals(SIZE - i, q.size()); |
620 |
|
for (int j = 0; j < i; ++j) { |
621 |
< |
Integer I = (Integer)(p.remove()); |
622 |
< |
assertFalse(q.contains(I)); |
621 |
> |
Integer x = (Integer)(p.remove()); |
622 |
> |
assertFalse(q.contains(x)); |
623 |
|
} |
624 |
|
} |
625 |
|
} |
629 |
|
*/ |
630 |
|
public void testToArray() { |
631 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
632 |
< |
Object[] o = q.toArray(); |
633 |
< |
for (int i = 0; i < o.length; i++) |
634 |
< |
assertSame(o[i], q.poll()); |
632 |
> |
Object[] a = q.toArray(); |
633 |
> |
assertSame(Object[].class, a.getClass()); |
634 |
> |
for (Object o : a) |
635 |
> |
assertSame(o, q.poll()); |
636 |
> |
assertTrue(q.isEmpty()); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
644 |
|
Integer[] ints = new Integer[SIZE]; |
645 |
|
Integer[] array = q.toArray(ints); |
646 |
|
assertSame(ints, array); |
647 |
< |
for (int i = 0; i < ints.length; i++) |
648 |
< |
assertSame(ints[i], q.poll()); |
647 |
> |
for (Integer o : ints) |
648 |
> |
assertSame(o, q.poll()); |
649 |
> |
assertTrue(q.isEmpty()); |
650 |
|
} |
651 |
|
|
652 |
|
/** |
666 |
|
public void testIterator() throws InterruptedException { |
667 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
668 |
|
Iterator it = q.iterator(); |
669 |
< |
while (it.hasNext()) { |
669 |
> |
int i; |
670 |
> |
for (i = 0; it.hasNext(); i++) |
671 |
> |
assertTrue(q.contains(it.next())); |
672 |
> |
assertEquals(i, SIZE); |
673 |
> |
assertIteratorExhausted(it); |
674 |
> |
|
675 |
> |
it = q.iterator(); |
676 |
> |
for (i = 0; it.hasNext(); i++) |
677 |
|
assertEquals(it.next(), q.take()); |
678 |
< |
} |
678 |
> |
assertEquals(i, SIZE); |
679 |
> |
assertIteratorExhausted(it); |
680 |
> |
} |
681 |
> |
|
682 |
> |
/** |
683 |
> |
* iterator of empty collection has no elements |
684 |
> |
*/ |
685 |
> |
public void testEmptyIterator() { |
686 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
754 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
755 |
|
q.add(one); |
756 |
|
q.add(two); |
710 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
757 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
758 |
< |
executor.execute(new CheckedRunnable() { |
759 |
< |
public void realRun() throws InterruptedException { |
760 |
< |
assertFalse(q.offer(three)); |
761 |
< |
threadsStarted.await(); |
762 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
763 |
< |
assertEquals(0, q.remainingCapacity()); |
764 |
< |
}}); |
765 |
< |
|
766 |
< |
executor.execute(new CheckedRunnable() { |
767 |
< |
public void realRun() throws InterruptedException { |
768 |
< |
threadsStarted.await(); |
769 |
< |
assertSame(one, q.take()); |
770 |
< |
}}); |
771 |
< |
|
772 |
< |
joinPool(executor); |
758 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
759 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
760 |
> |
executor.execute(new CheckedRunnable() { |
761 |
> |
public void realRun() throws InterruptedException { |
762 |
> |
assertFalse(q.offer(three)); |
763 |
> |
threadsStarted.await(); |
764 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertEquals(0, q.remainingCapacity()); |
766 |
> |
}}); |
767 |
> |
|
768 |
> |
executor.execute(new CheckedRunnable() { |
769 |
> |
public void realRun() throws InterruptedException { |
770 |
> |
threadsStarted.await(); |
771 |
> |
assertSame(one, q.take()); |
772 |
> |
}}); |
773 |
> |
} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
779 |
|
public void testPollInExecutor() { |
780 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
781 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
782 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
< |
executor.execute(new CheckedRunnable() { |
784 |
< |
public void realRun() throws InterruptedException { |
785 |
< |
assertNull(q.poll()); |
786 |
< |
threadsStarted.await(); |
787 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
788 |
< |
checkEmpty(q); |
789 |
< |
}}); |
790 |
< |
|
791 |
< |
executor.execute(new CheckedRunnable() { |
792 |
< |
public void realRun() throws InterruptedException { |
793 |
< |
threadsStarted.await(); |
794 |
< |
q.put(one); |
795 |
< |
}}); |
796 |
< |
|
797 |
< |
joinPool(executor); |
782 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
784 |
> |
executor.execute(new CheckedRunnable() { |
785 |
> |
public void realRun() throws InterruptedException { |
786 |
> |
assertNull(q.poll()); |
787 |
> |
threadsStarted.await(); |
788 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
789 |
> |
checkEmpty(q); |
790 |
> |
}}); |
791 |
> |
|
792 |
> |
executor.execute(new CheckedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
threadsStarted.await(); |
795 |
> |
q.put(one); |
796 |
> |
}}); |
797 |
> |
} |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
< |
* A deserialized serialized queue has same elements in same order |
801 |
> |
* A deserialized/reserialized queue has same elements in same order |
802 |
|
*/ |
803 |
|
public void testSerialization() throws Exception { |
804 |
|
Queue x = populatedQueue(SIZE); |
846 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
847 |
|
Thread t = new Thread(new CheckedRunnable() { |
848 |
|
public void realRun() throws InterruptedException { |
849 |
< |
q.put(new Integer(SIZE+1)); |
849 |
> |
q.put(new Integer(SIZE + 1)); |
850 |
|
}}); |
851 |
|
|
852 |
|
t.start(); |
871 |
|
q.drainTo(l, i); |
872 |
|
int k = (i < SIZE) ? i : SIZE; |
873 |
|
assertEquals(k, l.size()); |
874 |
< |
assertEquals(SIZE-k, q.size()); |
874 |
> |
assertEquals(SIZE - k, q.size()); |
875 |
|
for (int j = 0; j < k; ++j) |
876 |
|
assertEquals(l.get(j), new Integer(j)); |
877 |
|
do {} while (q.poll() != null); |