5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
< |
import java.io.BufferedInputStream; |
9 |
< |
import java.io.BufferedOutputStream; |
10 |
< |
import java.io.ByteArrayInputStream; |
11 |
< |
import java.io.ByteArrayOutputStream; |
12 |
< |
import java.io.ObjectInputStream; |
13 |
< |
import java.io.ObjectOutputStream; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Arrays; |
12 |
+ |
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.List; |
15 |
|
import java.util.NoSuchElementException; |
16 |
< |
import java.util.concurrent.*; |
17 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedTransferQueue; |
22 |
> |
|
23 |
|
import junit.framework.Test; |
23 |
– |
import junit.framework.TestSuite; |
24 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
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 |
|
|
35 |
|
public static class Generic extends BlockingQueueTest { |
36 |
|
protected BlockingQueue emptyCollection() { |
39 |
|
} |
40 |
|
|
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
|
45 |
|
public static Test suite() { |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
< |
new Generic().testSuite()); |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
73 |
|
* NullPointerException |
74 |
|
*/ |
75 |
|
public void testConstructor3() { |
76 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
70 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
78 |
> |
new LinkedTransferQueue(elements); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
85 |
|
* throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor4() { |
88 |
+ |
Integer[] ints = new Integer[SIZE]; |
89 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
+ |
ints[i] = i; |
91 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
93 |
< |
Integer[] ints = new Integer[SIZE]; |
82 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
83 |
< |
ints[i] = i; |
84 |
< |
} |
85 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
new LinkedTransferQueue(elements); |
94 |
|
shouldThrow(); |
95 |
|
} catch (NullPointerException success) {} |
96 |
|
} |
123 |
|
* remainingCapacity() always returns Integer.MAX_VALUE |
124 |
|
*/ |
125 |
|
public void testRemainingCapacity() { |
126 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
126 |
> |
BlockingQueue q = populatedQueue(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
|
assertEquals(SIZE - i, q.size()); |
130 |
< |
q.remove(); |
130 |
> |
assertEquals(i, q.remove()); |
131 |
|
} |
132 |
|
for (int i = 0; i < SIZE; ++i) { |
133 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
134 |
|
assertEquals(i, q.size()); |
135 |
< |
q.add(i); |
135 |
> |
assertTrue(q.add(i)); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
/** |
132 |
– |
* offer(null) throws NullPointerException |
133 |
– |
*/ |
134 |
– |
public void testOfferNull() { |
135 |
– |
try { |
136 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
137 |
– |
q.offer(null); |
138 |
– |
shouldThrow(); |
139 |
– |
} catch (NullPointerException success) {} |
140 |
– |
} |
141 |
– |
|
142 |
– |
/** |
143 |
– |
* add(null) throws NullPointerException |
144 |
– |
*/ |
145 |
– |
public void testAddNull() { |
146 |
– |
try { |
147 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
148 |
– |
q.add(null); |
149 |
– |
shouldThrow(); |
150 |
– |
} catch (NullPointerException success) {} |
151 |
– |
} |
152 |
– |
|
153 |
– |
/** |
154 |
– |
* addAll(null) throws NullPointerException |
155 |
– |
*/ |
156 |
– |
public void testAddAll1() { |
157 |
– |
try { |
158 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
159 |
– |
q.addAll(null); |
160 |
– |
shouldThrow(); |
161 |
– |
} catch (NullPointerException success) {} |
162 |
– |
} |
163 |
– |
|
164 |
– |
/** |
140 |
|
* addAll(this) throws IllegalArgumentException |
141 |
|
*/ |
142 |
|
public void testAddAllSelf() { |
143 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
|
try { |
169 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
145 |
|
q.addAll(q); |
146 |
|
shouldThrow(); |
147 |
|
} catch (IllegalArgumentException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
176 |
– |
* addAll of a collection with null elements throws NullPointerException |
177 |
– |
*/ |
178 |
– |
public void testAddAll2() { |
179 |
– |
try { |
180 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
181 |
– |
Integer[] ints = new Integer[SIZE]; |
182 |
– |
q.addAll(Arrays.asList(ints)); |
183 |
– |
shouldThrow(); |
184 |
– |
} catch (NullPointerException success) {} |
185 |
– |
} |
186 |
– |
|
187 |
– |
/** |
151 |
|
* addAll of a collection with any null elements throws |
152 |
|
* NullPointerException after possibly adding some elements |
153 |
|
*/ |
154 |
|
public void testAddAll3() { |
155 |
+ |
LinkedTransferQueue q = new LinkedTransferQueue(); |
156 |
+ |
Integer[] ints = new Integer[SIZE]; |
157 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
158 |
+ |
ints[i] = i; |
159 |
|
try { |
193 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
194 |
– |
Integer[] ints = new Integer[SIZE]; |
195 |
– |
for (int i = 0; i < SIZE - 1; ++i) { |
196 |
– |
ints[i] = i; |
197 |
– |
} |
160 |
|
q.addAll(Arrays.asList(ints)); |
161 |
|
shouldThrow(); |
162 |
|
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
|
182 |
|
/** |
221 |
– |
* put(null) throws NullPointerException |
222 |
– |
*/ |
223 |
– |
public void testPutNull() throws InterruptedException { |
224 |
– |
try { |
225 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
226 |
– |
q.put(null); |
227 |
– |
shouldThrow(); |
228 |
– |
} catch (NullPointerException success) {} |
229 |
– |
} |
230 |
– |
|
231 |
– |
/** |
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
187 |
|
for (int i = 0; i < SIZE; ++i) { |
188 |
< |
assertEquals(q.size(), i); |
188 |
> |
assertEquals(i, q.size()); |
189 |
|
q.put(i); |
190 |
|
assertTrue(q.contains(i)); |
191 |
|
} |
263 |
|
*/ |
264 |
|
public void testTimedPoll() throws InterruptedException { |
265 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
315 |
– |
for (int i = 0; i < SIZE; ++i) { |
316 |
– |
long startTime = System.nanoTime(); |
317 |
– |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
318 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
319 |
– |
} |
266 |
|
long startTime = System.nanoTime(); |
267 |
+ |
for (int i = 0; i < SIZE; ++i) |
268 |
+ |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
+ |
|
271 |
+ |
startTime = System.nanoTime(); |
272 |
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
273 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
274 |
|
checkEmpty(q); |
283 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
|
public void realRun() throws InterruptedException { |
286 |
< |
for (int i = 0; i < SIZE; ++i) { |
287 |
< |
long t0 = System.nanoTime(); |
286 |
> |
long startTime = System.nanoTime(); |
287 |
> |
for (int i = 0; i < SIZE; ++i) |
288 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
338 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
339 |
– |
} |
340 |
– |
long t0 = System.nanoTime(); |
289 |
|
aboutToWait.countDown(); |
290 |
|
try { |
291 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
291 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
292 |
|
shouldThrow(); |
293 |
< |
} catch (InterruptedException success) { |
294 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
347 |
< |
} |
293 |
> |
} catch (InterruptedException success) {} |
294 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
295 |
|
}}); |
296 |
|
|
297 |
|
aboutToWait.await(); |
298 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
298 |
> |
waitForThreadToEnterWaitState(t); |
299 |
|
t.interrupt(); |
300 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
300 |
> |
awaitTermination(t); |
301 |
|
checkEmpty(q); |
302 |
|
} |
303 |
|
|
309 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
310 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
311 |
|
public void realRun() throws InterruptedException { |
312 |
+ |
long startTime = System.nanoTime(); |
313 |
|
Thread.currentThread().interrupt(); |
314 |
< |
for (int i = 0; i < SIZE; ++i) { |
367 |
< |
long t0 = System.nanoTime(); |
314 |
> |
for (int i = 0; i < SIZE; ++i) |
315 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
369 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
370 |
– |
} |
316 |
|
try { |
317 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
317 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
318 |
|
shouldThrow(); |
319 |
|
} catch (InterruptedException success) {} |
320 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
321 |
|
}}); |
322 |
|
|
323 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
323 |
> |
awaitTermination(t); |
324 |
|
checkEmpty(q); |
325 |
|
} |
326 |
|
|
371 |
|
} |
372 |
|
|
373 |
|
/** |
428 |
– |
* remove(x) removes x and returns true if present |
429 |
– |
*/ |
430 |
– |
public void testRemoveElement() throws InterruptedException { |
431 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
432 |
– |
for (int i = 1; i < SIZE; i+=2) { |
433 |
– |
assertTrue(q.contains(i)); |
434 |
– |
assertTrue(q.remove(i)); |
435 |
– |
assertFalse(q.contains(i)); |
436 |
– |
assertTrue(q.contains(i-1)); |
437 |
– |
} |
438 |
– |
for (int i = 0; i < SIZE; i+=2) { |
439 |
– |
assertTrue(q.contains(i)); |
440 |
– |
assertTrue(q.remove(i)); |
441 |
– |
assertFalse(q.contains(i)); |
442 |
– |
assertFalse(q.remove(i+1)); |
443 |
– |
assertFalse(q.contains(i+1)); |
444 |
– |
} |
445 |
– |
checkEmpty(q); |
446 |
– |
} |
447 |
– |
|
448 |
– |
/** |
374 |
|
* An add following remove(x) succeeds |
375 |
|
*/ |
376 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
486 |
|
} |
487 |
|
|
488 |
|
/** |
564 |
– |
* toArray(null) throws NullPointerException |
565 |
– |
*/ |
566 |
– |
public void testToArray_NullArg() { |
567 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
568 |
– |
try { |
569 |
– |
q.toArray(null); |
570 |
– |
shouldThrow(); |
571 |
– |
} catch (NullPointerException success) {} |
572 |
– |
} |
573 |
– |
|
574 |
– |
/** |
489 |
|
* toArray(incompatible array type) throws ArrayStoreException |
490 |
|
*/ |
491 |
|
public void testToArray1_BadArg() { |
502 |
|
public void testIterator() throws InterruptedException { |
503 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
504 |
|
Iterator it = q.iterator(); |
505 |
< |
int i = 0; |
506 |
< |
while (it.hasNext()) { |
507 |
< |
assertEquals(it.next(), i++); |
508 |
< |
} |
505 |
> |
int i; |
506 |
> |
for (i = 0; it.hasNext(); i++) |
507 |
> |
assertTrue(q.contains(it.next())); |
508 |
> |
assertEquals(i, SIZE); |
509 |
> |
assertIteratorExhausted(it); |
510 |
> |
|
511 |
> |
it = q.iterator(); |
512 |
> |
for (i = 0; it.hasNext(); i++) |
513 |
> |
assertEquals(it.next(), q.take()); |
514 |
|
assertEquals(i, SIZE); |
515 |
+ |
assertIteratorExhausted(it); |
516 |
+ |
} |
517 |
+ |
|
518 |
+ |
/** |
519 |
+ |
* iterator of empty collection has no elements |
520 |
+ |
*/ |
521 |
+ |
public void testEmptyIterator() { |
522 |
+ |
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
523 |
|
} |
524 |
|
|
525 |
|
/** |
591 |
|
public void testOfferInExecutor() { |
592 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
593 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
594 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
594 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
595 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
596 |
|
|
597 |
< |
executor.execute(new CheckedRunnable() { |
598 |
< |
public void realRun() throws InterruptedException { |
599 |
< |
threadsStarted.await(); |
600 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
601 |
< |
}}); |
602 |
< |
|
603 |
< |
executor.execute(new CheckedRunnable() { |
676 |
< |
public void realRun() throws InterruptedException { |
677 |
< |
threadsStarted.await(); |
678 |
< |
assertSame(one, q.take()); |
679 |
< |
checkEmpty(q); |
680 |
< |
}}); |
597 |
> |
executor.execute(new CheckedRunnable() { |
598 |
> |
public void realRun() throws InterruptedException { |
599 |
> |
threadsStarted.await(); |
600 |
> |
long startTime = System.nanoTime(); |
601 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
602 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
603 |
> |
}}); |
604 |
|
|
605 |
< |
joinPool(executor); |
605 |
> |
executor.execute(new CheckedRunnable() { |
606 |
> |
public void realRun() throws InterruptedException { |
607 |
> |
threadsStarted.await(); |
608 |
> |
assertSame(one, q.take()); |
609 |
> |
checkEmpty(q); |
610 |
> |
}}); |
611 |
> |
} |
612 |
|
} |
613 |
|
|
614 |
|
/** |
617 |
|
public void testPollInExecutor() { |
618 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
619 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
620 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
620 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
621 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
622 |
|
|
623 |
< |
executor.execute(new CheckedRunnable() { |
624 |
< |
public void realRun() throws InterruptedException { |
625 |
< |
assertNull(q.poll()); |
626 |
< |
threadsStarted.await(); |
627 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
628 |
< |
checkEmpty(q); |
629 |
< |
}}); |
630 |
< |
|
631 |
< |
executor.execute(new CheckedRunnable() { |
702 |
< |
public void realRun() throws InterruptedException { |
703 |
< |
threadsStarted.await(); |
704 |
< |
q.put(one); |
705 |
< |
}}); |
623 |
> |
executor.execute(new CheckedRunnable() { |
624 |
> |
public void realRun() throws InterruptedException { |
625 |
> |
assertNull(q.poll()); |
626 |
> |
threadsStarted.await(); |
627 |
> |
long startTime = System.nanoTime(); |
628 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
630 |
> |
checkEmpty(q); |
631 |
> |
}}); |
632 |
|
|
633 |
< |
joinPool(executor); |
633 |
> |
executor.execute(new CheckedRunnable() { |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
threadsStarted.await(); |
636 |
> |
q.put(one); |
637 |
> |
}}); |
638 |
> |
} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
642 |
|
* A deserialized serialized queue has same elements in same order |
643 |
|
*/ |
644 |
|
public void testSerialization() throws Exception { |
645 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
645 |
> |
Queue x = populatedQueue(SIZE); |
646 |
> |
Queue y = serialClone(x); |
647 |
|
|
648 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
649 |
< |
ObjectOutputStream out |
650 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
651 |
< |
out.writeObject(q); |
652 |
< |
out.close(); |
653 |
< |
|
654 |
< |
ByteArrayInputStream bin |
723 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
724 |
< |
ObjectInputStream in |
725 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
726 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
727 |
< |
|
728 |
< |
assertEquals(q.size(), r.size()); |
729 |
< |
assertEquals(q.toString(), r.toString()); |
730 |
< |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
731 |
< |
while (!q.isEmpty()) { |
732 |
< |
assertEquals(q.remove(), r.remove()); |
648 |
> |
assertNotSame(y, x); |
649 |
> |
assertEquals(x.size(), y.size()); |
650 |
> |
assertEquals(x.toString(), y.toString()); |
651 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
652 |
> |
while (!x.isEmpty()) { |
653 |
> |
assertFalse(y.isEmpty()); |
654 |
> |
assertEquals(x.remove(), y.remove()); |
655 |
|
} |
656 |
< |
} |
735 |
< |
|
736 |
< |
/** |
737 |
< |
* drainTo(null) throws NullPointerException |
738 |
< |
*/ |
739 |
< |
public void testDrainToNull() { |
740 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
741 |
< |
try { |
742 |
< |
q.drainTo(null); |
743 |
< |
shouldThrow(); |
744 |
< |
} catch (NullPointerException success) {} |
745 |
< |
} |
746 |
< |
|
747 |
< |
/** |
748 |
< |
* drainTo(this) throws IllegalArgumentException |
749 |
< |
*/ |
750 |
< |
public void testDrainToSelf() { |
751 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
752 |
< |
try { |
753 |
< |
q.drainTo(q); |
754 |
< |
shouldThrow(); |
755 |
< |
} catch (IllegalArgumentException success) {} |
656 |
> |
assertTrue(y.isEmpty()); |
657 |
|
} |
658 |
|
|
659 |
|
/** |
663 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
664 |
|
ArrayList l = new ArrayList(); |
665 |
|
q.drainTo(l); |
666 |
< |
assertEquals(q.size(), 0); |
667 |
< |
assertEquals(l.size(), SIZE); |
666 |
> |
assertEquals(0, q.size()); |
667 |
> |
assertEquals(SIZE, l.size()); |
668 |
|
for (int i = 0; i < SIZE; ++i) { |
669 |
< |
assertEquals(l.get(i), i); |
669 |
> |
assertEquals(i, l.get(i)); |
670 |
|
} |
671 |
|
q.add(zero); |
672 |
|
q.add(one); |
675 |
|
assertTrue(q.contains(one)); |
676 |
|
l.clear(); |
677 |
|
q.drainTo(l); |
678 |
< |
assertEquals(q.size(), 0); |
679 |
< |
assertEquals(l.size(), 2); |
678 |
> |
assertEquals(0, q.size()); |
679 |
> |
assertEquals(2, l.size()); |
680 |
|
for (int i = 0; i < 2; ++i) { |
681 |
< |
assertEquals(l.get(i), i); |
681 |
> |
assertEquals(i, l.get(i)); |
682 |
|
} |
683 |
|
} |
684 |
|
|
694 |
|
ArrayList l = new ArrayList(); |
695 |
|
q.drainTo(l); |
696 |
|
assertTrue(l.size() >= SIZE); |
697 |
< |
for (int i = 0; i < SIZE; ++i) { |
698 |
< |
assertEquals(l.get(i), i); |
699 |
< |
} |
799 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
697 |
> |
for (int i = 0; i < SIZE; ++i) |
698 |
> |
assertEquals(i, l.get(i)); |
699 |
> |
awaitTermination(t); |
700 |
|
assertTrue(q.size() + l.size() >= SIZE); |
701 |
|
} |
702 |
|
|
703 |
|
/** |
804 |
– |
* drainTo(null, n) throws NullPointerException |
805 |
– |
*/ |
806 |
– |
public void testDrainToNullN() { |
807 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
808 |
– |
try { |
809 |
– |
q.drainTo(null, SIZE); |
810 |
– |
shouldThrow(); |
811 |
– |
} catch (NullPointerException success) {} |
812 |
– |
} |
813 |
– |
|
814 |
– |
/** |
815 |
– |
* drainTo(this, n) throws IllegalArgumentException |
816 |
– |
*/ |
817 |
– |
public void testDrainToSelfN() { |
818 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
819 |
– |
try { |
820 |
– |
q.drainTo(q, SIZE); |
821 |
– |
shouldThrow(); |
822 |
– |
} catch (IllegalArgumentException success) {} |
823 |
– |
} |
824 |
– |
|
825 |
– |
/** |
704 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
705 |
|
*/ |
706 |
|
public void testDrainToN() { |
712 |
|
ArrayList l = new ArrayList(); |
713 |
|
q.drainTo(l, i); |
714 |
|
int k = (i < SIZE) ? i : SIZE; |
715 |
< |
assertEquals(l.size(), k); |
716 |
< |
assertEquals(q.size(), SIZE - k); |
717 |
< |
for (int j = 0; j < k; ++j) { |
718 |
< |
assertEquals(l.get(j), j); |
719 |
< |
} |
842 |
< |
while (q.poll() != null) |
843 |
< |
; |
715 |
> |
assertEquals(k, l.size()); |
716 |
> |
assertEquals(SIZE - k, q.size()); |
717 |
> |
for (int j = 0; j < k; ++j) |
718 |
> |
assertEquals(j, l.get(j)); |
719 |
> |
do {} while (q.poll() != null); |
720 |
|
} |
721 |
|
} |
722 |
|
|
726 |
|
*/ |
727 |
|
public void testWaitingConsumer() throws InterruptedException { |
728 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
729 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
729 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
730 |
|
assertFalse(q.hasWaitingConsumer()); |
731 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
732 |
|
|
733 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
734 |
|
public void realRun() throws InterruptedException { |
735 |
|
threadStarted.countDown(); |
736 |
+ |
long startTime = System.nanoTime(); |
737 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
738 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
738 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
739 |
|
assertFalse(q.hasWaitingConsumer()); |
740 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
741 |
|
}}); |
742 |
|
|
743 |
|
threadStarted.await(); |
744 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
745 |
< |
assertEquals(q.getWaitingConsumerCount(), 1); |
744 |
> |
waitForThreadToEnterWaitState(t); |
745 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
746 |
|
assertTrue(q.hasWaitingConsumer()); |
747 |
|
|
748 |
|
assertTrue(q.offer(one)); |
749 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
749 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
750 |
|
assertFalse(q.hasWaitingConsumer()); |
751 |
|
|
752 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
752 |
> |
awaitTermination(t); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
780 |
|
}}); |
781 |
|
|
782 |
|
threadStarted.await(); |
783 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
783 |
> |
waitForThreadToEnterWaitState(t); |
784 |
|
assertEquals(1, q.size()); |
785 |
|
assertSame(five, q.poll()); |
786 |
|
checkEmpty(q); |
787 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
787 |
> |
awaitTermination(t); |
788 |
|
} |
789 |
|
|
790 |
|
/** |
840 |
|
assertEquals(1, q.size()); |
841 |
|
assertTrue(q.offer(three)); |
842 |
|
assertSame(four, q.poll()); |
843 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
843 |
> |
awaitTermination(t); |
844 |
|
} |
845 |
|
|
846 |
|
/** |
863 |
|
assertEquals(1, q.size()); |
864 |
|
assertSame(four, q.take()); |
865 |
|
checkEmpty(q); |
866 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
866 |
> |
awaitTermination(t); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
870 |
|
* tryTransfer(null) throws NullPointerException |
871 |
|
*/ |
872 |
|
public void testTryTransfer1() { |
873 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
874 |
|
try { |
996 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
875 |
|
q.tryTransfer(null); |
876 |
|
shouldThrow(); |
877 |
|
} catch (NullPointerException success) {} |
905 |
|
assertTrue(q.tryTransfer(hotPotato)); |
906 |
|
}}); |
907 |
|
|
908 |
< |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
908 |
> |
long startTime = System.nanoTime(); |
909 |
> |
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
910 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
911 |
|
checkEmpty(q); |
912 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
912 |
> |
awaitTermination(t); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
931 |
|
|
932 |
|
assertSame(q.take(), hotPotato); |
933 |
|
checkEmpty(q); |
934 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
934 |
> |
awaitTermination(t); |
935 |
|
} |
936 |
|
|
937 |
|
/** |
944 |
|
|
945 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
946 |
|
public void realRun() throws InterruptedException { |
947 |
+ |
long startTime = System.nanoTime(); |
948 |
|
Thread.currentThread().interrupt(); |
949 |
|
try { |
950 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
958 |
|
shouldThrow(); |
959 |
|
} catch (InterruptedException success) {} |
960 |
|
assertFalse(Thread.interrupted()); |
961 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
962 |
|
}}); |
963 |
|
|
964 |
|
await(pleaseInterrupt); |
976 |
|
|
977 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
978 |
|
public void realRun() throws InterruptedException { |
979 |
< |
long t0 = System.nanoTime(); |
979 |
> |
long startTime = System.nanoTime(); |
980 |
|
assertFalse(q.tryTransfer(new Object(), |
981 |
|
timeoutMillis(), MILLISECONDS)); |
982 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
982 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
983 |
|
checkEmpty(q); |
984 |
|
}}); |
985 |
|
|
997 |
|
|
998 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
999 |
|
public void realRun() throws InterruptedException { |
1000 |
< |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1000 |
> |
long startTime = System.nanoTime(); |
1001 |
> |
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1002 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1003 |
|
checkEmpty(q); |
1004 |
|
}}); |
1005 |
|
|
1009 |
|
assertSame(four, q.poll()); |
1010 |
|
assertSame(five, q.poll()); |
1011 |
|
checkEmpty(q); |
1012 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1012 |
> |
awaitTermination(t); |
1013 |
|
} |
1014 |
|
|
1015 |
|
/** |
1020 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1021 |
|
assertTrue(q.offer(four)); |
1022 |
|
assertEquals(1, q.size()); |
1023 |
< |
long t0 = System.nanoTime(); |
1023 |
> |
long startTime = System.nanoTime(); |
1024 |
|
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1025 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1025 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1026 |
|
assertEquals(1, q.size()); |
1027 |
|
assertSame(four, q.poll()); |
1028 |
|
assertNull(q.poll()); |
1040 |
|
assertFalse(q.isEmpty()); |
1041 |
|
return q; |
1042 |
|
} |
1043 |
+ |
|
1044 |
+ |
/** |
1045 |
+ |
* remove(null), contains(null) always return false |
1046 |
+ |
*/ |
1047 |
+ |
public void testNeverContainsNull() { |
1048 |
+ |
Collection<?>[] qs = { |
1049 |
+ |
new LinkedTransferQueue<Object>(), |
1050 |
+ |
populatedQueue(2), |
1051 |
+ |
}; |
1052 |
+ |
|
1053 |
+ |
for (Collection<?> q : qs) { |
1054 |
+ |
assertFalse(q.contains(null)); |
1055 |
+ |
assertFalse(q.remove(null)); |
1056 |
+ |
} |
1057 |
+ |
} |
1058 |
|
} |