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 |
> |
if (randomBoolean()) 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(); |
286 |
< |
for (int i = 0; i < SIZE; ++i) |
284 |
> |
for (int i = 0; i < SIZE; i++) |
285 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
286 |
< |
aboutToWait.countDown(); |
286 |
> |
|
287 |
> |
Thread.currentThread().interrupt(); |
288 |
|
try { |
289 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
289 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
290 |
|
shouldThrow(); |
291 |
|
} catch (InterruptedException success) {} |
292 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
292 |
> |
assertFalse(Thread.interrupted()); |
293 |
> |
|
294 |
> |
pleaseInterrupt.countDown(); |
295 |
> |
try { |
296 |
> |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
297 |
> |
shouldThrow(); |
298 |
> |
} catch (InterruptedException success) {} |
299 |
> |
assertFalse(Thread.interrupted()); |
300 |
|
}}); |
301 |
|
|
302 |
< |
aboutToWait.await(); |
303 |
< |
waitForThreadToEnterWaitState(t); |
302 |
> |
await(pleaseInterrupt); |
303 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
304 |
|
t.interrupt(); |
305 |
|
awaitTermination(t); |
306 |
|
checkEmpty(q); |
317 |
|
long startTime = System.nanoTime(); |
318 |
|
Thread.currentThread().interrupt(); |
319 |
|
for (int i = 0; i < SIZE; ++i) |
320 |
< |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
321 |
|
try { |
322 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
322 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
323 |
|
shouldThrow(); |
324 |
|
} catch (InterruptedException success) {} |
325 |
+ |
assertFalse(Thread.interrupted()); |
326 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
327 |
|
}}); |
328 |
|
|
472 |
|
*/ |
473 |
|
public void testToArray() { |
474 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
475 |
< |
Object[] o = q.toArray(); |
476 |
< |
for (int i = 0; i < o.length; i++) { |
477 |
< |
assertSame(o[i], q.poll()); |
478 |
< |
} |
475 |
> |
Object[] a = q.toArray(); |
476 |
> |
assertSame(Object[].class, a.getClass()); |
477 |
> |
for (Object o : a) |
478 |
> |
assertSame(o, q.poll()); |
479 |
> |
assertTrue(q.isEmpty()); |
480 |
|
} |
481 |
|
|
482 |
|
/** |
487 |
|
Integer[] ints = new Integer[SIZE]; |
488 |
|
Integer[] array = q.toArray(ints); |
489 |
|
assertSame(ints, array); |
490 |
< |
for (int i = 0; i < ints.length; i++) { |
491 |
< |
assertSame(ints[i], q.poll()); |
492 |
< |
} |
490 |
> |
for (Integer o : ints) |
491 |
> |
assertSame(o, q.poll()); |
492 |
> |
assertTrue(q.isEmpty()); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* A deserialized serialized queue has same elements in same order |
648 |
> |
* A deserialized/reserialized queue has same elements in same order |
649 |
|
*/ |
650 |
|
public void testSerialization() throws Exception { |
651 |
|
Queue x = populatedQueue(SIZE); |
747 |
|
}}); |
748 |
|
|
749 |
|
threadStarted.await(); |
750 |
< |
waitForThreadToEnterWaitState(t); |
751 |
< |
assertEquals(1, q.getWaitingConsumerCount()); |
752 |
< |
assertTrue(q.hasWaitingConsumer()); |
750 |
> |
Callable<Boolean> oneConsumer |
751 |
> |
= new Callable<Boolean>() { public Boolean call() { |
752 |
> |
return q.hasWaitingConsumer() |
753 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
754 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
755 |
|
|
756 |
|
assertTrue(q.offer(one)); |
757 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
< |
* transfer waits until a poll occurs. The transfered element |
775 |
> |
* transfer waits until a poll occurs. The transferred element |
776 |
|
* is returned by the associated poll. |
777 |
|
*/ |
778 |
|
public void testTransfer2() throws InterruptedException { |
787 |
|
}}); |
788 |
|
|
789 |
|
threadStarted.await(); |
790 |
< |
waitForThreadToEnterWaitState(t); |
791 |
< |
assertEquals(1, q.size()); |
790 |
> |
Callable<Boolean> oneElement |
791 |
> |
= new Callable<Boolean>() { public Boolean call() { |
792 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
793 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
794 |
> |
|
795 |
|
assertSame(five, q.poll()); |
796 |
|
checkEmpty(q); |
797 |
|
awaitTermination(t); |
806 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
807 |
|
public void realRun() throws InterruptedException { |
808 |
|
q.transfer(four); |
809 |
< |
assertTrue(!q.contains(four)); |
809 |
> |
assertFalse(q.contains(four)); |
810 |
|
assertEquals(1, q.size()); |
811 |
|
}}); |
812 |
|
|
853 |
|
} |
854 |
|
|
855 |
|
/** |
856 |
< |
* transfer waits until a take occurs. The transfered element |
856 |
> |
* transfer waits until a take occurs. The transferred element |
857 |
|
* is returned by the associated take. |
858 |
|
*/ |
859 |
|
public void testTransfer5() throws InterruptedException { |
955 |
|
long startTime = System.nanoTime(); |
956 |
|
Thread.currentThread().interrupt(); |
957 |
|
try { |
958 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
958 |
> |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
959 |
|
shouldThrow(); |
960 |
|
} catch (InterruptedException success) {} |
961 |
|
assertFalse(Thread.interrupted()); |
966 |
|
shouldThrow(); |
967 |
|
} catch (InterruptedException success) {} |
968 |
|
assertFalse(Thread.interrupted()); |
969 |
+ |
|
970 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
971 |
|
}}); |
972 |
|
|
973 |
|
await(pleaseInterrupt); |
974 |
< |
assertThreadStaysAlive(t); |
974 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
975 |
|
t.interrupt(); |
976 |
|
awaitTermination(t); |
977 |
|
checkEmpty(q); |