ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.39
Committed: Fri Nov 5 00:17:22 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +2 -1 lines
Log Message:
very small improvements to testToArray2

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