ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.86
Committed: Tue Jan 26 13:33:06 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.85: +162 -170 lines
Log Message:
Replace Integer with Item class

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.44 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 * Other contributors include John Vint
6     */
7    
8 jsr166 1.55 import static java.util.concurrent.TimeUnit.MILLISECONDS;
9    
10     import java.util.ArrayList;
11 jsr166 1.48 import java.util.Arrays;
12 jsr166 1.47 import java.util.Collection;
13 dl 1.1 import java.util.Iterator;
14 jsr166 1.12 import java.util.List;
15 dl 1.1 import java.util.NoSuchElementException;
16 jsr166 1.48 import java.util.Queue;
17 jsr166 1.47 import java.util.concurrent.BlockingQueue;
18 jsr166 1.70 import java.util.concurrent.Callable;
19 jsr166 1.47 import java.util.concurrent.CountDownLatch;
20     import java.util.concurrent.Executors;
21     import java.util.concurrent.ExecutorService;
22     import java.util.concurrent.LinkedTransferQueue;
23 jsr166 1.55
24     import junit.framework.Test;
25 dl 1.1
26 jsr166 1.10 @SuppressWarnings({"unchecked", "rawtypes"})
27 dl 1.1 public class LinkedTransferQueueTest extends JSR166TestCase {
28 jsr166 1.25 public static class Generic extends BlockingQueueTest {
29     protected BlockingQueue emptyCollection() {
30     return new LinkedTransferQueue();
31     }
32     }
33    
34 dl 1.1 public static void main(String[] args) {
35 jsr166 1.59 main(suite(), args);
36 dl 1.1 }
37    
38     public static Test suite() {
39 jsr166 1.67 class Implementation implements CollectionImplementation {
40     public Class<?> klazz() { return LinkedTransferQueue.class; }
41     public Collection emptyCollection() { return new LinkedTransferQueue(); }
42 dl 1.86 public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
43 jsr166 1.67 public boolean isConcurrent() { return true; }
44     public boolean permitsNulls() { return false; }
45     }
46 jsr166 1.25 return newTestSuite(LinkedTransferQueueTest.class,
47 jsr166 1.62 new Generic().testSuite(),
48     CollectionTest.testSuite(new Implementation()));
49 dl 1.1 }
50    
51 jsr166 1.3 /**
52 jsr166 1.7 * Constructor builds new queue with size being zero and empty
53     * being true
54 dl 1.1 */
55     public void testConstructor1() {
56 dl 1.86 mustEqual(0, new LinkedTransferQueue().size());
57 dl 1.1 assertTrue(new LinkedTransferQueue().isEmpty());
58     }
59    
60 jsr166 1.3 /**
61 jsr166 1.7 * Initializing constructor with null collection throws
62     * NullPointerException
63 dl 1.1 */
64     public void testConstructor2() {
65     try {
66     new LinkedTransferQueue(null);
67     shouldThrow();
68 jsr166 1.17 } catch (NullPointerException success) {}
69 dl 1.1 }
70    
71     /**
72 jsr166 1.7 * Initializing from Collection of null elements throws
73     * NullPointerException
74 dl 1.1 */
75     public void testConstructor3() {
76 dl 1.86 Collection<Item> elements = Arrays.asList(new Item[SIZE]);
77 dl 1.1 try {
78 jsr166 1.47 new LinkedTransferQueue(elements);
79 dl 1.1 shouldThrow();
80 jsr166 1.17 } catch (NullPointerException success) {}
81 dl 1.1 }
82 jsr166 1.3
83     /**
84 dl 1.1 * Initializing constructor with a collection containing some null elements
85 jsr166 1.5 * throws NullPointerException
86 dl 1.1 */
87     public void testConstructor4() {
88 dl 1.86 Item[] items = new Item[2];
89     items[0] = zero;
90     Collection<Item> elements = Arrays.asList(items);
91 dl 1.1 try {
92 jsr166 1.47 new LinkedTransferQueue(elements);
93 dl 1.1 shouldThrow();
94 jsr166 1.17 } catch (NullPointerException success) {}
95 dl 1.1 }
96    
97 jsr166 1.3 /**
98 dl 1.1 * Queue contains all elements of the collection it is initialized by
99     */
100     public void testConstructor5() {
101 dl 1.86 Item[] items = defaultItems;
102     List<Item> intList = Arrays.asList(items);
103     LinkedTransferQueue<Item> q
104     = new LinkedTransferQueue<Item>(intList);
105     mustEqual(q.size(), intList.size());
106     mustEqual(q.toString(), intList.toString());
107 jsr166 1.12 assertTrue(Arrays.equals(q.toArray(),
108     intList.toArray()));
109     assertTrue(Arrays.equals(q.toArray(new Object[0]),
110     intList.toArray(new Object[0])));
111     assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
112     intList.toArray(new Object[SIZE])));
113     for (int i = 0; i < SIZE; ++i) {
114 dl 1.86 mustEqual(items[i], q.poll());
115 dl 1.1 }
116     }
117    
118     /**
119 jsr166 1.12 * remainingCapacity() always returns Integer.MAX_VALUE
120 dl 1.1 */
121     public void testRemainingCapacity() {
122 dl 1.86 BlockingQueue<Item> q = populatedQueue(SIZE);
123 dl 1.1 for (int i = 0; i < SIZE; ++i) {
124 dl 1.86 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
125     mustEqual(SIZE - i, q.size());
126     mustEqual(i, q.remove());
127 dl 1.1 }
128     for (int i = 0; i < SIZE; ++i) {
129 dl 1.86 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
130     mustEqual(i, q.size());
131     mustAdd(q, i);
132 dl 1.1 }
133     }
134    
135     /**
136 jsr166 1.5 * addAll(this) throws IllegalArgumentException
137 dl 1.1 */
138     public void testAddAllSelf() {
139 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
140 dl 1.1 try {
141     q.addAll(q);
142     shouldThrow();
143 jsr166 1.17 } catch (IllegalArgumentException success) {}
144 dl 1.1 }
145    
146     /**
147 jsr166 1.7 * addAll of a collection with any null elements throws
148     * NullPointerException after possibly adding some elements
149 dl 1.1 */
150     public void testAddAll3() {
151 dl 1.86 LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
152     Item[] items = new Item[2]; items[0] = zero;
153 dl 1.1 try {
154 dl 1.86 q.addAll(Arrays.asList(items));
155 dl 1.1 shouldThrow();
156 jsr166 1.17 } catch (NullPointerException success) {}
157 dl 1.1 }
158    
159     /**
160     * Queue contains all elements, in traversal order, of successful addAll
161     */
162     public void testAddAll5() {
163 dl 1.86 Item[] empty = new Item[0];
164     Item[] items = defaultItems;
165 jsr166 1.5 LinkedTransferQueue q = new LinkedTransferQueue();
166     assertFalse(q.addAll(Arrays.asList(empty)));
167 dl 1.86 assertTrue(q.addAll(Arrays.asList(items)));
168 jsr166 1.5 for (int i = 0; i < SIZE; ++i) {
169 dl 1.86 mustEqual(items[i], q.poll());
170 dl 1.1 }
171     }
172    
173     /**
174     * all elements successfully put are contained
175     */
176     public void testPut() {
177 dl 1.86 LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
178     Item[] items = defaultItems;
179 jsr166 1.5 for (int i = 0; i < SIZE; ++i) {
180 dl 1.86 mustEqual(i, q.size());
181     q.put(items[i]);
182     mustContain(q, items[i]);
183 dl 1.1 }
184     }
185    
186     /**
187     * take retrieves elements in FIFO order
188     */
189 jsr166 1.5 public void testTake() throws InterruptedException {
190 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
191 jsr166 1.5 for (int i = 0; i < SIZE; ++i) {
192 dl 1.86 mustEqual(i, q.take());
193 dl 1.1 }
194     }
195    
196     /**
197 jsr166 1.28 * take removes existing elements until empty, then blocks interruptibly
198 dl 1.1 */
199 jsr166 1.5 public void testBlockingTake() throws InterruptedException {
200 dl 1.86 final BlockingQueue<Item> q = populatedQueue(SIZE);
201 jsr166 1.46 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
202 jsr166 1.31 Thread t = newStartedThread(new CheckedRunnable() {
203 jsr166 1.19 public void realRun() throws InterruptedException {
204 dl 1.86 for (int i = 0; i < SIZE; i++) mustEqual(i, q.take());
205 jsr166 1.46
206     Thread.currentThread().interrupt();
207     try {
208     q.take();
209     shouldThrow();
210     } catch (InterruptedException success) {}
211     assertFalse(Thread.interrupted());
212    
213     pleaseInterrupt.countDown();
214 jsr166 1.19 try {
215     q.take();
216     shouldThrow();
217     } catch (InterruptedException success) {}
218 jsr166 1.46 assertFalse(Thread.interrupted());
219 jsr166 1.5 }});
220 jsr166 1.19
221 jsr166 1.46 await(pleaseInterrupt);
222 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
223 jsr166 1.5 t.interrupt();
224 jsr166 1.46 awaitTermination(t);
225 dl 1.1 }
226    
227     /**
228     * poll succeeds unless empty
229     */
230 jsr166 1.12 public void testPoll() throws InterruptedException {
231 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
232 dl 1.1 for (int i = 0; i < SIZE; ++i) {
233 dl 1.86 mustEqual(i, q.poll());
234 dl 1.1 }
235     assertNull(q.poll());
236 jsr166 1.12 checkEmpty(q);
237 dl 1.1 }
238    
239     /**
240 jsr166 1.30 * timed poll with zero timeout succeeds when non-empty, else times out
241 dl 1.1 */
242 jsr166 1.5 public void testTimedPoll0() throws InterruptedException {
243 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
244 jsr166 1.5 for (int i = 0; i < SIZE; ++i) {
245 dl 1.86 mustEqual(i, q.poll(0, MILLISECONDS));
246 dl 1.1 }
247 jsr166 1.7 assertNull(q.poll(0, MILLISECONDS));
248 jsr166 1.12 checkEmpty(q);
249 dl 1.1 }
250    
251     /**
252 jsr166 1.30 * timed poll with nonzero timeout succeeds when non-empty, else times out
253 dl 1.1 */
254 jsr166 1.5 public void testTimedPoll() throws InterruptedException {
255 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
256 jsr166 1.66 long startTime = System.nanoTime();
257     for (int i = 0; i < SIZE; ++i)
258 dl 1.86 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
259 jsr166 1.66 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
260    
261     startTime = System.nanoTime();
262 jsr166 1.46 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
263     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
264 jsr166 1.12 checkEmpty(q);
265 dl 1.1 }
266    
267     /**
268     * Interrupted timed poll throws InterruptedException instead of
269     * returning timeout status
270     */
271 jsr166 1.5 public void testInterruptedTimedPoll() throws InterruptedException {
272 dl 1.86 final BlockingQueue<Item> q = populatedQueue(SIZE);
273 jsr166 1.76 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
274 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
275 jsr166 1.21 public void realRun() throws InterruptedException {
276 jsr166 1.78 for (int i = 0; i < SIZE; i++)
277 dl 1.86 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
278 jsr166 1.76
279 jsr166 1.78 Thread.currentThread().interrupt();
280     try {
281 jsr166 1.82 q.poll(randomTimeout(), randomTimeUnit());
282 jsr166 1.78 shouldThrow();
283     } catch (InterruptedException success) {}
284     assertFalse(Thread.interrupted());
285    
286 jsr166 1.76 pleaseInterrupt.countDown();
287 jsr166 1.16 try {
288 jsr166 1.83 q.poll(LONGER_DELAY_MS, MILLISECONDS);
289 jsr166 1.17 shouldThrow();
290 jsr166 1.66 } catch (InterruptedException success) {}
291 jsr166 1.76 assertFalse(Thread.interrupted());
292 jsr166 1.5 }});
293 jsr166 1.17
294 jsr166 1.76 await(pleaseInterrupt);
295 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
296 jsr166 1.5 t.interrupt();
297 jsr166 1.64 awaitTermination(t);
298 jsr166 1.12 checkEmpty(q);
299 dl 1.1 }
300    
301     /**
302 jsr166 1.31 * timed poll after thread interrupted throws InterruptedException
303     * instead of returning timeout status
304     */
305     public void testTimedPollAfterInterrupt() throws InterruptedException {
306 dl 1.86 final BlockingQueue<Item> q = populatedQueue(SIZE);
307 jsr166 1.31 Thread t = newStartedThread(new CheckedRunnable() {
308     public void realRun() throws InterruptedException {
309     Thread.currentThread().interrupt();
310 jsr166 1.66 for (int i = 0; i < SIZE; ++i)
311 dl 1.86 mustEqual(i, q.poll(randomTimeout(), randomTimeUnit()));
312 jsr166 1.31 try {
313 jsr166 1.82 q.poll(randomTimeout(), randomTimeUnit());
314 jsr166 1.31 shouldThrow();
315     } catch (InterruptedException success) {}
316 jsr166 1.77 assertFalse(Thread.interrupted());
317 jsr166 1.31 }});
318    
319 jsr166 1.66 awaitTermination(t);
320 jsr166 1.31 checkEmpty(q);
321     }
322    
323     /**
324 dl 1.1 * peek returns next element, or null if empty
325     */
326 jsr166 1.12 public void testPeek() throws InterruptedException {
327 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
328 dl 1.1 for (int i = 0; i < SIZE; ++i) {
329 dl 1.86 mustEqual(i, q.peek());
330     mustEqual(i, q.poll());
331 dl 1.1 assertTrue(q.peek() == null ||
332 dl 1.86 i != q.peek().value);
333 dl 1.1 }
334     assertNull(q.peek());
335 jsr166 1.12 checkEmpty(q);
336 dl 1.1 }
337    
338     /**
339 jsr166 1.5 * element returns next element, or throws NoSuchElementException if empty
340 dl 1.1 */
341 jsr166 1.12 public void testElement() throws InterruptedException {
342 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
343 dl 1.1 for (int i = 0; i < SIZE; ++i) {
344 dl 1.86 mustEqual(i, q.element());
345     mustEqual(i, q.poll());
346 dl 1.1 }
347     try {
348     q.element();
349     shouldThrow();
350 jsr166 1.17 } catch (NoSuchElementException success) {}
351 jsr166 1.12 checkEmpty(q);
352 dl 1.1 }
353    
354     /**
355 jsr166 1.5 * remove removes next element, or throws NoSuchElementException if empty
356 dl 1.1 */
357 jsr166 1.12 public void testRemove() throws InterruptedException {
358 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
359 dl 1.1 for (int i = 0; i < SIZE; ++i) {
360 dl 1.86 mustEqual(i, q.remove());
361 dl 1.1 }
362     try {
363     q.remove();
364     shouldThrow();
365 jsr166 1.17 } catch (NoSuchElementException success) {}
366 jsr166 1.12 checkEmpty(q);
367 dl 1.1 }
368    
369     /**
370     * An add following remove(x) succeeds
371     */
372 jsr166 1.5 public void testRemoveElementAndAdd() throws InterruptedException {
373 dl 1.86 LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
374     mustAdd(q, one);
375     mustAdd(q, two);
376     mustRemove(q, one);
377     mustRemove(q, two);
378     mustAdd(q, three);
379     mustEqual(q.take(), three);
380 dl 1.1 }
381    
382     /**
383     * contains(x) reports true when elements added but not yet removed
384     */
385     public void testContains() {
386 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
387 dl 1.1 for (int i = 0; i < SIZE; ++i) {
388 dl 1.86 mustContain(q, i);
389     mustEqual(i, q.poll());
390     mustNotContain(q, i);
391 dl 1.1 }
392     }
393    
394     /**
395     * clear removes all elements
396     */
397 jsr166 1.12 public void testClear() throws InterruptedException {
398 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
399 dl 1.1 q.clear();
400 jsr166 1.12 checkEmpty(q);
401 dl 1.86 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
402 dl 1.1 q.add(one);
403     assertFalse(q.isEmpty());
404 dl 1.86 mustEqual(1, q.size());
405     mustContain(q, one);
406 dl 1.1 q.clear();
407 jsr166 1.12 checkEmpty(q);
408 dl 1.1 }
409    
410     /**
411     * containsAll(c) is true when c contains a subset of elements
412     */
413     public void testContainsAll() {
414 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
415     LinkedTransferQueue<Item> p = new LinkedTransferQueue<>();
416 dl 1.1 for (int i = 0; i < SIZE; ++i) {
417     assertTrue(q.containsAll(p));
418     assertFalse(p.containsAll(q));
419 dl 1.86 mustAdd(p, i);
420 dl 1.1 }
421     assertTrue(p.containsAll(q));
422     }
423    
424     /**
425 jsr166 1.7 * retainAll(c) retains only those elements of c and reports true
426     * if changed
427 dl 1.1 */
428     public void testRetainAll() {
429 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
430     LinkedTransferQueue<Item> p = populatedQueue(SIZE);
431 dl 1.1 for (int i = 0; i < SIZE; ++i) {
432     boolean changed = q.retainAll(p);
433     if (i == 0) {
434     assertFalse(changed);
435     } else {
436     assertTrue(changed);
437     }
438     assertTrue(q.containsAll(p));
439 dl 1.86 mustEqual(SIZE - i, q.size());
440 dl 1.1 p.remove();
441     }
442     }
443    
444     /**
445 jsr166 1.7 * removeAll(c) removes only those elements of c and reports true
446     * if changed
447 dl 1.1 */
448     public void testRemoveAll() {
449     for (int i = 1; i < SIZE; ++i) {
450 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
451     LinkedTransferQueue<Item> p = populatedQueue(i);
452 dl 1.1 assertTrue(q.removeAll(p));
453 dl 1.86 mustEqual(SIZE - i, q.size());
454 dl 1.1 for (int j = 0; j < i; ++j) {
455 dl 1.86 mustNotContain(q, p.remove());
456 dl 1.1 }
457     }
458     }
459    
460     /**
461 jsr166 1.38 * toArray() contains all elements in FIFO order
462 dl 1.1 */
463 jsr166 1.38 public void testToArray() {
464 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
465 jsr166 1.81 Object[] a = q.toArray();
466     assertSame(Object[].class, a.getClass());
467     for (Object o : a)
468     assertSame(o, q.poll());
469     assertTrue(q.isEmpty());
470 dl 1.1 }
471    
472     /**
473 jsr166 1.38 * toArray(a) contains all elements in FIFO order
474 dl 1.1 */
475 jsr166 1.38 public void testToArray2() {
476 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
477     Item[] items = new Item[SIZE];
478     Item[] array = q.toArray(items);
479     assertSame(items, array);
480     for (Item o : items)
481 jsr166 1.81 assertSame(o, q.poll());
482     assertTrue(q.isEmpty());
483 dl 1.1 }
484    
485     /**
486 jsr166 1.36 * toArray(incompatible array type) throws ArrayStoreException
487 dl 1.1 */
488     public void testToArray1_BadArg() {
489 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
490 dl 1.1 try {
491 jsr166 1.36 q.toArray(new String[10]);
492 dl 1.1 shouldThrow();
493 jsr166 1.17 } catch (ArrayStoreException success) {}
494 dl 1.1 }
495    
496     /**
497     * iterator iterates through all elements
498     */
499 jsr166 1.5 public void testIterator() throws InterruptedException {
500 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
501     Iterator<? extends Item> it = q.iterator();
502 jsr166 1.57 int i;
503     for (i = 0; it.hasNext(); i++)
504 dl 1.86 mustContain(q, it.next());
505     mustEqual(i, SIZE);
506 jsr166 1.57 assertIteratorExhausted(it);
507    
508     it = q.iterator();
509     for (i = 0; it.hasNext(); i++)
510 dl 1.86 mustEqual(it.next(), q.take());
511     mustEqual(i, SIZE);
512 jsr166 1.57 assertIteratorExhausted(it);
513     }
514    
515     /**
516     * iterator of empty collection has no elements
517     */
518     public void testEmptyIterator() {
519     assertIteratorExhausted(new LinkedTransferQueue().iterator());
520 dl 1.1 }
521    
522     /**
523 jsr166 1.12 * iterator.remove() removes current element
524 dl 1.1 */
525     public void testIteratorRemove() {
526 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
527 dl 1.1 q.add(two);
528     q.add(one);
529     q.add(three);
530    
531 dl 1.86 Iterator<? extends Item> it = q.iterator();
532 dl 1.1 it.next();
533     it.remove();
534    
535     it = q.iterator();
536 jsr166 1.24 assertSame(it.next(), one);
537     assertSame(it.next(), three);
538 dl 1.1 assertFalse(it.hasNext());
539     }
540    
541     /**
542     * iterator ordering is FIFO
543     */
544     public void testIteratorOrdering() {
545 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
546     mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
547 dl 1.1 q.add(one);
548     q.add(two);
549     q.add(three);
550 dl 1.86 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
551 dl 1.1 int k = 0;
552 dl 1.86 for (Item n : q) {
553     mustEqual(++k, n);
554 dl 1.1 }
555 dl 1.86 mustEqual(3, k);
556 dl 1.1 }
557    
558     /**
559     * Modifications do not cause iterators to fail
560     */
561     public void testWeaklyConsistentIteration() {
562 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
563 dl 1.1 q.add(one);
564     q.add(two);
565     q.add(three);
566 dl 1.86 for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
567 jsr166 1.5 q.remove();
568     it.next();
569 dl 1.1 }
570 dl 1.86 mustEqual(0, q.size());
571 dl 1.1 }
572    
573     /**
574     * toString contains toStrings of elements
575     */
576     public void testToString() {
577 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
578 dl 1.1 String s = q.toString();
579     for (int i = 0; i < SIZE; ++i) {
580 jsr166 1.46 assertTrue(s.contains(String.valueOf(i)));
581 dl 1.1 }
582     }
583    
584     /**
585     * offer transfers elements across Executor tasks
586     */
587     public void testOfferInExecutor() {
588 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
589 jsr166 1.46 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
590 jsr166 1.63 final ExecutorService executor = Executors.newFixedThreadPool(2);
591     try (PoolCleaner cleaner = cleaner(executor)) {
592 jsr166 1.7
593 jsr166 1.63 executor.execute(new CheckedRunnable() {
594     public void realRun() throws InterruptedException {
595     threadsStarted.await();
596 jsr166 1.66 long startTime = System.nanoTime();
597 jsr166 1.63 assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
598 jsr166 1.66 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
599 jsr166 1.63 }});
600 dl 1.1
601 jsr166 1.63 executor.execute(new CheckedRunnable() {
602     public void realRun() throws InterruptedException {
603     threadsStarted.await();
604     assertSame(one, q.take());
605     checkEmpty(q);
606     }});
607     }
608 dl 1.1 }
609    
610     /**
611 jsr166 1.13 * timed poll retrieves elements across Executor threads
612 dl 1.1 */
613     public void testPollInExecutor() {
614 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
615 jsr166 1.46 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
616 jsr166 1.63 final ExecutorService executor = Executors.newFixedThreadPool(2);
617     try (PoolCleaner cleaner = cleaner(executor)) {
618 jsr166 1.7
619 jsr166 1.63 executor.execute(new CheckedRunnable() {
620     public void realRun() throws InterruptedException {
621     assertNull(q.poll());
622     threadsStarted.await();
623 jsr166 1.66 long startTime = System.nanoTime();
624 jsr166 1.63 assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
625 jsr166 1.66 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
626 jsr166 1.63 checkEmpty(q);
627     }});
628 dl 1.1
629 jsr166 1.63 executor.execute(new CheckedRunnable() {
630     public void realRun() throws InterruptedException {
631     threadsStarted.await();
632     q.put(one);
633     }});
634     }
635 dl 1.1 }
636    
637     /**
638 jsr166 1.79 * A deserialized/reserialized queue has same elements in same order
639 dl 1.1 */
640 jsr166 1.5 public void testSerialization() throws Exception {
641 dl 1.86 Queue<Item> x = populatedQueue(SIZE);
642     Queue<Item> y = serialClone(x);
643 dl 1.1
644 jsr166 1.52 assertNotSame(y, x);
645 dl 1.86 mustEqual(x.size(), y.size());
646     mustEqual(x.toString(), y.toString());
647 jsr166 1.48 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
648     while (!x.isEmpty()) {
649     assertFalse(y.isEmpty());
650 dl 1.86 mustEqual(x.remove(), y.remove());
651 dl 1.1 }
652 jsr166 1.48 assertTrue(y.isEmpty());
653 dl 1.1 }
654    
655     /**
656     * drainTo(c) empties queue into another collection c
657     */
658     public void testDrainTo() {
659 dl 1.86 LinkedTransferQueue<Item> q = populatedQueue(SIZE);
660 dl 1.1 ArrayList l = new ArrayList();
661     q.drainTo(l);
662 dl 1.86 mustEqual(0, q.size());
663     mustEqual(SIZE, l.size());
664 dl 1.1 for (int i = 0; i < SIZE; ++i) {
665 dl 1.86 mustEqual(i, l.get(i));
666 dl 1.1 }
667     q.add(zero);
668     q.add(one);
669     assertFalse(q.isEmpty());
670 dl 1.86 mustContain(q, zero);
671     mustContain(q, one);
672 dl 1.1 l.clear();
673     q.drainTo(l);
674 dl 1.86 mustEqual(0, q.size());
675     mustEqual(2, l.size());
676 dl 1.1 for (int i = 0; i < 2; ++i) {
677 dl 1.86 mustEqual(i, l.get(i));
678 dl 1.1 }
679     }
680    
681     /**
682 jsr166 1.13 * drainTo(c) empties full queue, unblocking a waiting put.
683 dl 1.1 */
684 jsr166 1.5 public void testDrainToWithActivePut() throws InterruptedException {
685 dl 1.86 final LinkedTransferQueue<Item> q = populatedQueue(SIZE);
686 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
687 jsr166 1.21 public void realRun() {
688 dl 1.86 q.put(new Item(SIZE + 1));
689 jsr166 1.5 }});
690     ArrayList l = new ArrayList();
691     q.drainTo(l);
692     assertTrue(l.size() >= SIZE);
693 jsr166 1.51 for (int i = 0; i < SIZE; ++i)
694 dl 1.86 mustEqual(i, l.get(i));
695 jsr166 1.66 awaitTermination(t);
696 jsr166 1.5 assertTrue(q.size() + l.size() >= SIZE);
697 dl 1.1 }
698    
699     /**
700 jsr166 1.26 * drainTo(c, n) empties first min(n, size) elements of queue into c
701 dl 1.1 */
702     public void testDrainToN() {
703 dl 1.86 LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
704 dl 1.1 for (int i = 0; i < SIZE + 2; ++i) {
705     for (int j = 0; j < SIZE; j++) {
706 dl 1.86 mustOffer(q, j);
707 dl 1.1 }
708     ArrayList l = new ArrayList();
709     q.drainTo(l, i);
710     int k = (i < SIZE) ? i : SIZE;
711 dl 1.86 mustEqual(k, l.size());
712     mustEqual(SIZE - k, q.size());
713 jsr166 1.51 for (int j = 0; j < k; ++j)
714 dl 1.86 mustEqual(j, l.get(j));
715 jsr166 1.56 do {} while (q.poll() != null);
716 dl 1.1 }
717     }
718    
719 jsr166 1.3 /**
720 jsr166 1.13 * timed poll() or take() increments the waiting consumer count;
721     * offer(e) decrements the waiting consumer count
722 dl 1.1 */
723 jsr166 1.5 public void testWaitingConsumer() throws InterruptedException {
724 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
725     mustEqual(0, q.getWaitingConsumerCount());
726 jsr166 1.13 assertFalse(q.hasWaitingConsumer());
727 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
728 jsr166 1.9
729     Thread t = newStartedThread(new CheckedRunnable() {
730 jsr166 1.21 public void realRun() throws InterruptedException {
731 jsr166 1.31 threadStarted.countDown();
732 jsr166 1.66 long startTime = System.nanoTime();
733 jsr166 1.31 assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
734 dl 1.86 mustEqual(0, q.getWaitingConsumerCount());
735 jsr166 1.22 assertFalse(q.hasWaitingConsumer());
736 jsr166 1.66 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
737 jsr166 1.9 }});
738    
739 jsr166 1.31 threadStarted.await();
740 jsr166 1.70 Callable<Boolean> oneConsumer
741     = new Callable<Boolean>() { public Boolean call() {
742     return q.hasWaitingConsumer()
743     && q.getWaitingConsumerCount() == 1; }};
744     waitForThreadToEnterWaitState(t, oneConsumer);
745 jsr166 1.31
746     assertTrue(q.offer(one));
747 dl 1.86 mustEqual(0, q.getWaitingConsumerCount());
748 jsr166 1.13 assertFalse(q.hasWaitingConsumer());
749 jsr166 1.31
750 jsr166 1.66 awaitTermination(t);
751 dl 1.1 }
752 jsr166 1.3
753     /**
754 jsr166 1.5 * transfer(null) throws NullPointerException
755 dl 1.1 */
756 jsr166 1.5 public void testTransfer1() throws InterruptedException {
757 dl 1.1 try {
758 dl 1.86 LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
759 dl 1.1 q.transfer(null);
760     shouldThrow();
761 jsr166 1.15 } catch (NullPointerException success) {}
762 dl 1.1 }
763    
764 jsr166 1.3 /**
765 jsr166 1.80 * transfer waits until a poll occurs. The transferred element
766 jsr166 1.69 * is returned by the associated poll.
767 dl 1.1 */
768 jsr166 1.5 public void testTransfer2() throws InterruptedException {
769 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
770 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
771 jsr166 1.5
772 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
773 jsr166 1.21 public void realRun() throws InterruptedException {
774 jsr166 1.31 threadStarted.countDown();
775 jsr166 1.34 q.transfer(five);
776 jsr166 1.31 checkEmpty(q);
777 jsr166 1.9 }});
778 dl 1.1
779 jsr166 1.31 threadStarted.await();
780 jsr166 1.70 Callable<Boolean> oneElement
781     = new Callable<Boolean>() { public Boolean call() {
782     return !q.isEmpty() && q.size() == 1; }};
783     waitForThreadToEnterWaitState(t, oneElement);
784    
785 jsr166 1.34 assertSame(five, q.poll());
786 jsr166 1.31 checkEmpty(q);
787 jsr166 1.66 awaitTermination(t);
788 dl 1.1 }
789 jsr166 1.3
790     /**
791 dl 1.6 * transfer waits until a poll occurs, and then transfers in fifo order
792 dl 1.1 */
793 jsr166 1.5 public void testTransfer3() throws InterruptedException {
794 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
795 jsr166 1.5
796 jsr166 1.9 Thread first = newStartedThread(new CheckedRunnable() {
797 jsr166 1.21 public void realRun() throws InterruptedException {
798 jsr166 1.32 q.transfer(four);
799 dl 1.86 mustNotContain(q, four);
800     mustEqual(1, q.size());
801 jsr166 1.7 }});
802 jsr166 1.5
803 jsr166 1.9 Thread interruptedThread = newStartedThread(
804     new CheckedInterruptedRunnable() {
805 jsr166 1.21 public void realRun() throws InterruptedException {
806 jsr166 1.32 while (q.isEmpty())
807 jsr166 1.9 Thread.yield();
808 jsr166 1.32 q.transfer(five);
809 jsr166 1.9 }});
810 jsr166 1.5
811     while (q.size() < 2)
812     Thread.yield();
813 dl 1.86 mustEqual(2, q.size());
814 jsr166 1.32 assertSame(four, q.poll());
815 jsr166 1.5 first.join();
816 dl 1.86 mustEqual(1, q.size());
817 jsr166 1.5 interruptedThread.interrupt();
818     interruptedThread.join();
819 jsr166 1.31 checkEmpty(q);
820 dl 1.1 }
821    
822     /**
823 dl 1.6 * transfer waits until a poll occurs, at which point the polling
824     * thread returns the element
825 dl 1.1 */
826 jsr166 1.5 public void testTransfer4() throws InterruptedException {
827 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
828 jsr166 1.9
829     Thread t = newStartedThread(new CheckedRunnable() {
830 jsr166 1.21 public void realRun() throws InterruptedException {
831 jsr166 1.7 q.transfer(four);
832 dl 1.86 mustNotContain(q, four);
833 jsr166 1.24 assertSame(three, q.poll());
834 jsr166 1.9 }});
835    
836 jsr166 1.31 while (q.isEmpty())
837     Thread.yield();
838     assertFalse(q.isEmpty());
839 dl 1.86 mustEqual(1, q.size());
840 jsr166 1.5 assertTrue(q.offer(three));
841 jsr166 1.24 assertSame(four, q.poll());
842 jsr166 1.66 awaitTermination(t);
843 dl 1.1 }
844 jsr166 1.3
845     /**
846 jsr166 1.80 * transfer waits until a take occurs. The transferred element
847 jsr166 1.69 * is returned by the associated take.
848 dl 1.6 */
849     public void testTransfer5() throws InterruptedException {
850 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
851 dl 1.6
852 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
853 jsr166 1.21 public void realRun() throws InterruptedException {
854 jsr166 1.31 q.transfer(four);
855 jsr166 1.12 checkEmpty(q);
856 jsr166 1.9 }});
857 dl 1.6
858 jsr166 1.31 while (q.isEmpty())
859     Thread.yield();
860     assertFalse(q.isEmpty());
861 dl 1.86 mustEqual(1, q.size());
862 jsr166 1.31 assertSame(four, q.take());
863 jsr166 1.12 checkEmpty(q);
864 jsr166 1.66 awaitTermination(t);
865 dl 1.6 }
866    
867     /**
868 jsr166 1.5 * tryTransfer(null) throws NullPointerException
869 dl 1.1 */
870     public void testTryTransfer1() {
871 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
872 dl 1.1 try {
873     q.tryTransfer(null);
874 jsr166 1.8 shouldThrow();
875 jsr166 1.15 } catch (NullPointerException success) {}
876 dl 1.1 }
877 jsr166 1.3
878     /**
879 jsr166 1.5 * tryTransfer returns false and does not enqueue if there are no
880     * consumers waiting to poll or take.
881 dl 1.1 */
882 jsr166 1.12 public void testTryTransfer2() throws InterruptedException {
883 dl 1.86 final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
884 jsr166 1.5 assertFalse(q.tryTransfer(new Object()));
885     assertFalse(q.hasWaitingConsumer());
886 jsr166 1.12 checkEmpty(q);
887 dl 1.1 }
888 jsr166 1.3
889     /**
890 jsr166 1.5 * If there is a consumer waiting in timed poll, tryTransfer
891     * returns true while successfully transfering object.
892 dl 1.1 */
893 jsr166 1.5 public void testTryTransfer3() throws InterruptedException {
894     final Object hotPotato = new Object();
895 dl 1.86 final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
896 jsr166 1.9
897     Thread t = newStartedThread(new CheckedRunnable() {
898 jsr166 1.21 public void realRun() {
899 jsr166 1.7 while (! q.hasWaitingConsumer())
900     Thread.yield();
901 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
902 jsr166 1.31 checkEmpty(q);
903 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
904 jsr166 1.9 }});
905    
906 jsr166 1.66 long startTime = System.nanoTime();
907     assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
908     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
909 jsr166 1.12 checkEmpty(q);
910 jsr166 1.66 awaitTermination(t);
911 jsr166 1.5 }
912 dl 1.1
913 jsr166 1.5 /**
914     * If there is a consumer waiting in take, tryTransfer returns
915     * true while successfully transfering object.
916     */
917     public void testTryTransfer4() throws InterruptedException {
918     final Object hotPotato = new Object();
919 dl 1.86 final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
920 jsr166 1.9
921     Thread t = newStartedThread(new CheckedRunnable() {
922 jsr166 1.21 public void realRun() {
923 jsr166 1.7 while (! q.hasWaitingConsumer())
924     Thread.yield();
925 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
926 jsr166 1.31 checkEmpty(q);
927 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
928 jsr166 1.9 }});
929    
930 jsr166 1.23 assertSame(q.take(), hotPotato);
931 jsr166 1.12 checkEmpty(q);
932 jsr166 1.66 awaitTermination(t);
933 dl 1.1 }
934    
935 jsr166 1.3 /**
936 jsr166 1.46 * tryTransfer blocks interruptibly if no takers
937 dl 1.1 */
938 jsr166 1.5 public void testTryTransfer5() throws InterruptedException {
939 dl 1.86 final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
940 jsr166 1.46 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
941 jsr166 1.40 assertTrue(q.isEmpty());
942 jsr166 1.9
943 jsr166 1.31 Thread t = newStartedThread(new CheckedRunnable() {
944 jsr166 1.21 public void realRun() throws InterruptedException {
945 jsr166 1.46 Thread.currentThread().interrupt();
946     try {
947 jsr166 1.82 q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit());
948 jsr166 1.46 shouldThrow();
949     } catch (InterruptedException success) {}
950     assertFalse(Thread.interrupted());
951    
952     pleaseInterrupt.countDown();
953 jsr166 1.31 try {
954 jsr166 1.85 q.tryTransfer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
955 jsr166 1.31 shouldThrow();
956     } catch (InterruptedException success) {}
957 jsr166 1.46 assertFalse(Thread.interrupted());
958 jsr166 1.5 }});
959 jsr166 1.9
960 jsr166 1.46 await(pleaseInterrupt);
961 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
962 jsr166 1.31 t.interrupt();
963 jsr166 1.46 awaitTermination(t);
964 jsr166 1.31 checkEmpty(q);
965 dl 1.1 }
966    
967 jsr166 1.3 /**
968 jsr166 1.33 * tryTransfer gives up after the timeout and returns false
969 dl 1.1 */
970 jsr166 1.5 public void testTryTransfer6() throws InterruptedException {
971 dl 1.86 final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
972 jsr166 1.9
973     Thread t = newStartedThread(new CheckedRunnable() {
974 jsr166 1.21 public void realRun() throws InterruptedException {
975 jsr166 1.65 long startTime = System.nanoTime();
976 jsr166 1.23 assertFalse(q.tryTransfer(new Object(),
977 jsr166 1.46 timeoutMillis(), MILLISECONDS));
978 jsr166 1.65 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
979 jsr166 1.32 checkEmpty(q);
980 jsr166 1.9 }});
981    
982 jsr166 1.46 awaitTermination(t);
983 jsr166 1.27 checkEmpty(q);
984 dl 1.1 }
985    
986 jsr166 1.3 /**
987 dl 1.1 * tryTransfer waits for any elements previously in to be removed
988     * before transfering to a poll or take
989     */
990 jsr166 1.5 public void testTryTransfer7() throws InterruptedException {
991 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
992 jsr166 1.5 assertTrue(q.offer(four));
993 jsr166 1.9
994     Thread t = newStartedThread(new CheckedRunnable() {
995 jsr166 1.21 public void realRun() throws InterruptedException {
996 jsr166 1.66 long startTime = System.nanoTime();
997     assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
998     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
999 jsr166 1.31 checkEmpty(q);
1000 jsr166 1.9 }});
1001    
1002 jsr166 1.32 while (q.size() != 2)
1003     Thread.yield();
1004 dl 1.86 mustEqual(2, q.size());
1005 jsr166 1.24 assertSame(four, q.poll());
1006     assertSame(five, q.poll());
1007 jsr166 1.12 checkEmpty(q);
1008 jsr166 1.66 awaitTermination(t);
1009 dl 1.1 }
1010    
1011 jsr166 1.3 /**
1012 jsr166 1.41 * tryTransfer attempts to enqueue into the queue and fails
1013     * returning false not enqueueing and the successive poll is null
1014 dl 1.1 */
1015 jsr166 1.5 public void testTryTransfer8() throws InterruptedException {
1016 dl 1.86 final LinkedTransferQueue<Item> q = new LinkedTransferQueue<Item>();
1017 jsr166 1.5 assertTrue(q.offer(four));
1018 dl 1.86 mustEqual(1, q.size());
1019 jsr166 1.65 long startTime = System.nanoTime();
1020 jsr166 1.46 assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1021 jsr166 1.65 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1022 dl 1.86 mustEqual(1, q.size());
1023 jsr166 1.24 assertSame(four, q.poll());
1024 jsr166 1.13 assertNull(q.poll());
1025 jsr166 1.12 checkEmpty(q);
1026 dl 1.1 }
1027    
1028 dl 1.86 private LinkedTransferQueue<Item> populatedQueue(int n) {
1029     LinkedTransferQueue<Item> q = new LinkedTransferQueue<>();
1030 jsr166 1.31 checkEmpty(q);
1031 jsr166 1.5 for (int i = 0; i < n; i++) {
1032 dl 1.86 mustEqual(i, q.size());
1033     mustOffer(q, i);
1034     mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
1035 dl 1.1 }
1036     assertFalse(q.isEmpty());
1037     return q;
1038     }
1039 jsr166 1.53
1040     /**
1041     * remove(null), contains(null) always return false
1042     */
1043     public void testNeverContainsNull() {
1044     Collection<?>[] qs = {
1045     new LinkedTransferQueue<Object>(),
1046     populatedQueue(2),
1047     };
1048    
1049     for (Collection<?> q : qs) {
1050     assertFalse(q.contains(null));
1051     assertFalse(q.remove(null));
1052     }
1053     }
1054 dl 1.1 }