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); |
314 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
315 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
316 |
|
public void realRun() throws InterruptedException { |
311 |
– |
long startTime = System.nanoTime(); |
317 |
|
Thread.currentThread().interrupt(); |
318 |
|
for (int i = 0; i < SIZE; ++i) |
319 |
< |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
319 |
> |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
320 |
|
try { |
321 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
321 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
322 |
|
shouldThrow(); |
323 |
|
} catch (InterruptedException success) {} |
324 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
324 |
> |
assertFalse(Thread.interrupted()); |
325 |
|
}}); |
326 |
|
|
327 |
|
awaitTermination(t); |
470 |
|
*/ |
471 |
|
public void testToArray() { |
472 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
473 |
< |
Object[] o = q.toArray(); |
474 |
< |
for (int i = 0; i < o.length; i++) { |
475 |
< |
assertSame(o[i], q.poll()); |
476 |
< |
} |
473 |
> |
Object[] a = q.toArray(); |
474 |
> |
assertSame(Object[].class, a.getClass()); |
475 |
> |
for (Object o : a) |
476 |
> |
assertSame(o, q.poll()); |
477 |
> |
assertTrue(q.isEmpty()); |
478 |
|
} |
479 |
|
|
480 |
|
/** |
485 |
|
Integer[] ints = new Integer[SIZE]; |
486 |
|
Integer[] array = q.toArray(ints); |
487 |
|
assertSame(ints, array); |
488 |
< |
for (int i = 0; i < ints.length; i++) { |
489 |
< |
assertSame(ints[i], q.poll()); |
490 |
< |
} |
488 |
> |
for (Integer o : ints) |
489 |
> |
assertSame(o, q.poll()); |
490 |
> |
assertTrue(q.isEmpty()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* A deserialized serialized queue has same elements in same order |
646 |
> |
* A deserialized/reserialized queue has same elements in same order |
647 |
|
*/ |
648 |
|
public void testSerialization() throws Exception { |
649 |
|
Queue x = populatedQueue(SIZE); |
745 |
|
}}); |
746 |
|
|
747 |
|
threadStarted.await(); |
748 |
< |
waitForThreadToEnterWaitState(t); |
749 |
< |
assertEquals(1, q.getWaitingConsumerCount()); |
750 |
< |
assertTrue(q.hasWaitingConsumer()); |
748 |
> |
Callable<Boolean> oneConsumer |
749 |
> |
= new Callable<Boolean>() { public Boolean call() { |
750 |
> |
return q.hasWaitingConsumer() |
751 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
752 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
753 |
|
|
754 |
|
assertTrue(q.offer(one)); |
755 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* transfer waits until a poll occurs. The transfered element |
774 |
< |
* is returned by this associated poll. |
773 |
> |
* transfer waits until a poll occurs. The transferred element |
774 |
> |
* is returned by the associated poll. |
775 |
|
*/ |
776 |
|
public void testTransfer2() throws InterruptedException { |
777 |
|
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
785 |
|
}}); |
786 |
|
|
787 |
|
threadStarted.await(); |
788 |
< |
waitForThreadToEnterWaitState(t); |
789 |
< |
assertEquals(1, q.size()); |
788 |
> |
Callable<Boolean> oneElement |
789 |
> |
= new Callable<Boolean>() { public Boolean call() { |
790 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
791 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
792 |
> |
|
793 |
|
assertSame(five, q.poll()); |
794 |
|
checkEmpty(q); |
795 |
|
awaitTermination(t); |
804 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
805 |
|
public void realRun() throws InterruptedException { |
806 |
|
q.transfer(four); |
807 |
< |
assertTrue(!q.contains(four)); |
807 |
> |
assertFalse(q.contains(four)); |
808 |
|
assertEquals(1, q.size()); |
809 |
|
}}); |
810 |
|
|
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* transfer waits until a take occurs. The transfered element |
855 |
< |
* is returned by this associated take. |
854 |
> |
* transfer waits until a take occurs. The transferred element |
855 |
> |
* is returned by the associated take. |
856 |
|
*/ |
857 |
|
public void testTransfer5() throws InterruptedException { |
858 |
|
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
953 |
|
long startTime = System.nanoTime(); |
954 |
|
Thread.currentThread().interrupt(); |
955 |
|
try { |
956 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
956 |
> |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
957 |
|
shouldThrow(); |
958 |
|
} catch (InterruptedException success) {} |
959 |
|
assertFalse(Thread.interrupted()); |
964 |
|
shouldThrow(); |
965 |
|
} catch (InterruptedException success) {} |
966 |
|
assertFalse(Thread.interrupted()); |
967 |
+ |
|
968 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
969 |
|
}}); |
970 |
|
|
971 |
|
await(pleaseInterrupt); |
972 |
< |
assertThreadStaysAlive(t); |
972 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
973 |
|
t.interrupt(); |
974 |
|
awaitTermination(t); |
975 |
|
checkEmpty(q); |