ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.34
Committed: Fri Oct 29 07:01:51 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +2 -3 lines
Log Message:
very small improvement

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.31 assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
358 dl 1.1 }
359 jsr166 1.31 aboutToWait.countDown();
360 jsr166 1.16 try {
361 jsr166 1.31 q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
362 jsr166 1.17 shouldThrow();
363 jsr166 1.16 } catch (InterruptedException success) {}
364 jsr166 1.5 }});
365 jsr166 1.17
366 jsr166 1.31 aboutToWait.await();
367     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
368 jsr166 1.5 t.interrupt();
369 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
370 jsr166 1.12 checkEmpty(q);
371 dl 1.1 }
372    
373     /**
374 jsr166 1.31 * timed poll after thread interrupted throws InterruptedException
375     * instead of returning timeout status
376     */
377     public void testTimedPollAfterInterrupt() throws InterruptedException {
378     final BlockingQueue<Integer> q = populatedQueue(SIZE);
379     Thread t = newStartedThread(new CheckedRunnable() {
380     public void realRun() throws InterruptedException {
381     Thread.currentThread().interrupt();
382     for (int i = 0; i < SIZE; ++i) {
383     long t0 = System.nanoTime();
384     assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
385     assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
386     }
387     try {
388     q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
389     shouldThrow();
390     } catch (InterruptedException success) {}
391     }});
392    
393     awaitTermination(t, MEDIUM_DELAY_MS);
394     checkEmpty(q);
395     }
396    
397     /**
398 dl 1.1 * peek returns next element, or null if empty
399     */
400 jsr166 1.12 public void testPeek() throws InterruptedException {
401 jsr166 1.5 LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
402 dl 1.1 for (int i = 0; i < SIZE; ++i) {
403 jsr166 1.5 assertEquals(i, (int) q.peek());
404     assertEquals(i, (int) q.poll());
405 dl 1.1 assertTrue(q.peek() == null ||
406 jsr166 1.5 i != (int) q.peek());
407 dl 1.1 }
408     assertNull(q.peek());
409 jsr166 1.12 checkEmpty(q);
410 dl 1.1 }
411    
412     /**
413 jsr166 1.5 * element returns next element, or throws NoSuchElementException if empty
414 dl 1.1 */
415 jsr166 1.12 public void testElement() throws InterruptedException {
416 jsr166 1.5 LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
417 dl 1.1 for (int i = 0; i < SIZE; ++i) {
418 jsr166 1.5 assertEquals(i, (int) q.element());
419     assertEquals(i, (int) q.poll());
420 dl 1.1 }
421     try {
422     q.element();
423     shouldThrow();
424 jsr166 1.17 } catch (NoSuchElementException success) {}
425 jsr166 1.12 checkEmpty(q);
426 dl 1.1 }
427    
428     /**
429 jsr166 1.5 * remove removes next element, or throws NoSuchElementException if empty
430 dl 1.1 */
431 jsr166 1.12 public void testRemove() throws InterruptedException {
432 jsr166 1.5 LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
433 dl 1.1 for (int i = 0; i < SIZE; ++i) {
434 jsr166 1.5 assertEquals(i, (int) q.remove());
435 dl 1.1 }
436     try {
437     q.remove();
438     shouldThrow();
439 jsr166 1.17 } catch (NoSuchElementException success) {}
440 jsr166 1.12 checkEmpty(q);
441 dl 1.1 }
442    
443     /**
444     * remove(x) removes x and returns true if present
445     */
446 jsr166 1.12 public void testRemoveElement() throws InterruptedException {
447 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
448     for (int i = 1; i < SIZE; i += 2) {
449 jsr166 1.5 assertTrue(q.remove(i));
450 dl 1.1 }
451     for (int i = 0; i < SIZE; i += 2) {
452 jsr166 1.5 assertTrue(q.remove(i));
453     assertFalse(q.remove(i + 1));
454 dl 1.1 }
455 jsr166 1.12 checkEmpty(q);
456 dl 1.1 }
457    
458     /**
459     * An add following remove(x) succeeds
460     */
461 jsr166 1.5 public void testRemoveElementAndAdd() throws InterruptedException {
462     LinkedTransferQueue q = new LinkedTransferQueue();
463     assertTrue(q.add(one));
464     assertTrue(q.add(two));
465     assertTrue(q.remove(one));
466     assertTrue(q.remove(two));
467     assertTrue(q.add(three));
468 jsr166 1.23 assertSame(q.take(), three);
469 dl 1.1 }
470    
471     /**
472     * contains(x) reports true when elements added but not yet removed
473     */
474     public void testContains() {
475 jsr166 1.5 LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
476 dl 1.1 for (int i = 0; i < SIZE; ++i) {
477 jsr166 1.5 assertTrue(q.contains(i));
478     assertEquals(i, (int) q.poll());
479     assertFalse(q.contains(i));
480 dl 1.1 }
481     }
482    
483     /**
484     * clear removes all elements
485     */
486 jsr166 1.12 public void testClear() throws InterruptedException {
487 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
488     q.clear();
489 jsr166 1.12 checkEmpty(q);
490 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
491 dl 1.1 q.add(one);
492     assertFalse(q.isEmpty());
493 jsr166 1.12 assertEquals(1, q.size());
494 dl 1.1 assertTrue(q.contains(one));
495     q.clear();
496 jsr166 1.12 checkEmpty(q);
497 dl 1.1 }
498    
499     /**
500     * containsAll(c) is true when c contains a subset of elements
501     */
502     public void testContainsAll() {
503 jsr166 1.5 LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
504     LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
505 dl 1.1 for (int i = 0; i < SIZE; ++i) {
506     assertTrue(q.containsAll(p));
507     assertFalse(p.containsAll(q));
508 jsr166 1.5 p.add(i);
509 dl 1.1 }
510     assertTrue(p.containsAll(q));
511     }
512    
513     /**
514 jsr166 1.7 * retainAll(c) retains only those elements of c and reports true
515     * if changed
516 dl 1.1 */
517     public void testRetainAll() {
518     LinkedTransferQueue q = populatedQueue(SIZE);
519     LinkedTransferQueue p = populatedQueue(SIZE);
520     for (int i = 0; i < SIZE; ++i) {
521     boolean changed = q.retainAll(p);
522     if (i == 0) {
523     assertFalse(changed);
524     } else {
525     assertTrue(changed);
526     }
527     assertTrue(q.containsAll(p));
528     assertEquals(SIZE - i, q.size());
529     p.remove();
530     }
531     }
532    
533     /**
534 jsr166 1.7 * removeAll(c) removes only those elements of c and reports true
535     * if changed
536 dl 1.1 */
537     public void testRemoveAll() {
538     for (int i = 1; i < SIZE; ++i) {
539     LinkedTransferQueue q = populatedQueue(SIZE);
540     LinkedTransferQueue p = populatedQueue(i);
541     assertTrue(q.removeAll(p));
542     assertEquals(SIZE - i, q.size());
543     for (int j = 0; j < i; ++j) {
544 jsr166 1.5 assertFalse(q.contains(p.remove()));
545 dl 1.1 }
546     }
547     }
548    
549     /**
550 jsr166 1.13 * toArray() contains all elements
551 dl 1.1 */
552 jsr166 1.5 public void testToArray() throws InterruptedException {
553 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
554     Object[] o = q.toArray();
555 jsr166 1.5 for (int i = 0; i < o.length; i++) {
556     assertEquals(o[i], q.take());
557 dl 1.1 }
558     }
559    
560     /**
561     * toArray(a) contains all elements
562     */
563 jsr166 1.5 public void testToArray2() throws InterruptedException {
564     LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
565 dl 1.1 Integer[] ints = new Integer[SIZE];
566 jsr166 1.5 ints = q.toArray(ints);
567     for (int i = 0; i < ints.length; i++) {
568     assertEquals(ints[i], q.take());
569 dl 1.1 }
570     }
571    
572     /**
573 jsr166 1.5 * toArray(null) throws NullPointerException
574 dl 1.1 */
575     public void testToArray_BadArg() {
576 jsr166 1.20 LinkedTransferQueue q = populatedQueue(SIZE);
577 dl 1.1 try {
578     Object o[] = q.toArray(null);
579     shouldThrow();
580 jsr166 1.17 } catch (NullPointerException success) {}
581 dl 1.1 }
582    
583     /**
584 jsr166 1.13 * toArray(incompatible array type) throws CCE
585 dl 1.1 */
586     public void testToArray1_BadArg() {
587 jsr166 1.20 LinkedTransferQueue q = populatedQueue(SIZE);
588 dl 1.1 try {
589     Object o[] = q.toArray(new String[10]);
590     shouldThrow();
591 jsr166 1.17 } catch (ArrayStoreException success) {}
592 dl 1.1 }
593    
594     /**
595     * iterator iterates through all elements
596     */
597 jsr166 1.5 public void testIterator() throws InterruptedException {
598 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
599     Iterator it = q.iterator();
600 jsr166 1.10 int i = 0;
601 jsr166 1.5 while (it.hasNext()) {
602 jsr166 1.10 assertEquals(it.next(), i++);
603 dl 1.1 }
604 jsr166 1.10 assertEquals(i, SIZE);
605 dl 1.1 }
606    
607     /**
608 jsr166 1.12 * iterator.remove() removes current element
609 dl 1.1 */
610     public void testIteratorRemove() {
611     final LinkedTransferQueue q = new LinkedTransferQueue();
612     q.add(two);
613     q.add(one);
614     q.add(three);
615    
616     Iterator it = q.iterator();
617     it.next();
618     it.remove();
619    
620     it = q.iterator();
621 jsr166 1.24 assertSame(it.next(), one);
622     assertSame(it.next(), three);
623 dl 1.1 assertFalse(it.hasNext());
624     }
625    
626     /**
627     * iterator ordering is FIFO
628     */
629     public void testIteratorOrdering() {
630 jsr166 1.7 final LinkedTransferQueue<Integer> q
631     = new LinkedTransferQueue<Integer>();
632 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
633 dl 1.1 q.add(one);
634     q.add(two);
635     q.add(three);
636 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
637 dl 1.1 int k = 0;
638 jsr166 1.5 for (Integer n : q) {
639     assertEquals(++k, (int) n);
640 dl 1.1 }
641     assertEquals(3, k);
642     }
643    
644     /**
645     * Modifications do not cause iterators to fail
646     */
647     public void testWeaklyConsistentIteration() {
648     final LinkedTransferQueue q = new LinkedTransferQueue();
649     q.add(one);
650     q.add(two);
651     q.add(three);
652 jsr166 1.5 for (Iterator it = q.iterator(); it.hasNext();) {
653     q.remove();
654     it.next();
655 dl 1.1 }
656     assertEquals(0, q.size());
657     }
658    
659     /**
660     * toString contains toStrings of elements
661     */
662     public void testToString() {
663     LinkedTransferQueue q = populatedQueue(SIZE);
664     String s = q.toString();
665     for (int i = 0; i < SIZE; ++i) {
666     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
667     }
668     }
669    
670     /**
671     * offer transfers elements across Executor tasks
672     */
673     public void testOfferInExecutor() {
674     final LinkedTransferQueue q = new LinkedTransferQueue();
675 jsr166 1.31 final CountDownLatch threadsStarted = new CountDownLatch(2);
676 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
677 jsr166 1.7
678     executor.execute(new CheckedRunnable() {
679 jsr166 1.31 public void realRun() throws InterruptedException {
680     threadsStarted.countDown();
681     threadsStarted.await();
682     assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
683 jsr166 1.5 }});
684 dl 1.1
685 jsr166 1.7 executor.execute(new CheckedRunnable() {
686 jsr166 1.21 public void realRun() throws InterruptedException {
687 jsr166 1.31 threadsStarted.countDown();
688     threadsStarted.await();
689 jsr166 1.24 assertSame(one, q.take());
690 jsr166 1.31 checkEmpty(q);
691 jsr166 1.5 }});
692 dl 1.1
693     joinPool(executor);
694     }
695    
696     /**
697 jsr166 1.13 * timed poll retrieves elements across Executor threads
698 dl 1.1 */
699     public void testPollInExecutor() {
700     final LinkedTransferQueue q = new LinkedTransferQueue();
701 jsr166 1.31 final CountDownLatch threadsStarted = new CountDownLatch(2);
702 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
703 jsr166 1.7
704     executor.execute(new CheckedRunnable() {
705 jsr166 1.21 public void realRun() throws InterruptedException {
706 jsr166 1.22 assertNull(q.poll());
707 jsr166 1.31 threadsStarted.countDown();
708     threadsStarted.await();
709     assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
710     checkEmpty(q);
711 jsr166 1.5 }});
712 dl 1.1
713 jsr166 1.7 executor.execute(new CheckedRunnable() {
714 jsr166 1.21 public void realRun() throws InterruptedException {
715 jsr166 1.31 threadsStarted.countDown();
716     threadsStarted.await();
717 jsr166 1.7 q.put(one);
718 jsr166 1.5 }});
719 dl 1.1
720     joinPool(executor);
721     }
722    
723     /**
724     * A deserialized serialized queue has same elements in same order
725     */
726 jsr166 1.5 public void testSerialization() throws Exception {
727 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
728    
729 jsr166 1.5 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
730 jsr166 1.7 ObjectOutputStream out
731     = new ObjectOutputStream(new BufferedOutputStream(bout));
732 jsr166 1.5 out.writeObject(q);
733     out.close();
734    
735 jsr166 1.7 ByteArrayInputStream bin
736     = new ByteArrayInputStream(bout.toByteArray());
737     ObjectInputStream in
738     = new ObjectInputStream(new BufferedInputStream(bin));
739 jsr166 1.5 LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
740    
741     assertEquals(q.size(), r.size());
742 jsr166 1.32 assertEquals(q.toString(), r.toString());
743     assertTrue(Arrays.equals(q.toArray(), r.toArray()));
744 jsr166 1.5 while (!q.isEmpty()) {
745     assertEquals(q.remove(), r.remove());
746 dl 1.1 }
747     }
748    
749     /**
750 jsr166 1.5 * drainTo(null) throws NullPointerException
751 dl 1.1 */
752     public void testDrainToNull() {
753     LinkedTransferQueue q = populatedQueue(SIZE);
754     try {
755     q.drainTo(null);
756     shouldThrow();
757 jsr166 1.17 } catch (NullPointerException success) {}
758 dl 1.1 }
759    
760     /**
761 jsr166 1.5 * drainTo(this) throws IllegalArgumentException
762 dl 1.1 */
763     public void testDrainToSelf() {
764     LinkedTransferQueue q = populatedQueue(SIZE);
765     try {
766     q.drainTo(q);
767     shouldThrow();
768 jsr166 1.17 } catch (IllegalArgumentException success) {}
769 dl 1.1 }
770    
771     /**
772     * drainTo(c) empties queue into another collection c
773     */
774     public void testDrainTo() {
775     LinkedTransferQueue q = populatedQueue(SIZE);
776     ArrayList l = new ArrayList();
777     q.drainTo(l);
778     assertEquals(q.size(), 0);
779     assertEquals(l.size(), SIZE);
780     for (int i = 0; i < SIZE; ++i) {
781 jsr166 1.5 assertEquals(l.get(i), i);
782 dl 1.1 }
783     q.add(zero);
784     q.add(one);
785     assertFalse(q.isEmpty());
786     assertTrue(q.contains(zero));
787     assertTrue(q.contains(one));
788     l.clear();
789     q.drainTo(l);
790     assertEquals(q.size(), 0);
791     assertEquals(l.size(), 2);
792     for (int i = 0; i < 2; ++i) {
793 jsr166 1.5 assertEquals(l.get(i), i);
794 dl 1.1 }
795     }
796    
797     /**
798 jsr166 1.13 * drainTo(c) empties full queue, unblocking a waiting put.
799 dl 1.1 */
800 jsr166 1.5 public void testDrainToWithActivePut() throws InterruptedException {
801 dl 1.1 final LinkedTransferQueue q = populatedQueue(SIZE);
802 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
803 jsr166 1.21 public void realRun() {
804 jsr166 1.7 q.put(SIZE + 1);
805 jsr166 1.5 }});
806     ArrayList l = new ArrayList();
807     q.drainTo(l);
808     assertTrue(l.size() >= SIZE);
809     for (int i = 0; i < SIZE; ++i) {
810     assertEquals(l.get(i), i);
811 dl 1.1 }
812 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
813 jsr166 1.5 assertTrue(q.size() + l.size() >= SIZE);
814 dl 1.1 }
815    
816     /**
817 jsr166 1.5 * drainTo(null, n) throws NullPointerException
818 dl 1.1 */
819     public void testDrainToNullN() {
820     LinkedTransferQueue q = populatedQueue(SIZE);
821     try {
822 jsr166 1.12 q.drainTo(null, SIZE);
823 dl 1.1 shouldThrow();
824 jsr166 1.17 } catch (NullPointerException success) {}
825 dl 1.1 }
826    
827     /**
828 jsr166 1.5 * drainTo(this, n) throws IllegalArgumentException
829 dl 1.1 */
830     public void testDrainToSelfN() {
831     LinkedTransferQueue q = populatedQueue(SIZE);
832     try {
833 jsr166 1.12 q.drainTo(q, SIZE);
834 dl 1.1 shouldThrow();
835 jsr166 1.17 } catch (IllegalArgumentException success) {}
836 dl 1.1 }
837    
838     /**
839 jsr166 1.26 * drainTo(c, n) empties first min(n, size) elements of queue into c
840 dl 1.1 */
841     public void testDrainToN() {
842     LinkedTransferQueue q = new LinkedTransferQueue();
843     for (int i = 0; i < SIZE + 2; ++i) {
844     for (int j = 0; j < SIZE; j++) {
845 jsr166 1.5 assertTrue(q.offer(j));
846 dl 1.1 }
847     ArrayList l = new ArrayList();
848     q.drainTo(l, i);
849     int k = (i < SIZE) ? i : SIZE;
850     assertEquals(l.size(), k);
851     assertEquals(q.size(), SIZE - k);
852     for (int j = 0; j < k; ++j) {
853 jsr166 1.5 assertEquals(l.get(j), j);
854 dl 1.1 }
855 jsr166 1.5 while (q.poll() != null)
856     ;
857 dl 1.1 }
858     }
859    
860 jsr166 1.3 /**
861 jsr166 1.13 * timed poll() or take() increments the waiting consumer count;
862     * offer(e) decrements the waiting consumer count
863 dl 1.1 */
864 jsr166 1.5 public void testWaitingConsumer() throws InterruptedException {
865     final LinkedTransferQueue q = new LinkedTransferQueue();
866 jsr166 1.13 assertEquals(q.getWaitingConsumerCount(), 0);
867     assertFalse(q.hasWaitingConsumer());
868 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
869 jsr166 1.9
870     Thread t = newStartedThread(new CheckedRunnable() {
871 jsr166 1.21 public void realRun() throws InterruptedException {
872 jsr166 1.31 threadStarted.countDown();
873     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
874     assertEquals(q.getWaitingConsumerCount(), 0);
875 jsr166 1.22 assertFalse(q.hasWaitingConsumer());
876 jsr166 1.9 }});
877    
878 jsr166 1.31 threadStarted.await();
879     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
880     assertEquals(q.getWaitingConsumerCount(), 1);
881     assertTrue(q.hasWaitingConsumer());
882    
883     assertTrue(q.offer(one));
884 jsr166 1.13 assertEquals(q.getWaitingConsumerCount(), 0);
885     assertFalse(q.hasWaitingConsumer());
886 jsr166 1.31
887 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
888 dl 1.1 }
889 jsr166 1.3
890     /**
891 jsr166 1.5 * transfer(null) throws NullPointerException
892 dl 1.1 */
893 jsr166 1.5 public void testTransfer1() throws InterruptedException {
894 dl 1.1 try {
895     LinkedTransferQueue q = new LinkedTransferQueue();
896     q.transfer(null);
897     shouldThrow();
898 jsr166 1.15 } catch (NullPointerException success) {}
899 dl 1.1 }
900    
901 jsr166 1.3 /**
902 dl 1.6 * transfer waits until a poll occurs. The transfered element
903     * is returned by this associated poll.
904 dl 1.1 */
905 jsr166 1.5 public void testTransfer2() throws InterruptedException {
906 jsr166 1.7 final LinkedTransferQueue<Integer> q
907     = new LinkedTransferQueue<Integer>();
908 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
909 jsr166 1.5
910 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
911 jsr166 1.21 public void realRun() throws InterruptedException {
912 jsr166 1.31 threadStarted.countDown();
913 jsr166 1.34 q.transfer(five);
914 jsr166 1.31 checkEmpty(q);
915 jsr166 1.9 }});
916 dl 1.1
917 jsr166 1.31 threadStarted.await();
918     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
919 jsr166 1.5 assertEquals(1, q.size());
920 jsr166 1.34 assertSame(five, q.poll());
921 jsr166 1.31 checkEmpty(q);
922 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
923 dl 1.1 }
924 jsr166 1.3
925     /**
926 dl 1.6 * transfer waits until a poll occurs, and then transfers in fifo order
927 dl 1.1 */
928 jsr166 1.5 public void testTransfer3() throws InterruptedException {
929 jsr166 1.7 final LinkedTransferQueue<Integer> q
930     = new LinkedTransferQueue<Integer>();
931 jsr166 1.5
932 jsr166 1.9 Thread first = newStartedThread(new CheckedRunnable() {
933 jsr166 1.21 public void realRun() throws InterruptedException {
934 jsr166 1.32 q.transfer(four);
935     assertTrue(!q.contains(four));
936 jsr166 1.24 assertEquals(1, q.size());
937 jsr166 1.7 }});
938 jsr166 1.5
939 jsr166 1.9 Thread interruptedThread = newStartedThread(
940     new CheckedInterruptedRunnable() {
941 jsr166 1.21 public void realRun() throws InterruptedException {
942 jsr166 1.32 while (q.isEmpty())
943 jsr166 1.9 Thread.yield();
944 jsr166 1.32 q.transfer(five);
945 jsr166 1.9 }});
946 jsr166 1.5
947     while (q.size() < 2)
948     Thread.yield();
949     assertEquals(2, q.size());
950 jsr166 1.32 assertSame(four, q.poll());
951 jsr166 1.5 first.join();
952     assertEquals(1, q.size());
953     interruptedThread.interrupt();
954     interruptedThread.join();
955 jsr166 1.31 checkEmpty(q);
956 dl 1.1 }
957    
958     /**
959 dl 1.6 * transfer waits until a poll occurs, at which point the polling
960     * thread returns the element
961 dl 1.1 */
962 jsr166 1.5 public void testTransfer4() throws InterruptedException {
963 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
964 jsr166 1.9
965     Thread t = newStartedThread(new CheckedRunnable() {
966 jsr166 1.21 public void realRun() throws InterruptedException {
967 jsr166 1.7 q.transfer(four);
968 jsr166 1.24 assertFalse(q.contains(four));
969     assertSame(three, q.poll());
970 jsr166 1.9 }});
971    
972 jsr166 1.31 while (q.isEmpty())
973     Thread.yield();
974     assertFalse(q.isEmpty());
975     assertEquals(1, q.size());
976 jsr166 1.5 assertTrue(q.offer(three));
977 jsr166 1.24 assertSame(four, q.poll());
978 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
979 dl 1.1 }
980 jsr166 1.3
981     /**
982 dl 1.6 * transfer waits until a take occurs. The transfered element
983     * is returned by this associated take.
984     */
985     public void testTransfer5() throws InterruptedException {
986 jsr166 1.7 final LinkedTransferQueue<Integer> q
987     = new LinkedTransferQueue<Integer>();
988 dl 1.6
989 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
990 jsr166 1.21 public void realRun() throws InterruptedException {
991 jsr166 1.31 q.transfer(four);
992 jsr166 1.12 checkEmpty(q);
993 jsr166 1.9 }});
994 dl 1.6
995 jsr166 1.31 while (q.isEmpty())
996     Thread.yield();
997     assertFalse(q.isEmpty());
998     assertEquals(1, q.size());
999     assertSame(four, q.take());
1000 jsr166 1.12 checkEmpty(q);
1001 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1002 dl 1.6 }
1003    
1004     /**
1005 jsr166 1.5 * tryTransfer(null) throws NullPointerException
1006 dl 1.1 */
1007     public void testTryTransfer1() {
1008     try {
1009     final LinkedTransferQueue q = new LinkedTransferQueue();
1010     q.tryTransfer(null);
1011 jsr166 1.8 shouldThrow();
1012 jsr166 1.15 } catch (NullPointerException success) {}
1013 dl 1.1 }
1014 jsr166 1.3
1015     /**
1016 jsr166 1.5 * tryTransfer returns false and does not enqueue if there are no
1017     * consumers waiting to poll or take.
1018 dl 1.1 */
1019 jsr166 1.12 public void testTryTransfer2() throws InterruptedException {
1020 jsr166 1.5 final LinkedTransferQueue q = new LinkedTransferQueue();
1021     assertFalse(q.tryTransfer(new Object()));
1022     assertFalse(q.hasWaitingConsumer());
1023 jsr166 1.12 checkEmpty(q);
1024 dl 1.1 }
1025 jsr166 1.3
1026     /**
1027 jsr166 1.5 * If there is a consumer waiting in timed poll, tryTransfer
1028     * returns true while successfully transfering object.
1029 dl 1.1 */
1030 jsr166 1.5 public void testTryTransfer3() throws InterruptedException {
1031     final Object hotPotato = new Object();
1032     final LinkedTransferQueue q = new LinkedTransferQueue();
1033 jsr166 1.9
1034     Thread t = newStartedThread(new CheckedRunnable() {
1035 jsr166 1.21 public void realRun() {
1036 jsr166 1.7 while (! q.hasWaitingConsumer())
1037     Thread.yield();
1038 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
1039 jsr166 1.31 checkEmpty(q);
1040 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
1041 jsr166 1.9 }});
1042    
1043 jsr166 1.23 assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1044 jsr166 1.12 checkEmpty(q);
1045 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1046 jsr166 1.5 }
1047 dl 1.1
1048 jsr166 1.5 /**
1049     * If there is a consumer waiting in take, tryTransfer returns
1050     * true while successfully transfering object.
1051     */
1052     public void testTryTransfer4() throws InterruptedException {
1053     final Object hotPotato = new Object();
1054     final LinkedTransferQueue q = new LinkedTransferQueue();
1055 jsr166 1.9
1056     Thread t = newStartedThread(new CheckedRunnable() {
1057 jsr166 1.21 public void realRun() {
1058 jsr166 1.7 while (! q.hasWaitingConsumer())
1059     Thread.yield();
1060 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
1061 jsr166 1.31 checkEmpty(q);
1062 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
1063 jsr166 1.9 }});
1064    
1065 jsr166 1.23 assertSame(q.take(), hotPotato);
1066 jsr166 1.12 checkEmpty(q);
1067 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1068 dl 1.1 }
1069    
1070 jsr166 1.3 /**
1071 dl 1.6 * tryTransfer waits the amount given if interrupted, and
1072     * throws interrupted exception
1073 dl 1.1 */
1074 jsr166 1.5 public void testTryTransfer5() throws InterruptedException {
1075 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1076 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
1077 jsr166 1.9
1078 jsr166 1.31 Thread t = newStartedThread(new CheckedRunnable() {
1079 jsr166 1.21 public void realRun() throws InterruptedException {
1080 jsr166 1.31 long t0 = System.nanoTime();
1081     threadStarted.countDown();
1082     try {
1083     q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1084     shouldThrow();
1085     } catch (InterruptedException success) {}
1086     assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1087 jsr166 1.5 }});
1088 jsr166 1.9
1089 jsr166 1.31 threadStarted.await();
1090     Thread.sleep(SHORT_DELAY_MS);
1091     t.interrupt();
1092     awaitTermination(t, MEDIUM_DELAY_MS);
1093     checkEmpty(q);
1094 dl 1.1 }
1095    
1096 jsr166 1.3 /**
1097 jsr166 1.33 * tryTransfer gives up after the timeout and returns false
1098 dl 1.1 */
1099 jsr166 1.5 public void testTryTransfer6() throws InterruptedException {
1100 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1101 jsr166 1.9
1102     Thread t = newStartedThread(new CheckedRunnable() {
1103 jsr166 1.21 public void realRun() throws InterruptedException {
1104 jsr166 1.27 long t0 = System.nanoTime();
1105 jsr166 1.23 assertFalse(q.tryTransfer(new Object(),
1106     SHORT_DELAY_MS, MILLISECONDS));
1107 jsr166 1.28 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1108 jsr166 1.32 checkEmpty(q);
1109 jsr166 1.9 }});
1110    
1111 jsr166 1.27 awaitTermination(t, MEDIUM_DELAY_MS);
1112     checkEmpty(q);
1113 dl 1.1 }
1114    
1115 jsr166 1.3 /**
1116 dl 1.1 * tryTransfer waits for any elements previously in to be removed
1117     * before transfering to a poll or take
1118     */
1119 jsr166 1.5 public void testTryTransfer7() throws InterruptedException {
1120 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1121 jsr166 1.5 assertTrue(q.offer(four));
1122 jsr166 1.9
1123     Thread t = newStartedThread(new CheckedRunnable() {
1124 jsr166 1.21 public void realRun() throws InterruptedException {
1125 jsr166 1.23 assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1126 jsr166 1.31 checkEmpty(q);
1127 jsr166 1.9 }});
1128    
1129 jsr166 1.32 while (q.size() != 2)
1130     Thread.yield();
1131 jsr166 1.5 assertEquals(2, q.size());
1132 jsr166 1.24 assertSame(four, q.poll());
1133     assertSame(five, q.poll());
1134 jsr166 1.12 checkEmpty(q);
1135 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1136 dl 1.1 }
1137    
1138 jsr166 1.3 /**
1139 jsr166 1.5 * tryTransfer attempts to enqueue into the q and fails returning
1140     * false not enqueueing and the successive poll is null
1141 dl 1.1 */
1142 jsr166 1.5 public void testTryTransfer8() throws InterruptedException {
1143 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1144 jsr166 1.5 assertTrue(q.offer(four));
1145     assertEquals(1, q.size());
1146 jsr166 1.32 long t0 = System.nanoTime();
1147 jsr166 1.7 assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1148 jsr166 1.32 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1149 jsr166 1.5 assertEquals(1, q.size());
1150 jsr166 1.24 assertSame(four, q.poll());
1151 jsr166 1.13 assertNull(q.poll());
1152 jsr166 1.12 checkEmpty(q);
1153 dl 1.1 }
1154    
1155 jsr166 1.5 private LinkedTransferQueue<Integer> populatedQueue(int n) {
1156     LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1157 jsr166 1.31 checkEmpty(q);
1158 jsr166 1.5 for (int i = 0; i < n; i++) {
1159     assertEquals(i, q.size());
1160 dl 1.1 assertTrue(q.offer(i));
1161 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1162 dl 1.1 }
1163     assertFalse(q.isEmpty());
1164     return q;
1165     }
1166     }