6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
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 static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.io.*; |
22 |
|
|
23 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static class Fair extends BlockingQueueTest { |
26 |
|
protected BlockingQueue emptyCollection() { |
27 |
< |
return new ArrayBlockingQueue(20, true); |
27 |
> |
return new ArrayBlockingQueue(SIZE, true); |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
public static class NonFair extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new ArrayBlockingQueue(20, false); |
33 |
> |
return new ArrayBlockingQueue(SIZE, false); |
34 |
|
} |
35 |
|
} |
36 |
|
|
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* Create a queue of given size containing consecutive |
48 |
> |
* Returns a new queue of given size containing consecutive |
49 |
|
* Integers 0 ... n. |
50 |
|
*/ |
51 |
|
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
71 |
|
*/ |
72 |
|
public void testConstructor2() { |
73 |
|
try { |
74 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
74 |
> |
new ArrayBlockingQueue(0); |
75 |
|
shouldThrow(); |
76 |
|
} catch (IllegalArgumentException success) {} |
77 |
|
} |
81 |
|
*/ |
82 |
|
public void testConstructor3() { |
83 |
|
try { |
84 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
84 |
> |
new ArrayBlockingQueue(1, true, null); |
85 |
|
shouldThrow(); |
86 |
|
} catch (NullPointerException success) {} |
87 |
|
} |
90 |
|
* Initializing from Collection of null elements throws NPE |
91 |
|
*/ |
92 |
|
public void testConstructor4() { |
93 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
94 |
|
try { |
95 |
< |
Integer[] ints = new Integer[SIZE]; |
88 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
95 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
96 |
|
shouldThrow(); |
97 |
|
} catch (NullPointerException success) {} |
98 |
|
} |
101 |
|
* Initializing from Collection with some null elements throws NPE |
102 |
|
*/ |
103 |
|
public void testConstructor5() { |
104 |
+ |
Integer[] ints = new Integer[SIZE]; |
105 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
106 |
+ |
ints[i] = i; |
107 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
108 |
|
try { |
109 |
< |
Integer[] ints = new Integer[SIZE]; |
99 |
< |
for (int i = 0; i < SIZE-1; ++i) |
100 |
< |
ints[i] = new Integer(i); |
101 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
109 |
> |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
110 |
|
shouldThrow(); |
111 |
|
} catch (NullPointerException success) {} |
112 |
|
} |
115 |
|
* Initializing from too large collection throws IAE |
116 |
|
*/ |
117 |
|
public void testConstructor6() { |
118 |
+ |
Integer[] ints = new Integer[SIZE]; |
119 |
+ |
for (int i = 0; i < SIZE; ++i) |
120 |
+ |
ints[i] = i; |
121 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
122 |
|
try { |
123 |
< |
Integer[] ints = new Integer[SIZE]; |
112 |
< |
for (int i = 0; i < SIZE; ++i) |
113 |
< |
ints[i] = new Integer(i); |
114 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
123 |
> |
new ArrayBlockingQueue(SIZE - 1, false, elements); |
124 |
|
shouldThrow(); |
125 |
|
} catch (IllegalArgumentException success) {} |
126 |
|
} |
131 |
|
public void testConstructor7() { |
132 |
|
Integer[] ints = new Integer[SIZE]; |
133 |
|
for (int i = 0; i < SIZE; ++i) |
134 |
< |
ints[i] = new Integer(i); |
135 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
134 |
> |
ints[i] = i; |
135 |
> |
Collection<Integer> elements = Arrays.asList(ints); |
136 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements); |
137 |
|
for (int i = 0; i < SIZE; ++i) |
138 |
|
assertEquals(ints[i], q.poll()); |
139 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
164 |
– |
* offer(null) throws NPE |
165 |
– |
*/ |
166 |
– |
public void testOfferNull() { |
167 |
– |
try { |
168 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
169 |
– |
q.offer(null); |
170 |
– |
shouldThrow(); |
171 |
– |
} catch (NullPointerException success) {} |
172 |
– |
} |
173 |
– |
|
174 |
– |
/** |
175 |
– |
* add(null) throws NPE |
176 |
– |
*/ |
177 |
– |
public void testAddNull() { |
178 |
– |
try { |
179 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
180 |
– |
q.add(null); |
181 |
– |
shouldThrow(); |
182 |
– |
} catch (NullPointerException success) {} |
183 |
– |
} |
184 |
– |
|
185 |
– |
/** |
174 |
|
* Offer succeeds if not full; fails if full |
175 |
|
*/ |
176 |
|
public void testOffer() { |
195 |
|
} |
196 |
|
|
197 |
|
/** |
210 |
– |
* addAll(null) throws NPE |
211 |
– |
*/ |
212 |
– |
public void testAddAll1() { |
213 |
– |
try { |
214 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
215 |
– |
q.addAll(null); |
216 |
– |
shouldThrow(); |
217 |
– |
} catch (NullPointerException success) {} |
218 |
– |
} |
219 |
– |
|
220 |
– |
/** |
198 |
|
* addAll(this) throws IAE |
199 |
|
*/ |
200 |
|
public void testAddAllSelf() { |
205 |
|
} catch (IllegalArgumentException success) {} |
206 |
|
} |
207 |
|
|
231 |
– |
|
232 |
– |
/** |
233 |
– |
* addAll of a collection with null elements throws NPE |
234 |
– |
*/ |
235 |
– |
public void testAddAll2() { |
236 |
– |
try { |
237 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
238 |
– |
Integer[] ints = new Integer[SIZE]; |
239 |
– |
q.addAll(Arrays.asList(ints)); |
240 |
– |
shouldThrow(); |
241 |
– |
} catch (NullPointerException success) {} |
242 |
– |
} |
243 |
– |
|
208 |
|
/** |
209 |
|
* addAll of a collection with any null elements throws NPE after |
210 |
|
* possibly adding some elements |
250 |
|
} |
251 |
|
|
252 |
|
/** |
289 |
– |
* put(null) throws NPE |
290 |
– |
*/ |
291 |
– |
public void testPutNull() throws InterruptedException { |
292 |
– |
try { |
293 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
294 |
– |
q.put(null); |
295 |
– |
shouldThrow(); |
296 |
– |
} catch (NullPointerException success) {} |
297 |
– |
} |
298 |
– |
|
299 |
– |
/** |
253 |
|
* all elements successfully put are contained |
254 |
|
*/ |
255 |
|
public void testPut() throws InterruptedException { |
267 |
|
*/ |
268 |
|
public void testBlockingPut() throws InterruptedException { |
269 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
270 |
< |
Thread t = new Thread(new CheckedRunnable() { |
270 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
271 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
272 |
|
public void realRun() throws InterruptedException { |
273 |
|
for (int i = 0; i < SIZE; ++i) |
274 |
|
q.put(i); |
275 |
|
assertEquals(SIZE, q.size()); |
276 |
|
assertEquals(0, q.remainingCapacity()); |
277 |
+ |
|
278 |
+ |
Thread.currentThread().interrupt(); |
279 |
|
try { |
280 |
|
q.put(99); |
281 |
|
shouldThrow(); |
282 |
|
} catch (InterruptedException success) {} |
283 |
+ |
assertFalse(Thread.interrupted()); |
284 |
+ |
|
285 |
+ |
pleaseInterrupt.countDown(); |
286 |
+ |
try { |
287 |
+ |
q.put(99); |
288 |
+ |
shouldThrow(); |
289 |
+ |
} catch (InterruptedException success) {} |
290 |
+ |
assertFalse(Thread.interrupted()); |
291 |
|
}}); |
292 |
|
|
293 |
< |
t.start(); |
294 |
< |
delay(SHORT_DELAY_MS); |
293 |
> |
await(pleaseInterrupt); |
294 |
> |
assertThreadStaysAlive(t); |
295 |
|
t.interrupt(); |
296 |
< |
t.join(); |
296 |
> |
awaitTermination(t); |
297 |
|
assertEquals(SIZE, q.size()); |
298 |
|
assertEquals(0, q.remainingCapacity()); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* put blocks waiting for take when full |
302 |
> |
* put blocks interruptibly waiting for take when full |
303 |
|
*/ |
304 |
|
public void testPutWithTake() throws InterruptedException { |
305 |
|
final int capacity = 2; |
306 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
307 |
< |
Thread t = new Thread(new CheckedRunnable() { |
307 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
308 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
309 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
310 |
|
public void realRun() throws InterruptedException { |
311 |
< |
for (int i = 0; i < capacity + 1; i++) |
311 |
> |
for (int i = 0; i < capacity; i++) |
312 |
|
q.put(i); |
313 |
+ |
pleaseTake.countDown(); |
314 |
+ |
q.put(86); |
315 |
+ |
|
316 |
+ |
pleaseInterrupt.countDown(); |
317 |
|
try { |
318 |
|
q.put(99); |
319 |
|
shouldThrow(); |
320 |
|
} catch (InterruptedException success) {} |
321 |
+ |
assertFalse(Thread.interrupted()); |
322 |
|
}}); |
323 |
|
|
324 |
< |
t.start(); |
325 |
< |
delay(SHORT_DELAY_MS); |
355 |
< |
assertEquals(q.remainingCapacity(), 0); |
324 |
> |
await(pleaseTake); |
325 |
> |
assertEquals(0, q.remainingCapacity()); |
326 |
|
assertEquals(0, q.take()); |
327 |
< |
delay(SHORT_DELAY_MS); |
327 |
> |
|
328 |
> |
await(pleaseInterrupt); |
329 |
> |
assertThreadStaysAlive(t); |
330 |
|
t.interrupt(); |
331 |
< |
t.join(); |
332 |
< |
assertEquals(q.remainingCapacity(), 0); |
331 |
> |
awaitTermination(t); |
332 |
> |
assertEquals(0, q.remainingCapacity()); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
353 |
|
}}); |
354 |
|
|
355 |
|
await(pleaseInterrupt); |
356 |
+ |
assertThreadStaysAlive(t); |
357 |
|
t.interrupt(); |
358 |
|
awaitTermination(t); |
359 |
|
} |
373 |
|
*/ |
374 |
|
public void testBlockingTake() throws InterruptedException { |
375 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
376 |
< |
Thread t = new Thread(new CheckedRunnable() { |
376 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
377 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
378 |
|
public void realRun() throws InterruptedException { |
379 |
|
for (int i = 0; i < SIZE; ++i) { |
380 |
|
assertEquals(i, q.take()); |
381 |
|
} |
382 |
+ |
|
383 |
+ |
Thread.currentThread().interrupt(); |
384 |
|
try { |
385 |
|
q.take(); |
386 |
|
shouldThrow(); |
387 |
|
} catch (InterruptedException success) {} |
388 |
+ |
assertFalse(Thread.interrupted()); |
389 |
+ |
|
390 |
+ |
pleaseInterrupt.countDown(); |
391 |
+ |
try { |
392 |
+ |
q.take(); |
393 |
+ |
shouldThrow(); |
394 |
+ |
} catch (InterruptedException success) {} |
395 |
+ |
assertFalse(Thread.interrupted()); |
396 |
|
}}); |
397 |
|
|
398 |
< |
t.start(); |
399 |
< |
delay(SHORT_DELAY_MS); |
398 |
> |
await(pleaseInterrupt); |
399 |
> |
assertThreadStaysAlive(t); |
400 |
|
t.interrupt(); |
401 |
< |
t.join(); |
401 |
> |
awaitTermination(t); |
402 |
|
} |
403 |
|
|
420 |
– |
|
404 |
|
/** |
405 |
|
* poll succeeds unless empty |
406 |
|
*/ |
421 |
|
assertEquals(i, q.poll(0, MILLISECONDS)); |
422 |
|
} |
423 |
|
assertNull(q.poll(0, MILLISECONDS)); |
424 |
+ |
checkEmpty(q); |
425 |
|
} |
426 |
|
|
427 |
|
/** |
430 |
|
public void testTimedPoll() throws InterruptedException { |
431 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
432 |
|
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
434 |
< |
} |
435 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
433 |
> |
long startTime = System.nanoTime(); |
434 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
435 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
436 |
> |
} |
437 |
> |
long startTime = System.nanoTime(); |
438 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
439 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
440 |
> |
checkEmpty(q); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
515 |
|
} |
516 |
|
|
517 |
|
/** |
529 |
– |
* remove(x) removes x and returns true if present |
530 |
– |
*/ |
531 |
– |
public void testRemoveElement() { |
532 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
533 |
– |
for (int i = 1; i < SIZE; i+=2) { |
534 |
– |
assertTrue(q.remove(new Integer(i))); |
535 |
– |
} |
536 |
– |
for (int i = 0; i < SIZE; i+=2) { |
537 |
– |
assertTrue(q.remove(new Integer(i))); |
538 |
– |
assertFalse(q.remove(new Integer(i+1))); |
539 |
– |
} |
540 |
– |
assertTrue(q.isEmpty()); |
541 |
– |
} |
542 |
– |
|
543 |
– |
/** |
518 |
|
* contains(x) reports true when elements added but not yet removed |
519 |
|
*/ |
520 |
|
public void testContains() { |
591 |
|
} |
592 |
|
} |
593 |
|
|
594 |
< |
/** |
595 |
< |
* toArray contains all elements in FIFO order |
622 |
< |
*/ |
623 |
< |
public void testToArray() { |
624 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
594 |
> |
void checkToArray(ArrayBlockingQueue q) { |
595 |
> |
int size = q.size(); |
596 |
|
Object[] o = q.toArray(); |
597 |
< |
for (int i = 0; i < o.length; i++) |
598 |
< |
assertSame(o[i], q.poll()); |
597 |
> |
assertEquals(size, o.length); |
598 |
> |
Iterator it = q.iterator(); |
599 |
> |
for (int i = 0; i < size; i++) { |
600 |
> |
Integer x = (Integer) it.next(); |
601 |
> |
assertEquals((Integer)o[0] + i, (int) x); |
602 |
> |
assertSame(o[i], x); |
603 |
> |
} |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* toArray(a) contains all elements in FIFO order |
607 |
> |
* toArray() contains all elements in FIFO order |
608 |
|
*/ |
609 |
< |
public void testToArray2() { |
610 |
< |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
611 |
< |
Integer[] ints = new Integer[SIZE]; |
612 |
< |
Integer[] array = q.toArray(ints); |
613 |
< |
assertSame(ints, array); |
614 |
< |
for (int i = 0; i < ints.length; i++) |
615 |
< |
assertSame(ints[i], q.poll()); |
609 |
> |
public void testToArray() { |
610 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
611 |
> |
for (int i = 0; i < SIZE; i++) { |
612 |
> |
checkToArray(q); |
613 |
> |
q.add(i); |
614 |
> |
} |
615 |
> |
// Provoke wraparound |
616 |
> |
for (int i = 0; i < SIZE; i++) { |
617 |
> |
checkToArray(q); |
618 |
> |
assertEquals(i, q.poll()); |
619 |
> |
checkToArray(q); |
620 |
> |
q.add(SIZE+i); |
621 |
> |
} |
622 |
> |
for (int i = 0; i < SIZE; i++) { |
623 |
> |
checkToArray(q); |
624 |
> |
assertEquals(SIZE+i, q.poll()); |
625 |
> |
} |
626 |
> |
} |
627 |
> |
|
628 |
> |
void checkToArray2(ArrayBlockingQueue q) { |
629 |
> |
int size = q.size(); |
630 |
> |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
631 |
> |
Integer[] a2 = new Integer[size]; |
632 |
> |
Integer[] a3 = new Integer[size+2]; |
633 |
> |
if (size > 0) Arrays.fill(a1, 42); |
634 |
> |
Arrays.fill(a2, 42); |
635 |
> |
Arrays.fill(a3, 42); |
636 |
> |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
637 |
> |
Integer[] b2 = (Integer[]) q.toArray(a2); |
638 |
> |
Integer[] b3 = (Integer[]) q.toArray(a3); |
639 |
> |
assertSame(a2, b2); |
640 |
> |
assertSame(a3, b3); |
641 |
> |
Iterator it = q.iterator(); |
642 |
> |
for (int i = 0; i < size; i++) { |
643 |
> |
Integer x = (Integer) it.next(); |
644 |
> |
assertSame(b1[i], x); |
645 |
> |
assertEquals(b1[0] + i, (int) x); |
646 |
> |
assertSame(b2[i], x); |
647 |
> |
assertSame(b3[i], x); |
648 |
> |
} |
649 |
> |
assertNull(a3[size]); |
650 |
> |
assertEquals(42, (int) a3[size+1]); |
651 |
> |
if (size > 0) { |
652 |
> |
assertNotSame(a1, b1); |
653 |
> |
assertEquals(size, b1.length); |
654 |
> |
for (int i = 0; i < a1.length; i++) { |
655 |
> |
assertEquals(42, (int) a1[i]); |
656 |
> |
} |
657 |
> |
} |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* toArray(null) throws NullPointerException |
661 |
> |
* toArray(a) contains all elements in FIFO order |
662 |
|
*/ |
663 |
< |
public void testToArray_NullArg() { |
664 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
665 |
< |
try { |
666 |
< |
q.toArray(null); |
667 |
< |
shouldThrow(); |
668 |
< |
} catch (NullPointerException success) {} |
663 |
> |
public void testToArray2() { |
664 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
665 |
> |
for (int i = 0; i < SIZE; i++) { |
666 |
> |
checkToArray2(q); |
667 |
> |
q.add(i); |
668 |
> |
} |
669 |
> |
// Provoke wraparound |
670 |
> |
for (int i = 0; i < SIZE; i++) { |
671 |
> |
checkToArray2(q); |
672 |
> |
assertEquals(i, q.poll()); |
673 |
> |
checkToArray2(q); |
674 |
> |
q.add(SIZE+i); |
675 |
> |
} |
676 |
> |
for (int i = 0; i < SIZE; i++) { |
677 |
> |
checkToArray2(q); |
678 |
> |
assertEquals(SIZE+i, q.poll()); |
679 |
> |
} |
680 |
|
} |
681 |
|
|
682 |
|
/** |
690 |
|
} catch (ArrayStoreException success) {} |
691 |
|
} |
692 |
|
|
664 |
– |
|
693 |
|
/** |
694 |
|
* iterator iterates through all elements |
695 |
|
*/ |
753 |
|
assertEquals(0, q.size()); |
754 |
|
} |
755 |
|
|
728 |
– |
|
756 |
|
/** |
757 |
|
* toString contains toStrings of elements |
758 |
|
*/ |
760 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
761 |
|
String s = q.toString(); |
762 |
|
for (int i = 0; i < SIZE; ++i) { |
763 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
763 |
> |
assertTrue(s.contains(String.valueOf(i))); |
764 |
|
} |
765 |
|
} |
766 |
|
|
740 |
– |
|
767 |
|
/** |
768 |
|
* offer transfers elements across Executor tasks |
769 |
|
*/ |
772 |
|
q.add(one); |
773 |
|
q.add(two); |
774 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
775 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
776 |
|
executor.execute(new CheckedRunnable() { |
777 |
|
public void realRun() throws InterruptedException { |
778 |
|
assertFalse(q.offer(three)); |
779 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
779 |
> |
threadsStarted.await(); |
780 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
781 |
|
assertEquals(0, q.remainingCapacity()); |
782 |
|
}}); |
783 |
|
|
784 |
|
executor.execute(new CheckedRunnable() { |
785 |
|
public void realRun() throws InterruptedException { |
786 |
< |
delay(SMALL_DELAY_MS); |
786 |
> |
threadsStarted.await(); |
787 |
> |
assertEquals(0, q.remainingCapacity()); |
788 |
|
assertSame(one, q.take()); |
789 |
|
}}); |
790 |
|
|
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* poll retrieves elements across Executor threads |
795 |
> |
* timed poll retrieves elements across Executor threads |
796 |
|
*/ |
797 |
|
public void testPollInExecutor() { |
798 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
799 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
800 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
801 |
|
executor.execute(new CheckedRunnable() { |
802 |
|
public void realRun() throws InterruptedException { |
803 |
|
assertNull(q.poll()); |
804 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
805 |
< |
assertTrue(q.isEmpty()); |
804 |
> |
threadsStarted.await(); |
805 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
806 |
> |
checkEmpty(q); |
807 |
|
}}); |
808 |
|
|
809 |
|
executor.execute(new CheckedRunnable() { |
810 |
|
public void realRun() throws InterruptedException { |
811 |
< |
delay(SMALL_DELAY_MS); |
811 |
> |
threadsStarted.await(); |
812 |
|
q.put(one); |
813 |
|
}}); |
814 |
|
|
819 |
|
* A deserialized serialized queue has same elements in same order |
820 |
|
*/ |
821 |
|
public void testSerialization() throws Exception { |
822 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
823 |
< |
|
793 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
794 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
795 |
< |
out.writeObject(q); |
796 |
< |
out.close(); |
797 |
< |
|
798 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
799 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
800 |
< |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
801 |
< |
assertEquals(q.size(), r.size()); |
802 |
< |
while (!q.isEmpty()) |
803 |
< |
assertEquals(q.remove(), r.remove()); |
804 |
< |
} |
822 |
> |
Queue x = populatedQueue(SIZE); |
823 |
> |
Queue y = serialClone(x); |
824 |
|
|
825 |
< |
/** |
826 |
< |
* drainTo(null) throws NPE |
827 |
< |
*/ |
828 |
< |
public void testDrainToNull() { |
829 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
830 |
< |
try { |
831 |
< |
q.drainTo(null); |
832 |
< |
shouldThrow(); |
833 |
< |
} catch (NullPointerException success) {} |
815 |
< |
} |
816 |
< |
|
817 |
< |
/** |
818 |
< |
* drainTo(this) throws IAE |
819 |
< |
*/ |
820 |
< |
public void testDrainToSelf() { |
821 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
822 |
< |
try { |
823 |
< |
q.drainTo(q); |
824 |
< |
shouldThrow(); |
825 |
< |
} catch (IllegalArgumentException success) {} |
825 |
> |
assertTrue(x != y); |
826 |
> |
assertEquals(x.size(), y.size()); |
827 |
> |
assertEquals(x.toString(), y.toString()); |
828 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
829 |
> |
while (!x.isEmpty()) { |
830 |
> |
assertFalse(y.isEmpty()); |
831 |
> |
assertEquals(x.remove(), y.remove()); |
832 |
> |
} |
833 |
> |
assertTrue(y.isEmpty()); |
834 |
|
} |
835 |
|
|
836 |
|
/** |
840 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
841 |
|
ArrayList l = new ArrayList(); |
842 |
|
q.drainTo(l); |
843 |
< |
assertEquals(q.size(), 0); |
844 |
< |
assertEquals(l.size(), SIZE); |
843 |
> |
assertEquals(0, q.size()); |
844 |
> |
assertEquals(SIZE, l.size()); |
845 |
|
for (int i = 0; i < SIZE; ++i) |
846 |
|
assertEquals(l.get(i), new Integer(i)); |
847 |
|
q.add(zero); |
851 |
|
assertTrue(q.contains(one)); |
852 |
|
l.clear(); |
853 |
|
q.drainTo(l); |
854 |
< |
assertEquals(q.size(), 0); |
855 |
< |
assertEquals(l.size(), 2); |
854 |
> |
assertEquals(0, q.size()); |
855 |
> |
assertEquals(2, l.size()); |
856 |
|
for (int i = 0; i < 2; ++i) |
857 |
|
assertEquals(l.get(i), new Integer(i)); |
858 |
|
} |
878 |
|
} |
879 |
|
|
880 |
|
/** |
873 |
– |
* drainTo(null, n) throws NPE |
874 |
– |
*/ |
875 |
– |
public void testDrainToNullN() { |
876 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
877 |
– |
try { |
878 |
– |
q.drainTo(null, 0); |
879 |
– |
shouldThrow(); |
880 |
– |
} catch (NullPointerException success) {} |
881 |
– |
} |
882 |
– |
|
883 |
– |
/** |
884 |
– |
* drainTo(this, n) throws IAE |
885 |
– |
*/ |
886 |
– |
public void testDrainToSelfN() { |
887 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
888 |
– |
try { |
889 |
– |
q.drainTo(q, 0); |
890 |
– |
shouldThrow(); |
891 |
– |
} catch (IllegalArgumentException success) {} |
892 |
– |
} |
893 |
– |
|
894 |
– |
/** |
881 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
882 |
|
*/ |
883 |
|
public void testDrainToN() { |
888 |
|
ArrayList l = new ArrayList(); |
889 |
|
q.drainTo(l, i); |
890 |
|
int k = (i < SIZE) ? i : SIZE; |
891 |
< |
assertEquals(l.size(), k); |
892 |
< |
assertEquals(q.size(), SIZE-k); |
891 |
> |
assertEquals(k, l.size()); |
892 |
> |
assertEquals(SIZE-k, q.size()); |
893 |
|
for (int j = 0; j < k; ++j) |
894 |
|
assertEquals(l.get(j), new Integer(j)); |
895 |
|
while (q.poll() != null) ; |