59 |
|
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
|
private static LinkedBlockingQueue<Integer> populatedQueue(int n) { |
62 |
< |
LinkedBlockingQueue<Integer> q = |
63 |
< |
new LinkedBlockingQueue<Integer>(n); |
62 |
> |
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(n); |
63 |
|
assertTrue(q.isEmpty()); |
64 |
|
for (int i = 0; i < n; i++) |
65 |
|
assertTrue(q.offer(new Integer(i))); |
291 |
|
}}); |
292 |
|
|
293 |
|
await(pleaseInterrupt); |
294 |
< |
assertThreadStaysAlive(t); |
294 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
295 |
|
t.interrupt(); |
296 |
|
awaitTermination(t); |
297 |
|
assertEquals(SIZE, q.size()); |
313 |
|
pleaseTake.countDown(); |
314 |
|
q.put(86); |
315 |
|
|
316 |
+ |
Thread.currentThread().interrupt(); |
317 |
+ |
try { |
318 |
+ |
q.put(99); |
319 |
+ |
shouldThrow(); |
320 |
+ |
} catch (InterruptedException success) {} |
321 |
+ |
assertFalse(Thread.interrupted()); |
322 |
+ |
|
323 |
|
pleaseInterrupt.countDown(); |
324 |
|
try { |
325 |
|
q.put(99); |
349 |
|
public void realRun() throws InterruptedException { |
350 |
|
q.put(new Object()); |
351 |
|
q.put(new Object()); |
352 |
+ |
|
353 |
|
long startTime = System.nanoTime(); |
354 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
355 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
356 |
+ |
|
357 |
+ |
Thread.currentThread().interrupt(); |
358 |
+ |
try { |
359 |
+ |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
360 |
+ |
shouldThrow(); |
361 |
+ |
} catch (InterruptedException success) {} |
362 |
+ |
assertFalse(Thread.interrupted()); |
363 |
+ |
|
364 |
|
pleaseInterrupt.countDown(); |
365 |
|
try { |
366 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
367 |
|
shouldThrow(); |
368 |
|
} catch (InterruptedException success) {} |
369 |
+ |
assertFalse(Thread.interrupted()); |
370 |
|
}}); |
371 |
|
|
372 |
|
await(pleaseInterrupt); |
373 |
< |
assertThreadStaysAlive(t); |
373 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
374 |
|
t.interrupt(); |
375 |
|
awaitTermination(t); |
376 |
|
} |
460 |
|
*/ |
461 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
462 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
463 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
463 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
464 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
465 |
|
public void realRun() throws InterruptedException { |
466 |
|
long startTime = System.nanoTime(); |
467 |
< |
for (int i = 0; i < SIZE; ++i) { |
467 |
> |
for (int i = 0; i < SIZE; i++) |
468 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
469 |
< |
} |
470 |
< |
aboutToWait.countDown(); |
469 |
> |
|
470 |
> |
Thread.currentThread().interrupt(); |
471 |
> |
try { |
472 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
473 |
> |
shouldThrow(); |
474 |
> |
} catch (InterruptedException success) {} |
475 |
> |
assertFalse(Thread.interrupted()); |
476 |
> |
|
477 |
> |
pleaseInterrupt.countDown(); |
478 |
|
try { |
479 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
480 |
|
shouldThrow(); |
481 |
< |
} catch (InterruptedException success) { |
482 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
483 |
< |
} |
481 |
> |
} catch (InterruptedException success) {} |
482 |
> |
assertFalse(Thread.interrupted()); |
483 |
> |
|
484 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
485 |
|
}}); |
486 |
|
|
487 |
< |
await(aboutToWait); |
488 |
< |
waitForThreadToEnterWaitState(t); |
487 |
> |
await(pleaseInterrupt); |
488 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
489 |
|
t.interrupt(); |
490 |
|
awaitTermination(t); |
491 |
|
checkEmpty(q); |
795 |
|
} |
796 |
|
|
797 |
|
/** |
798 |
< |
* A deserialized serialized queue has same elements in same order |
798 |
> |
* A deserialized/reserialized queue has same elements in same order |
799 |
|
*/ |
800 |
|
public void testSerialization() throws Exception { |
801 |
|
Queue x = populatedQueue(SIZE); |