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.14 by jsr166, Sat Aug 15 03:20:36 2009 UTC vs.
Revision 1.66 by jsr166, Sun Oct 18 04:48:32 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 +    static class Implementation implements CollectionImplementation {
28 +        public Class<?> klazz() { return LinkedTransferQueue.class; }
29 +        public Collection emptyCollection() { return new LinkedTransferQueue(); }
30 +        public Object makeElement(int i) { return i; }
31 +        public boolean isConcurrent() { return true; }
32 +        public boolean permitsNulls() { return false; }
33 +    }
34  
35 <    public static void main(String[] args) {
36 <        junit.textui.TestRunner.run(suite());
35 >    public static class Generic extends BlockingQueueTest {
36 >        protected BlockingQueue emptyCollection() {
37 >            return new LinkedTransferQueue();
38 >        }
39      }
40  
41 <    public static Test suite() {
42 <        return new TestSuite(LinkedTransferQueueTest.class);
41 >    public static void main(String[] args) {
42 >        main(suite(), args);
43      }
44  
45 <    void checkEmpty(LinkedTransferQueue q) throws InterruptedException {
46 <        assertTrue(q.isEmpty());
47 <        assertEquals(0, q.size());
48 <        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 <        }
45 >    public static Test suite() {
46 >        return newTestSuite(LinkedTransferQueueTest.class,
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
# Line 75 | Line 65 | public class LinkedTransferQueueTest ext
65          try {
66              new LinkedTransferQueue(null);
67              shouldThrow();
68 <        } catch (NullPointerException success) {
79 <        }
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
# Line 84 | Line 73 | public class LinkedTransferQueueTest ext
73       * NullPointerException
74       */
75      public void testConstructor3() {
76 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
77          try {
78 <            Integer[] ints = new Integer[SIZE];
89 <            new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(elements);
79              shouldThrow();
80 <        } catch (NullPointerException success) {
92 <        }
80 >        } catch (NullPointerException success) {}
81      }
82  
83      /**
# Line 97 | Line 85 | public class LinkedTransferQueueTest ext
85       * throws NullPointerException
86       */
87      public void testConstructor4() {
88 +        Integer[] ints = new Integer[SIZE];
89 +        for (int i = 0; i < SIZE - 1; ++i)
90 +            ints[i] = i;
91 +        Collection<Integer> elements = Arrays.asList(ints);
92          try {
93 <            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));
93 >            new LinkedTransferQueue(elements);
94              shouldThrow();
95 <        } catch (NullPointerException success) {
108 <        }
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
# Line 136 | Line 123 | public class LinkedTransferQueueTest ext
123       * remainingCapacity() always returns Integer.MAX_VALUE
124       */
125      public void testRemainingCapacity() {
126 <        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
126 >        BlockingQueue q = populatedQueue(SIZE);
127          for (int i = 0; i < SIZE; ++i) {
128              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
129              assertEquals(SIZE - i, q.size());
130 <            q.remove();
130 >            assertEquals(i, q.remove());
131          }
132          for (int i = 0; i < SIZE; ++i) {
133              assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
134              assertEquals(i, q.size());
135 <            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) {
135 >            assertTrue(q.add(i));
136          }
137      }
138  
# Line 189 | Line 140 | public class LinkedTransferQueueTest ext
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
193            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147 <        } 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 <        }
147 >        } catch (IllegalArgumentException success) {}
148      }
149  
150      /**
# Line 215 | Line 152 | public class LinkedTransferQueueTest ext
152       * NullPointerException after possibly adding some elements
153       */
154      public void testAddAll3() {
155 +        LinkedTransferQueue q = new LinkedTransferQueue();
156 +        Integer[] ints = new Integer[SIZE];
157 +        for (int i = 0; i < SIZE - 1; ++i)
158 +            ints[i] = i;
159          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            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162 <        } catch (NullPointerException success) {
227 <        }
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 245 | Line 180 | public class LinkedTransferQueueTest ext
180      }
181  
182      /**
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    /**
183       * all elements successfully put are contained
184       */
185      public void testPut() {
186          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
187          for (int i = 0; i < SIZE; ++i) {
188 <            assertEquals(q.size(), i);
188 >            assertEquals(i, q.size());
189              q.put(i);
190              assertTrue(q.contains(i));
191          }
# Line 278 | Line 202 | public class LinkedTransferQueueTest ext
202      }
203  
204      /**
205 <     * 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
205 >     * take removes existing elements until empty, then blocks interruptibly
206       */
207      public void testBlockingTake() throws InterruptedException {
208 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
209 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
210 <            void realRun() throws InterruptedException {
208 >        final BlockingQueue q = populatedQueue(SIZE);
209 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
210 >        Thread t = newStartedThread(new CheckedRunnable() {
211 >            public void realRun() throws InterruptedException {
212                  for (int i = 0; i < SIZE; ++i) {
213 <                    threadAssertEquals(i, (int) q.take());
213 >                    assertEquals(i, q.take());
214                  }
215 <                q.take();
215 >
216 >                Thread.currentThread().interrupt();
217 >                try {
218 >                    q.take();
219 >                    shouldThrow();
220 >                } catch (InterruptedException success) {}
221 >                assertFalse(Thread.interrupted());
222 >
223 >                pleaseInterrupt.countDown();
224 >                try {
225 >                    q.take();
226 >                    shouldThrow();
227 >                } catch (InterruptedException success) {}
228 >                assertFalse(Thread.interrupted());
229              }});
230 <        Thread.sleep(SMALL_DELAY_MS);
230 >
231 >        await(pleaseInterrupt);
232 >        assertThreadStaysAlive(t);
233          t.interrupt();
234 <        t.join();
309 <        checkEmpty(q);
234 >        awaitTermination(t);
235      }
236  
237      /**
# Line 322 | Line 247 | public class LinkedTransferQueueTest ext
247      }
248  
249      /**
250 <     * timed pool with zero timeout succeeds when non-empty, else times out
250 >     * timed poll with zero timeout succeeds when non-empty, else times out
251       */
252      public void testTimedPoll0() throws InterruptedException {
253          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
# Line 334 | Line 259 | public class LinkedTransferQueueTest ext
259      }
260  
261      /**
262 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
262 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
263       */
264      public void testTimedPoll() throws InterruptedException {
265          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
266 <        for (int i = 0; i < SIZE; ++i) {
267 <            long t0 = System.nanoTime();
266 >        long startTime = System.nanoTime();
267 >        for (int i = 0; i < SIZE; ++i)
268              assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
269 <            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
270 <            assertTrue(millisElapsed < SMALL_DELAY_MS);
271 <        }
272 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
269 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
270 >
271 >        startTime = System.nanoTime();
272 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
273 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
274          checkEmpty(q);
275      }
276  
# Line 353 | Line 279 | public class LinkedTransferQueueTest ext
279       * returning timeout status
280       */
281      public void testInterruptedTimedPoll() throws InterruptedException {
282 <        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
283 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
284 <            void realRun() throws InterruptedException {
285 <                for (int i = 0; i < SIZE; ++i) {
286 <                    long t0 = System.nanoTime();
287 <                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
288 <                                                       MILLISECONDS));
289 <                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
290 <                    assertTrue(millisElapsed < SMALL_DELAY_MS);
291 <                }
292 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
282 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
283 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
284 >        Thread t = newStartedThread(new CheckedRunnable() {
285 >            public void realRun() throws InterruptedException {
286 >                long startTime = System.nanoTime();
287 >                for (int i = 0; i < SIZE; ++i)
288 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
289 >                aboutToWait.countDown();
290 >                try {
291 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
292 >                    shouldThrow();
293 >                } catch (InterruptedException success) {}
294 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
295              }});
296 <        Thread.sleep(SMALL_DELAY_MS);
296 >
297 >        aboutToWait.await();
298 >        waitForThreadToEnterWaitState(t);
299          t.interrupt();
300 <        t.join();
300 >        awaitTermination(t);
301          checkEmpty(q);
302      }
303  
304      /**
305 <     * timed poll before a delayed offer fails; after offer succeeds;
306 <     * on interruption throws
305 >     * timed poll after thread interrupted throws InterruptedException
306 >     * instead of returning timeout status
307       */
308 <    public void testTimedPollWithOffer() throws InterruptedException {
309 <        final LinkedTransferQueue q = new LinkedTransferQueue();
310 <        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
311 <            void realRun() throws InterruptedException {
312 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
313 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
314 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
308 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
309 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
310 >        Thread t = newStartedThread(new CheckedRunnable() {
311 >            public void realRun() throws InterruptedException {
312 >                long startTime = System.nanoTime();
313 >                Thread.currentThread().interrupt();
314 >                for (int i = 0; i < SIZE; ++i)
315 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
316 >                try {
317 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
318 >                    shouldThrow();
319 >                } catch (InterruptedException success) {}
320 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
321              }});
322 <        Thread.sleep(SMALL_DELAY_MS);
323 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
324 <        t.interrupt();
389 <        t.join();
322 >
323 >        awaitTermination(t);
324 >        checkEmpty(q);
325      }
326  
327      /**
# Line 416 | Line 351 | public class LinkedTransferQueueTest ext
351          try {
352              q.element();
353              shouldThrow();
354 <        } catch (NoSuchElementException success) {
420 <        }
354 >        } catch (NoSuchElementException success) {}
355          checkEmpty(q);
356      }
357  
# Line 432 | Line 366 | public class LinkedTransferQueueTest ext
366          try {
367              q.remove();
368              shouldThrow();
369 <        } 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 <        }
369 >        } catch (NoSuchElementException success) {}
370          checkEmpty(q);
371      }
372  
# Line 462 | Line 380 | public class LinkedTransferQueueTest ext
380          assertTrue(q.remove(one));
381          assertTrue(q.remove(two));
382          assertTrue(q.add(three));
383 <        assertTrue(q.take() == three);
383 >        assertSame(q.take(), three);
384      }
385  
386      /**
# Line 544 | Line 462 | public class LinkedTransferQueueTest ext
462      }
463  
464      /**
465 <     * toArray() contains all elements
465 >     * toArray() contains all elements in FIFO order
466       */
467 <    public void testToArray() throws InterruptedException {
467 >    public void testToArray() {
468          LinkedTransferQueue q = populatedQueue(SIZE);
469          Object[] o = q.toArray();
470          for (int i = 0; i < o.length; i++) {
471 <            assertEquals(o[i], q.take());
471 >            assertSame(o[i], q.poll());
472          }
473      }
474  
475      /**
476 <     * toArray(a) contains all elements
476 >     * toArray(a) contains all elements in FIFO order
477       */
478 <    public void testToArray2() throws InterruptedException {
478 >    public void testToArray2() {
479          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
480          Integer[] ints = new Integer[SIZE];
481 <        ints = q.toArray(ints);
481 >        Integer[] array = q.toArray(ints);
482 >        assertSame(ints, array);
483          for (int i = 0; i < ints.length; i++) {
484 <            assertEquals(ints[i], q.take());
484 >            assertSame(ints[i], q.poll());
485          }
486      }
487  
488      /**
489 <     * 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
489 >     * toArray(incompatible array type) throws ArrayStoreException
490       */
491      public void testToArray1_BadArg() {
492 +        LinkedTransferQueue q = populatedQueue(SIZE);
493          try {
494 <            LinkedTransferQueue q = populatedQueue(SIZE);
587 <            Object o[] = q.toArray(new String[10]);
494 >            q.toArray(new String[10]);
495              shouldThrow();
496 <        } catch (ArrayStoreException success) {
590 <        }
496 >        } catch (ArrayStoreException success) {}
497      }
498  
499      /**
# Line 596 | Line 502 | public class LinkedTransferQueueTest ext
502      public void testIterator() throws InterruptedException {
503          LinkedTransferQueue q = populatedQueue(SIZE);
504          Iterator it = q.iterator();
505 <        int i = 0;
506 <        while (it.hasNext()) {
507 <            assertEquals(it.next(), i++);
508 <        }
505 >        int i;
506 >        for (i = 0; it.hasNext(); i++)
507 >            assertTrue(q.contains(it.next()));
508 >        assertEquals(i, SIZE);
509 >        assertIteratorExhausted(it);
510 >
511 >        it = q.iterator();
512 >        for (i = 0; it.hasNext(); i++)
513 >            assertEquals(it.next(), q.take());
514          assertEquals(i, SIZE);
515 +        assertIteratorExhausted(it);
516 +    }
517 +
518 +    /**
519 +     * iterator of empty collection has no elements
520 +     */
521 +    public void testEmptyIterator() {
522 +        assertIteratorExhausted(new LinkedTransferQueue().iterator());
523      }
524  
525      /**
# Line 617 | Line 536 | public class LinkedTransferQueueTest ext
536          it.remove();
537  
538          it = q.iterator();
539 <        assertEquals(it.next(), one);
540 <        assertEquals(it.next(), three);
539 >        assertSame(it.next(), one);
540 >        assertSame(it.next(), three);
541          assertFalse(it.hasNext());
542      }
543  
# Line 662 | Line 581 | public class LinkedTransferQueueTest ext
581          LinkedTransferQueue q = populatedQueue(SIZE);
582          String s = q.toString();
583          for (int i = 0; i < SIZE; ++i) {
584 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
584 >            assertTrue(s.contains(String.valueOf(i)));
585          }
586      }
587  
# Line 671 | Line 590 | public class LinkedTransferQueueTest ext
590       */
591      public void testOfferInExecutor() {
592          final LinkedTransferQueue q = new LinkedTransferQueue();
593 <        q.add(one);
594 <        q.add(two);
595 <        ExecutorService executor = Executors.newFixedThreadPool(2);
596 <
597 <        executor.execute(new CheckedRunnable() {
598 <            void realRun() {
599 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
600 <                                         MILLISECONDS));
601 <            }});
602 <
603 <        executor.execute(new CheckedRunnable() {
685 <            void realRun() throws InterruptedException {
686 <                Thread.sleep(SMALL_DELAY_MS);
687 <                threadAssertEquals(one, q.take());
688 <            }});
593 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
594 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
595 >        try (PoolCleaner cleaner = cleaner(executor)) {
596 >
597 >            executor.execute(new CheckedRunnable() {
598 >                public void realRun() throws InterruptedException {
599 >                    threadsStarted.await();
600 >                    long startTime = System.nanoTime();
601 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
602 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
603 >                }});
604  
605 <        joinPool(executor);
605 >            executor.execute(new CheckedRunnable() {
606 >                public void realRun() throws InterruptedException {
607 >                    threadsStarted.await();
608 >                    assertSame(one, q.take());
609 >                    checkEmpty(q);
610 >                }});
611 >        }
612      }
613  
614      /**
# Line 695 | Line 616 | public class LinkedTransferQueueTest ext
616       */
617      public void testPollInExecutor() {
618          final LinkedTransferQueue q = new LinkedTransferQueue();
619 <        ExecutorService executor = Executors.newFixedThreadPool(2);
620 <
621 <        executor.execute(new CheckedRunnable() {
622 <            void realRun() throws InterruptedException {
623 <                threadAssertNull(q.poll());
624 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
625 <                                                MILLISECONDS));
626 <                threadAssertTrue(q.isEmpty());
627 <            }});
628 <
629 <        executor.execute(new CheckedRunnable() {
630 <            void realRun() throws InterruptedException {
631 <                Thread.sleep(SMALL_DELAY_MS);
711 <                q.put(one);
712 <            }});
619 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
620 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
621 >        try (PoolCleaner cleaner = cleaner(executor)) {
622 >
623 >            executor.execute(new CheckedRunnable() {
624 >                public void realRun() throws InterruptedException {
625 >                    assertNull(q.poll());
626 >                    threadsStarted.await();
627 >                    long startTime = System.nanoTime();
628 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
629 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
630 >                    checkEmpty(q);
631 >                }});
632  
633 <        joinPool(executor);
633 >            executor.execute(new CheckedRunnable() {
634 >                public void realRun() throws InterruptedException {
635 >                    threadsStarted.await();
636 >                    q.put(one);
637 >                }});
638 >        }
639      }
640  
641      /**
642       * A deserialized serialized queue has same elements in same order
643       */
644      public void testSerialization() throws Exception {
645 <        LinkedTransferQueue q = populatedQueue(SIZE);
646 <
723 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
724 <        ObjectOutputStream out
725 <            = new ObjectOutputStream(new BufferedOutputStream(bout));
726 <        out.writeObject(q);
727 <        out.close();
645 >        Queue x = populatedQueue(SIZE);
646 >        Queue y = serialClone(x);
647  
648 <        ByteArrayInputStream bin
649 <            = new ByteArrayInputStream(bout.toByteArray());
650 <        ObjectInputStream in
651 <            = new ObjectInputStream(new BufferedInputStream(bin));
652 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
653 <
654 <        assertEquals(q.size(), r.size());
736 <        while (!q.isEmpty()) {
737 <            assertEquals(q.remove(), r.remove());
738 <        }
739 <    }
740 <
741 <    /**
742 <     * drainTo(null) throws NullPointerException
743 <     */
744 <    public void testDrainToNull() {
745 <        LinkedTransferQueue q = populatedQueue(SIZE);
746 <        try {
747 <            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) {
648 >        assertNotSame(y, x);
649 >        assertEquals(x.size(), y.size());
650 >        assertEquals(x.toString(), y.toString());
651 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
652 >        while (!x.isEmpty()) {
653 >            assertFalse(y.isEmpty());
654 >            assertEquals(x.remove(), y.remove());
655          }
656 +        assertTrue(y.isEmpty());
657      }
658  
659      /**
# Line 769 | Line 663 | public class LinkedTransferQueueTest ext
663          LinkedTransferQueue q = populatedQueue(SIZE);
664          ArrayList l = new ArrayList();
665          q.drainTo(l);
666 <        assertEquals(q.size(), 0);
667 <        assertEquals(l.size(), SIZE);
666 >        assertEquals(0, q.size());
667 >        assertEquals(SIZE, l.size());
668          for (int i = 0; i < SIZE; ++i) {
669 <            assertEquals(l.get(i), i);
669 >            assertEquals(i, l.get(i));
670          }
671          q.add(zero);
672          q.add(one);
# Line 781 | Line 675 | public class LinkedTransferQueueTest ext
675          assertTrue(q.contains(one));
676          l.clear();
677          q.drainTo(l);
678 <        assertEquals(q.size(), 0);
679 <        assertEquals(l.size(), 2);
678 >        assertEquals(0, q.size());
679 >        assertEquals(2, l.size());
680          for (int i = 0; i < 2; ++i) {
681 <            assertEquals(l.get(i), i);
681 >            assertEquals(i, l.get(i));
682          }
683      }
684  
# Line 794 | Line 688 | public class LinkedTransferQueueTest ext
688      public void testDrainToWithActivePut() throws InterruptedException {
689          final LinkedTransferQueue q = populatedQueue(SIZE);
690          Thread t = newStartedThread(new CheckedRunnable() {
691 <            void realRun() {
691 >            public void realRun() {
692                  q.put(SIZE + 1);
693              }});
694          ArrayList l = new ArrayList();
695          q.drainTo(l);
696          assertTrue(l.size() >= SIZE);
697 <        for (int i = 0; i < SIZE; ++i) {
698 <            assertEquals(l.get(i), i);
699 <        }
806 <        t.join();
697 >        for (int i = 0; i < SIZE; ++i)
698 >            assertEquals(i, l.get(i));
699 >        awaitTermination(t);
700          assertTrue(q.size() + l.size() >= SIZE);
701      }
702  
703      /**
704 <     * 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
704 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
705       */
706      public void testDrainToN() {
707          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 843 | Line 712 | public class LinkedTransferQueueTest ext
712              ArrayList l = new ArrayList();
713              q.drainTo(l, i);
714              int k = (i < SIZE) ? i : SIZE;
715 <            assertEquals(l.size(), k);
716 <            assertEquals(q.size(), SIZE - k);
717 <            for (int j = 0; j < k; ++j) {
718 <                assertEquals(l.get(j), j);
719 <            }
851 <            while (q.poll() != null)
852 <                ;
715 >            assertEquals(k, l.size());
716 >            assertEquals(SIZE - k, q.size());
717 >            for (int j = 0; j < k; ++j)
718 >                assertEquals(j, l.get(j));
719 >            do {} while (q.poll() != null);
720          }
721      }
722  
# Line 859 | Line 726 | public class LinkedTransferQueueTest ext
726       */
727      public void testWaitingConsumer() throws InterruptedException {
728          final LinkedTransferQueue q = new LinkedTransferQueue();
729 <        assertEquals(q.getWaitingConsumerCount(), 0);
729 >        assertEquals(0, q.getWaitingConsumerCount());
730          assertFalse(q.hasWaitingConsumer());
731 +        final CountDownLatch threadStarted = new CountDownLatch(1);
732  
733          Thread t = newStartedThread(new CheckedRunnable() {
734 <            void realRun() throws InterruptedException {
735 <                Thread.sleep(SMALL_DELAY_MS);
736 <                threadAssertTrue(q.hasWaitingConsumer());
737 <                threadAssertEquals(q.getWaitingConsumerCount(), 1);
738 <                threadAssertTrue(q.offer(new Object()));
739 <                threadAssertFalse(q.hasWaitingConsumer());
740 <                threadAssertEquals(q.getWaitingConsumerCount(), 0);
734 >            public void realRun() throws InterruptedException {
735 >                threadStarted.countDown();
736 >                long startTime = System.nanoTime();
737 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
738 >                assertEquals(0, q.getWaitingConsumerCount());
739 >                assertFalse(q.hasWaitingConsumer());
740 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
741              }});
742  
743 <        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
744 <        assertEquals(q.getWaitingConsumerCount(), 0);
743 >        threadStarted.await();
744 >        waitForThreadToEnterWaitState(t);
745 >        assertEquals(1, q.getWaitingConsumerCount());
746 >        assertTrue(q.hasWaitingConsumer());
747 >
748 >        assertTrue(q.offer(one));
749 >        assertEquals(0, q.getWaitingConsumerCount());
750          assertFalse(q.hasWaitingConsumer());
751 <        t.join();
751 >
752 >        awaitTermination(t);
753      }
754  
755      /**
# Line 886 | Line 760 | public class LinkedTransferQueueTest ext
760              LinkedTransferQueue q = new LinkedTransferQueue();
761              q.transfer(null);
762              shouldThrow();
763 <        } catch (NullPointerException ex) {
890 <        }
763 >        } catch (NullPointerException success) {}
764      }
765  
766      /**
# Line 897 | Line 770 | public class LinkedTransferQueueTest ext
770      public void testTransfer2() throws InterruptedException {
771          final LinkedTransferQueue<Integer> q
772              = new LinkedTransferQueue<Integer>();
773 +        final CountDownLatch threadStarted = new CountDownLatch(1);
774  
775          Thread t = newStartedThread(new CheckedRunnable() {
776 <            void realRun() throws InterruptedException {
777 <                q.transfer(SIZE);
778 <                threadAssertTrue(q.isEmpty());
776 >            public void realRun() throws InterruptedException {
777 >                threadStarted.countDown();
778 >                q.transfer(five);
779 >                checkEmpty(q);
780              }});
781  
782 <        Thread.sleep(SHORT_DELAY_MS);
782 >        threadStarted.await();
783 >        waitForThreadToEnterWaitState(t);
784          assertEquals(1, q.size());
785 <        assertEquals(SIZE, (int) q.poll());
786 <        assertTrue(q.isEmpty());
787 <        t.join();
785 >        assertSame(five, q.poll());
786 >        checkEmpty(q);
787 >        awaitTermination(t);
788      }
789  
790      /**
# Line 919 | Line 795 | public class LinkedTransferQueueTest ext
795              = new LinkedTransferQueue<Integer>();
796  
797          Thread first = newStartedThread(new CheckedRunnable() {
798 <            void realRun() throws InterruptedException {
799 <                Integer i = SIZE + 1;
800 <                q.transfer(i);
801 <                threadAssertTrue(!q.contains(i));
926 <                threadAssertEquals(1, q.size());
798 >            public void realRun() throws InterruptedException {
799 >                q.transfer(four);
800 >                assertTrue(!q.contains(four));
801 >                assertEquals(1, q.size());
802              }});
803  
804          Thread interruptedThread = newStartedThread(
805              new CheckedInterruptedRunnable() {
806 <                void realRun() throws InterruptedException {
807 <                    while (q.size() == 0)
806 >                public void realRun() throws InterruptedException {
807 >                    while (q.isEmpty())
808                          Thread.yield();
809 <                    q.transfer(SIZE);
809 >                    q.transfer(five);
810                  }});
811  
812          while (q.size() < 2)
813              Thread.yield();
814          assertEquals(2, q.size());
815 <        assertEquals(SIZE + 1, (int) q.poll());
815 >        assertSame(four, q.poll());
816          first.join();
817          assertEquals(1, q.size());
818          interruptedThread.interrupt();
819          interruptedThread.join();
820 <        assertEquals(0, q.size());
946 <        assertTrue(q.isEmpty());
820 >        checkEmpty(q);
821      }
822  
823      /**
# Line 954 | Line 828 | public class LinkedTransferQueueTest ext
828          final LinkedTransferQueue q = new LinkedTransferQueue();
829  
830          Thread t = newStartedThread(new CheckedRunnable() {
831 <            void realRun() throws InterruptedException {
831 >            public void realRun() throws InterruptedException {
832                  q.transfer(four);
833 <                threadAssertFalse(q.contains(four));
834 <                threadAssertEquals(three, q.poll());
833 >                assertFalse(q.contains(four));
834 >                assertSame(three, q.poll());
835              }});
836  
837 <        Thread.sleep(SHORT_DELAY_MS);
837 >        while (q.isEmpty())
838 >            Thread.yield();
839 >        assertFalse(q.isEmpty());
840 >        assertEquals(1, q.size());
841          assertTrue(q.offer(three));
842 <        assertEquals(four, q.poll());
843 <        t.join();
842 >        assertSame(four, q.poll());
843 >        awaitTermination(t);
844      }
845  
846      /**
# Line 975 | Line 852 | public class LinkedTransferQueueTest ext
852              = new LinkedTransferQueue<Integer>();
853  
854          Thread t = newStartedThread(new CheckedRunnable() {
855 <            void realRun() throws InterruptedException {
856 <                q.transfer(SIZE);
855 >            public void realRun() throws InterruptedException {
856 >                q.transfer(four);
857                  checkEmpty(q);
858              }});
859  
860 <        Thread.sleep(SHORT_DELAY_MS);
861 <        assertEquals(SIZE, (int) q.take());
860 >        while (q.isEmpty())
861 >            Thread.yield();
862 >        assertFalse(q.isEmpty());
863 >        assertEquals(1, q.size());
864 >        assertSame(four, q.take());
865          checkEmpty(q);
866 <        t.join();
866 >        awaitTermination(t);
867      }
868  
869      /**
870       * tryTransfer(null) throws NullPointerException
871       */
872      public void testTryTransfer1() {
873 +        final LinkedTransferQueue q = new LinkedTransferQueue();
874          try {
994            final LinkedTransferQueue q = new LinkedTransferQueue();
875              q.tryTransfer(null);
876              shouldThrow();
877 <        } catch (NullPointerException ex) {
998 <        }
877 >        } catch (NullPointerException success) {}
878      }
879  
880      /**
# Line 1018 | Line 897 | public class LinkedTransferQueueTest ext
897          final LinkedTransferQueue q = new LinkedTransferQueue();
898  
899          Thread t = newStartedThread(new CheckedRunnable() {
900 <            void realRun() {
900 >            public void realRun() {
901                  while (! q.hasWaitingConsumer())
902                      Thread.yield();
903 <                threadAssertTrue(q.hasWaitingConsumer());
904 <                threadAssertTrue(q.isEmpty());
905 <                threadAssertTrue(q.size() == 0);
1027 <                threadAssertTrue(q.tryTransfer(hotPotato));
903 >                assertTrue(q.hasWaitingConsumer());
904 >                checkEmpty(q);
905 >                assertTrue(q.tryTransfer(hotPotato));
906              }});
907  
908 <        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
908 >        long startTime = System.nanoTime();
909 >        assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
910 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
911          checkEmpty(q);
912 <        t.join();
912 >        awaitTermination(t);
913      }
914  
915      /**
# Line 1041 | Line 921 | public class LinkedTransferQueueTest ext
921          final LinkedTransferQueue q = new LinkedTransferQueue();
922  
923          Thread t = newStartedThread(new CheckedRunnable() {
924 <            void realRun() {
924 >            public void realRun() {
925                  while (! q.hasWaitingConsumer())
926                      Thread.yield();
927 <                threadAssertTrue(q.hasWaitingConsumer());
928 <                threadAssertTrue(q.isEmpty());
929 <                threadAssertTrue(q.size() == 0);
1050 <                threadAssertTrue(q.tryTransfer(hotPotato));
927 >                assertTrue(q.hasWaitingConsumer());
928 >                checkEmpty(q);
929 >                assertTrue(q.tryTransfer(hotPotato));
930              }});
931  
932 <        assertTrue(q.take() == hotPotato);
932 >        assertSame(q.take(), hotPotato);
933          checkEmpty(q);
934 <        t.join();
934 >        awaitTermination(t);
935      }
936  
937      /**
938 <     * tryTransfer waits the amount given if interrupted, and
1060 <     * throws interrupted exception
938 >     * tryTransfer blocks interruptibly if no takers
939       */
940      public void testTryTransfer5() throws InterruptedException {
941          final LinkedTransferQueue q = new LinkedTransferQueue();
942 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
943 +        assertTrue(q.isEmpty());
944  
945 <        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
946 <            void realRun() throws InterruptedException {
947 <                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
945 >        Thread t = newStartedThread(new CheckedRunnable() {
946 >            public void realRun() throws InterruptedException {
947 >                long startTime = System.nanoTime();
948 >                Thread.currentThread().interrupt();
949 >                try {
950 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
951 >                    shouldThrow();
952 >                } catch (InterruptedException success) {}
953 >                assertFalse(Thread.interrupted());
954 >
955 >                pleaseInterrupt.countDown();
956 >                try {
957 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
958 >                    shouldThrow();
959 >                } catch (InterruptedException success) {}
960 >                assertFalse(Thread.interrupted());
961 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
962              }});
963  
964 <        Thread.sleep(SMALL_DELAY_MS);
965 <        toInterrupt.interrupt();
966 <        toInterrupt.join();
964 >        await(pleaseInterrupt);
965 >        assertThreadStaysAlive(t);
966 >        t.interrupt();
967 >        awaitTermination(t);
968 >        checkEmpty(q);
969      }
970  
971      /**
972 <     * tryTransfer gives up after the timeout and return false
972 >     * tryTransfer gives up after the timeout and returns false
973       */
974      public void testTryTransfer6() throws InterruptedException {
975          final LinkedTransferQueue q = new LinkedTransferQueue();
976  
977          Thread t = newStartedThread(new CheckedRunnable() {
978 <            void realRun() throws InterruptedException {
979 <                threadAssertFalse
980 <                    (q.tryTransfer(new Object(),
981 <                                   SHORT_DELAY_MS, MILLISECONDS));
978 >            public void realRun() throws InterruptedException {
979 >                long startTime = System.nanoTime();
980 >                assertFalse(q.tryTransfer(new Object(),
981 >                                          timeoutMillis(), MILLISECONDS));
982 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
983 >                checkEmpty(q);
984              }});
985  
986 <        Thread.sleep(SMALL_DELAY_MS);
986 >        awaitTermination(t);
987          checkEmpty(q);
1090        t.join();
988      }
989  
990      /**
# Line 1099 | Line 996 | public class LinkedTransferQueueTest ext
996          assertTrue(q.offer(four));
997  
998          Thread t = newStartedThread(new CheckedRunnable() {
999 <            void realRun() throws InterruptedException {
1000 <                threadAssertTrue(q.tryTransfer(five,
1001 <                                               MEDIUM_DELAY_MS, MILLISECONDS));
1002 <                threadAssertTrue(q.isEmpty());
999 >            public void realRun() throws InterruptedException {
1000 >                long startTime = System.nanoTime();
1001 >                assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
1002 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1003 >                checkEmpty(q);
1004              }});
1005  
1006 <        Thread.sleep(SHORT_DELAY_MS);
1006 >        while (q.size() != 2)
1007 >            Thread.yield();
1008          assertEquals(2, q.size());
1009 <        assertEquals(four, q.poll());
1010 <        assertEquals(five, q.poll());
1009 >        assertSame(four, q.poll());
1010 >        assertSame(five, q.poll());
1011          checkEmpty(q);
1012 <        t.join();
1012 >        awaitTermination(t);
1013      }
1014  
1015      /**
1016 <     * tryTransfer attempts to enqueue into the q and fails returning
1017 <     * false not enqueueing and the successive poll is null
1016 >     * tryTransfer attempts to enqueue into the queue and fails
1017 >     * returning false not enqueueing and the successive poll is null
1018       */
1019      public void testTryTransfer8() throws InterruptedException {
1020          final LinkedTransferQueue q = new LinkedTransferQueue();
1021          assertTrue(q.offer(four));
1022          assertEquals(1, q.size());
1023 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1023 >        long startTime = System.nanoTime();
1024 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1025 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1026          assertEquals(1, q.size());
1027 <        assertEquals(four, q.poll());
1027 >        assertSame(four, q.poll());
1028          assertNull(q.poll());
1029          checkEmpty(q);
1030      }
1031  
1032      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1033          LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1034 <        assertTrue(q.isEmpty());
1034 >        checkEmpty(q);
1035          for (int i = 0; i < n; i++) {
1036              assertEquals(i, q.size());
1037              assertTrue(q.offer(i));
# Line 1139 | Line 1040 | public class LinkedTransferQueueTest ext
1040          assertFalse(q.isEmpty());
1041          return q;
1042      }
1043 +
1044 +    /**
1045 +     * remove(null), contains(null) always return false
1046 +     */
1047 +    public void testNeverContainsNull() {
1048 +        Collection<?>[] qs = {
1049 +            new LinkedTransferQueue<Object>(),
1050 +            populatedQueue(2),
1051 +        };
1052 +
1053 +        for (Collection<?> q : qs) {
1054 +            assertFalse(q.contains(null));
1055 +            assertFalse(q.remove(null));
1056 +        }
1057 +    }
1058   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines