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 |
|
*/ |
36 |
|
new Bounded().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
– |
|
39 |
|
/** |
40 |
|
* Create a queue of given size containing consecutive |
41 |
|
* Integers 0 ... n. |
276 |
|
/** |
277 |
|
* put(null) throws NPE |
278 |
|
*/ |
279 |
< |
public void testPutNull() throws InterruptedException { |
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 |
< |
} |
285 |
> |
} |
286 |
|
|
287 |
|
/** |
288 |
|
* all elements successfully put are contained |
302 |
|
*/ |
303 |
|
public void testBlockingPut() throws InterruptedException { |
304 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
305 |
< |
Thread t = new Thread(new CheckedRunnable() { |
305 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
306 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
307 |
|
public void realRun() throws InterruptedException { |
308 |
|
for (int i = 0; i < SIZE; ++i) |
309 |
|
q.put(i); |
310 |
|
assertEquals(SIZE, q.size()); |
311 |
|
assertEquals(0, q.remainingCapacity()); |
312 |
+ |
|
313 |
+ |
Thread.currentThread().interrupt(); |
314 |
|
try { |
315 |
|
q.put(99); |
316 |
|
shouldThrow(); |
317 |
|
} catch (InterruptedException success) {} |
318 |
+ |
assertFalse(Thread.interrupted()); |
319 |
+ |
|
320 |
+ |
pleaseInterrupt.countDown(); |
321 |
+ |
try { |
322 |
+ |
q.put(99); |
323 |
+ |
shouldThrow(); |
324 |
+ |
} catch (InterruptedException success) {} |
325 |
+ |
assertFalse(Thread.interrupted()); |
326 |
|
}}); |
327 |
|
|
328 |
< |
t.start(); |
329 |
< |
Thread.sleep(SHORT_DELAY_MS); |
328 |
> |
await(pleaseInterrupt); |
329 |
> |
assertThreadStaysAlive(t); |
330 |
|
t.interrupt(); |
331 |
< |
t.join(); |
331 |
> |
awaitTermination(t); |
332 |
|
assertEquals(SIZE, q.size()); |
333 |
|
assertEquals(0, q.remainingCapacity()); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* put blocks waiting for take when full |
337 |
> |
* put blocks interruptibly waiting for take when full |
338 |
|
*/ |
339 |
|
public void testPutWithTake() throws InterruptedException { |
340 |
|
final int capacity = 2; |
341 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
342 |
< |
Thread t = new Thread(new CheckedRunnable() { |
342 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
343 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
344 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
345 |
|
public void realRun() throws InterruptedException { |
346 |
< |
for (int i = 0; i < capacity + 1; i++) |
346 |
> |
for (int i = 0; i < capacity; i++) |
347 |
|
q.put(i); |
348 |
+ |
pleaseTake.countDown(); |
349 |
+ |
q.put(86); |
350 |
+ |
|
351 |
+ |
pleaseInterrupt.countDown(); |
352 |
|
try { |
353 |
|
q.put(99); |
354 |
|
shouldThrow(); |
355 |
|
} catch (InterruptedException success) {} |
356 |
+ |
assertFalse(Thread.interrupted()); |
357 |
|
}}); |
358 |
|
|
359 |
< |
t.start(); |
343 |
< |
Thread.sleep(SHORT_DELAY_MS); |
359 |
> |
await(pleaseTake); |
360 |
|
assertEquals(q.remainingCapacity(), 0); |
361 |
|
assertEquals(0, q.take()); |
362 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
> |
|
363 |
> |
await(pleaseInterrupt); |
364 |
> |
assertThreadStaysAlive(t); |
365 |
|
t.interrupt(); |
366 |
< |
t.join(); |
366 |
> |
awaitTermination(t); |
367 |
|
assertEquals(q.remainingCapacity(), 0); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
|
* timed offer times out if full and elements not taken |
372 |
|
*/ |
373 |
< |
public void testTimedOffer() throws InterruptedException { |
373 |
> |
public void testTimedOffer() { |
374 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
375 |
< |
Thread t = new Thread(new CheckedRunnable() { |
375 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
376 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
377 |
|
public void realRun() throws InterruptedException { |
378 |
|
q.put(new Object()); |
379 |
|
q.put(new Object()); |
380 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
380 |
> |
long startTime = System.nanoTime(); |
381 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
382 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
383 |
> |
pleaseInterrupt.countDown(); |
384 |
|
try { |
385 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
385 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
386 |
|
shouldThrow(); |
387 |
|
} catch (InterruptedException success) {} |
388 |
|
}}); |
389 |
|
|
390 |
< |
t.start(); |
391 |
< |
Thread.sleep(SMALL_DELAY_MS); |
390 |
> |
await(pleaseInterrupt); |
391 |
> |
assertThreadStaysAlive(t); |
392 |
|
t.interrupt(); |
393 |
< |
t.join(); |
393 |
> |
awaitTermination(t); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
407 |
|
* Take removes existing elements until empty, then blocks interruptibly |
408 |
|
*/ |
409 |
|
public void testBlockingTake() throws InterruptedException { |
410 |
< |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
411 |
< |
Thread t = new Thread(new CheckedRunnable() { |
410 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
411 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
412 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
413 |
|
public void realRun() throws InterruptedException { |
414 |
|
for (int i = 0; i < SIZE; ++i) { |
415 |
|
assertEquals(i, q.take()); |
416 |
|
} |
417 |
+ |
|
418 |
+ |
Thread.currentThread().interrupt(); |
419 |
|
try { |
420 |
|
q.take(); |
421 |
|
shouldThrow(); |
422 |
|
} catch (InterruptedException success) {} |
423 |
+ |
assertFalse(Thread.interrupted()); |
424 |
+ |
|
425 |
+ |
pleaseInterrupt.countDown(); |
426 |
+ |
try { |
427 |
+ |
q.take(); |
428 |
+ |
shouldThrow(); |
429 |
+ |
} catch (InterruptedException success) {} |
430 |
+ |
assertFalse(Thread.interrupted()); |
431 |
|
}}); |
432 |
|
|
433 |
< |
t.start(); |
434 |
< |
Thread.sleep(SHORT_DELAY_MS); |
433 |
> |
await(pleaseInterrupt); |
434 |
> |
assertThreadStaysAlive(t); |
435 |
|
t.interrupt(); |
436 |
< |
t.join(); |
436 |
> |
awaitTermination(t); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
462 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
463 |
|
*/ |
464 |
|
public void testTimedPoll() throws InterruptedException { |
465 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
465 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
466 |
|
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
468 |
< |
} |
469 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
467 |
> |
long startTime = System.nanoTime(); |
468 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
469 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
470 |
> |
} |
471 |
> |
long startTime = System.nanoTime(); |
472 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
473 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
474 |
> |
checkEmpty(q); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
479 |
|
* returning timeout status |
480 |
|
*/ |
481 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
482 |
< |
Thread t = new Thread(new CheckedRunnable() { |
482 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
483 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
484 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
485 |
|
public void realRun() throws InterruptedException { |
446 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
486 |
|
for (int i = 0; i < SIZE; ++i) { |
487 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
> |
long t0 = System.nanoTime(); |
488 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
489 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
490 |
|
} |
491 |
+ |
long t0 = System.nanoTime(); |
492 |
+ |
aboutToWait.countDown(); |
493 |
|
try { |
494 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
494 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
495 |
|
shouldThrow(); |
496 |
< |
} catch (InterruptedException success) {} |
496 |
> |
} catch (InterruptedException success) { |
497 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
498 |
> |
} |
499 |
|
}}); |
500 |
|
|
501 |
< |
t.start(); |
502 |
< |
Thread.sleep(SHORT_DELAY_MS); |
501 |
> |
aboutToWait.await(); |
502 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
503 |
|
t.interrupt(); |
504 |
< |
t.join(); |
504 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
505 |
> |
checkEmpty(q); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
554 |
|
public void testRemoveElement() { |
555 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
556 |
|
for (int i = 1; i < SIZE; i+=2) { |
557 |
< |
assertTrue(q.remove(new Integer(i))); |
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.remove(new Integer(i))); |
564 |
< |
assertFalse(q.remove(new Integer(i+1))); |
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 |
|
} |
703 |
|
} catch (ArrayStoreException success) {} |
704 |
|
} |
705 |
|
|
654 |
– |
|
706 |
|
/** |
707 |
|
* iterator iterates through all elements |
708 |
|
*/ |
733 |
|
assertFalse(it.hasNext()); |
734 |
|
} |
735 |
|
|
685 |
– |
|
736 |
|
/** |
737 |
|
* iterator ordering is FIFO |
738 |
|
*/ |
764 |
|
assertEquals(0, q.size()); |
765 |
|
} |
766 |
|
|
717 |
– |
|
767 |
|
/** |
768 |
|
* toString contains toStrings of elements |
769 |
|
*/ |
771 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
772 |
|
String s = q.toString(); |
773 |
|
for (int i = 0; i < SIZE; ++i) { |
774 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
774 |
> |
assertTrue(s.contains(String.valueOf(i))); |
775 |
|
} |
776 |
|
} |
777 |
|
|
729 |
– |
|
778 |
|
/** |
779 |
|
* offer transfers elements across Executor tasks |
780 |
|
*/ |
783 |
|
q.add(one); |
784 |
|
q.add(two); |
785 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
786 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
787 |
|
executor.execute(new CheckedRunnable() { |
788 |
|
public void realRun() throws InterruptedException { |
789 |
|
assertFalse(q.offer(three)); |
790 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
790 |
> |
threadsStarted.await(); |
791 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
792 |
|
assertEquals(0, q.remainingCapacity()); |
793 |
|
}}); |
794 |
|
|
795 |
|
executor.execute(new CheckedRunnable() { |
796 |
|
public void realRun() throws InterruptedException { |
797 |
< |
Thread.sleep(SMALL_DELAY_MS); |
797 |
> |
threadsStarted.await(); |
798 |
|
assertSame(one, q.take()); |
799 |
|
}}); |
800 |
|
|
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
< |
* poll retrieves elements across Executor threads |
805 |
> |
* timed poll retrieves elements across Executor threads |
806 |
|
*/ |
807 |
|
public void testPollInExecutor() { |
808 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
809 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
810 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
811 |
|
executor.execute(new CheckedRunnable() { |
812 |
|
public void realRun() throws InterruptedException { |
813 |
|
assertNull(q.poll()); |
814 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
815 |
< |
assertTrue(q.isEmpty()); |
814 |
> |
threadsStarted.await(); |
815 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
816 |
> |
checkEmpty(q); |
817 |
|
}}); |
818 |
|
|
819 |
|
executor.execute(new CheckedRunnable() { |
820 |
|
public void realRun() throws InterruptedException { |
821 |
< |
Thread.sleep(SMALL_DELAY_MS); |
821 |
> |
threadsStarted.await(); |
822 |
|
q.put(one); |
823 |
|
}}); |
824 |
|
|