296 |
|
public void testBlockingPut() throws InterruptedException { |
297 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
298 |
|
Thread t = new Thread(new CheckedRunnable() { |
299 |
< |
public void realRun() { |
300 |
< |
int added = 0; |
299 |
> |
public void realRun() throws InterruptedException { |
300 |
> |
for (int i = 0; i < SIZE; ++i) |
301 |
> |
q.put(i); |
302 |
> |
assertEquals(SIZE, q.size()); |
303 |
> |
assertEquals(0, q.remainingCapacity()); |
304 |
|
try { |
305 |
< |
for (int i = 0; i < SIZE; ++i) { |
306 |
< |
q.put(new Integer(i)); |
307 |
< |
++added; |
308 |
< |
} |
306 |
< |
q.put(new Integer(SIZE)); |
307 |
< |
threadShouldThrow(); |
308 |
< |
} catch (InterruptedException success) { |
309 |
< |
threadAssertEquals(added, SIZE); |
310 |
< |
}}}); |
305 |
> |
q.put(99); |
306 |
> |
shouldThrow(); |
307 |
> |
} catch (InterruptedException success) {} |
308 |
> |
}}); |
309 |
|
|
310 |
|
t.start(); |
311 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
311 |
> |
Thread.sleep(SHORT_DELAY_MS); |
312 |
|
t.interrupt(); |
313 |
|
t.join(); |
314 |
+ |
assertEquals(SIZE, q.size()); |
315 |
+ |
assertEquals(0, q.remainingCapacity()); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
350 |
|
*/ |
351 |
|
public void testTimedOffer() throws InterruptedException { |
352 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
353 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
353 |
> |
Thread t = new Thread(new CheckedRunnable() { |
354 |
|
public void realRun() throws InterruptedException { |
355 |
|
q.put(new Object()); |
356 |
|
q.put(new Object()); |
357 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
358 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
359 |
< |
}}; |
357 |
> |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
358 |
> |
try { |
359 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
360 |
> |
shouldThrow(); |
361 |
> |
} catch (InterruptedException success) {} |
362 |
> |
}}); |
363 |
|
|
364 |
|
t.start(); |
365 |
|
Thread.sleep(SHORT_DELAY_MS); |
373 |
|
public void testTake() throws InterruptedException { |
374 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
375 |
|
for (int i = 0; i < SIZE; ++i) { |
376 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
376 |
> |
assertEquals(i, q.take()); |
377 |
|
} |
378 |
|
} |
379 |
|
|
397 |
|
* Take removes existing elements until empty, then blocks interruptibly |
398 |
|
*/ |
399 |
|
public void testBlockingTake() throws InterruptedException { |
400 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
400 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
401 |
> |
Thread t = new Thread(new CheckedRunnable() { |
402 |
|
public void realRun() throws InterruptedException { |
399 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
403 |
|
for (int i = 0; i < SIZE; ++i) { |
404 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
404 |
> |
assertEquals(i, q.take()); |
405 |
|
} |
406 |
< |
q.take(); |
407 |
< |
}}; |
406 |
> |
try { |
407 |
> |
q.take(); |
408 |
> |
shouldThrow(); |
409 |
> |
} catch (InterruptedException success) {} |
410 |
> |
}}); |
411 |
|
|
412 |
|
t.start(); |
413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
414 |
< |
t.interrupt(); |
415 |
< |
t.join(); |
413 |
> |
Thread.sleep(SHORT_DELAY_MS); |
414 |
> |
t.interrupt(); |
415 |
> |
t.join(); |
416 |
|
} |
417 |
|
|
418 |
|
|
422 |
|
public void testPoll() { |
423 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
425 |
> |
assertEquals(i, q.poll()); |
426 |
|
} |
427 |
|
assertNull(q.poll()); |
428 |
|
} |
433 |
|
public void testTimedPoll0() throws InterruptedException { |
434 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
435 |
|
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
436 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
437 |
|
} |
438 |
|
assertNull(q.poll(0, MILLISECONDS)); |
439 |
|
} |
444 |
|
public void testTimedPoll() throws InterruptedException { |
445 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
446 |
|
for (int i = 0; i < SIZE; ++i) { |
447 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
447 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
448 |
|
} |
449 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
458 |
|
public void realRun() throws InterruptedException { |
459 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
460 |
|
for (int i = 0; i < SIZE; ++i) { |
461 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
461 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
462 |
|
} |
463 |
|
try { |
464 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
502 |
|
public void testPeek() { |
503 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
504 |
|
for (int i = 0; i < SIZE; ++i) { |
505 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
506 |
< |
q.poll(); |
505 |
> |
assertEquals(i, q.peek()); |
506 |
> |
assertEquals(i, q.poll()); |
507 |
|
assertTrue(q.peek() == null || |
508 |
< |
i != ((Integer)q.peek()).intValue()); |
508 |
> |
!q.peek().equals(i)); |
509 |
|
} |
510 |
|
assertNull(q.peek()); |
511 |
|
} |
516 |
|
public void testElement() { |
517 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
518 |
|
for (int i = 0; i < SIZE; ++i) { |
519 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
520 |
< |
q.poll(); |
519 |
> |
assertEquals(i, q.element()); |
520 |
> |
assertEquals(i, q.poll()); |
521 |
|
} |
522 |
|
try { |
523 |
|
q.element(); |
531 |
|
public void testRemove() { |
532 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
533 |
|
for (int i = 0; i < SIZE; ++i) { |
534 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
534 |
> |
assertEquals(i, q.remove()); |
535 |
|
} |
536 |
|
try { |
537 |
|
q.remove(); |
561 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
562 |
|
for (int i = 0; i < SIZE; ++i) { |
563 |
|
assertTrue(q.contains(new Integer(i))); |
564 |
< |
q.poll(); |
564 |
> |
assertEquals(i, q.poll()); |
565 |
|
assertFalse(q.contains(new Integer(i))); |
566 |
|
} |
567 |
|
} |
656 |
|
* toArray(null) throws NPE |
657 |
|
*/ |
658 |
|
public void testToArray_BadArg() { |
659 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
660 |
|
try { |
654 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
661 |
|
Object o[] = q.toArray(null); |
662 |
|
shouldThrow(); |
663 |
|
} catch (NullPointerException success) {} |
667 |
|
* toArray with incompatible array type throws CCE |
668 |
|
*/ |
669 |
|
public void testToArray1_BadArg() { |
670 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
671 |
|
try { |
672 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
666 |
< |
Object o[] = q.toArray(new String[10] ); |
672 |
> |
Object o[] = q.toArray(new String[10]); |
673 |
|
shouldThrow(); |
674 |
|
} catch (ArrayStoreException success) {} |
675 |
|
} |
718 |
|
|
719 |
|
int k = 0; |
720 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
721 |
< |
int i = ((Integer)(it.next())).intValue(); |
716 |
< |
assertEquals(++k, i); |
721 |
> |
assertEquals(++k, it.next()); |
722 |
|
} |
723 |
|
assertEquals(3, k); |
724 |
|
} |