296 |
|
/** |
297 |
|
* put(null) throws NPE |
298 |
|
*/ |
299 |
< |
public void testPutNull() { |
299 |
> |
public void testPutNull() { |
300 |
|
try { |
301 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
302 |
|
q.put(null); |
303 |
|
shouldThrow(); |
304 |
|
} catch (NullPointerException success) {} |
305 |
< |
} |
305 |
> |
} |
306 |
|
|
307 |
|
/** |
308 |
|
* all elements successfully put are contained |
309 |
|
*/ |
310 |
< |
public void testPut() { |
311 |
< |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
312 |
< |
for (int i = 0; i < SIZE; ++i) { |
313 |
< |
Integer I = new Integer(i); |
314 |
< |
q.put(I); |
315 |
< |
assertTrue(q.contains(I)); |
316 |
< |
} |
317 |
< |
assertEquals(SIZE, q.size()); |
310 |
> |
public void testPut() { |
311 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
312 |
> |
for (int i = 0; i < SIZE; ++i) { |
313 |
> |
Integer I = new Integer(i); |
314 |
> |
q.put(I); |
315 |
> |
assertTrue(q.contains(I)); |
316 |
> |
} |
317 |
> |
assertEquals(SIZE, q.size()); |
318 |
|
} |
319 |
|
|
320 |
|
/** |
323 |
|
public void testPutWithTake() throws InterruptedException { |
324 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
325 |
|
final int size = 4; |
326 |
< |
Thread t = new Thread(new CheckedRunnable() { |
326 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
327 |
|
public void realRun() { |
328 |
|
for (int i = 0; i < size; i++) |
329 |
|
q.put(new Integer(0)); |
330 |
|
}}); |
331 |
|
|
332 |
< |
t.start(); |
333 |
< |
Thread.sleep(SHORT_DELAY_MS); |
334 |
< |
assertEquals(q.size(), size); |
332 |
> |
awaitTermination(t); |
333 |
> |
assertEquals(size, q.size()); |
334 |
|
q.take(); |
336 |
– |
t.interrupt(); |
337 |
– |
t.join(); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
339 |
|
*/ |
340 |
|
public void testTimedOffer() throws InterruptedException { |
341 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
342 |
< |
Thread t = new Thread(new CheckedRunnable() { |
342 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
343 |
|
public void realRun() { |
344 |
|
q.put(new Integer(0)); |
345 |
|
q.put(new Integer(0)); |
347 |
|
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
348 |
|
}}); |
349 |
|
|
350 |
< |
t.start(); |
354 |
< |
Thread.sleep(SMALL_DELAY_MS); |
355 |
< |
t.interrupt(); |
356 |
< |
t.join(); |
350 |
> |
awaitTermination(t); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
365 |
|
*/ |
366 |
|
public void testBlockingTake() throws InterruptedException { |
367 |
|
final PriorityBlockingQueue q = populatedQueue(SIZE); |
368 |
< |
Thread t = new Thread(new CheckedRunnable() { |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
370 |
|
public void realRun() throws InterruptedException { |
371 |
|
for (int i = 0; i < SIZE; ++i) { |
372 |
|
assertEquals(i, q.take()); |
373 |
|
} |
374 |
+ |
|
375 |
+ |
Thread.currentThread().interrupt(); |
376 |
|
try { |
377 |
|
q.take(); |
378 |
|
shouldThrow(); |
379 |
|
} catch (InterruptedException success) {} |
380 |
+ |
assertFalse(Thread.interrupted()); |
381 |
+ |
|
382 |
+ |
pleaseInterrupt.countDown(); |
383 |
+ |
try { |
384 |
+ |
q.take(); |
385 |
+ |
shouldThrow(); |
386 |
+ |
} catch (InterruptedException success) {} |
387 |
+ |
assertFalse(Thread.interrupted()); |
388 |
|
}}); |
389 |
|
|
390 |
< |
t.start(); |
391 |
< |
Thread.sleep(SHORT_DELAY_MS); |
390 |
> |
await(pleaseInterrupt); |
391 |
> |
assertThreadStaysAlive(t); |
392 |
|
t.interrupt(); |
393 |
< |
t.join(); |
393 |
> |
awaitTermination(t); |
394 |
|
} |
395 |
|
|
391 |
– |
|
396 |
|
/** |
397 |
|
* poll succeeds unless empty |
398 |
|
*/ |
419 |
|
* timed poll with nonzero timeout succeeds when non-empty, else times out |
420 |
|
*/ |
421 |
|
public void testTimedPoll() throws InterruptedException { |
422 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
422 |
> |
PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
425 |
< |
} |
426 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
424 |
> |
long startTime = System.nanoTime(); |
425 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
426 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
427 |
> |
} |
428 |
> |
long startTime = System.nanoTime(); |
429 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
430 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
431 |
> |
checkEmpty(q); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
448 |
|
long t0 = System.nanoTime(); |
449 |
|
aboutToWait.countDown(); |
450 |
|
try { |
451 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
451 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
452 |
|
shouldThrow(); |
453 |
|
} catch (InterruptedException success) { |
454 |
|
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
680 |
|
assertFalse(it.hasNext()); |
681 |
|
} |
682 |
|
|
674 |
– |
|
683 |
|
/** |
684 |
|
* toString contains toStrings of elements |
685 |
|
*/ |
687 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
688 |
|
String s = q.toString(); |
689 |
|
for (int i = 0; i < SIZE; ++i) { |
690 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
690 |
> |
assertTrue(s.contains(String.valueOf(i))); |
691 |
|
} |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
< |
* offer transfers elements across Executor tasks |
695 |
> |
* timed poll transfers elements across Executor tasks |
696 |
|
*/ |
697 |
|
public void testPollInExecutor() { |
698 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
699 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
700 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
701 |
|
executor.execute(new CheckedRunnable() { |
702 |
|
public void realRun() throws InterruptedException { |
703 |
|
assertNull(q.poll()); |
704 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
< |
assertTrue(q.isEmpty()); |
704 |
> |
threadsStarted.await(); |
705 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
706 |
> |
checkEmpty(q); |
707 |
|
}}); |
708 |
|
|
709 |
|
executor.execute(new CheckedRunnable() { |
710 |
|
public void realRun() throws InterruptedException { |
711 |
< |
Thread.sleep(SMALL_DELAY_MS); |
711 |
> |
threadsStarted.await(); |
712 |
|
q.put(one); |
713 |
|
}}); |
714 |
|
|