6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.io.*; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
32 |
|
|
33 |
|
public static class Bounded extends BlockingQueueTest { |
34 |
|
protected BlockingQueue emptyCollection() { |
35 |
< |
return new LinkedBlockingQueue(20); |
35 |
> |
return new LinkedBlockingQueue(SIZE); |
36 |
|
} |
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() { |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
|
* Integers 0 ... n. |
52 |
|
*/ |
53 |
|
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* Constructor throws IAE if capacity argument nonpositive |
75 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
76 |
|
*/ |
77 |
|
public void testConstructor2() { |
78 |
|
try { |
79 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
79 |
> |
new LinkedBlockingQueue(0); |
80 |
|
shouldThrow(); |
81 |
|
} catch (IllegalArgumentException success) {} |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Initializing from null Collection throws NPE |
85 |
> |
* Initializing from null Collection throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor3() { |
88 |
|
try { |
89 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
89 |
> |
new LinkedBlockingQueue(null); |
90 |
|
shouldThrow(); |
91 |
|
} catch (NullPointerException success) {} |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
< |
* Initializing from Collection of null elements throws NPE |
95 |
> |
* Initializing from Collection of null elements throws NullPointerException |
96 |
|
*/ |
97 |
|
public void testConstructor4() { |
98 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
99 |
|
try { |
100 |
< |
Integer[] ints = new Integer[SIZE]; |
90 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
100 |
> |
new LinkedBlockingQueue(elements); |
101 |
|
shouldThrow(); |
102 |
|
} catch (NullPointerException success) {} |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
< |
* Initializing from Collection with some null elements throws NPE |
106 |
> |
* Initializing from Collection with some null elements throws |
107 |
> |
* NullPointerException |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
+ |
Integer[] ints = new Integer[SIZE]; |
111 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
112 |
+ |
ints[i] = new Integer(i); |
113 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
114 |
|
try { |
115 |
< |
Integer[] ints = new Integer[SIZE]; |
101 |
< |
for (int i = 0; i < SIZE-1; ++i) |
102 |
< |
ints[i] = new Integer(i); |
103 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
115 |
> |
new LinkedBlockingQueue(elements); |
116 |
|
shouldThrow(); |
117 |
|
} catch (NullPointerException success) {} |
118 |
|
} |
148 |
|
* remainingCapacity decreases on add, increases on remove |
149 |
|
*/ |
150 |
|
public void testRemainingCapacity() { |
151 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
151 |
> |
BlockingQueue q = populatedQueue(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
|
assertEquals(i, q.remainingCapacity()); |
154 |
< |
assertEquals(SIZE-i, q.size()); |
155 |
< |
q.remove(); |
154 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
155 |
> |
assertEquals(i, q.remove()); |
156 |
|
} |
157 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
159 |
< |
assertEquals(i, q.size()); |
160 |
< |
q.add(new Integer(i)); |
158 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
159 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
160 |
> |
assertTrue(q.add(i)); |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
153 |
– |
* offer(null) throws NPE |
154 |
– |
*/ |
155 |
– |
public void testOfferNull() { |
156 |
– |
try { |
157 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
158 |
– |
q.offer(null); |
159 |
– |
shouldThrow(); |
160 |
– |
} catch (NullPointerException success) {} |
161 |
– |
} |
162 |
– |
|
163 |
– |
/** |
164 |
– |
* add(null) throws NPE |
165 |
– |
*/ |
166 |
– |
public void testAddNull() { |
167 |
– |
try { |
168 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
169 |
– |
q.add(null); |
170 |
– |
shouldThrow(); |
171 |
– |
} catch (NullPointerException success) {} |
172 |
– |
} |
173 |
– |
|
174 |
– |
/** |
165 |
|
* Offer succeeds if not full; fails if full |
166 |
|
*/ |
167 |
|
public void testOffer() { |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* add succeeds if not full; throws ISE if full |
174 |
> |
* add succeeds if not full; throws IllegalStateException if full |
175 |
|
*/ |
176 |
|
public void testAdd() { |
177 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
178 |
+ |
for (int i = 0; i < SIZE; ++i) |
179 |
+ |
assertTrue(q.add(new Integer(i))); |
180 |
+ |
assertEquals(0, q.remainingCapacity()); |
181 |
|
try { |
188 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
189 |
– |
for (int i = 0; i < SIZE; ++i) { |
190 |
– |
assertTrue(q.add(new Integer(i))); |
191 |
– |
} |
192 |
– |
assertEquals(0, q.remainingCapacity()); |
182 |
|
q.add(new Integer(SIZE)); |
183 |
|
shouldThrow(); |
184 |
|
} catch (IllegalStateException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* addAll(null) throws NPE |
200 |
< |
*/ |
201 |
< |
public void testAddAll1() { |
202 |
< |
try { |
203 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
204 |
< |
q.addAll(null); |
205 |
< |
shouldThrow(); |
206 |
< |
} catch (NullPointerException success) {} |
207 |
< |
} |
208 |
< |
|
209 |
< |
/** |
210 |
< |
* addAll(this) throws IAE |
188 |
> |
* addAll(this) throws IllegalArgumentException |
189 |
|
*/ |
190 |
|
public void testAddAllSelf() { |
191 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
192 |
|
try { |
214 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
193 |
|
q.addAll(q); |
194 |
|
shouldThrow(); |
195 |
|
} catch (IllegalArgumentException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
221 |
– |
* addAll of a collection with null elements throws NPE |
222 |
– |
*/ |
223 |
– |
public void testAddAll2() { |
224 |
– |
try { |
225 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
226 |
– |
Integer[] ints = new Integer[SIZE]; |
227 |
– |
q.addAll(Arrays.asList(ints)); |
228 |
– |
shouldThrow(); |
229 |
– |
} catch (NullPointerException success) {} |
230 |
– |
} |
231 |
– |
|
232 |
– |
/** |
199 |
|
* addAll of a collection with any null elements throws NPE after |
200 |
|
* possibly adding some elements |
201 |
|
*/ |
202 |
|
public void testAddAll3() { |
203 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
204 |
+ |
Integer[] ints = new Integer[SIZE]; |
205 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
206 |
+ |
ints[i] = new Integer(i); |
207 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
208 |
|
try { |
209 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
239 |
< |
Integer[] ints = new Integer[SIZE]; |
240 |
< |
for (int i = 0; i < SIZE-1; ++i) |
241 |
< |
ints[i] = new Integer(i); |
242 |
< |
q.addAll(Arrays.asList(ints)); |
209 |
> |
q.addAll(elements); |
210 |
|
shouldThrow(); |
211 |
|
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* addAll throws ISE if not enough room |
215 |
> |
* addAll throws IllegalStateException if not enough room |
216 |
|
*/ |
217 |
|
public void testAddAll4() { |
218 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); |
219 |
+ |
Integer[] ints = new Integer[SIZE]; |
220 |
+ |
for (int i = 0; i < SIZE; ++i) |
221 |
+ |
ints[i] = new Integer(i); |
222 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
223 |
|
try { |
224 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
253 |
< |
Integer[] ints = new Integer[SIZE]; |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
255 |
< |
ints[i] = new Integer(i); |
256 |
< |
q.addAll(Arrays.asList(ints)); |
224 |
> |
q.addAll(elements); |
225 |
|
shouldThrow(); |
226 |
|
} catch (IllegalStateException success) {} |
227 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
/** |
277 |
– |
* put(null) throws NPE |
278 |
– |
*/ |
279 |
– |
public void testPutNull() throws InterruptedException { |
280 |
– |
try { |
281 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
282 |
– |
q.put(null); |
283 |
– |
shouldThrow(); |
284 |
– |
} catch (NullPointerException success) {} |
285 |
– |
} |
286 |
– |
|
287 |
– |
/** |
245 |
|
* all elements successfully put are contained |
246 |
|
*/ |
247 |
|
public void testPut() throws InterruptedException { |
248 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
249 |
|
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
Integer I = new Integer(i); |
251 |
< |
q.put(I); |
252 |
< |
assertTrue(q.contains(I)); |
250 |
> |
Integer x = new Integer(i); |
251 |
> |
q.put(x); |
252 |
> |
assertTrue(q.contains(x)); |
253 |
|
} |
254 |
|
assertEquals(0, q.remainingCapacity()); |
255 |
|
} |
314 |
|
}}); |
315 |
|
|
316 |
|
await(pleaseTake); |
317 |
< |
assertEquals(q.remainingCapacity(), 0); |
317 |
> |
assertEquals(0, q.remainingCapacity()); |
318 |
|
assertEquals(0, q.take()); |
319 |
|
|
320 |
|
await(pleaseInterrupt); |
321 |
|
assertThreadStaysAlive(t); |
322 |
|
t.interrupt(); |
323 |
|
awaitTermination(t); |
324 |
< |
assertEquals(q.remainingCapacity(), 0); |
324 |
> |
assertEquals(0, q.remainingCapacity()); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
440 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
441 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
442 |
|
public void realRun() throws InterruptedException { |
443 |
+ |
long startTime = System.nanoTime(); |
444 |
|
for (int i = 0; i < SIZE; ++i) { |
487 |
– |
long t0 = System.nanoTime(); |
445 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
489 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
446 |
|
} |
491 |
– |
long t0 = System.nanoTime(); |
447 |
|
aboutToWait.countDown(); |
448 |
|
try { |
449 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
449 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
450 |
|
shouldThrow(); |
451 |
|
} catch (InterruptedException success) { |
452 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
452 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
453 |
|
} |
454 |
|
}}); |
455 |
|
|
456 |
< |
aboutToWait.await(); |
457 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
456 |
> |
await(aboutToWait); |
457 |
> |
waitForThreadToEnterWaitState(t); |
458 |
|
t.interrupt(); |
459 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
459 |
> |
awaitTermination(t); |
460 |
|
checkEmpty(q); |
461 |
|
} |
462 |
|
|
504 |
|
} |
505 |
|
|
506 |
|
/** |
552 |
– |
* remove(x) removes x and returns true if present |
553 |
– |
*/ |
554 |
– |
public void testRemoveElement() { |
555 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
556 |
– |
for (int i = 1; i < SIZE; i+=2) { |
557 |
– |
assertTrue(q.contains(i)); |
558 |
– |
assertTrue(q.remove(i)); |
559 |
– |
assertFalse(q.contains(i)); |
560 |
– |
assertTrue(q.contains(i-1)); |
561 |
– |
} |
562 |
– |
for (int i = 0; i < SIZE; i+=2) { |
563 |
– |
assertTrue(q.contains(i)); |
564 |
– |
assertTrue(q.remove(i)); |
565 |
– |
assertFalse(q.contains(i)); |
566 |
– |
assertFalse(q.remove(i+1)); |
567 |
– |
assertFalse(q.contains(i+1)); |
568 |
– |
} |
569 |
– |
assertTrue(q.isEmpty()); |
570 |
– |
} |
571 |
– |
|
572 |
– |
/** |
507 |
|
* An add following remove(x) succeeds |
508 |
|
*/ |
509 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
513 |
|
assertTrue(q.remove(new Integer(1))); |
514 |
|
assertTrue(q.remove(new Integer(2))); |
515 |
|
assertTrue(q.add(new Integer(3))); |
516 |
< |
assertTrue(q.take() != null); |
516 |
> |
assertNotNull(q.take()); |
517 |
|
} |
518 |
|
|
519 |
|
/** |
572 |
|
assertTrue(changed); |
573 |
|
|
574 |
|
assertTrue(q.containsAll(p)); |
575 |
< |
assertEquals(SIZE-i, q.size()); |
575 |
> |
assertEquals(SIZE - i, q.size()); |
576 |
|
p.remove(); |
577 |
|
} |
578 |
|
} |
585 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
586 |
|
LinkedBlockingQueue p = populatedQueue(i); |
587 |
|
assertTrue(q.removeAll(p)); |
588 |
< |
assertEquals(SIZE-i, q.size()); |
588 |
> |
assertEquals(SIZE - i, q.size()); |
589 |
|
for (int j = 0; j < i; ++j) { |
590 |
< |
Integer I = (Integer)(p.remove()); |
591 |
< |
assertFalse(q.contains(I)); |
590 |
> |
Integer x = (Integer)(p.remove()); |
591 |
> |
assertFalse(q.contains(x)); |
592 |
|
} |
593 |
|
} |
594 |
|
} |
616 |
|
} |
617 |
|
|
618 |
|
/** |
685 |
– |
* toArray(null) throws NullPointerException |
686 |
– |
*/ |
687 |
– |
public void testToArray_NullArg() { |
688 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
689 |
– |
try { |
690 |
– |
q.toArray(null); |
691 |
– |
shouldThrow(); |
692 |
– |
} catch (NullPointerException success) {} |
693 |
– |
} |
694 |
– |
|
695 |
– |
/** |
619 |
|
* toArray(incompatible array type) throws ArrayStoreException |
620 |
|
*/ |
621 |
|
public void testToArray1_BadArg() { |
632 |
|
public void testIterator() throws InterruptedException { |
633 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
634 |
|
Iterator it = q.iterator(); |
635 |
< |
while (it.hasNext()) { |
635 |
> |
int i; |
636 |
> |
for (i = 0; it.hasNext(); i++) |
637 |
> |
assertTrue(q.contains(it.next())); |
638 |
> |
assertEquals(i, SIZE); |
639 |
> |
assertIteratorExhausted(it); |
640 |
> |
|
641 |
> |
it = q.iterator(); |
642 |
> |
for (i = 0; it.hasNext(); i++) |
643 |
|
assertEquals(it.next(), q.take()); |
644 |
< |
} |
644 |
> |
assertEquals(i, SIZE); |
645 |
> |
assertIteratorExhausted(it); |
646 |
> |
} |
647 |
> |
|
648 |
> |
/** |
649 |
> |
* iterator of empty collection has no elements |
650 |
> |
*/ |
651 |
> |
public void testEmptyIterator() { |
652 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
720 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
721 |
|
q.add(one); |
722 |
|
q.add(two); |
785 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
723 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
724 |
< |
executor.execute(new CheckedRunnable() { |
725 |
< |
public void realRun() throws InterruptedException { |
726 |
< |
assertFalse(q.offer(three)); |
727 |
< |
threadsStarted.await(); |
728 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
729 |
< |
assertEquals(0, q.remainingCapacity()); |
730 |
< |
}}); |
731 |
< |
|
732 |
< |
executor.execute(new CheckedRunnable() { |
733 |
< |
public void realRun() throws InterruptedException { |
734 |
< |
threadsStarted.await(); |
735 |
< |
assertSame(one, q.take()); |
736 |
< |
}}); |
737 |
< |
|
738 |
< |
joinPool(executor); |
724 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
725 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
726 |
> |
executor.execute(new CheckedRunnable() { |
727 |
> |
public void realRun() throws InterruptedException { |
728 |
> |
assertFalse(q.offer(three)); |
729 |
> |
threadsStarted.await(); |
730 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
731 |
> |
assertEquals(0, q.remainingCapacity()); |
732 |
> |
}}); |
733 |
> |
|
734 |
> |
executor.execute(new CheckedRunnable() { |
735 |
> |
public void realRun() throws InterruptedException { |
736 |
> |
threadsStarted.await(); |
737 |
> |
assertSame(one, q.take()); |
738 |
> |
}}); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
745 |
|
public void testPollInExecutor() { |
746 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
747 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
748 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
< |
executor.execute(new CheckedRunnable() { |
750 |
< |
public void realRun() throws InterruptedException { |
751 |
< |
assertNull(q.poll()); |
752 |
< |
threadsStarted.await(); |
753 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
754 |
< |
checkEmpty(q); |
755 |
< |
}}); |
756 |
< |
|
757 |
< |
executor.execute(new CheckedRunnable() { |
758 |
< |
public void realRun() throws InterruptedException { |
759 |
< |
threadsStarted.await(); |
760 |
< |
q.put(one); |
761 |
< |
}}); |
762 |
< |
|
763 |
< |
joinPool(executor); |
748 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
749 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
750 |
> |
executor.execute(new CheckedRunnable() { |
751 |
> |
public void realRun() throws InterruptedException { |
752 |
> |
assertNull(q.poll()); |
753 |
> |
threadsStarted.await(); |
754 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
755 |
> |
checkEmpty(q); |
756 |
> |
}}); |
757 |
> |
|
758 |
> |
executor.execute(new CheckedRunnable() { |
759 |
> |
public void realRun() throws InterruptedException { |
760 |
> |
threadsStarted.await(); |
761 |
> |
q.put(one); |
762 |
> |
}}); |
763 |
> |
} |
764 |
|
} |
765 |
|
|
766 |
|
/** |
767 |
|
* A deserialized serialized queue has same elements in same order |
768 |
|
*/ |
769 |
|
public void testSerialization() throws Exception { |
770 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
770 |
> |
Queue x = populatedQueue(SIZE); |
771 |
> |
Queue y = serialClone(x); |
772 |
|
|
773 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
774 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
775 |
< |
out.writeObject(q); |
776 |
< |
out.close(); |
777 |
< |
|
778 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
779 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
780 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
781 |
< |
assertEquals(q.size(), r.size()); |
843 |
< |
while (!q.isEmpty()) |
844 |
< |
assertEquals(q.remove(), r.remove()); |
845 |
< |
} |
846 |
< |
|
847 |
< |
/** |
848 |
< |
* drainTo(null) throws NPE |
849 |
< |
*/ |
850 |
< |
public void testDrainToNull() { |
851 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
852 |
< |
try { |
853 |
< |
q.drainTo(null); |
854 |
< |
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) {} |
856 |
< |
} |
857 |
< |
|
858 |
< |
/** |
859 |
< |
* drainTo(this) throws IAE |
860 |
< |
*/ |
861 |
< |
public void testDrainToSelf() { |
862 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
863 |
< |
try { |
864 |
< |
q.drainTo(q); |
865 |
< |
shouldThrow(); |
866 |
< |
} catch (IllegalArgumentException success) {} |
773 |
> |
assertNotSame(x, y); |
774 |
> |
assertEquals(x.size(), y.size()); |
775 |
> |
assertEquals(x.toString(), y.toString()); |
776 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
777 |
> |
while (!x.isEmpty()) { |
778 |
> |
assertFalse(y.isEmpty()); |
779 |
> |
assertEquals(x.remove(), y.remove()); |
780 |
> |
} |
781 |
> |
assertTrue(y.isEmpty()); |
782 |
|
} |
783 |
|
|
784 |
|
/** |
788 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
789 |
|
ArrayList l = new ArrayList(); |
790 |
|
q.drainTo(l); |
791 |
< |
assertEquals(q.size(), 0); |
792 |
< |
assertEquals(l.size(), SIZE); |
791 |
> |
assertEquals(0, q.size()); |
792 |
> |
assertEquals(SIZE, l.size()); |
793 |
|
for (int i = 0; i < SIZE; ++i) |
794 |
|
assertEquals(l.get(i), new Integer(i)); |
795 |
|
q.add(zero); |
799 |
|
assertTrue(q.contains(one)); |
800 |
|
l.clear(); |
801 |
|
q.drainTo(l); |
802 |
< |
assertEquals(q.size(), 0); |
803 |
< |
assertEquals(l.size(), 2); |
802 |
> |
assertEquals(0, q.size()); |
803 |
> |
assertEquals(2, l.size()); |
804 |
|
for (int i = 0; i < 2; ++i) |
805 |
|
assertEquals(l.get(i), new Integer(i)); |
806 |
|
} |
812 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
813 |
|
Thread t = new Thread(new CheckedRunnable() { |
814 |
|
public void realRun() throws InterruptedException { |
815 |
< |
q.put(new Integer(SIZE+1)); |
815 |
> |
q.put(new Integer(SIZE + 1)); |
816 |
|
}}); |
817 |
|
|
818 |
|
t.start(); |
826 |
|
} |
827 |
|
|
828 |
|
/** |
914 |
– |
* drainTo(null, n) throws NPE |
915 |
– |
*/ |
916 |
– |
public void testDrainToNullN() { |
917 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
918 |
– |
try { |
919 |
– |
q.drainTo(null, 0); |
920 |
– |
shouldThrow(); |
921 |
– |
} catch (NullPointerException success) {} |
922 |
– |
} |
923 |
– |
|
924 |
– |
/** |
925 |
– |
* drainTo(this, n) throws IAE |
926 |
– |
*/ |
927 |
– |
public void testDrainToSelfN() { |
928 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
929 |
– |
try { |
930 |
– |
q.drainTo(q, 0); |
931 |
– |
shouldThrow(); |
932 |
– |
} catch (IllegalArgumentException success) {} |
933 |
– |
} |
934 |
– |
|
935 |
– |
/** |
829 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
830 |
|
*/ |
831 |
|
public void testDrainToN() { |
836 |
|
ArrayList l = new ArrayList(); |
837 |
|
q.drainTo(l, i); |
838 |
|
int k = (i < SIZE) ? i : SIZE; |
839 |
< |
assertEquals(l.size(), k); |
840 |
< |
assertEquals(q.size(), SIZE-k); |
839 |
> |
assertEquals(k, l.size()); |
840 |
> |
assertEquals(SIZE - k, q.size()); |
841 |
|
for (int j = 0; j < k; ++j) |
842 |
|
assertEquals(l.get(j), new Integer(j)); |
843 |
< |
while (q.poll() != null) ; |
843 |
> |
do {} while (q.poll() != null); |
844 |
> |
} |
845 |
> |
} |
846 |
> |
|
847 |
> |
/** |
848 |
> |
* remove(null), contains(null) always return false |
849 |
> |
*/ |
850 |
> |
public void testNeverContainsNull() { |
851 |
> |
Collection<?>[] qs = { |
852 |
> |
new LinkedBlockingQueue<Object>(), |
853 |
> |
populatedQueue(2), |
854 |
> |
}; |
855 |
> |
|
856 |
> |
for (Collection<?> q : qs) { |
857 |
> |
assertFalse(q.contains(null)); |
858 |
> |
assertFalse(q.remove(null)); |
859 |
|
} |
860 |
|
} |
861 |
|
|