1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
16 |
+ |
|
17 |
+ |
public static class Fair extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new ArrayBlockingQueue(20, true); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class NonFair extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new ArrayBlockingQueue(20, false); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
+ |
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
34 |
> |
return newTestSuite(ArrayBlockingQueueTest.class, |
35 |
> |
new Fair().testSuite(), |
36 |
> |
new NonFair().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
|
* Create a queue of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
42 |
|
*/ |
43 |
< |
private ArrayBlockingQueue populatedQueue(int n) { |
44 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
43 |
> |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
44 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
45 |
|
assertTrue(q.isEmpty()); |
46 |
|
for (int i = 0; i < n; i++) |
47 |
|
assertTrue(q.offer(new Integer(i))); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* offer(null) throws NPE |
163 |
> |
* offer(null) throws NPE |
164 |
|
*/ |
165 |
|
public void testOfferNull() { |
166 |
|
try { |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* add(null) throws NPE |
174 |
> |
* add(null) throws NPE |
175 |
|
*/ |
176 |
|
public void testAddNull() { |
177 |
|
try { |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* addAll(null) throws NPE |
209 |
> |
* addAll(null) throws NPE |
210 |
|
*/ |
211 |
|
public void testAddAll1() { |
212 |
|
try { |
227 |
|
} catch (IllegalArgumentException success) {} |
228 |
|
} |
229 |
|
|
215 |
– |
|
230 |
|
/** |
231 |
< |
* addAll of a collection with null elements throws NPE |
231 |
> |
* addAll of a collection with null elements throws NPE |
232 |
|
*/ |
233 |
|
public void testAddAll2() { |
234 |
|
try { |
238 |
|
shouldThrow(); |
239 |
|
} catch (NullPointerException success) {} |
240 |
|
} |
241 |
+ |
|
242 |
|
/** |
243 |
|
* addAll of a collection with any null elements throws NPE after |
244 |
|
* possibly adding some elements |
253 |
|
shouldThrow(); |
254 |
|
} catch (NullPointerException success) {} |
255 |
|
} |
256 |
+ |
|
257 |
|
/** |
258 |
|
* addAll throws ISE if not enough room |
259 |
|
*/ |
267 |
|
shouldThrow(); |
268 |
|
} catch (IllegalStateException success) {} |
269 |
|
} |
270 |
+ |
|
271 |
|
/** |
272 |
|
* Queue contains all elements, in traversal order, of successful addAll |
273 |
|
*/ |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* put(null) throws NPE |
287 |
> |
* put(null) throws NPE |
288 |
|
*/ |
289 |
|
public void testPutNull() throws InterruptedException { |
290 |
|
try { |
292 |
|
q.put(null); |
293 |
|
shouldThrow(); |
294 |
|
} catch (NullPointerException success) {} |
295 |
< |
} |
295 |
> |
} |
296 |
|
|
297 |
|
/** |
298 |
|
* all elements successfully put are contained |
312 |
|
*/ |
313 |
|
public void testBlockingPut() throws InterruptedException { |
314 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
315 |
< |
Thread t = new Thread(new CheckedRunnable() { |
316 |
< |
public void realRun() { |
317 |
< |
int added = 0; |
315 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
316 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
317 |
> |
public void realRun() throws InterruptedException { |
318 |
> |
for (int i = 0; i < SIZE; ++i) |
319 |
> |
q.put(i); |
320 |
> |
assertEquals(SIZE, q.size()); |
321 |
> |
assertEquals(0, q.remainingCapacity()); |
322 |
> |
|
323 |
> |
Thread.currentThread().interrupt(); |
324 |
|
try { |
325 |
< |
for (int i = 0; i < SIZE; ++i) { |
326 |
< |
q.put(new Integer(i)); |
327 |
< |
++added; |
328 |
< |
} |
306 |
< |
q.put(new Integer(SIZE)); |
307 |
< |
threadShouldThrow(); |
308 |
< |
} catch (InterruptedException success) { |
309 |
< |
threadAssertEquals(added, SIZE); |
310 |
< |
}}}); |
325 |
> |
q.put(99); |
326 |
> |
shouldThrow(); |
327 |
> |
} catch (InterruptedException success) {} |
328 |
> |
assertFalse(Thread.interrupted()); |
329 |
|
|
330 |
< |
t.start(); |
331 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
330 |
> |
pleaseInterrupt.countDown(); |
331 |
> |
try { |
332 |
> |
q.put(99); |
333 |
> |
shouldThrow(); |
334 |
> |
} catch (InterruptedException success) {} |
335 |
> |
assertFalse(Thread.interrupted()); |
336 |
> |
}}); |
337 |
> |
|
338 |
> |
await(pleaseInterrupt); |
339 |
> |
assertThreadStaysAlive(t); |
340 |
|
t.interrupt(); |
341 |
< |
t.join(); |
341 |
> |
awaitTermination(t); |
342 |
> |
assertEquals(SIZE, q.size()); |
343 |
> |
assertEquals(0, q.remainingCapacity()); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* put blocks waiting for take when full |
347 |
> |
* put blocks interruptibly waiting for take when full |
348 |
|
*/ |
349 |
|
public void testPutWithTake() throws InterruptedException { |
350 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
351 |
< |
Thread t = new Thread(new CheckedRunnable() { |
352 |
< |
public void realRun() { |
353 |
< |
int added = 0; |
350 |
> |
final int capacity = 2; |
351 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
352 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
353 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
354 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
355 |
> |
public void realRun() throws InterruptedException { |
356 |
> |
for (int i = 0; i < capacity; i++) |
357 |
> |
q.put(i); |
358 |
> |
pleaseTake.countDown(); |
359 |
> |
q.put(86); |
360 |
> |
|
361 |
> |
pleaseInterrupt.countDown(); |
362 |
|
try { |
363 |
< |
q.put(new Object()); |
364 |
< |
++added; |
365 |
< |
q.put(new Object()); |
366 |
< |
++added; |
331 |
< |
q.put(new Object()); |
332 |
< |
++added; |
333 |
< |
q.put(new Object()); |
334 |
< |
++added; |
335 |
< |
threadShouldThrow(); |
336 |
< |
} catch (InterruptedException success) { |
337 |
< |
threadAssertTrue(added >= 2); |
338 |
< |
} |
363 |
> |
q.put(99); |
364 |
> |
shouldThrow(); |
365 |
> |
} catch (InterruptedException success) {} |
366 |
> |
assertFalse(Thread.interrupted()); |
367 |
|
}}); |
368 |
|
|
369 |
< |
t.start(); |
370 |
< |
Thread.sleep(SHORT_DELAY_MS); |
371 |
< |
q.take(); |
369 |
> |
await(pleaseTake); |
370 |
> |
assertEquals(q.remainingCapacity(), 0); |
371 |
> |
assertEquals(0, q.take()); |
372 |
> |
|
373 |
> |
await(pleaseInterrupt); |
374 |
> |
assertThreadStaysAlive(t); |
375 |
|
t.interrupt(); |
376 |
< |
t.join(); |
376 |
> |
awaitTermination(t); |
377 |
> |
assertEquals(q.remainingCapacity(), 0); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
382 |
|
*/ |
383 |
|
public void testTimedOffer() throws InterruptedException { |
384 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
385 |
< |
Thread t = new Thread(new CheckedRunnable() { |
385 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
386 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
387 |
|
public void realRun() throws InterruptedException { |
388 |
|
q.put(new Object()); |
389 |
|
q.put(new Object()); |
390 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
390 |
> |
long startTime = System.nanoTime(); |
391 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
392 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
393 |
> |
pleaseInterrupt.countDown(); |
394 |
|
try { |
395 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
395 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
396 |
|
shouldThrow(); |
397 |
|
} catch (InterruptedException success) {} |
398 |
|
}}); |
399 |
|
|
400 |
< |
t.start(); |
401 |
< |
Thread.sleep(SHORT_DELAY_MS); |
400 |
> |
await(pleaseInterrupt); |
401 |
> |
assertThreadStaysAlive(t); |
402 |
|
t.interrupt(); |
403 |
< |
t.join(); |
403 |
> |
awaitTermination(t); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
409 |
|
public void testTake() throws InterruptedException { |
410 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
411 |
|
for (int i = 0; i < SIZE; ++i) { |
412 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
412 |
> |
assertEquals(i, q.take()); |
413 |
|
} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
381 |
– |
* take blocks interruptibly when empty |
382 |
– |
*/ |
383 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
384 |
– |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
385 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
386 |
– |
public void realRun() throws InterruptedException { |
387 |
– |
q.take(); |
388 |
– |
}}; |
389 |
– |
|
390 |
– |
t.start(); |
391 |
– |
Thread.sleep(SHORT_DELAY_MS); |
392 |
– |
t.interrupt(); |
393 |
– |
t.join(); |
394 |
– |
} |
395 |
– |
|
396 |
– |
/** |
417 |
|
* Take removes existing elements until empty, then blocks interruptibly |
418 |
|
*/ |
419 |
|
public void testBlockingTake() throws InterruptedException { |
420 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
420 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
421 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
422 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
423 |
|
public void realRun() throws InterruptedException { |
402 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
425 |
> |
assertEquals(i, q.take()); |
426 |
|
} |
406 |
– |
q.take(); |
407 |
– |
}}; |
427 |
|
|
428 |
< |
t.start(); |
429 |
< |
Thread.sleep(SHORT_DELAY_MS); |
430 |
< |
t.interrupt(); |
431 |
< |
t.join(); |
432 |
< |
} |
428 |
> |
Thread.currentThread().interrupt(); |
429 |
> |
try { |
430 |
> |
q.take(); |
431 |
> |
shouldThrow(); |
432 |
> |
} catch (InterruptedException success) {} |
433 |
> |
assertFalse(Thread.interrupted()); |
434 |
> |
|
435 |
> |
pleaseInterrupt.countDown(); |
436 |
> |
try { |
437 |
> |
q.take(); |
438 |
> |
shouldThrow(); |
439 |
> |
} catch (InterruptedException success) {} |
440 |
> |
assertFalse(Thread.interrupted()); |
441 |
> |
}}); |
442 |
|
|
443 |
+ |
await(pleaseInterrupt); |
444 |
+ |
assertThreadStaysAlive(t); |
445 |
+ |
t.interrupt(); |
446 |
+ |
awaitTermination(t); |
447 |
+ |
} |
448 |
|
|
449 |
|
/** |
450 |
|
* poll succeeds unless empty |
452 |
|
public void testPoll() { |
453 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
454 |
|
for (int i = 0; i < SIZE; ++i) { |
455 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
455 |
> |
assertEquals(i, q.poll()); |
456 |
|
} |
457 |
|
assertNull(q.poll()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
461 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
462 |
|
*/ |
463 |
|
public void testTimedPoll0() throws InterruptedException { |
464 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
465 |
|
for (int i = 0; i < SIZE; ++i) { |
466 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
466 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
467 |
|
} |
468 |
|
assertNull(q.poll(0, MILLISECONDS)); |
469 |
+ |
checkEmpty(q); |
470 |
|
} |
471 |
|
|
472 |
|
/** |
473 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
473 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
474 |
|
*/ |
475 |
|
public void testTimedPoll() throws InterruptedException { |
476 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
479 |
< |
} |
480 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
478 |
> |
long startTime = System.nanoTime(); |
479 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
480 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
481 |
> |
} |
482 |
> |
long startTime = System.nanoTime(); |
483 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
484 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
485 |
> |
checkEmpty(q); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
490 |
|
* returning timeout status |
491 |
|
*/ |
492 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
493 |
< |
Thread t = new Thread(new CheckedRunnable() { |
493 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
494 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
495 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
496 |
|
public void realRun() throws InterruptedException { |
456 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
497 |
|
for (int i = 0; i < SIZE; ++i) { |
498 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
498 |
> |
long t0 = System.nanoTime(); |
499 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
500 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
501 |
|
} |
502 |
+ |
long t0 = System.nanoTime(); |
503 |
+ |
aboutToWait.countDown(); |
504 |
|
try { |
505 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
462 |
< |
shouldThrow(); |
463 |
< |
} catch (InterruptedException success) {} |
464 |
< |
}}); |
465 |
< |
|
466 |
< |
t.start(); |
467 |
< |
Thread.sleep(SHORT_DELAY_MS); |
468 |
< |
t.interrupt(); |
469 |
< |
t.join(); |
470 |
< |
} |
471 |
< |
|
472 |
< |
/** |
473 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
474 |
< |
* on interruption throws |
475 |
< |
*/ |
476 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
477 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
478 |
< |
Thread t = new Thread(new CheckedRunnable() { |
479 |
< |
public void realRun() throws InterruptedException { |
480 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
481 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
482 |
< |
try { |
483 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
505 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
506 |
|
shouldThrow(); |
507 |
< |
} catch (InterruptedException success) {} |
507 |
> |
} catch (InterruptedException success) { |
508 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
509 |
> |
} |
510 |
|
}}); |
511 |
|
|
512 |
< |
t.start(); |
513 |
< |
Thread.sleep(SMALL_DELAY_MS); |
490 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
512 |
> |
aboutToWait.await(); |
513 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
514 |
|
t.interrupt(); |
515 |
< |
t.join(); |
515 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
516 |
> |
checkEmpty(q); |
517 |
|
} |
518 |
|
|
495 |
– |
|
519 |
|
/** |
520 |
|
* peek returns next element, or null if empty |
521 |
|
*/ |
522 |
|
public void testPeek() { |
523 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
524 |
|
for (int i = 0; i < SIZE; ++i) { |
525 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
526 |
< |
q.poll(); |
525 |
> |
assertEquals(i, q.peek()); |
526 |
> |
assertEquals(i, q.poll()); |
527 |
|
assertTrue(q.peek() == null || |
528 |
< |
i != ((Integer)q.peek()).intValue()); |
528 |
> |
!q.peek().equals(i)); |
529 |
|
} |
530 |
|
assertNull(q.peek()); |
531 |
|
} |
536 |
|
public void testElement() { |
537 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
538 |
|
for (int i = 0; i < SIZE; ++i) { |
539 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
540 |
< |
q.poll(); |
539 |
> |
assertEquals(i, q.element()); |
540 |
> |
assertEquals(i, q.poll()); |
541 |
|
} |
542 |
|
try { |
543 |
|
q.element(); |
551 |
|
public void testRemove() { |
552 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
553 |
|
for (int i = 0; i < SIZE; ++i) { |
554 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
554 |
> |
assertEquals(i, q.remove()); |
555 |
|
} |
556 |
|
try { |
557 |
|
q.remove(); |
581 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
582 |
|
for (int i = 0; i < SIZE; ++i) { |
583 |
|
assertTrue(q.contains(new Integer(i))); |
584 |
< |
q.poll(); |
584 |
> |
assertEquals(i, q.poll()); |
585 |
|
assertFalse(q.contains(new Integer(i))); |
586 |
|
} |
587 |
|
} |
652 |
|
} |
653 |
|
|
654 |
|
/** |
655 |
< |
* toArray contains all elements |
655 |
> |
* toArray contains all elements in FIFO order |
656 |
|
*/ |
657 |
< |
public void testToArray() throws InterruptedException { |
657 |
> |
public void testToArray() { |
658 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
659 |
|
Object[] o = q.toArray(); |
660 |
|
for (int i = 0; i < o.length; i++) |
661 |
< |
assertEquals(o[i], q.take()); |
661 |
> |
assertSame(o[i], q.poll()); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* toArray(a) contains all elements |
665 |
> |
* toArray(a) contains all elements in FIFO order |
666 |
|
*/ |
667 |
< |
public void testToArray2() throws InterruptedException { |
668 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
667 |
> |
public void testToArray2() { |
668 |
> |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
669 |
|
Integer[] ints = new Integer[SIZE]; |
670 |
< |
ints = (Integer[])q.toArray(ints); |
670 |
> |
Integer[] array = q.toArray(ints); |
671 |
> |
assertSame(ints, array); |
672 |
|
for (int i = 0; i < ints.length; i++) |
673 |
< |
assertEquals(ints[i], q.take()); |
673 |
> |
assertSame(ints[i], q.poll()); |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
< |
* toArray(null) throws NPE |
677 |
> |
* toArray(null) throws NullPointerException |
678 |
|
*/ |
679 |
< |
public void testToArray_BadArg() { |
679 |
> |
public void testToArray_NullArg() { |
680 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
681 |
|
try { |
682 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
658 |
< |
Object o[] = q.toArray(null); |
682 |
> |
q.toArray(null); |
683 |
|
shouldThrow(); |
684 |
|
} catch (NullPointerException success) {} |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
< |
* toArray with incompatible array type throws CCE |
688 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
689 |
|
*/ |
690 |
|
public void testToArray1_BadArg() { |
691 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
692 |
|
try { |
693 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
669 |
< |
Object o[] = q.toArray(new String[10] ); |
693 |
> |
q.toArray(new String[10]); |
694 |
|
shouldThrow(); |
695 |
|
} catch (ArrayStoreException success) {} |
696 |
|
} |
697 |
|
|
674 |
– |
|
698 |
|
/** |
699 |
|
* iterator iterates through all elements |
700 |
|
*/ |
709 |
|
/** |
710 |
|
* iterator.remove removes current element |
711 |
|
*/ |
712 |
< |
public void testIteratorRemove () { |
712 |
> |
public void testIteratorRemove() { |
713 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
714 |
|
q.add(two); |
715 |
|
q.add(one); |
720 |
|
it.remove(); |
721 |
|
|
722 |
|
it = q.iterator(); |
723 |
< |
assertEquals(it.next(), one); |
724 |
< |
assertEquals(it.next(), three); |
723 |
> |
assertSame(it.next(), one); |
724 |
> |
assertSame(it.next(), three); |
725 |
|
assertFalse(it.hasNext()); |
726 |
|
} |
727 |
|
|
738 |
|
|
739 |
|
int k = 0; |
740 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
741 |
< |
int i = ((Integer)(it.next())).intValue(); |
719 |
< |
assertEquals(++k, i); |
741 |
> |
assertEquals(++k, it.next()); |
742 |
|
} |
743 |
|
assertEquals(3, k); |
744 |
|
} |
746 |
|
/** |
747 |
|
* Modifications do not cause iterators to fail |
748 |
|
*/ |
749 |
< |
public void testWeaklyConsistentIteration () { |
749 |
> |
public void testWeaklyConsistentIteration() { |
750 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
751 |
|
q.add(one); |
752 |
|
q.add(two); |
758 |
|
assertEquals(0, q.size()); |
759 |
|
} |
760 |
|
|
739 |
– |
|
761 |
|
/** |
762 |
|
* toString contains toStrings of elements |
763 |
|
*/ |
765 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
766 |
|
String s = q.toString(); |
767 |
|
for (int i = 0; i < SIZE; ++i) { |
768 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
768 |
> |
assertTrue(s.contains(String.valueOf(i))); |
769 |
|
} |
770 |
|
} |
771 |
|
|
751 |
– |
|
772 |
|
/** |
773 |
|
* offer transfers elements across Executor tasks |
774 |
|
*/ |
777 |
|
q.add(one); |
778 |
|
q.add(two); |
779 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
780 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
781 |
|
executor.execute(new CheckedRunnable() { |
782 |
|
public void realRun() throws InterruptedException { |
783 |
< |
threadAssertFalse(q.offer(three)); |
784 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
785 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
783 |
> |
assertFalse(q.offer(three)); |
784 |
> |
threadsStarted.await(); |
785 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
786 |
> |
assertEquals(0, q.remainingCapacity()); |
787 |
|
}}); |
788 |
|
|
789 |
|
executor.execute(new CheckedRunnable() { |
790 |
|
public void realRun() throws InterruptedException { |
791 |
< |
Thread.sleep(SMALL_DELAY_MS); |
792 |
< |
threadAssertEquals(one, q.take()); |
791 |
> |
threadsStarted.await(); |
792 |
> |
assertEquals(0, q.remainingCapacity()); |
793 |
> |
assertSame(one, q.take()); |
794 |
|
}}); |
795 |
|
|
796 |
|
joinPool(executor); |
774 |
– |
|
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
< |
* poll retrieves elements across Executor threads |
800 |
> |
* timed poll retrieves elements across Executor threads |
801 |
|
*/ |
802 |
|
public void testPollInExecutor() { |
803 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
804 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
805 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
806 |
|
executor.execute(new CheckedRunnable() { |
807 |
|
public void realRun() throws InterruptedException { |
808 |
< |
threadAssertNull(q.poll()); |
809 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
810 |
< |
threadAssertTrue(q.isEmpty()); |
808 |
> |
assertNull(q.poll()); |
809 |
> |
threadsStarted.await(); |
810 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
811 |
> |
checkEmpty(q); |
812 |
|
}}); |
813 |
|
|
814 |
|
executor.execute(new CheckedRunnable() { |
815 |
|
public void realRun() throws InterruptedException { |
816 |
< |
Thread.sleep(SMALL_DELAY_MS); |
816 |
> |
threadsStarted.await(); |
817 |
|
q.put(one); |
818 |
|
}}); |
819 |
|
|
928 |
|
} |
929 |
|
|
930 |
|
/** |
931 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
931 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
932 |
|
*/ |
933 |
|
public void testDrainToN() { |
934 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
937 |
|
assertTrue(q.offer(new Integer(j))); |
938 |
|
ArrayList l = new ArrayList(); |
939 |
|
q.drainTo(l, i); |
940 |
< |
int k = (i < SIZE)? i : SIZE; |
940 |
> |
int k = (i < SIZE) ? i : SIZE; |
941 |
|
assertEquals(l.size(), k); |
942 |
|
assertEquals(q.size(), SIZE-k); |
943 |
|
for (int j = 0; j < k; ++j) |