ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
Revision: 1.37
Committed: Wed Nov 3 16:46:34 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.36: +2 -2 lines
Log Message:
minor improvements to testToArray_BadArg

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.13 * toArray() contains all elements
554 dl 1.1 */
555 jsr166 1.5 public void testToArray() throws InterruptedException {
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     assertEquals(o[i], q.take());
560 dl 1.1 }
561     }
562    
563     /**
564     * toArray(a) contains all elements
565     */
566 jsr166 1.5 public void testToArray2() throws InterruptedException {
567     LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
568 dl 1.1 Integer[] ints = new Integer[SIZE];
569 jsr166 1.5 ints = q.toArray(ints);
570     for (int i = 0; i < ints.length; i++) {
571     assertEquals(ints[i], q.take());
572 dl 1.1 }
573     }
574    
575     /**
576 jsr166 1.5 * toArray(null) throws NullPointerException
577 dl 1.1 */
578 jsr166 1.37 public void testToArray_NullArg() {
579 jsr166 1.20 LinkedTransferQueue q = populatedQueue(SIZE);
580 dl 1.1 try {
581 jsr166 1.37 q.toArray(null);
582 dl 1.1 shouldThrow();
583 jsr166 1.17 } catch (NullPointerException success) {}
584 dl 1.1 }
585    
586     /**
587 jsr166 1.36 * toArray(incompatible array type) throws ArrayStoreException
588 dl 1.1 */
589     public void testToArray1_BadArg() {
590 jsr166 1.20 LinkedTransferQueue q = populatedQueue(SIZE);
591 dl 1.1 try {
592 jsr166 1.36 q.toArray(new String[10]);
593 dl 1.1 shouldThrow();
594 jsr166 1.17 } catch (ArrayStoreException success) {}
595 dl 1.1 }
596    
597     /**
598     * iterator iterates through all elements
599     */
600 jsr166 1.5 public void testIterator() throws InterruptedException {
601 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
602     Iterator it = q.iterator();
603 jsr166 1.10 int i = 0;
604 jsr166 1.5 while (it.hasNext()) {
605 jsr166 1.10 assertEquals(it.next(), i++);
606 dl 1.1 }
607 jsr166 1.10 assertEquals(i, SIZE);
608 dl 1.1 }
609    
610     /**
611 jsr166 1.12 * iterator.remove() removes current element
612 dl 1.1 */
613     public void testIteratorRemove() {
614     final LinkedTransferQueue q = new LinkedTransferQueue();
615     q.add(two);
616     q.add(one);
617     q.add(three);
618    
619     Iterator it = q.iterator();
620     it.next();
621     it.remove();
622    
623     it = q.iterator();
624 jsr166 1.24 assertSame(it.next(), one);
625     assertSame(it.next(), three);
626 dl 1.1 assertFalse(it.hasNext());
627     }
628    
629     /**
630     * iterator ordering is FIFO
631     */
632     public void testIteratorOrdering() {
633 jsr166 1.7 final LinkedTransferQueue<Integer> q
634     = new LinkedTransferQueue<Integer>();
635 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
636 dl 1.1 q.add(one);
637     q.add(two);
638     q.add(three);
639 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
640 dl 1.1 int k = 0;
641 jsr166 1.5 for (Integer n : q) {
642     assertEquals(++k, (int) n);
643 dl 1.1 }
644     assertEquals(3, k);
645     }
646    
647     /**
648     * Modifications do not cause iterators to fail
649     */
650     public void testWeaklyConsistentIteration() {
651     final LinkedTransferQueue q = new LinkedTransferQueue();
652     q.add(one);
653     q.add(two);
654     q.add(three);
655 jsr166 1.5 for (Iterator it = q.iterator(); it.hasNext();) {
656     q.remove();
657     it.next();
658 dl 1.1 }
659     assertEquals(0, q.size());
660     }
661    
662     /**
663     * toString contains toStrings of elements
664     */
665     public void testToString() {
666     LinkedTransferQueue q = populatedQueue(SIZE);
667     String s = q.toString();
668     for (int i = 0; i < SIZE; ++i) {
669     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
670     }
671     }
672    
673     /**
674     * offer transfers elements across Executor tasks
675     */
676     public void testOfferInExecutor() {
677     final LinkedTransferQueue q = new LinkedTransferQueue();
678 jsr166 1.31 final CountDownLatch threadsStarted = new CountDownLatch(2);
679 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
680 jsr166 1.7
681     executor.execute(new CheckedRunnable() {
682 jsr166 1.31 public void realRun() throws InterruptedException {
683     threadsStarted.countDown();
684     threadsStarted.await();
685     assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
686 jsr166 1.5 }});
687 dl 1.1
688 jsr166 1.7 executor.execute(new CheckedRunnable() {
689 jsr166 1.21 public void realRun() throws InterruptedException {
690 jsr166 1.31 threadsStarted.countDown();
691     threadsStarted.await();
692 jsr166 1.24 assertSame(one, q.take());
693 jsr166 1.31 checkEmpty(q);
694 jsr166 1.5 }});
695 dl 1.1
696     joinPool(executor);
697     }
698    
699     /**
700 jsr166 1.13 * timed poll retrieves elements across Executor threads
701 dl 1.1 */
702     public void testPollInExecutor() {
703     final LinkedTransferQueue q = new LinkedTransferQueue();
704 jsr166 1.31 final CountDownLatch threadsStarted = new CountDownLatch(2);
705 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
706 jsr166 1.7
707     executor.execute(new CheckedRunnable() {
708 jsr166 1.21 public void realRun() throws InterruptedException {
709 jsr166 1.22 assertNull(q.poll());
710 jsr166 1.31 threadsStarted.countDown();
711     threadsStarted.await();
712     assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
713     checkEmpty(q);
714 jsr166 1.5 }});
715 dl 1.1
716 jsr166 1.7 executor.execute(new CheckedRunnable() {
717 jsr166 1.21 public void realRun() throws InterruptedException {
718 jsr166 1.31 threadsStarted.countDown();
719     threadsStarted.await();
720 jsr166 1.7 q.put(one);
721 jsr166 1.5 }});
722 dl 1.1
723     joinPool(executor);
724     }
725    
726     /**
727     * A deserialized serialized queue has same elements in same order
728     */
729 jsr166 1.5 public void testSerialization() throws Exception {
730 dl 1.1 LinkedTransferQueue q = populatedQueue(SIZE);
731    
732 jsr166 1.5 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
733 jsr166 1.7 ObjectOutputStream out
734     = new ObjectOutputStream(new BufferedOutputStream(bout));
735 jsr166 1.5 out.writeObject(q);
736     out.close();
737    
738 jsr166 1.7 ByteArrayInputStream bin
739     = new ByteArrayInputStream(bout.toByteArray());
740     ObjectInputStream in
741     = new ObjectInputStream(new BufferedInputStream(bin));
742 jsr166 1.5 LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
743    
744     assertEquals(q.size(), r.size());
745 jsr166 1.32 assertEquals(q.toString(), r.toString());
746     assertTrue(Arrays.equals(q.toArray(), r.toArray()));
747 jsr166 1.5 while (!q.isEmpty()) {
748     assertEquals(q.remove(), r.remove());
749 dl 1.1 }
750     }
751    
752     /**
753 jsr166 1.5 * drainTo(null) throws NullPointerException
754 dl 1.1 */
755     public void testDrainToNull() {
756     LinkedTransferQueue q = populatedQueue(SIZE);
757     try {
758     q.drainTo(null);
759     shouldThrow();
760 jsr166 1.17 } catch (NullPointerException success) {}
761 dl 1.1 }
762    
763     /**
764 jsr166 1.5 * drainTo(this) throws IllegalArgumentException
765 dl 1.1 */
766     public void testDrainToSelf() {
767     LinkedTransferQueue q = populatedQueue(SIZE);
768     try {
769     q.drainTo(q);
770     shouldThrow();
771 jsr166 1.17 } catch (IllegalArgumentException success) {}
772 dl 1.1 }
773    
774     /**
775     * drainTo(c) empties queue into another collection c
776     */
777     public void testDrainTo() {
778     LinkedTransferQueue q = populatedQueue(SIZE);
779     ArrayList l = new ArrayList();
780     q.drainTo(l);
781     assertEquals(q.size(), 0);
782     assertEquals(l.size(), SIZE);
783     for (int i = 0; i < SIZE; ++i) {
784 jsr166 1.5 assertEquals(l.get(i), i);
785 dl 1.1 }
786     q.add(zero);
787     q.add(one);
788     assertFalse(q.isEmpty());
789     assertTrue(q.contains(zero));
790     assertTrue(q.contains(one));
791     l.clear();
792     q.drainTo(l);
793     assertEquals(q.size(), 0);
794     assertEquals(l.size(), 2);
795     for (int i = 0; i < 2; ++i) {
796 jsr166 1.5 assertEquals(l.get(i), i);
797 dl 1.1 }
798     }
799    
800     /**
801 jsr166 1.13 * drainTo(c) empties full queue, unblocking a waiting put.
802 dl 1.1 */
803 jsr166 1.5 public void testDrainToWithActivePut() throws InterruptedException {
804 dl 1.1 final LinkedTransferQueue q = populatedQueue(SIZE);
805 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
806 jsr166 1.21 public void realRun() {
807 jsr166 1.7 q.put(SIZE + 1);
808 jsr166 1.5 }});
809     ArrayList l = new ArrayList();
810     q.drainTo(l);
811     assertTrue(l.size() >= SIZE);
812     for (int i = 0; i < SIZE; ++i) {
813     assertEquals(l.get(i), i);
814 dl 1.1 }
815 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
816 jsr166 1.5 assertTrue(q.size() + l.size() >= SIZE);
817 dl 1.1 }
818    
819     /**
820 jsr166 1.5 * drainTo(null, n) throws NullPointerException
821 dl 1.1 */
822     public void testDrainToNullN() {
823     LinkedTransferQueue q = populatedQueue(SIZE);
824     try {
825 jsr166 1.12 q.drainTo(null, SIZE);
826 dl 1.1 shouldThrow();
827 jsr166 1.17 } catch (NullPointerException success) {}
828 dl 1.1 }
829    
830     /**
831 jsr166 1.5 * drainTo(this, n) throws IllegalArgumentException
832 dl 1.1 */
833     public void testDrainToSelfN() {
834     LinkedTransferQueue q = populatedQueue(SIZE);
835     try {
836 jsr166 1.12 q.drainTo(q, SIZE);
837 dl 1.1 shouldThrow();
838 jsr166 1.17 } catch (IllegalArgumentException success) {}
839 dl 1.1 }
840    
841     /**
842 jsr166 1.26 * drainTo(c, n) empties first min(n, size) elements of queue into c
843 dl 1.1 */
844     public void testDrainToN() {
845     LinkedTransferQueue q = new LinkedTransferQueue();
846     for (int i = 0; i < SIZE + 2; ++i) {
847     for (int j = 0; j < SIZE; j++) {
848 jsr166 1.5 assertTrue(q.offer(j));
849 dl 1.1 }
850     ArrayList l = new ArrayList();
851     q.drainTo(l, i);
852     int k = (i < SIZE) ? i : SIZE;
853     assertEquals(l.size(), k);
854     assertEquals(q.size(), SIZE - k);
855     for (int j = 0; j < k; ++j) {
856 jsr166 1.5 assertEquals(l.get(j), j);
857 dl 1.1 }
858 jsr166 1.5 while (q.poll() != null)
859     ;
860 dl 1.1 }
861     }
862    
863 jsr166 1.3 /**
864 jsr166 1.13 * timed poll() or take() increments the waiting consumer count;
865     * offer(e) decrements the waiting consumer count
866 dl 1.1 */
867 jsr166 1.5 public void testWaitingConsumer() throws InterruptedException {
868     final LinkedTransferQueue q = new LinkedTransferQueue();
869 jsr166 1.13 assertEquals(q.getWaitingConsumerCount(), 0);
870     assertFalse(q.hasWaitingConsumer());
871 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
872 jsr166 1.9
873     Thread t = newStartedThread(new CheckedRunnable() {
874 jsr166 1.21 public void realRun() throws InterruptedException {
875 jsr166 1.31 threadStarted.countDown();
876     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
877     assertEquals(q.getWaitingConsumerCount(), 0);
878 jsr166 1.22 assertFalse(q.hasWaitingConsumer());
879 jsr166 1.9 }});
880    
881 jsr166 1.31 threadStarted.await();
882     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
883     assertEquals(q.getWaitingConsumerCount(), 1);
884     assertTrue(q.hasWaitingConsumer());
885    
886     assertTrue(q.offer(one));
887 jsr166 1.13 assertEquals(q.getWaitingConsumerCount(), 0);
888     assertFalse(q.hasWaitingConsumer());
889 jsr166 1.31
890 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
891 dl 1.1 }
892 jsr166 1.3
893     /**
894 jsr166 1.5 * transfer(null) throws NullPointerException
895 dl 1.1 */
896 jsr166 1.5 public void testTransfer1() throws InterruptedException {
897 dl 1.1 try {
898     LinkedTransferQueue q = new LinkedTransferQueue();
899     q.transfer(null);
900     shouldThrow();
901 jsr166 1.15 } catch (NullPointerException success) {}
902 dl 1.1 }
903    
904 jsr166 1.3 /**
905 dl 1.6 * transfer waits until a poll occurs. The transfered element
906     * is returned by this associated poll.
907 dl 1.1 */
908 jsr166 1.5 public void testTransfer2() throws InterruptedException {
909 jsr166 1.7 final LinkedTransferQueue<Integer> q
910     = new LinkedTransferQueue<Integer>();
911 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
912 jsr166 1.5
913 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
914 jsr166 1.21 public void realRun() throws InterruptedException {
915 jsr166 1.31 threadStarted.countDown();
916 jsr166 1.34 q.transfer(five);
917 jsr166 1.31 checkEmpty(q);
918 jsr166 1.9 }});
919 dl 1.1
920 jsr166 1.31 threadStarted.await();
921     waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
922 jsr166 1.5 assertEquals(1, q.size());
923 jsr166 1.34 assertSame(five, q.poll());
924 jsr166 1.31 checkEmpty(q);
925 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
926 dl 1.1 }
927 jsr166 1.3
928     /**
929 dl 1.6 * transfer waits until a poll occurs, and then transfers in fifo order
930 dl 1.1 */
931 jsr166 1.5 public void testTransfer3() throws InterruptedException {
932 jsr166 1.7 final LinkedTransferQueue<Integer> q
933     = new LinkedTransferQueue<Integer>();
934 jsr166 1.5
935 jsr166 1.9 Thread first = newStartedThread(new CheckedRunnable() {
936 jsr166 1.21 public void realRun() throws InterruptedException {
937 jsr166 1.32 q.transfer(four);
938     assertTrue(!q.contains(four));
939 jsr166 1.24 assertEquals(1, q.size());
940 jsr166 1.7 }});
941 jsr166 1.5
942 jsr166 1.9 Thread interruptedThread = newStartedThread(
943     new CheckedInterruptedRunnable() {
944 jsr166 1.21 public void realRun() throws InterruptedException {
945 jsr166 1.32 while (q.isEmpty())
946 jsr166 1.9 Thread.yield();
947 jsr166 1.32 q.transfer(five);
948 jsr166 1.9 }});
949 jsr166 1.5
950     while (q.size() < 2)
951     Thread.yield();
952     assertEquals(2, q.size());
953 jsr166 1.32 assertSame(four, q.poll());
954 jsr166 1.5 first.join();
955     assertEquals(1, q.size());
956     interruptedThread.interrupt();
957     interruptedThread.join();
958 jsr166 1.31 checkEmpty(q);
959 dl 1.1 }
960    
961     /**
962 dl 1.6 * transfer waits until a poll occurs, at which point the polling
963     * thread returns the element
964 dl 1.1 */
965 jsr166 1.5 public void testTransfer4() throws InterruptedException {
966 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
967 jsr166 1.9
968     Thread t = newStartedThread(new CheckedRunnable() {
969 jsr166 1.21 public void realRun() throws InterruptedException {
970 jsr166 1.7 q.transfer(four);
971 jsr166 1.24 assertFalse(q.contains(four));
972     assertSame(three, q.poll());
973 jsr166 1.9 }});
974    
975 jsr166 1.31 while (q.isEmpty())
976     Thread.yield();
977     assertFalse(q.isEmpty());
978     assertEquals(1, q.size());
979 jsr166 1.5 assertTrue(q.offer(three));
980 jsr166 1.24 assertSame(four, q.poll());
981 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
982 dl 1.1 }
983 jsr166 1.3
984     /**
985 dl 1.6 * transfer waits until a take occurs. The transfered element
986     * is returned by this associated take.
987     */
988     public void testTransfer5() throws InterruptedException {
989 jsr166 1.7 final LinkedTransferQueue<Integer> q
990     = new LinkedTransferQueue<Integer>();
991 dl 1.6
992 jsr166 1.9 Thread t = newStartedThread(new CheckedRunnable() {
993 jsr166 1.21 public void realRun() throws InterruptedException {
994 jsr166 1.31 q.transfer(four);
995 jsr166 1.12 checkEmpty(q);
996 jsr166 1.9 }});
997 dl 1.6
998 jsr166 1.31 while (q.isEmpty())
999     Thread.yield();
1000     assertFalse(q.isEmpty());
1001     assertEquals(1, q.size());
1002     assertSame(four, q.take());
1003 jsr166 1.12 checkEmpty(q);
1004 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1005 dl 1.6 }
1006    
1007     /**
1008 jsr166 1.5 * tryTransfer(null) throws NullPointerException
1009 dl 1.1 */
1010     public void testTryTransfer1() {
1011     try {
1012     final LinkedTransferQueue q = new LinkedTransferQueue();
1013     q.tryTransfer(null);
1014 jsr166 1.8 shouldThrow();
1015 jsr166 1.15 } catch (NullPointerException success) {}
1016 dl 1.1 }
1017 jsr166 1.3
1018     /**
1019 jsr166 1.5 * tryTransfer returns false and does not enqueue if there are no
1020     * consumers waiting to poll or take.
1021 dl 1.1 */
1022 jsr166 1.12 public void testTryTransfer2() throws InterruptedException {
1023 jsr166 1.5 final LinkedTransferQueue q = new LinkedTransferQueue();
1024     assertFalse(q.tryTransfer(new Object()));
1025     assertFalse(q.hasWaitingConsumer());
1026 jsr166 1.12 checkEmpty(q);
1027 dl 1.1 }
1028 jsr166 1.3
1029     /**
1030 jsr166 1.5 * If there is a consumer waiting in timed poll, tryTransfer
1031     * returns true while successfully transfering object.
1032 dl 1.1 */
1033 jsr166 1.5 public void testTryTransfer3() throws InterruptedException {
1034     final Object hotPotato = new Object();
1035     final LinkedTransferQueue q = new LinkedTransferQueue();
1036 jsr166 1.9
1037     Thread t = newStartedThread(new CheckedRunnable() {
1038 jsr166 1.21 public void realRun() {
1039 jsr166 1.7 while (! q.hasWaitingConsumer())
1040     Thread.yield();
1041 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
1042 jsr166 1.31 checkEmpty(q);
1043 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
1044 jsr166 1.9 }});
1045    
1046 jsr166 1.23 assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1047 jsr166 1.12 checkEmpty(q);
1048 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1049 jsr166 1.5 }
1050 dl 1.1
1051 jsr166 1.5 /**
1052     * If there is a consumer waiting in take, tryTransfer returns
1053     * true while successfully transfering object.
1054     */
1055     public void testTryTransfer4() throws InterruptedException {
1056     final Object hotPotato = new Object();
1057     final LinkedTransferQueue q = new LinkedTransferQueue();
1058 jsr166 1.9
1059     Thread t = newStartedThread(new CheckedRunnable() {
1060 jsr166 1.21 public void realRun() {
1061 jsr166 1.7 while (! q.hasWaitingConsumer())
1062     Thread.yield();
1063 jsr166 1.24 assertTrue(q.hasWaitingConsumer());
1064 jsr166 1.31 checkEmpty(q);
1065 jsr166 1.24 assertTrue(q.tryTransfer(hotPotato));
1066 jsr166 1.9 }});
1067    
1068 jsr166 1.23 assertSame(q.take(), hotPotato);
1069 jsr166 1.12 checkEmpty(q);
1070 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1071 dl 1.1 }
1072    
1073 jsr166 1.3 /**
1074 dl 1.6 * tryTransfer waits the amount given if interrupted, and
1075     * throws interrupted exception
1076 dl 1.1 */
1077 jsr166 1.5 public void testTryTransfer5() throws InterruptedException {
1078 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1079 jsr166 1.31 final CountDownLatch threadStarted = new CountDownLatch(1);
1080 jsr166 1.9
1081 jsr166 1.31 Thread t = newStartedThread(new CheckedRunnable() {
1082 jsr166 1.21 public void realRun() throws InterruptedException {
1083 jsr166 1.31 long t0 = System.nanoTime();
1084     threadStarted.countDown();
1085     try {
1086     q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1087     shouldThrow();
1088     } catch (InterruptedException success) {}
1089     assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1090 jsr166 1.5 }});
1091 jsr166 1.9
1092 jsr166 1.31 threadStarted.await();
1093     Thread.sleep(SHORT_DELAY_MS);
1094     t.interrupt();
1095     awaitTermination(t, MEDIUM_DELAY_MS);
1096     checkEmpty(q);
1097 dl 1.1 }
1098    
1099 jsr166 1.3 /**
1100 jsr166 1.33 * tryTransfer gives up after the timeout and returns false
1101 dl 1.1 */
1102 jsr166 1.5 public void testTryTransfer6() throws InterruptedException {
1103 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1104 jsr166 1.9
1105     Thread t = newStartedThread(new CheckedRunnable() {
1106 jsr166 1.21 public void realRun() throws InterruptedException {
1107 jsr166 1.27 long t0 = System.nanoTime();
1108 jsr166 1.23 assertFalse(q.tryTransfer(new Object(),
1109     SHORT_DELAY_MS, MILLISECONDS));
1110 jsr166 1.28 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1111 jsr166 1.32 checkEmpty(q);
1112 jsr166 1.9 }});
1113    
1114 jsr166 1.27 awaitTermination(t, MEDIUM_DELAY_MS);
1115     checkEmpty(q);
1116 dl 1.1 }
1117    
1118 jsr166 1.3 /**
1119 dl 1.1 * tryTransfer waits for any elements previously in to be removed
1120     * before transfering to a poll or take
1121     */
1122 jsr166 1.5 public void testTryTransfer7() throws InterruptedException {
1123 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1124 jsr166 1.5 assertTrue(q.offer(four));
1125 jsr166 1.9
1126     Thread t = newStartedThread(new CheckedRunnable() {
1127 jsr166 1.21 public void realRun() throws InterruptedException {
1128 jsr166 1.23 assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1129 jsr166 1.31 checkEmpty(q);
1130 jsr166 1.9 }});
1131    
1132 jsr166 1.32 while (q.size() != 2)
1133     Thread.yield();
1134 jsr166 1.5 assertEquals(2, q.size());
1135 jsr166 1.24 assertSame(four, q.poll());
1136     assertSame(five, q.poll());
1137 jsr166 1.12 checkEmpty(q);
1138 jsr166 1.29 awaitTermination(t, MEDIUM_DELAY_MS);
1139 dl 1.1 }
1140    
1141 jsr166 1.3 /**
1142 jsr166 1.5 * tryTransfer attempts to enqueue into the q and fails returning
1143     * false not enqueueing and the successive poll is null
1144 dl 1.1 */
1145 jsr166 1.5 public void testTryTransfer8() throws InterruptedException {
1146 dl 1.1 final LinkedTransferQueue q = new LinkedTransferQueue();
1147 jsr166 1.5 assertTrue(q.offer(four));
1148     assertEquals(1, q.size());
1149 jsr166 1.32 long t0 = System.nanoTime();
1150 jsr166 1.7 assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1151 jsr166 1.32 assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1152 jsr166 1.5 assertEquals(1, q.size());
1153 jsr166 1.24 assertSame(four, q.poll());
1154 jsr166 1.13 assertNull(q.poll());
1155 jsr166 1.12 checkEmpty(q);
1156 dl 1.1 }
1157    
1158 jsr166 1.5 private LinkedTransferQueue<Integer> populatedQueue(int n) {
1159     LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1160 jsr166 1.31 checkEmpty(q);
1161 jsr166 1.5 for (int i = 0; i < n; i++) {
1162     assertEquals(i, q.size());
1163 dl 1.1 assertTrue(q.offer(i));
1164 jsr166 1.5 assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1165 dl 1.1 }
1166     assertFalse(q.isEmpty());
1167     return q;
1168     }
1169     }