15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.Queue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
18 |
+ |
import java.util.concurrent.Callable; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
209 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
210 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
211 |
|
public void realRun() throws InterruptedException { |
212 |
< |
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
assertEquals(i, q.take()); |
213 |
< |
} |
212 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
213 |
|
|
214 |
|
Thread.currentThread().interrupt(); |
215 |
|
try { |
227 |
|
}}); |
228 |
|
|
229 |
|
await(pleaseInterrupt); |
230 |
< |
assertThreadStaysAlive(t); |
230 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
231 |
|
t.interrupt(); |
232 |
|
awaitTermination(t); |
233 |
|
} |
278 |
|
*/ |
279 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
280 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
281 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
281 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
282 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
|
public void realRun() throws InterruptedException { |
284 |
|
long startTime = System.nanoTime(); |
285 |
|
for (int i = 0; i < SIZE; ++i) |
286 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
287 |
< |
aboutToWait.countDown(); |
287 |
> |
|
288 |
> |
pleaseInterrupt.countDown(); |
289 |
|
try { |
290 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
291 |
|
shouldThrow(); |
292 |
|
} catch (InterruptedException success) {} |
293 |
+ |
assertFalse(Thread.interrupted()); |
294 |
+ |
|
295 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
|
}}); |
297 |
|
|
298 |
< |
aboutToWait.await(); |
299 |
< |
waitForThreadToEnterWaitState(t); |
298 |
> |
await(pleaseInterrupt); |
299 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
300 |
|
t.interrupt(); |
301 |
|
awaitTermination(t); |
302 |
|
checkEmpty(q); |
318 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
319 |
|
shouldThrow(); |
320 |
|
} catch (InterruptedException success) {} |
321 |
+ |
assertFalse(Thread.interrupted()); |
322 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
323 |
|
}}); |
324 |
|
|
742 |
|
}}); |
743 |
|
|
744 |
|
threadStarted.await(); |
745 |
< |
waitForThreadToEnterWaitState(t); |
746 |
< |
assertEquals(1, q.getWaitingConsumerCount()); |
747 |
< |
assertTrue(q.hasWaitingConsumer()); |
745 |
> |
Callable<Boolean> oneConsumer |
746 |
> |
= new Callable<Boolean>() { public Boolean call() { |
747 |
> |
return q.hasWaitingConsumer() |
748 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
749 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
750 |
|
|
751 |
|
assertTrue(q.offer(one)); |
752 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
782 |
|
}}); |
783 |
|
|
784 |
|
threadStarted.await(); |
785 |
< |
waitForThreadToEnterWaitState(t); |
786 |
< |
assertEquals(1, q.size()); |
785 |
> |
Callable<Boolean> oneElement |
786 |
> |
= new Callable<Boolean>() { public Boolean call() { |
787 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
788 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
789 |
> |
|
790 |
|
assertSame(five, q.poll()); |
791 |
|
checkEmpty(q); |
792 |
|
awaitTermination(t); |
801 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
802 |
|
public void realRun() throws InterruptedException { |
803 |
|
q.transfer(four); |
804 |
< |
assertTrue(!q.contains(four)); |
804 |
> |
assertFalse(q.contains(four)); |
805 |
|
assertEquals(1, q.size()); |
806 |
|
}}); |
807 |
|
|
965 |
|
}}); |
966 |
|
|
967 |
|
await(pleaseInterrupt); |
968 |
< |
assertThreadStaysAlive(t); |
968 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
969 |
|
t.interrupt(); |
970 |
|
awaitTermination(t); |
971 |
|
checkEmpty(q); |