ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.43
Committed: Sun Nov 28 08:43:53 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.42: +0 -27 lines
Log Message:
update testInterruptedTimedPoll

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