ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.15 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.61 by jsr166, Sat May 23 00:53:08 2015 UTC

# Line 1 | Line 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10   import java.util.ArrayList;
11   import java.util.Arrays;
12 + import java.util.Collection;
13   import java.util.Iterator;
14   import java.util.List;
15   import java.util.NoSuchElementException;
16 < import java.util.concurrent.*;
17 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 > import java.util.Queue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.LinkedTransferQueue;
22 >
23   import junit.framework.Test;
22 import junit.framework.TestSuite;
24  
25   @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27  
28 <    public static void main(String[] args) {
29 <        junit.textui.TestRunner.run(suite());
28 >    public static class Generic extends BlockingQueueTest {
29 >        protected BlockingQueue emptyCollection() {
30 >            return new LinkedTransferQueue();
31 >        }
32      }
33  
34 <    public static Test suite() {
35 <        return new TestSuite(LinkedTransferQueueTest.class);
34 >    public static void main(String[] args) {
35 >        main(suite(), args);
36      }
37  
38 <    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
39 <        assertTrue(q.isEmpty());
40 <        assertEquals(0, q.size());
38 <        assertNull(q.peek());
39 <        assertNull(q.poll());
40 <        assertNull(q.poll(0, MILLISECONDS));
41 <        assertEquals(q.toString(), "[]");
42 <        assertTrue(Arrays.equals(q.toArray(), new Object[0]));
43 <        assertFalse(q.iterator().hasNext());
44 <        try {
45 <            q.element();
46 <            shouldThrow();
47 <        } catch (NoSuchElementException success) {
48 <        }
49 <        try {
50 <            q.iterator().next();
51 <            shouldThrow();
52 <        } catch (NoSuchElementException success) {
53 <        }
54 <        try {
55 <            q.remove();
56 <            shouldThrow();
57 <        } catch (NoSuchElementException success) {
58 <        }
38 >    public static Test suite() {
39 >        return newTestSuite(LinkedTransferQueueTest.class,
40 >                            new Generic().testSuite());
41      }
42  
43      /**
# Line 75 | Line 57 | public class LinkedTransferQueueTest ext
57          try {
58              new LinkedTransferQueue(null);
59              shouldThrow();
60 <        } catch (NullPointerException success) {
79 <        }
60 >        } catch (NullPointerException success) {}
61      }
62  
63      /**
# Line 84 | Line 65 | public class LinkedTransferQueueTest ext
65       * NullPointerException
66       */
67      public void testConstructor3() {
68 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
69          try {
70 <            Integer[] ints = new Integer[SIZE];
89 <            new LinkedTransferQueue(Arrays.asList(ints));
70 >            new LinkedTransferQueue(elements);
71              shouldThrow();
72 <        } catch (NullPointerException success) {
92 <        }
72 >        } catch (NullPointerException success) {}
73      }
74  
75      /**
# Line 97 | Line 77 | public class LinkedTransferQueueTest ext
77       * throws NullPointerException
78       */
79      public void testConstructor4() {
80 +        Integer[] ints = new Integer[SIZE];
81 +        for (int i = 0; i < SIZE - 1; ++i)
82 +            ints[i] = i;
83 +        Collection<Integer> elements = Arrays.asList(ints);
84          try {
85 <            Integer[] ints = new Integer[SIZE];
102 <            for (int i = 0; i < SIZE - 1; ++i) {
103 <                ints[i] = i;
104 <            }
105 <            new LinkedTransferQueue(Arrays.asList(ints));
85 >            new LinkedTransferQueue(elements);
86              shouldThrow();
87 <        } catch (NullPointerException success) {
108 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 136 | Line 115 | public class LinkedTransferQueueTest ext
115       * remainingCapacity() always returns Integer.MAX_VALUE
116       */
117      public void testRemainingCapacity() {
118 <        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
118 >        BlockingQueue q = populatedQueue(SIZE);
119          for (int i = 0; i < SIZE; ++i) {
120              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
121              assertEquals(SIZE - i, q.size());
122 <            q.remove();
122 >            assertEquals(i, q.remove());
123          }
124          for (int i = 0; i < SIZE; ++i) {
125              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
126              assertEquals(i, q.size());
127 <            q.add(i);
149 <        }
150 <    }
151 <
152 <    /**
153 <     * offer(null) throws NullPointerException
154 <     */
155 <    public void testOfferNull() {
156 <        try {
157 <            LinkedTransferQueue q = new LinkedTransferQueue();
158 <            q.offer(null);
159 <            shouldThrow();
160 <        } catch (NullPointerException success) {
161 <        }
162 <    }
163 <
164 <    /**
165 <     * add(null) throws NullPointerException
166 <     */
167 <    public void testAddNull() {
168 <        try {
169 <            LinkedTransferQueue q = new LinkedTransferQueue();
170 <            q.add(null);
171 <            shouldThrow();
172 <        } catch (NullPointerException success) {
173 <        }
174 <    }
175 <
176 <    /**
177 <     * addAll(null) throws NullPointerException
178 <     */
179 <    public void testAddAll1() {
180 <        try {
181 <            LinkedTransferQueue q = new LinkedTransferQueue();
182 <            q.addAll(null);
183 <            shouldThrow();
184 <        } catch (NullPointerException success) {
127 >            assertTrue(q.add(i));
128          }
129      }
130  
# Line 189 | Line 132 | public class LinkedTransferQueueTest ext
132       * addAll(this) throws IllegalArgumentException
133       */
134      public void testAddAllSelf() {
135 +        LinkedTransferQueue q = populatedQueue(SIZE);
136          try {
193            LinkedTransferQueue q = populatedQueue(SIZE);
137              q.addAll(q);
138              shouldThrow();
139 <        } catch (IllegalArgumentException success) {
197 <        }
198 <    }
199 <
200 <    /**
201 <     * addAll of a collection with null elements throws NullPointerException
202 <     */
203 <    public void testAddAll2() {
204 <        try {
205 <            LinkedTransferQueue q = new LinkedTransferQueue();
206 <            Integer[] ints = new Integer[SIZE];
207 <            q.addAll(Arrays.asList(ints));
208 <            shouldThrow();
209 <        } catch (NullPointerException success) {
210 <        }
139 >        } catch (IllegalArgumentException success) {}
140      }
141  
142      /**
# Line 215 | Line 144 | public class LinkedTransferQueueTest ext
144       * NullPointerException after possibly adding some elements
145       */
146      public void testAddAll3() {
147 +        LinkedTransferQueue q = new LinkedTransferQueue();
148 +        Integer[] ints = new Integer[SIZE];
149 +        for (int i = 0; i < SIZE - 1; ++i)
150 +            ints[i] = i;
151          try {
219            LinkedTransferQueue q = new LinkedTransferQueue();
220            Integer[] ints = new Integer[SIZE];
221            for (int i = 0; i < SIZE - 1; ++i) {
222                ints[i] = i;
223            }
152              q.addAll(Arrays.asList(ints));
153              shouldThrow();
154 <        } catch (NullPointerException success) {
227 <        }
154 >        } catch (NullPointerException success) {}
155      }
156  
157      /**
# Line 245 | Line 172 | public class LinkedTransferQueueTest ext
172      }
173  
174      /**
248     * put(null) throws NullPointerException
249     */
250    public void testPutNull() throws InterruptedException {
251        try {
252            LinkedTransferQueue q = new LinkedTransferQueue();
253            q.put(null);
254            shouldThrow();
255        } catch (NullPointerException success) {}
256    }
257
258    /**
175       * all elements successfully put are contained
176       */
177      public void testPut() {
178          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
179          for (int i = 0; i < SIZE; ++i) {
180 <            assertEquals(q.size(), i);
180 >            assertEquals(i, q.size());
181              q.put(i);
182              assertTrue(q.contains(i));
183          }
# Line 278 | Line 194 | public class LinkedTransferQueueTest ext
194      }
195  
196      /**
197 <     * take blocks interruptibly when empty
282 <     */
283 <    public void testTakeFromEmpty() throws InterruptedException {
284 <        final LinkedTransferQueue q = new LinkedTransferQueue();
285 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
286 <            void realRun() throws InterruptedException {
287 <                q.take();
288 <            }});
289 <        Thread.sleep(SHORT_DELAY_MS);
290 <        t.interrupt();
291 <        t.join();
292 <    }
293 <
294 <    /**
295 <     * Take removes existing elements until empty, then blocks interruptibly
197 >     * take removes existing elements until empty, then blocks interruptibly
198       */
199      public void testBlockingTake() throws InterruptedException {
200 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
201 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
202 <            void realRun() throws InterruptedException {
200 >        final BlockingQueue q = populatedQueue(SIZE);
201 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
202 >        Thread t = newStartedThread(new CheckedRunnable() {
203 >            public void realRun() throws InterruptedException {
204                  for (int i = 0; i < SIZE; ++i) {
205 <                    threadAssertEquals(i, (int) q.take());
205 >                    assertEquals(i, q.take());
206                  }
207 <                q.take();
207 >
208 >                Thread.currentThread().interrupt();
209 >                try {
210 >                    q.take();
211 >                    shouldThrow();
212 >                } catch (InterruptedException success) {}
213 >                assertFalse(Thread.interrupted());
214 >
215 >                pleaseInterrupt.countDown();
216 >                try {
217 >                    q.take();
218 >                    shouldThrow();
219 >                } catch (InterruptedException success) {}
220 >                assertFalse(Thread.interrupted());
221              }});
222 <        Thread.sleep(SMALL_DELAY_MS);
222 >
223 >        await(pleaseInterrupt);
224 >        assertThreadStaysAlive(t);
225          t.interrupt();
226 <        t.join();
309 <        checkEmpty(q);
226 >        awaitTermination(t);
227      }
228  
229      /**
# Line 322 | Line 239 | public class LinkedTransferQueueTest ext
239      }
240  
241      /**
242 <     * timed pool with zero timeout succeeds when non-empty, else times out
242 >     * timed poll with zero timeout succeeds when non-empty, else times out
243       */
244      public void testTimedPoll0() throws InterruptedException {
245          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 334 | Line 251 | public class LinkedTransferQueueTest ext
251      }
252  
253      /**
254 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
254 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
255       */
256      public void testTimedPoll() throws InterruptedException {
257          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
258          for (int i = 0; i < SIZE; ++i) {
259 <            long t0 = System.nanoTime();
259 >            long startTime = System.nanoTime();
260              assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
261 <            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
345 <            assertTrue(millisElapsed < SMALL_DELAY_MS);
261 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
262          }
263 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
263 >        long startTime = System.nanoTime();
264 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
265 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
266          checkEmpty(q);
267      }
268  
# Line 353 | Line 271 | public class LinkedTransferQueueTest ext
271       * returning timeout status
272       */
273      public void testInterruptedTimedPoll() throws InterruptedException {
274 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
275 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
276 <            void realRun() throws InterruptedException {
274 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
275 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
276 >        Thread t = newStartedThread(new CheckedRunnable() {
277 >            public void realRun() throws InterruptedException {
278                  for (int i = 0; i < SIZE; ++i) {
279                      long t0 = System.nanoTime();
280 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
281 <                                                       MILLISECONDS));
282 <                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
283 <                    assertTrue(millisElapsed < SMALL_DELAY_MS);
280 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
281 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
282 >                }
283 >                long t0 = System.nanoTime();
284 >                aboutToWait.countDown();
285 >                try {
286 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
287 >                    shouldThrow();
288 >                } catch (InterruptedException success) {
289 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
290                  }
366                q.poll(LONG_DELAY_MS, MILLISECONDS);
291              }});
292 <        Thread.sleep(SMALL_DELAY_MS);
292 >
293 >        aboutToWait.await();
294 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
295          t.interrupt();
296 <        t.join();
296 >        awaitTermination(t, MEDIUM_DELAY_MS);
297          checkEmpty(q);
298      }
299  
300      /**
301 <     * timed poll before a delayed offer fails; after offer succeeds;
302 <     * on interruption throws
301 >     * timed poll after thread interrupted throws InterruptedException
302 >     * instead of returning timeout status
303       */
304 <    public void testTimedPollWithOffer() throws InterruptedException {
305 <        final LinkedTransferQueue q = new LinkedTransferQueue();
306 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
307 <            void realRun() throws InterruptedException {
308 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
309 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
310 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
304 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
305 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
306 >        Thread t = newStartedThread(new CheckedRunnable() {
307 >            public void realRun() throws InterruptedException {
308 >                Thread.currentThread().interrupt();
309 >                for (int i = 0; i < SIZE; ++i) {
310 >                    long t0 = System.nanoTime();
311 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
312 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
313 >                }
314 >                try {
315 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
316 >                    shouldThrow();
317 >                } catch (InterruptedException success) {}
318              }});
319 <        Thread.sleep(SMALL_DELAY_MS);
320 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
321 <        t.interrupt();
389 <        t.join();
319 >
320 >        awaitTermination(t, MEDIUM_DELAY_MS);
321 >        checkEmpty(q);
322      }
323  
324      /**
# Line 416 | Line 348 | public class LinkedTransferQueueTest ext
348          try {
349              q.element();
350              shouldThrow();
351 <        } catch (NoSuchElementException success) {
420 <        }
351 >        } catch (NoSuchElementException success) {}
352          checkEmpty(q);
353      }
354  
# Line 432 | Line 363 | public class LinkedTransferQueueTest ext
363          try {
364              q.remove();
365              shouldThrow();
366 <        } catch (NoSuchElementException success) {
436 <        }
437 <        checkEmpty(q);
438 <    }
439 <
440 <    /**
441 <     * remove(x) removes x and returns true if present
442 <     */
443 <    public void testRemoveElement() throws InterruptedException {
444 <        LinkedTransferQueue q = populatedQueue(SIZE);
445 <        for (int i = 1; i < SIZE; i += 2) {
446 <            assertTrue(q.remove(i));
447 <        }
448 <        for (int i = 0; i < SIZE; i += 2) {
449 <            assertTrue(q.remove(i));
450 <            assertFalse(q.remove(i + 1));
451 <        }
366 >        } catch (NoSuchElementException success) {}
367          checkEmpty(q);
368      }
369  
# Line 462 | Line 377 | public class LinkedTransferQueueTest ext
377          assertTrue(q.remove(one));
378          assertTrue(q.remove(two));
379          assertTrue(q.add(three));
380 <        assertTrue(q.take() == three);
380 >        assertSame(q.take(), three);
381      }
382  
383      /**
# Line 544 | Line 459 | public class LinkedTransferQueueTest ext
459      }
460  
461      /**
462 <     * toArray() contains all elements
462 >     * toArray() contains all elements in FIFO order
463       */
464 <    public void testToArray() throws InterruptedException {
464 >    public void testToArray() {
465          LinkedTransferQueue q = populatedQueue(SIZE);
466          Object[] o = q.toArray();
467          for (int i = 0; i < o.length; i++) {
468 <            assertEquals(o[i], q.take());
468 >            assertSame(o[i], q.poll());
469          }
470      }
471  
472      /**
473 <     * toArray(a) contains all elements
473 >     * toArray(a) contains all elements in FIFO order
474       */
475 <    public void testToArray2() throws InterruptedException {
475 >    public void testToArray2() {
476          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
477          Integer[] ints = new Integer[SIZE];
478 <        ints = q.toArray(ints);
478 >        Integer[] array = q.toArray(ints);
479 >        assertSame(ints, array);
480          for (int i = 0; i < ints.length; i++) {
481 <            assertEquals(ints[i], q.take());
481 >            assertSame(ints[i], q.poll());
482          }
483      }
484  
485      /**
486 <     * toArray(null) throws NullPointerException
571 <     */
572 <    public void testToArray_BadArg() {
573 <        try {
574 <            LinkedTransferQueue q = populatedQueue(SIZE);
575 <            Object o[] = q.toArray(null);
576 <            shouldThrow();
577 <        } catch (NullPointerException success) {
578 <        }
579 <    }
580 <
581 <    /**
582 <     * toArray(incompatible array type) throws CCE
486 >     * toArray(incompatible array type) throws ArrayStoreException
487       */
488      public void testToArray1_BadArg() {
489 +        LinkedTransferQueue q = populatedQueue(SIZE);
490          try {
491 <            LinkedTransferQueue q = populatedQueue(SIZE);
587 <            Object o[] = q.toArray(new String[10]);
491 >            q.toArray(new String[10]);
492              shouldThrow();
493 <        } catch (ArrayStoreException success) {
590 <        }
493 >        } catch (ArrayStoreException success) {}
494      }
495  
496      /**
# Line 596 | Line 499 | public class LinkedTransferQueueTest ext
499      public void testIterator() throws InterruptedException {
500          LinkedTransferQueue q = populatedQueue(SIZE);
501          Iterator it = q.iterator();
502 <        int i = 0;
503 <        while (it.hasNext()) {
504 <            assertEquals(it.next(), i++);
505 <        }
502 >        int i;
503 >        for (i = 0; it.hasNext(); i++)
504 >            assertTrue(q.contains(it.next()));
505 >        assertEquals(i, SIZE);
506 >        assertIteratorExhausted(it);
507 >
508 >        it = q.iterator();
509 >        for (i = 0; it.hasNext(); i++)
510 >            assertEquals(it.next(), q.take());
511          assertEquals(i, SIZE);
512 +        assertIteratorExhausted(it);
513 +    }
514 +
515 +    /**
516 +     * iterator of empty collection has no elements
517 +     */
518 +    public void testEmptyIterator() {
519 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
520      }
521  
522      /**
# Line 617 | Line 533 | public class LinkedTransferQueueTest ext
533          it.remove();
534  
535          it = q.iterator();
536 <        assertEquals(it.next(), one);
537 <        assertEquals(it.next(), three);
536 >        assertSame(it.next(), one);
537 >        assertSame(it.next(), three);
538          assertFalse(it.hasNext());
539      }
540  
# Line 662 | Line 578 | public class LinkedTransferQueueTest ext
578          LinkedTransferQueue q = populatedQueue(SIZE);
579          String s = q.toString();
580          for (int i = 0; i < SIZE; ++i) {
581 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
581 >            assertTrue(s.contains(String.valueOf(i)));
582          }
583      }
584  
# Line 671 | Line 587 | public class LinkedTransferQueueTest ext
587       */
588      public void testOfferInExecutor() {
589          final LinkedTransferQueue q = new LinkedTransferQueue();
590 <        q.add(one);
675 <        q.add(two);
590 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
591          ExecutorService executor = Executors.newFixedThreadPool(2);
592  
593          executor.execute(new CheckedRunnable() {
594 <            void realRun() {
595 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
596 <                                         MILLISECONDS));
594 >            public void realRun() throws InterruptedException {
595 >                threadsStarted.await();
596 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
597              }});
598  
599          executor.execute(new CheckedRunnable() {
600 <            void realRun() throws InterruptedException {
601 <                Thread.sleep(SMALL_DELAY_MS);
602 <                threadAssertEquals(one, q.take());
600 >            public void realRun() throws InterruptedException {
601 >                threadsStarted.await();
602 >                assertSame(one, q.take());
603 >                checkEmpty(q);
604              }});
605  
606          joinPool(executor);
# Line 695 | Line 611 | public class LinkedTransferQueueTest ext
611       */
612      public void testPollInExecutor() {
613          final LinkedTransferQueue q = new LinkedTransferQueue();
614 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
615          ExecutorService executor = Executors.newFixedThreadPool(2);
616  
617          executor.execute(new CheckedRunnable() {
618 <            void realRun() throws InterruptedException {
619 <                threadAssertNull(q.poll());
620 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
621 <                                                MILLISECONDS));
622 <                threadAssertTrue(q.isEmpty());
618 >            public void realRun() throws InterruptedException {
619 >                assertNull(q.poll());
620 >                threadsStarted.await();
621 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
622 >                checkEmpty(q);
623              }});
624  
625          executor.execute(new CheckedRunnable() {
626 <            void realRun() throws InterruptedException {
627 <                Thread.sleep(SMALL_DELAY_MS);
626 >            public void realRun() throws InterruptedException {
627 >                threadsStarted.await();
628                  q.put(one);
629              }});
630  
# Line 718 | Line 635 | public class LinkedTransferQueueTest ext
635       * A deserialized serialized queue has same elements in same order
636       */
637      public void testSerialization() throws Exception {
638 <        LinkedTransferQueue q = populatedQueue(SIZE);
639 <
723 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
724 <        ObjectOutputStream out
725 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
726 <        out.writeObject(q);
727 <        out.close();
728 <
729 <        ByteArrayInputStream bin
730 <            = new ByteArrayInputStream(bout.toByteArray());
731 <        ObjectInputStream in
732 <            = new ObjectInputStream(new BufferedInputStream(bin));
733 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
734 <
735 <        assertEquals(q.size(), r.size());
736 <        while (!q.isEmpty()) {
737 <            assertEquals(q.remove(), r.remove());
738 <        }
739 <    }
638 >        Queue x = populatedQueue(SIZE);
639 >        Queue y = serialClone(x);
640  
641 <    /**
642 <     * drainTo(null) throws NullPointerException
643 <     */
644 <    public void testDrainToNull() {
645 <        LinkedTransferQueue q = populatedQueue(SIZE);
646 <        try {
647 <            q.drainTo(null);
748 <            shouldThrow();
749 <        } catch (NullPointerException success) {
750 <        }
751 <    }
752 <
753 <    /**
754 <     * drainTo(this) throws IllegalArgumentException
755 <     */
756 <    public void testDrainToSelf() {
757 <        LinkedTransferQueue q = populatedQueue(SIZE);
758 <        try {
759 <            q.drainTo(q);
760 <            shouldThrow();
761 <        } catch (IllegalArgumentException success) {
641 >        assertNotSame(y, x);
642 >        assertEquals(x.size(), y.size());
643 >        assertEquals(x.toString(), y.toString());
644 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
645 >        while (!x.isEmpty()) {
646 >            assertFalse(y.isEmpty());
647 >            assertEquals(x.remove(), y.remove());
648          }
649 +        assertTrue(y.isEmpty());
650      }
651  
652      /**
# Line 769 | Line 656 | public class LinkedTransferQueueTest ext
656          LinkedTransferQueue q = populatedQueue(SIZE);
657          ArrayList l = new ArrayList();
658          q.drainTo(l);
659 <        assertEquals(q.size(), 0);
660 <        assertEquals(l.size(), SIZE);
659 >        assertEquals(0, q.size());
660 >        assertEquals(SIZE, l.size());
661          for (int i = 0; i < SIZE; ++i) {
662 <            assertEquals(l.get(i), i);
662 >            assertEquals(i, l.get(i));
663          }
664          q.add(zero);
665          q.add(one);
# Line 781 | Line 668 | public class LinkedTransferQueueTest ext
668          assertTrue(q.contains(one));
669          l.clear();
670          q.drainTo(l);
671 <        assertEquals(q.size(), 0);
672 <        assertEquals(l.size(), 2);
671 >        assertEquals(0, q.size());
672 >        assertEquals(2, l.size());
673          for (int i = 0; i < 2; ++i) {
674 <            assertEquals(l.get(i), i);
674 >            assertEquals(i, l.get(i));
675          }
676      }
677  
# Line 794 | Line 681 | public class LinkedTransferQueueTest ext
681      public void testDrainToWithActivePut() throws InterruptedException {
682          final LinkedTransferQueue q = populatedQueue(SIZE);
683          Thread t = newStartedThread(new CheckedRunnable() {
684 <            void realRun() {
684 >            public void realRun() {
685                  q.put(SIZE + 1);
686              }});
687          ArrayList l = new ArrayList();
688          q.drainTo(l);
689          assertTrue(l.size() >= SIZE);
690 <        for (int i = 0; i < SIZE; ++i) {
691 <            assertEquals(l.get(i), i);
692 <        }
806 <        t.join();
690 >        for (int i = 0; i < SIZE; ++i)
691 >            assertEquals(i, l.get(i));
692 >        awaitTermination(t, MEDIUM_DELAY_MS);
693          assertTrue(q.size() + l.size() >= SIZE);
694      }
695  
696      /**
697 <     * drainTo(null, n) throws NullPointerException
812 <     */
813 <    public void testDrainToNullN() {
814 <        LinkedTransferQueue q = populatedQueue(SIZE);
815 <        try {
816 <            q.drainTo(null, SIZE);
817 <            shouldThrow();
818 <        } catch (NullPointerException success) {
819 <        }
820 <    }
821 <
822 <    /**
823 <     * drainTo(this, n) throws IllegalArgumentException
824 <     */
825 <    public void testDrainToSelfN() {
826 <        LinkedTransferQueue q = populatedQueue(SIZE);
827 <        try {
828 <            q.drainTo(q, SIZE);
829 <            shouldThrow();
830 <        } catch (IllegalArgumentException success) {
831 <        }
832 <    }
833 <
834 <    /**
835 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
697 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
698       */
699      public void testDrainToN() {
700          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 843 | Line 705 | public class LinkedTransferQueueTest ext
705              ArrayList l = new ArrayList();
706              q.drainTo(l, i);
707              int k = (i < SIZE) ? i : SIZE;
708 <            assertEquals(l.size(), k);
709 <            assertEquals(q.size(), SIZE - k);
710 <            for (int j = 0; j < k; ++j) {
711 <                assertEquals(l.get(j), j);
712 <            }
851 <            while (q.poll() != null)
852 <                ;
708 >            assertEquals(k, l.size());
709 >            assertEquals(SIZE - k, q.size());
710 >            for (int j = 0; j < k; ++j)
711 >                assertEquals(j, l.get(j));
712 >            do {} while (q.poll() != null);
713          }
714      }
715  
# Line 859 | Line 719 | public class LinkedTransferQueueTest ext
719       */
720      public void testWaitingConsumer() throws InterruptedException {
721          final LinkedTransferQueue q = new LinkedTransferQueue();
722 <        assertEquals(q.getWaitingConsumerCount(), 0);
722 >        assertEquals(0, q.getWaitingConsumerCount());
723          assertFalse(q.hasWaitingConsumer());
724 +        final CountDownLatch threadStarted = new CountDownLatch(1);
725  
726          Thread t = newStartedThread(new CheckedRunnable() {
727 <            void realRun() throws InterruptedException {
728 <                Thread.sleep(SMALL_DELAY_MS);
729 <                threadAssertTrue(q.hasWaitingConsumer());
730 <                threadAssertEquals(q.getWaitingConsumerCount(), 1);
731 <                threadAssertTrue(q.offer(new Object()));
871 <                threadAssertFalse(q.hasWaitingConsumer());
872 <                threadAssertEquals(q.getWaitingConsumerCount(), 0);
727 >            public void realRun() throws InterruptedException {
728 >                threadStarted.countDown();
729 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
730 >                assertEquals(0, q.getWaitingConsumerCount());
731 >                assertFalse(q.hasWaitingConsumer());
732              }});
733  
734 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
735 <        assertEquals(q.getWaitingConsumerCount(), 0);
734 >        threadStarted.await();
735 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
736 >        assertEquals(1, q.getWaitingConsumerCount());
737 >        assertTrue(q.hasWaitingConsumer());
738 >
739 >        assertTrue(q.offer(one));
740 >        assertEquals(0, q.getWaitingConsumerCount());
741          assertFalse(q.hasWaitingConsumer());
742 <        t.join();
742 >
743 >        awaitTermination(t, MEDIUM_DELAY_MS);
744      }
745  
746      /**
# Line 896 | Line 761 | public class LinkedTransferQueueTest ext
761      public void testTransfer2() throws InterruptedException {
762          final LinkedTransferQueue<Integer> q
763              = new LinkedTransferQueue<Integer>();
764 +        final CountDownLatch threadStarted = new CountDownLatch(1);
765  
766          Thread t = newStartedThread(new CheckedRunnable() {
767 <            void realRun() throws InterruptedException {
768 <                q.transfer(SIZE);
769 <                threadAssertTrue(q.isEmpty());
767 >            public void realRun() throws InterruptedException {
768 >                threadStarted.countDown();
769 >                q.transfer(five);
770 >                checkEmpty(q);
771              }});
772  
773 <        Thread.sleep(SHORT_DELAY_MS);
773 >        threadStarted.await();
774 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
775          assertEquals(1, q.size());
776 <        assertEquals(SIZE, (int) q.poll());
777 <        assertTrue(q.isEmpty());
778 <        t.join();
776 >        assertSame(five, q.poll());
777 >        checkEmpty(q);
778 >        awaitTermination(t, MEDIUM_DELAY_MS);
779      }
780  
781      /**
# Line 918 | Line 786 | public class LinkedTransferQueueTest ext
786              = new LinkedTransferQueue<Integer>();
787  
788          Thread first = newStartedThread(new CheckedRunnable() {
789 <            void realRun() throws InterruptedException {
790 <                Integer i = SIZE + 1;
791 <                q.transfer(i);
792 <                threadAssertTrue(!q.contains(i));
925 <                threadAssertEquals(1, q.size());
789 >            public void realRun() throws InterruptedException {
790 >                q.transfer(four);
791 >                assertTrue(!q.contains(four));
792 >                assertEquals(1, q.size());
793              }});
794  
795          Thread interruptedThread = newStartedThread(
796              new CheckedInterruptedRunnable() {
797 <                void realRun() throws InterruptedException {
798 <                    while (q.size() == 0)
797 >                public void realRun() throws InterruptedException {
798 >                    while (q.isEmpty())
799                          Thread.yield();
800 <                    q.transfer(SIZE);
800 >                    q.transfer(five);
801                  }});
802  
803          while (q.size() < 2)
804              Thread.yield();
805          assertEquals(2, q.size());
806 <        assertEquals(SIZE + 1, (int) q.poll());
806 >        assertSame(four, q.poll());
807          first.join();
808          assertEquals(1, q.size());
809          interruptedThread.interrupt();
810          interruptedThread.join();
811 <        assertEquals(0, q.size());
945 <        assertTrue(q.isEmpty());
811 >        checkEmpty(q);
812      }
813  
814      /**
# Line 953 | Line 819 | public class LinkedTransferQueueTest ext
819          final LinkedTransferQueue q = new LinkedTransferQueue();
820  
821          Thread t = newStartedThread(new CheckedRunnable() {
822 <            void realRun() throws InterruptedException {
822 >            public void realRun() throws InterruptedException {
823                  q.transfer(four);
824 <                threadAssertFalse(q.contains(four));
825 <                threadAssertEquals(three, q.poll());
824 >                assertFalse(q.contains(four));
825 >                assertSame(three, q.poll());
826              }});
827  
828 <        Thread.sleep(SHORT_DELAY_MS);
828 >        while (q.isEmpty())
829 >            Thread.yield();
830 >        assertFalse(q.isEmpty());
831 >        assertEquals(1, q.size());
832          assertTrue(q.offer(three));
833 <        assertEquals(four, q.poll());
834 <        t.join();
833 >        assertSame(four, q.poll());
834 >        awaitTermination(t, MEDIUM_DELAY_MS);
835      }
836  
837      /**
# Line 974 | Line 843 | public class LinkedTransferQueueTest ext
843              = new LinkedTransferQueue<Integer>();
844  
845          Thread t = newStartedThread(new CheckedRunnable() {
846 <            void realRun() throws InterruptedException {
847 <                q.transfer(SIZE);
846 >            public void realRun() throws InterruptedException {
847 >                q.transfer(four);
848                  checkEmpty(q);
849              }});
850  
851 <        Thread.sleep(SHORT_DELAY_MS);
852 <        assertEquals(SIZE, (int) q.take());
851 >        while (q.isEmpty())
852 >            Thread.yield();
853 >        assertFalse(q.isEmpty());
854 >        assertEquals(1, q.size());
855 >        assertSame(four, q.take());
856          checkEmpty(q);
857 <        t.join();
857 >        awaitTermination(t, MEDIUM_DELAY_MS);
858      }
859  
860      /**
861       * tryTransfer(null) throws NullPointerException
862       */
863      public void testTryTransfer1() {
864 +        final LinkedTransferQueue q = new LinkedTransferQueue();
865          try {
993            final LinkedTransferQueue q = new LinkedTransferQueue();
866              q.tryTransfer(null);
867              shouldThrow();
868          } catch (NullPointerException success) {}
# Line 1016 | Line 888 | public class LinkedTransferQueueTest ext
888          final LinkedTransferQueue q = new LinkedTransferQueue();
889  
890          Thread t = newStartedThread(new CheckedRunnable() {
891 <            void realRun() {
891 >            public void realRun() {
892                  while (! q.hasWaitingConsumer())
893                      Thread.yield();
894 <                threadAssertTrue(q.hasWaitingConsumer());
895 <                threadAssertTrue(q.isEmpty());
896 <                threadAssertTrue(q.size() == 0);
1025 <                threadAssertTrue(q.tryTransfer(hotPotato));
894 >                assertTrue(q.hasWaitingConsumer());
895 >                checkEmpty(q);
896 >                assertTrue(q.tryTransfer(hotPotato));
897              }});
898  
899 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
899 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
900          checkEmpty(q);
901 <        t.join();
901 >        awaitTermination(t, MEDIUM_DELAY_MS);
902      }
903  
904      /**
# Line 1039 | Line 910 | public class LinkedTransferQueueTest ext
910          final LinkedTransferQueue q = new LinkedTransferQueue();
911  
912          Thread t = newStartedThread(new CheckedRunnable() {
913 <            void realRun() {
913 >            public void realRun() {
914                  while (! q.hasWaitingConsumer())
915                      Thread.yield();
916 <                threadAssertTrue(q.hasWaitingConsumer());
917 <                threadAssertTrue(q.isEmpty());
918 <                threadAssertTrue(q.size() == 0);
1048 <                threadAssertTrue(q.tryTransfer(hotPotato));
916 >                assertTrue(q.hasWaitingConsumer());
917 >                checkEmpty(q);
918 >                assertTrue(q.tryTransfer(hotPotato));
919              }});
920  
921 <        assertTrue(q.take() == hotPotato);
921 >        assertSame(q.take(), hotPotato);
922          checkEmpty(q);
923 <        t.join();
923 >        awaitTermination(t, MEDIUM_DELAY_MS);
924      }
925  
926      /**
927 <     * tryTransfer waits the amount given if interrupted, and
1058 <     * throws interrupted exception
927 >     * tryTransfer blocks interruptibly if no takers
928       */
929      public void testTryTransfer5() throws InterruptedException {
930          final LinkedTransferQueue q = new LinkedTransferQueue();
931 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
932 +        assertTrue(q.isEmpty());
933  
934 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
935 <            void realRun() throws InterruptedException {
936 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
934 >        Thread t = newStartedThread(new CheckedRunnable() {
935 >            public void realRun() throws InterruptedException {
936 >                Thread.currentThread().interrupt();
937 >                try {
938 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
939 >                    shouldThrow();
940 >                } catch (InterruptedException success) {}
941 >                assertFalse(Thread.interrupted());
942 >
943 >                pleaseInterrupt.countDown();
944 >                try {
945 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
946 >                    shouldThrow();
947 >                } catch (InterruptedException success) {}
948 >                assertFalse(Thread.interrupted());
949              }});
950  
951 <        Thread.sleep(SMALL_DELAY_MS);
952 <        toInterrupt.interrupt();
953 <        toInterrupt.join();
951 >        await(pleaseInterrupt);
952 >        assertThreadStaysAlive(t);
953 >        t.interrupt();
954 >        awaitTermination(t);
955 >        checkEmpty(q);
956      }
957  
958      /**
959 <     * tryTransfer gives up after the timeout and return false
959 >     * tryTransfer gives up after the timeout and returns false
960       */
961      public void testTryTransfer6() throws InterruptedException {
962          final LinkedTransferQueue q = new LinkedTransferQueue();
963  
964          Thread t = newStartedThread(new CheckedRunnable() {
965 <            void realRun() throws InterruptedException {
966 <                threadAssertFalse
967 <                    (q.tryTransfer(new Object(),
968 <                                   SHORT_DELAY_MS, MILLISECONDS));
965 >            public void realRun() throws InterruptedException {
966 >                long t0 = System.nanoTime();
967 >                assertFalse(q.tryTransfer(new Object(),
968 >                                          timeoutMillis(), MILLISECONDS));
969 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
970 >                checkEmpty(q);
971              }});
972  
973 <        Thread.sleep(SMALL_DELAY_MS);
973 >        awaitTermination(t);
974          checkEmpty(q);
1088        t.join();
975      }
976  
977      /**
# Line 1097 | Line 983 | public class LinkedTransferQueueTest ext
983          assertTrue(q.offer(four));
984  
985          Thread t = newStartedThread(new CheckedRunnable() {
986 <            void realRun() throws InterruptedException {
987 <                threadAssertTrue(q.tryTransfer(five,
988 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1103 <                threadAssertTrue(q.isEmpty());
986 >            public void realRun() throws InterruptedException {
987 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
988 >                checkEmpty(q);
989              }});
990  
991 <        Thread.sleep(SHORT_DELAY_MS);
991 >        while (q.size() != 2)
992 >            Thread.yield();
993          assertEquals(2, q.size());
994 <        assertEquals(four, q.poll());
995 <        assertEquals(five, q.poll());
994 >        assertSame(four, q.poll());
995 >        assertSame(five, q.poll());
996          checkEmpty(q);
997 <        t.join();
997 >        awaitTermination(t, MEDIUM_DELAY_MS);
998      }
999  
1000      /**
1001 <     * tryTransfer attempts to enqueue into the q and fails returning
1002 <     * false not enqueueing and the successive poll is null
1001 >     * tryTransfer attempts to enqueue into the queue and fails
1002 >     * returning false not enqueueing and the successive poll is null
1003       */
1004      public void testTryTransfer8() throws InterruptedException {
1005          final LinkedTransferQueue q = new LinkedTransferQueue();
1006          assertTrue(q.offer(four));
1007          assertEquals(1, q.size());
1008 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1008 >        long t0 = System.nanoTime();
1009 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1010 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1011          assertEquals(1, q.size());
1012 <        assertEquals(four, q.poll());
1012 >        assertSame(four, q.poll());
1013          assertNull(q.poll());
1014          checkEmpty(q);
1015      }
1016  
1017      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1018          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1019 <        assertTrue(q.isEmpty());
1019 >        checkEmpty(q);
1020          for (int i = 0; i < n; i++) {
1021              assertEquals(i, q.size());
1022              assertTrue(q.offer(i));
# Line 1137 | Line 1025 | public class LinkedTransferQueueTest ext
1025          assertFalse(q.isEmpty());
1026          return q;
1027      }
1028 +
1029 +    /**
1030 +     * remove(null), contains(null) always return false
1031 +     */
1032 +    public void testNeverContainsNull() {
1033 +        Collection<?>[] qs = {
1034 +            new LinkedTransferQueue<Object>(),
1035 +            populatedQueue(2),
1036 +        };
1037 +
1038 +        for (Collection<?> q : qs) {
1039 +            assertFalse(q.contains(null));
1040 +            assertFalse(q.remove(null));
1041 +        }
1042 +    }
1043   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines