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) { |
61 |
> |
private static LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
|
LinkedBlockingQueue<Integer> q = |
63 |
|
new LinkedBlockingQueue<Integer>(n); |
64 |
|
assertTrue(q.isEmpty()); |
67 |
|
assertFalse(q.isEmpty()); |
68 |
|
assertEquals(0, q.remainingCapacity()); |
69 |
|
assertEquals(n, q.size()); |
70 |
+ |
assertEquals((Integer) 0, q.peek()); |
71 |
|
return q; |
72 |
|
} |
73 |
|
|
117 |
|
*/ |
118 |
|
public void testConstructor5() { |
119 |
|
Integer[] ints = new Integer[SIZE]; |
120 |
< |
for (int i = 0; i < SIZE-1; ++i) |
120 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
121 |
|
ints[i] = new Integer(i); |
122 |
|
Collection<Integer> elements = Arrays.asList(ints); |
123 |
|
try { |
157 |
|
* remainingCapacity decreases on add, increases on remove |
158 |
|
*/ |
159 |
|
public void testRemainingCapacity() { |
160 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
160 |
> |
BlockingQueue q = populatedQueue(SIZE); |
161 |
|
for (int i = 0; i < SIZE; ++i) { |
162 |
|
assertEquals(i, q.remainingCapacity()); |
163 |
< |
assertEquals(SIZE-i, q.size()); |
164 |
< |
q.remove(); |
163 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
164 |
> |
assertEquals(i, q.remove()); |
165 |
|
} |
166 |
|
for (int i = 0; i < SIZE; ++i) { |
167 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
168 |
< |
assertEquals(i, q.size()); |
169 |
< |
q.add(new Integer(i)); |
167 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
168 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
169 |
> |
assertTrue(q.add(i)); |
170 |
|
} |
171 |
|
} |
172 |
|
|
211 |
|
public void testAddAll3() { |
212 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
213 |
|
Integer[] ints = new Integer[SIZE]; |
214 |
< |
for (int i = 0; i < SIZE-1; ++i) |
214 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
215 |
|
ints[i] = new Integer(i); |
216 |
|
Collection<Integer> elements = Arrays.asList(ints); |
217 |
|
try { |
256 |
|
public void testPut() throws InterruptedException { |
257 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
258 |
|
for (int i = 0; i < SIZE; ++i) { |
259 |
< |
Integer I = new Integer(i); |
260 |
< |
q.put(I); |
261 |
< |
assertTrue(q.contains(I)); |
259 |
> |
Integer x = new Integer(i); |
260 |
> |
q.put(x); |
261 |
> |
assertTrue(q.contains(x)); |
262 |
|
} |
263 |
|
assertEquals(0, q.remainingCapacity()); |
264 |
|
} |
292 |
|
}}); |
293 |
|
|
294 |
|
await(pleaseInterrupt); |
295 |
< |
assertThreadStaysAlive(t); |
295 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
296 |
|
t.interrupt(); |
297 |
|
awaitTermination(t); |
298 |
|
assertEquals(SIZE, q.size()); |
314 |
|
pleaseTake.countDown(); |
315 |
|
q.put(86); |
316 |
|
|
317 |
+ |
Thread.currentThread().interrupt(); |
318 |
+ |
try { |
319 |
+ |
q.put(99); |
320 |
+ |
shouldThrow(); |
321 |
+ |
} catch (InterruptedException success) {} |
322 |
+ |
assertFalse(Thread.interrupted()); |
323 |
+ |
|
324 |
|
pleaseInterrupt.countDown(); |
325 |
|
try { |
326 |
|
q.put(99); |
334 |
|
assertEquals(0, q.take()); |
335 |
|
|
336 |
|
await(pleaseInterrupt); |
337 |
< |
assertThreadStaysAlive(t); |
337 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
338 |
|
t.interrupt(); |
339 |
|
awaitTermination(t); |
340 |
|
assertEquals(0, q.remainingCapacity()); |
358 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
359 |
|
shouldThrow(); |
360 |
|
} catch (InterruptedException success) {} |
361 |
+ |
assertFalse(Thread.interrupted()); |
362 |
|
}}); |
363 |
|
|
364 |
|
await(pleaseInterrupt); |
365 |
< |
assertThreadStaysAlive(t); |
365 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
366 |
|
t.interrupt(); |
367 |
|
awaitTermination(t); |
368 |
|
} |
385 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
386 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
387 |
|
public void realRun() throws InterruptedException { |
388 |
< |
for (int i = 0; i < SIZE; ++i) { |
372 |
< |
assertEquals(i, q.take()); |
373 |
< |
} |
388 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
389 |
|
|
390 |
|
Thread.currentThread().interrupt(); |
391 |
|
try { |
403 |
|
}}); |
404 |
|
|
405 |
|
await(pleaseInterrupt); |
406 |
< |
assertThreadStaysAlive(t); |
406 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
407 |
|
t.interrupt(); |
408 |
|
awaitTermination(t); |
409 |
|
} |
452 |
|
*/ |
453 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
454 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
455 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
455 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
456 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
457 |
|
public void realRun() throws InterruptedException { |
458 |
< |
for (int i = 0; i < SIZE; ++i) { |
459 |
< |
long t0 = System.nanoTime(); |
458 |
> |
long startTime = System.nanoTime(); |
459 |
> |
for (int i = 0; i < SIZE; i++) |
460 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
461 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
462 |
< |
} |
463 |
< |
long t0 = System.nanoTime(); |
464 |
< |
aboutToWait.countDown(); |
461 |
> |
|
462 |
> |
Thread.currentThread().interrupt(); |
463 |
> |
try { |
464 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
465 |
> |
shouldThrow(); |
466 |
> |
} catch (InterruptedException success) {} |
467 |
> |
assertFalse(Thread.interrupted()); |
468 |
> |
|
469 |
> |
pleaseInterrupt.countDown(); |
470 |
|
try { |
471 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
471 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
472 |
|
shouldThrow(); |
473 |
< |
} catch (InterruptedException success) { |
474 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
475 |
< |
} |
473 |
> |
} catch (InterruptedException success) {} |
474 |
> |
assertFalse(Thread.interrupted()); |
475 |
> |
|
476 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
477 |
|
}}); |
478 |
|
|
479 |
< |
aboutToWait.await(); |
480 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
479 |
> |
await(pleaseInterrupt); |
480 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
481 |
|
t.interrupt(); |
482 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
482 |
> |
awaitTermination(t); |
483 |
|
checkEmpty(q); |
484 |
|
} |
485 |
|
|
595 |
|
assertTrue(changed); |
596 |
|
|
597 |
|
assertTrue(q.containsAll(p)); |
598 |
< |
assertEquals(SIZE-i, q.size()); |
598 |
> |
assertEquals(SIZE - i, q.size()); |
599 |
|
p.remove(); |
600 |
|
} |
601 |
|
} |
608 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
609 |
|
LinkedBlockingQueue p = populatedQueue(i); |
610 |
|
assertTrue(q.removeAll(p)); |
611 |
< |
assertEquals(SIZE-i, q.size()); |
611 |
> |
assertEquals(SIZE - i, q.size()); |
612 |
|
for (int j = 0; j < i; ++j) { |
613 |
< |
Integer I = (Integer)(p.remove()); |
614 |
< |
assertFalse(q.contains(I)); |
613 |
> |
Integer x = (Integer)(p.remove()); |
614 |
> |
assertFalse(q.contains(x)); |
615 |
|
} |
616 |
|
} |
617 |
|
} |
655 |
|
public void testIterator() throws InterruptedException { |
656 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
657 |
|
Iterator it = q.iterator(); |
658 |
< |
while (it.hasNext()) { |
658 |
> |
int i; |
659 |
> |
for (i = 0; it.hasNext(); i++) |
660 |
> |
assertTrue(q.contains(it.next())); |
661 |
> |
assertEquals(i, SIZE); |
662 |
> |
assertIteratorExhausted(it); |
663 |
> |
|
664 |
> |
it = q.iterator(); |
665 |
> |
for (i = 0; it.hasNext(); i++) |
666 |
|
assertEquals(it.next(), q.take()); |
667 |
< |
} |
667 |
> |
assertEquals(i, SIZE); |
668 |
> |
assertIteratorExhausted(it); |
669 |
> |
} |
670 |
> |
|
671 |
> |
/** |
672 |
> |
* iterator of empty collection has no elements |
673 |
> |
*/ |
674 |
> |
public void testEmptyIterator() { |
675 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
743 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
744 |
|
q.add(one); |
745 |
|
q.add(two); |
710 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
746 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
747 |
< |
executor.execute(new CheckedRunnable() { |
748 |
< |
public void realRun() throws InterruptedException { |
749 |
< |
assertFalse(q.offer(three)); |
750 |
< |
threadsStarted.await(); |
751 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
752 |
< |
assertEquals(0, q.remainingCapacity()); |
753 |
< |
}}); |
754 |
< |
|
755 |
< |
executor.execute(new CheckedRunnable() { |
756 |
< |
public void realRun() throws InterruptedException { |
757 |
< |
threadsStarted.await(); |
758 |
< |
assertSame(one, q.take()); |
759 |
< |
}}); |
760 |
< |
|
761 |
< |
joinPool(executor); |
747 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
748 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
749 |
> |
executor.execute(new CheckedRunnable() { |
750 |
> |
public void realRun() throws InterruptedException { |
751 |
> |
assertFalse(q.offer(three)); |
752 |
> |
threadsStarted.await(); |
753 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
754 |
> |
assertEquals(0, q.remainingCapacity()); |
755 |
> |
}}); |
756 |
> |
|
757 |
> |
executor.execute(new CheckedRunnable() { |
758 |
> |
public void realRun() throws InterruptedException { |
759 |
> |
threadsStarted.await(); |
760 |
> |
assertSame(one, q.take()); |
761 |
> |
}}); |
762 |
> |
} |
763 |
|
} |
764 |
|
|
765 |
|
/** |
768 |
|
public void testPollInExecutor() { |
769 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
770 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
771 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
772 |
< |
executor.execute(new CheckedRunnable() { |
773 |
< |
public void realRun() throws InterruptedException { |
774 |
< |
assertNull(q.poll()); |
775 |
< |
threadsStarted.await(); |
776 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
777 |
< |
checkEmpty(q); |
778 |
< |
}}); |
779 |
< |
|
780 |
< |
executor.execute(new CheckedRunnable() { |
781 |
< |
public void realRun() throws InterruptedException { |
782 |
< |
threadsStarted.await(); |
783 |
< |
q.put(one); |
784 |
< |
}}); |
785 |
< |
|
786 |
< |
joinPool(executor); |
771 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
772 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
773 |
> |
executor.execute(new CheckedRunnable() { |
774 |
> |
public void realRun() throws InterruptedException { |
775 |
> |
assertNull(q.poll()); |
776 |
> |
threadsStarted.await(); |
777 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
778 |
> |
checkEmpty(q); |
779 |
> |
}}); |
780 |
> |
|
781 |
> |
executor.execute(new CheckedRunnable() { |
782 |
> |
public void realRun() throws InterruptedException { |
783 |
> |
threadsStarted.await(); |
784 |
> |
q.put(one); |
785 |
> |
}}); |
786 |
> |
} |
787 |
|
} |
788 |
|
|
789 |
|
/** |
835 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
836 |
|
Thread t = new Thread(new CheckedRunnable() { |
837 |
|
public void realRun() throws InterruptedException { |
838 |
< |
q.put(new Integer(SIZE+1)); |
838 |
> |
q.put(new Integer(SIZE + 1)); |
839 |
|
}}); |
840 |
|
|
841 |
|
t.start(); |
860 |
|
q.drainTo(l, i); |
861 |
|
int k = (i < SIZE) ? i : SIZE; |
862 |
|
assertEquals(k, l.size()); |
863 |
< |
assertEquals(SIZE-k, q.size()); |
863 |
> |
assertEquals(SIZE - k, q.size()); |
864 |
|
for (int j = 0; j < k; ++j) |
865 |
|
assertEquals(l.get(j), new Integer(j)); |
866 |
< |
while (q.poll() != null) ; |
866 |
> |
do {} while (q.poll() != null); |
867 |
|
} |
868 |
|
} |
869 |
|
|