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); |
465 |
+ |
shouldThrow(); |
466 |
|
} catch (InterruptedException success) {} |
467 |
|
}}); |
468 |
|
|
478 |
|
*/ |
479 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
480 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
481 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
481 |
> |
Thread t = new Thread(new CheckedRunnable() { |
482 |
|
public void realRun() throws InterruptedException { |
483 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
485 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
< |
}}; |
483 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
> |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
485 |
> |
try { |
486 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
487 |
> |
shouldThrow(); |
488 |
> |
} catch (InterruptedException success) {} |
489 |
> |
}}); |
490 |
|
|
491 |
|
t.start(); |
492 |
|
Thread.sleep(SMALL_DELAY_MS); |