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; |
25 |
|
|
26 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
27 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
– |
static class Implementation implements CollectionImplementation { |
28 |
– |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
29 |
– |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
30 |
– |
public Object makeElement(int i) { return i; } |
31 |
– |
public boolean isConcurrent() { return true; } |
32 |
– |
public boolean permitsNulls() { return false; } |
33 |
– |
} |
34 |
– |
|
28 |
|
public static class Generic extends BlockingQueueTest { |
29 |
|
protected BlockingQueue emptyCollection() { |
30 |
|
return new LinkedTransferQueue(); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
+ |
class Implementation implements CollectionImplementation { |
40 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
41 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
42 |
+ |
public Object makeElement(int i) { return i; } |
43 |
+ |
public boolean isConcurrent() { return true; } |
44 |
+ |
public boolean permitsNulls() { return false; } |
45 |
+ |
} |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
|
new Generic().testSuite(), |
48 |
|
CollectionTest.testSuite(new Implementation())); |
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
< |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
186 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
187 |
|
for (int i = 0; i < SIZE; ++i) { |
188 |
|
assertEquals(i, q.size()); |
189 |
|
q.put(i); |
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) { |
213 |
< |
assertEquals(i, q.take()); |
214 |
< |
} |
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(); |
285 |
< |
for (int i = 0; i < SIZE; ++i) |
285 |
> |
for (int i = 0; i < SIZE; i++) |
286 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
287 |
< |
aboutToWait.countDown(); |
287 |
> |
|
288 |
> |
Thread.currentThread().interrupt(); |
289 |
> |
try { |
290 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (InterruptedException success) {} |
293 |
> |
assertFalse(Thread.interrupted()); |
294 |
> |
|
295 |
> |
pleaseInterrupt.countDown(); |
296 |
|
try { |
297 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
298 |
|
shouldThrow(); |
299 |
|
} catch (InterruptedException success) {} |
300 |
+ |
assertFalse(Thread.interrupted()); |
301 |
+ |
|
302 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
303 |
|
}}); |
304 |
|
|
305 |
< |
aboutToWait.await(); |
306 |
< |
waitForThreadToEnterWaitState(t); |
305 |
> |
await(pleaseInterrupt); |
306 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
307 |
|
t.interrupt(); |
308 |
|
awaitTermination(t); |
309 |
|
checkEmpty(q); |
320 |
|
long startTime = System.nanoTime(); |
321 |
|
Thread.currentThread().interrupt(); |
322 |
|
for (int i = 0; i < SIZE; ++i) |
323 |
< |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
323 |
> |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
324 |
|
try { |
325 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
325 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
326 |
|
shouldThrow(); |
327 |
|
} catch (InterruptedException success) {} |
328 |
+ |
assertFalse(Thread.interrupted()); |
329 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
330 |
|
}}); |
331 |
|
|
425 |
|
*/ |
426 |
|
public void testContainsAll() { |
427 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
428 |
< |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
428 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); |
429 |
|
for (int i = 0; i < SIZE; ++i) { |
430 |
|
assertTrue(q.containsAll(p)); |
431 |
|
assertFalse(p.containsAll(q)); |
475 |
|
*/ |
476 |
|
public void testToArray() { |
477 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
478 |
< |
Object[] o = q.toArray(); |
479 |
< |
for (int i = 0; i < o.length; i++) { |
480 |
< |
assertSame(o[i], q.poll()); |
481 |
< |
} |
478 |
> |
Object[] a = q.toArray(); |
479 |
> |
assertSame(Object[].class, a.getClass()); |
480 |
> |
for (Object o : a) |
481 |
> |
assertSame(o, q.poll()); |
482 |
> |
assertTrue(q.isEmpty()); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
490 |
|
Integer[] ints = new Integer[SIZE]; |
491 |
|
Integer[] array = q.toArray(ints); |
492 |
|
assertSame(ints, array); |
493 |
< |
for (int i = 0; i < ints.length; i++) { |
494 |
< |
assertSame(ints[i], q.poll()); |
495 |
< |
} |
493 |
> |
for (Integer o : ints) |
494 |
> |
assertSame(o, q.poll()); |
495 |
> |
assertTrue(q.isEmpty()); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
555 |
|
* iterator ordering is FIFO |
556 |
|
*/ |
557 |
|
public void testIteratorOrdering() { |
558 |
< |
final LinkedTransferQueue<Integer> q |
549 |
< |
= new LinkedTransferQueue<Integer>(); |
558 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
559 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
560 |
|
q.add(one); |
561 |
|
q.add(two); |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
< |
* A deserialized serialized queue has same elements in same order |
651 |
> |
* A deserialized/reserialized queue has same elements in same order |
652 |
|
*/ |
653 |
|
public void testSerialization() throws Exception { |
654 |
|
Queue x = populatedQueue(SIZE); |
750 |
|
}}); |
751 |
|
|
752 |
|
threadStarted.await(); |
753 |
< |
waitForThreadToEnterWaitState(t); |
754 |
< |
assertEquals(1, q.getWaitingConsumerCount()); |
755 |
< |
assertTrue(q.hasWaitingConsumer()); |
753 |
> |
Callable<Boolean> oneConsumer |
754 |
> |
= new Callable<Boolean>() { public Boolean call() { |
755 |
> |
return q.hasWaitingConsumer() |
756 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
757 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
758 |
|
|
759 |
|
assertTrue(q.offer(one)); |
760 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* transfer waits until a poll occurs. The transfered element |
779 |
< |
* is returned by this associated poll. |
778 |
> |
* transfer waits until a poll occurs. The transferred element |
779 |
> |
* is returned by the associated poll. |
780 |
|
*/ |
781 |
|
public void testTransfer2() throws InterruptedException { |
782 |
< |
final LinkedTransferQueue<Integer> q |
772 |
< |
= new LinkedTransferQueue<Integer>(); |
782 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
783 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
784 |
|
|
785 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
790 |
|
}}); |
791 |
|
|
792 |
|
threadStarted.await(); |
793 |
< |
waitForThreadToEnterWaitState(t); |
794 |
< |
assertEquals(1, q.size()); |
793 |
> |
Callable<Boolean> oneElement |
794 |
> |
= new Callable<Boolean>() { public Boolean call() { |
795 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
796 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
797 |
> |
|
798 |
|
assertSame(five, q.poll()); |
799 |
|
checkEmpty(q); |
800 |
|
awaitTermination(t); |
804 |
|
* transfer waits until a poll occurs, and then transfers in fifo order |
805 |
|
*/ |
806 |
|
public void testTransfer3() throws InterruptedException { |
807 |
< |
final LinkedTransferQueue<Integer> q |
795 |
< |
= new LinkedTransferQueue<Integer>(); |
807 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
808 |
|
|
809 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
810 |
|
public void realRun() throws InterruptedException { |
811 |
|
q.transfer(four); |
812 |
< |
assertTrue(!q.contains(four)); |
812 |
> |
assertFalse(q.contains(four)); |
813 |
|
assertEquals(1, q.size()); |
814 |
|
}}); |
815 |
|
|
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
< |
* transfer waits until a take occurs. The transfered element |
860 |
< |
* is returned by this associated take. |
859 |
> |
* transfer waits until a take occurs. The transferred element |
860 |
> |
* is returned by the associated take. |
861 |
|
*/ |
862 |
|
public void testTransfer5() throws InterruptedException { |
863 |
< |
final LinkedTransferQueue<Integer> q |
852 |
< |
= new LinkedTransferQueue<Integer>(); |
863 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
864 |
|
|
865 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
866 |
|
public void realRun() throws InterruptedException { |
958 |
|
long startTime = System.nanoTime(); |
959 |
|
Thread.currentThread().interrupt(); |
960 |
|
try { |
961 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
961 |
> |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
962 |
|
shouldThrow(); |
963 |
|
} catch (InterruptedException success) {} |
964 |
|
assertFalse(Thread.interrupted()); |
969 |
|
shouldThrow(); |
970 |
|
} catch (InterruptedException success) {} |
971 |
|
assertFalse(Thread.interrupted()); |
972 |
+ |
|
973 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
974 |
|
}}); |
975 |
|
|
976 |
|
await(pleaseInterrupt); |
977 |
< |
assertThreadStaysAlive(t); |
977 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
978 |
|
t.interrupt(); |
979 |
|
awaitTermination(t); |
980 |
|
checkEmpty(q); |
1042 |
|
} |
1043 |
|
|
1044 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1045 |
< |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1045 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
1046 |
|
checkEmpty(q); |
1047 |
|
for (int i = 0; i < n; i++) { |
1048 |
|
assertEquals(i, q.size()); |