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.5 by jsr166, Sun Aug 2 08:17:31 2009 UTC vs.
Revision 1.84 by jsr166, Fri Sep 6 22:43:50 2019 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.ConcurrentModificationException;
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.*;
16 > import java.util.Queue;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.LinkedTransferQueue;
23 >
24   import junit.framework.Test;
21 import junit.framework.TestSuite;
25  
26 + @SuppressWarnings({"unchecked", "rawtypes"})
27   public class LinkedTransferQueueTest extends JSR166TestCase {
28 +    public static class Generic extends BlockingQueueTest {
29 +        protected BlockingQueue emptyCollection() {
30 +            return new LinkedTransferQueue();
31 +        }
32 +    }
33  
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run(suite());
35 >        main(suite(), args);
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(LinkedTransferQueueTest.class);
39 >        class Implementation implements CollectionImplementation {
40 >            public Class<?> klazz() { return LinkedTransferQueue.class; }
41 >            public Collection emptyCollection() { return new LinkedTransferQueue(); }
42 >            public Object makeElement(int i) { return i; }
43 >            public boolean isConcurrent() { return true; }
44 >            public boolean permitsNulls() { return false; }
45 >        }
46 >        return newTestSuite(LinkedTransferQueueTest.class,
47 >                            new Generic().testSuite(),
48 >                            CollectionTest.testSuite(new Implementation()));
49      }
50  
51      /**
52 <     * Constructor builds new queue with size being zero and empty being true
52 >     * Constructor builds new queue with size being zero and empty
53 >     * being true
54       */
55      public void testConstructor1() {
56          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 58 | public class LinkedTransferQueueTest ext
58      }
59  
60      /**
61 <     * Initializing constructor with null collection throws NullPointerException
61 >     * Initializing constructor with null collection throws
62 >     * NullPointerException
63       */
64      public void testConstructor2() {
65          try {
66              new LinkedTransferQueue(null);
67              shouldThrow();
68 <        } catch (NullPointerException success) {
49 <        }
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
72 <     * Initializing from Collection of null elements throws NullPointerException
72 >     * Initializing from Collection of null elements throws
73 >     * NullPointerException
74       */
75      public void testConstructor3() {
76 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
77          try {
78 <            Integer[] ints = new Integer[SIZE];
58 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(elements);
79              shouldThrow();
80 <        } catch (NullPointerException success) {
61 <        }
80 >        } catch (NullPointerException success) {}
81      }
82  
83      /**
# Line 66 | 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];
71 <            for (int i = 0; i < SIZE - 1; ++i) {
72 <                ints[i] = i;
73 <            }
74 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
93 >            new LinkedTransferQueue(elements);
94              shouldThrow();
95 <        } catch (NullPointerException success) {
77 <        }
95 >        } catch (NullPointerException success) {}
96      }
97  
98      /**
99       * Queue contains all elements of the collection it is initialized by
100       */
101      public void testConstructor5() {
102 <        try {
103 <            Integer[] ints = new Integer[SIZE];
104 <            for (int i = 0; i < SIZE; ++i) {
105 <                ints[i] = i;
106 <            }
107 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
108 <            for (int i = 0; i < SIZE; ++i) {
109 <                assertEquals(ints[i], q.poll());
110 <            }
111 <        } finally {
102 >        Integer[] ints = new Integer[SIZE];
103 >        for (int i = 0; i < SIZE; ++i) {
104 >            ints[i] = i;
105 >        }
106 >        List intList = Arrays.asList(ints);
107 >        LinkedTransferQueue q
108 >            = new LinkedTransferQueue(intList);
109 >        assertEquals(q.size(), intList.size());
110 >        assertEquals(q.toString(), intList.toString());
111 >        assertTrue(Arrays.equals(q.toArray(),
112 >                                     intList.toArray()));
113 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
114 >                                 intList.toArray(new Object[0])));
115 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
116 >                                 intList.toArray(new Object[SIZE])));
117 >        for (int i = 0; i < SIZE; ++i) {
118 >            assertEquals(ints[i], q.poll());
119          }
120      }
121  
122      /**
123 <     * Remaining capacity never decrease nor increase on add or remove
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);
111 <        }
112 <    }
113 <
114 <    /**
115 <     * offer(null) throws NullPointerException
116 <     */
117 <    public void testOfferNull() {
118 <        try {
119 <            LinkedTransferQueue q = new LinkedTransferQueue();
120 <            q.offer(null);
121 <            shouldThrow();
122 <        } catch (NullPointerException success) {
123 <        }
124 <    }
125 <
126 <    /**
127 <     * add(null) throws NullPointerException
128 <     */
129 <    public void testAddNull() {
130 <        try {
131 <            LinkedTransferQueue q = new LinkedTransferQueue();
132 <            q.add(null);
133 <            shouldThrow();
134 <        } catch (NullPointerException success) {
135 <        }
136 <    }
137 <
138 <    /**
139 <     * addAll(null) throws NullPointerException
140 <     */
141 <    public void testAddAll1() {
142 <        try {
143 <            LinkedTransferQueue q = new LinkedTransferQueue();
144 <            q.addAll(null);
145 <            shouldThrow();
146 <        } catch (NullPointerException success) {
135 >            assertTrue(q.add(i));
136          }
137      }
138  
# Line 151 | Line 140 | public class LinkedTransferQueueTest ext
140       * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
155            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147 <        } catch (IllegalArgumentException success) {
159 <        }
160 <    }
161 <
162 <    /**
163 <     * addAll of a collection with null elements throws NullPointerException
164 <     */
165 <    public void testAddAll2() {
166 <        try {
167 <            LinkedTransferQueue q = new LinkedTransferQueue();
168 <            Integer[] ints = new Integer[SIZE];
169 <            q.addAll(Arrays.asList(ints));
170 <            shouldThrow();
171 <        } catch (NullPointerException success) {
172 <        }
147 >        } catch (IllegalArgumentException success) {}
148      }
149  
150      /**
151 <     * addAll of a collection with any null elements throws NullPointerException after
152 <     * possibly adding some elements
151 >     * addAll of a collection with any null elements throws
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 {
181            LinkedTransferQueue q = new LinkedTransferQueue();
182            Integer[] ints = new Integer[SIZE];
183            for (int i = 0; i < SIZE - 1; ++i) {
184                ints[i] = i;
185            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162 <        } catch (NullPointerException success) {
189 <        }
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 207 | Line 180 | public class LinkedTransferQueueTest ext
180      }
181  
182      /**
210     * put(null) throws NullPointerException
211     */
212    public void testPutNull() throws InterruptedException {
213        try {
214            LinkedTransferQueue q = new LinkedTransferQueue();
215            q.put(null);
216            shouldThrow();
217        } catch (NullPointerException success) {}
218    }
219
220    /**
183       * all elements successfully put are contained
184       */
185      public void testPut() {
186 <        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
186 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
187          for (int i = 0; i < SIZE; ++i) {
188 +            assertEquals(i, q.size());
189              q.put(i);
190              assertTrue(q.contains(i));
191          }
229        assertEquals(q.size(), SIZE);
192      }
193  
194      /**
# Line 240 | Line 202 | public class LinkedTransferQueueTest ext
202      }
203  
204      /**
205 <     * take blocks interruptibly when empty
205 >     * take removes existing elements until empty, then blocks interruptibly
206       */
207 <    public void testTakeFromEmpty() throws InterruptedException {
208 <        final LinkedTransferQueue q = new LinkedTransferQueue();
209 <        Thread t = new Thread(new Runnable() {
210 <            public void run() {
207 >    public void testBlockingTake() 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++) assertEquals(i, q.take());
213 >
214 >                Thread.currentThread().interrupt();
215                  try {
216                      q.take();
217 <                    threadShouldThrow();
218 <                } catch (InterruptedException success) {
219 <                } catch (Throwable ex) {
254 <                    threadUnexpectedException(ex);
255 <                }
256 <            }});
257 <        t.start();
258 <        Thread.sleep(SHORT_DELAY_MS);
259 <        t.interrupt();
260 <        t.join();
261 <    }
217 >                    shouldThrow();
218 >                } catch (InterruptedException success) {}
219 >                assertFalse(Thread.interrupted());
220  
221 <    /**
264 <     * Take removes existing elements until empty, then blocks interruptibly
265 <     */
266 <    public void testBlockingTake() throws InterruptedException {
267 <        Thread t = new Thread(new Runnable() {
268 <            public void run() {
221 >                pleaseInterrupt.countDown();
222                  try {
270                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
271                    for (int i = 0; i < SIZE; ++i) {
272                        threadAssertEquals(i, (int) q.take());
273                    }
223                      q.take();
224 <                    threadShouldThrow();
225 <                } catch (InterruptedException success) {
226 <                } catch (Throwable ex) {
278 <                    threadUnexpectedException(ex);
279 <                }
224 >                    shouldThrow();
225 >                } catch (InterruptedException success) {}
226 >                assertFalse(Thread.interrupted());
227              }});
228 <        t.start();
229 <        Thread.sleep(SHORT_DELAY_MS);
228 >
229 >        await(pleaseInterrupt);
230 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
231          t.interrupt();
232 <        t.join();
232 >        awaitTermination(t);
233      }
234  
235      /**
236       * poll succeeds unless empty
237       */
238 <    public void testPoll() {
238 >    public void testPoll() throws InterruptedException {
239          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
240          for (int i = 0; i < SIZE; ++i) {
241              assertEquals(i, (int) q.poll());
242          }
243          assertNull(q.poll());
244 +        checkEmpty(q);
245      }
246  
247      /**
248 <     * timed pool with zero timeout succeeds when non-empty, else times out
248 >     * timed poll with zero timeout succeeds when non-empty, else times out
249       */
250      public void testTimedPoll0() throws InterruptedException {
251          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
252          for (int i = 0; i < SIZE; ++i) {
253 <            assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS));
253 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
254          }
255 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
255 >        assertNull(q.poll(0, MILLISECONDS));
256 >        checkEmpty(q);
257      }
258  
259      /**
260 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
260 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
261       */
262      public void testTimedPoll() throws InterruptedException {
263          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
264 <        for (int i = 0; i < SIZE; ++i) {
265 <            assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
266 <        }
267 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
264 >        long startTime = System.nanoTime();
265 >        for (int i = 0; i < SIZE; ++i)
266 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
267 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
268 >
269 >        startTime = System.nanoTime();
270 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
271 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
272 >        checkEmpty(q);
273      }
274  
275      /**
# Line 322 | Line 277 | public class LinkedTransferQueueTest ext
277       * returning timeout status
278       */
279      public void testInterruptedTimedPoll() throws InterruptedException {
280 <        Thread t = new Thread(new Runnable() {
281 <            public void run() {
280 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
281 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
282 >        Thread t = newStartedThread(new CheckedRunnable() {
283 >            public void realRun() throws InterruptedException {
284 >                for (int i = 0; i < SIZE; i++)
285 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
286 >
287 >                Thread.currentThread().interrupt();
288                  try {
289 <                    LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
290 <                    for (int i = 0; i < SIZE; ++i) {
291 <                        threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
292 <                    }
293 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
294 <                } catch (InterruptedException success) {
295 <                } catch (Throwable ex) {
296 <                    threadUnexpectedException(ex);
297 <                }
289 >                    q.poll(randomTimeout(), randomTimeUnit());
290 >                    shouldThrow();
291 >                } catch (InterruptedException success) {}
292 >                assertFalse(Thread.interrupted());
293 >
294 >                pleaseInterrupt.countDown();
295 >                try {
296 >                    q.poll(LONGER_DELAY_MS, MILLISECONDS);
297 >                    shouldThrow();
298 >                } catch (InterruptedException success) {}
299 >                assertFalse(Thread.interrupted());
300              }});
301 <        t.start();
302 <        Thread.sleep(SHORT_DELAY_MS);
301 >
302 >        await(pleaseInterrupt);
303 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
304          t.interrupt();
305 <        t.join();
305 >        awaitTermination(t);
306 >        checkEmpty(q);
307      }
308  
309      /**
310 <     * timed poll before a delayed offer fails; after offer succeeds;
311 <     * on interruption throws
310 >     * timed poll after thread interrupted throws InterruptedException
311 >     * instead of returning timeout status
312       */
313 <    public void testTimedPollWithOffer() throws InterruptedException {
314 <        final LinkedTransferQueue q = new LinkedTransferQueue();
315 <        Thread t = new Thread(new Runnable() {
316 <            public void run() {
313 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
314 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
315 >        Thread t = newStartedThread(new CheckedRunnable() {
316 >            public void realRun() throws InterruptedException {
317 >                Thread.currentThread().interrupt();
318 >                for (int i = 0; i < SIZE; ++i)
319 >                    assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit()));
320                  try {
321 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
322 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
323 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
324 <                    threadShouldThrow();
357 <                } catch (InterruptedException success) {
358 <                } catch (Throwable ex) {
359 <                    threadUnexpectedException(ex);
360 <                }
321 >                    q.poll(randomTimeout(), randomTimeUnit());
322 >                    shouldThrow();
323 >                } catch (InterruptedException success) {}
324 >                assertFalse(Thread.interrupted());
325              }});
326 <        t.start();
327 <        Thread.sleep(SMALL_DELAY_MS);
328 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
365 <        t.interrupt();
366 <        t.join();
326 >
327 >        awaitTermination(t);
328 >        checkEmpty(q);
329      }
330  
331      /**
332       * peek returns next element, or null if empty
333       */
334 <    public void testPeek() {
334 >    public void testPeek() throws InterruptedException {
335          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
336          for (int i = 0; i < SIZE; ++i) {
337              assertEquals(i, (int) q.peek());
# Line 378 | Line 340 | public class LinkedTransferQueueTest ext
340                         i != (int) q.peek());
341          }
342          assertNull(q.peek());
343 +        checkEmpty(q);
344      }
345  
346      /**
347       * element returns next element, or throws NoSuchElementException if empty
348       */
349 <    public void testElement() {
349 >    public void testElement() throws InterruptedException {
350          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
351          for (int i = 0; i < SIZE; ++i) {
352              assertEquals(i, (int) q.element());
# Line 392 | Line 355 | public class LinkedTransferQueueTest ext
355          try {
356              q.element();
357              shouldThrow();
358 <        } catch (NoSuchElementException success) {
359 <        }
358 >        } catch (NoSuchElementException success) {}
359 >        checkEmpty(q);
360      }
361  
362      /**
363       * remove removes next element, or throws NoSuchElementException if empty
364       */
365 <    public void testRemove() {
365 >    public void testRemove() throws InterruptedException {
366          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
367          for (int i = 0; i < SIZE; ++i) {
368              assertEquals(i, (int) q.remove());
# Line 407 | Line 370 | public class LinkedTransferQueueTest ext
370          try {
371              q.remove();
372              shouldThrow();
373 <        } catch (NoSuchElementException success) {
374 <        }
412 <    }
413 <
414 <    /**
415 <     * remove(x) removes x and returns true if present
416 <     */
417 <    public void testRemoveElement() {
418 <        LinkedTransferQueue q = populatedQueue(SIZE);
419 <        for (int i = 1; i < SIZE; i += 2) {
420 <            assertTrue(q.remove(i));
421 <        }
422 <        for (int i = 0; i < SIZE; i += 2) {
423 <            assertTrue(q.remove(i));
424 <            assertFalse(q.remove(i + 1));
425 <        }
426 <        assertTrue(q.isEmpty());
373 >        } catch (NoSuchElementException success) {}
374 >        checkEmpty(q);
375      }
376  
377      /**
# Line 436 | Line 384 | public class LinkedTransferQueueTest ext
384          assertTrue(q.remove(one));
385          assertTrue(q.remove(two));
386          assertTrue(q.add(three));
387 <        assertTrue(q.take() != null);
387 >        assertSame(q.take(), three);
388      }
389  
390      /**
# Line 454 | Line 402 | public class LinkedTransferQueueTest ext
402      /**
403       * clear removes all elements
404       */
405 <    public void testClear() {
405 >    public void testClear() throws InterruptedException {
406          LinkedTransferQueue q = populatedQueue(SIZE);
407          q.clear();
408 <        assertTrue(q.isEmpty());
461 <        assertEquals(0, q.size());
408 >        checkEmpty(q);
409          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
410          q.add(one);
411          assertFalse(q.isEmpty());
412 +        assertEquals(1, q.size());
413          assertTrue(q.contains(one));
414          q.clear();
415 <        assertTrue(q.isEmpty());
415 >        checkEmpty(q);
416      }
417  
418      /**
# Line 472 | Line 420 | public class LinkedTransferQueueTest ext
420       */
421      public void testContainsAll() {
422          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
423 <        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
423 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>();
424          for (int i = 0; i < SIZE; ++i) {
425              assertTrue(q.containsAll(p));
426              assertFalse(p.containsAll(q));
# Line 482 | Line 430 | public class LinkedTransferQueueTest ext
430      }
431  
432      /**
433 <     * retainAll(c) retains only those elements of c and reports true if changed
433 >     * retainAll(c) retains only those elements of c and reports true
434 >     * if changed
435       */
436      public void testRetainAll() {
437          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 501 | Line 450 | public class LinkedTransferQueueTest ext
450      }
451  
452      /**
453 <     * removeAll(c) removes only those elements of c and reports true if changed
453 >     * removeAll(c) removes only those elements of c and reports true
454 >     * if changed
455       */
456      public void testRemoveAll() {
457          for (int i = 1; i < SIZE; ++i) {
# Line 516 | Line 466 | public class LinkedTransferQueueTest ext
466      }
467  
468      /**
469 <     * toArray contains all elements
469 >     * toArray() contains all elements in FIFO order
470       */
471 <    public void testToArray() throws InterruptedException {
471 >    public void testToArray() {
472          LinkedTransferQueue q = populatedQueue(SIZE);
473 <        Object[] o = q.toArray();
474 <        for (int i = 0; i < o.length; i++) {
475 <            assertEquals(o[i], q.take());
476 <        }
473 >        Object[] a = q.toArray();
474 >        assertSame(Object[].class, a.getClass());
475 >        for (Object o : a)
476 >            assertSame(o, q.poll());
477 >        assertTrue(q.isEmpty());
478      }
479  
480      /**
481 <     * toArray(a) contains all elements
481 >     * toArray(a) contains all elements in FIFO order
482       */
483 <    public void testToArray2() throws InterruptedException {
483 >    public void testToArray2() {
484          LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
485          Integer[] ints = new Integer[SIZE];
486 <        ints = q.toArray(ints);
487 <        for (int i = 0; i < ints.length; i++) {
488 <            assertEquals(ints[i], q.take());
489 <        }
490 <    }
540 <
541 <    /**
542 <     * toArray(null) throws NullPointerException
543 <     */
544 <    public void testToArray_BadArg() {
545 <        try {
546 <            LinkedTransferQueue q = populatedQueue(SIZE);
547 <            Object o[] = q.toArray(null);
548 <            shouldThrow();
549 <        } catch (NullPointerException success) {
550 <        }
486 >        Integer[] array = q.toArray(ints);
487 >        assertSame(ints, array);
488 >        for (Integer o : ints)
489 >            assertSame(o, q.poll());
490 >        assertTrue(q.isEmpty());
491      }
492  
493      /**
494 <     * toArray with incompatible array type throws CCE
494 >     * toArray(incompatible array type) throws ArrayStoreException
495       */
496      public void testToArray1_BadArg() {
497 +        LinkedTransferQueue q = populatedQueue(SIZE);
498          try {
499 <            LinkedTransferQueue q = populatedQueue(SIZE);
559 <            Object o[] = q.toArray(new String[10]);
499 >            q.toArray(new String[10]);
500              shouldThrow();
501 <        } catch (ArrayStoreException success) {
562 <        }
501 >        } catch (ArrayStoreException success) {}
502      }
503  
504      /**
# Line 568 | Line 507 | public class LinkedTransferQueueTest ext
507      public void testIterator() throws InterruptedException {
508          LinkedTransferQueue q = populatedQueue(SIZE);
509          Iterator it = q.iterator();
510 <        while (it.hasNext()) {
510 >        int i;
511 >        for (i = 0; it.hasNext(); i++)
512 >            assertTrue(q.contains(it.next()));
513 >        assertEquals(i, SIZE);
514 >        assertIteratorExhausted(it);
515 >
516 >        it = q.iterator();
517 >        for (i = 0; it.hasNext(); i++)
518              assertEquals(it.next(), q.take());
519 <        }
519 >        assertEquals(i, SIZE);
520 >        assertIteratorExhausted(it);
521      }
522  
523      /**
524 <     * iterator.remove removes current element
524 >     * iterator of empty collection has no elements
525 >     */
526 >    public void testEmptyIterator() {
527 >        assertIteratorExhausted(new LinkedTransferQueue().iterator());
528 >    }
529 >
530 >    /**
531 >     * iterator.remove() removes current element
532       */
533      public void testIteratorRemove() {
534          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 587 | Line 541 | public class LinkedTransferQueueTest ext
541          it.remove();
542  
543          it = q.iterator();
544 <        assertEquals(it.next(), one);
545 <        assertEquals(it.next(), three);
544 >        assertSame(it.next(), one);
545 >        assertSame(it.next(), three);
546          assertFalse(it.hasNext());
547      }
548  
# Line 596 | Line 550 | public class LinkedTransferQueueTest ext
550       * iterator ordering is FIFO
551       */
552      public void testIteratorOrdering() {
553 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
553 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
554          assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
555          q.add(one);
556          q.add(two);
# Line 631 | Line 585 | public class LinkedTransferQueueTest ext
585          LinkedTransferQueue q = populatedQueue(SIZE);
586          String s = q.toString();
587          for (int i = 0; i < SIZE; ++i) {
588 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
588 >            assertTrue(s.contains(String.valueOf(i)));
589          }
590      }
591  
# Line 640 | Line 594 | public class LinkedTransferQueueTest ext
594       */
595      public void testOfferInExecutor() {
596          final LinkedTransferQueue q = new LinkedTransferQueue();
597 <        q.add(one);
598 <        q.add(two);
599 <        ExecutorService executor = Executors.newFixedThreadPool(2);
600 <        executor.execute(new Runnable() {
601 <            public void run() {
602 <                try {
603 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
604 <                } catch (Throwable ex) {
605 <                    threadUnexpectedException(ex);
606 <                }
607 <            }});
654 <
655 <        executor.execute(new Runnable() {
656 <            public void run() {
657 <                try {
658 <                    Thread.sleep(SMALL_DELAY_MS);
659 <                    threadAssertEquals(one, q.take());
660 <                } catch (Throwable ex) {
661 <                    threadUnexpectedException(ex);
662 <                }
663 <            }});
597 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
598 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
599 >        try (PoolCleaner cleaner = cleaner(executor)) {
600 >
601 >            executor.execute(new CheckedRunnable() {
602 >                public void realRun() throws InterruptedException {
603 >                    threadsStarted.await();
604 >                    long startTime = System.nanoTime();
605 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
606 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
607 >                }});
608  
609 <        joinPool(executor);
609 >            executor.execute(new CheckedRunnable() {
610 >                public void realRun() throws InterruptedException {
611 >                    threadsStarted.await();
612 >                    assertSame(one, q.take());
613 >                    checkEmpty(q);
614 >                }});
615 >        }
616      }
617  
618      /**
619 <     * poll retrieves elements across Executor threads
619 >     * timed poll retrieves elements across Executor threads
620       */
621      public void testPollInExecutor() {
622          final LinkedTransferQueue q = new LinkedTransferQueue();
623 <        ExecutorService executor = Executors.newFixedThreadPool(2);
624 <        executor.execute(new Runnable() {
625 <            public void run() {
626 <                try {
627 <                    threadAssertNull(q.poll());
628 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
629 <                    threadAssertTrue(q.isEmpty());
630 <                } catch (Throwable  ex) {
631 <                    threadUnexpectedException(ex);
632 <                }
633 <            }});
623 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
624 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
625 >        try (PoolCleaner cleaner = cleaner(executor)) {
626 >
627 >            executor.execute(new CheckedRunnable() {
628 >                public void realRun() throws InterruptedException {
629 >                    assertNull(q.poll());
630 >                    threadsStarted.await();
631 >                    long startTime = System.nanoTime();
632 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
633 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
634 >                    checkEmpty(q);
635 >                }});
636  
637 <        executor.execute(new Runnable() {
638 <            public void run() {
639 <                try {
688 <                    Thread.sleep(SMALL_DELAY_MS);
637 >            executor.execute(new CheckedRunnable() {
638 >                public void realRun() throws InterruptedException {
639 >                    threadsStarted.await();
640                      q.put(one);
641 <                } catch (Throwable ex) {
691 <                    threadUnexpectedException(ex);
692 <                }
693 <            }});
694 <
695 <        joinPool(executor);
696 <    }
697 <
698 <    /**
699 <     * A deserialized serialized queue has same elements in same order
700 <     */
701 <    public void testSerialization() throws Exception {
702 <        LinkedTransferQueue q = populatedQueue(SIZE);
703 <
704 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
705 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
706 <        out.writeObject(q);
707 <        out.close();
708 <
709 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
710 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
711 <        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
712 <
713 <        assertEquals(q.size(), r.size());
714 <        while (!q.isEmpty()) {
715 <            assertEquals(q.remove(), r.remove());
641 >                }});
642          }
643      }
644  
645      /**
646 <     * drainTo(null) throws NullPointerException
646 >     * A deserialized/reserialized queue has same elements in same order
647       */
648 <    public void testDrainToNull() {
649 <        LinkedTransferQueue q = populatedQueue(SIZE);
650 <        try {
725 <            q.drainTo(null);
726 <            shouldThrow();
727 <        } catch (NullPointerException success) {
728 <        }
729 <    }
648 >    public void testSerialization() throws Exception {
649 >        Queue x = populatedQueue(SIZE);
650 >        Queue y = serialClone(x);
651  
652 <    /**
653 <     * drainTo(this) throws IllegalArgumentException
654 <     */
655 <    public void testDrainToSelf() {
656 <        LinkedTransferQueue q = populatedQueue(SIZE);
657 <        try {
658 <            q.drainTo(q);
738 <            shouldThrow();
739 <        } catch (IllegalArgumentException success) {
652 >        assertNotSame(y, x);
653 >        assertEquals(x.size(), y.size());
654 >        assertEquals(x.toString(), y.toString());
655 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
656 >        while (!x.isEmpty()) {
657 >            assertFalse(y.isEmpty());
658 >            assertEquals(x.remove(), y.remove());
659          }
660 +        assertTrue(y.isEmpty());
661      }
662  
663      /**
# Line 747 | Line 667 | public class LinkedTransferQueueTest ext
667          LinkedTransferQueue q = populatedQueue(SIZE);
668          ArrayList l = new ArrayList();
669          q.drainTo(l);
670 <        assertEquals(q.size(), 0);
671 <        assertEquals(l.size(), SIZE);
670 >        assertEquals(0, q.size());
671 >        assertEquals(SIZE, l.size());
672          for (int i = 0; i < SIZE; ++i) {
673 <            assertEquals(l.get(i), i);
673 >            assertEquals(i, l.get(i));
674          }
675          q.add(zero);
676          q.add(one);
# Line 759 | Line 679 | public class LinkedTransferQueueTest ext
679          assertTrue(q.contains(one));
680          l.clear();
681          q.drainTo(l);
682 <        assertEquals(q.size(), 0);
683 <        assertEquals(l.size(), 2);
682 >        assertEquals(0, q.size());
683 >        assertEquals(2, l.size());
684          for (int i = 0; i < 2; ++i) {
685 <            assertEquals(l.get(i), i);
685 >            assertEquals(i, l.get(i));
686          }
687      }
688  
689      /**
690 <     * drainTo empties full queue, unblocking a waiting put.
690 >     * drainTo(c) empties full queue, unblocking a waiting put.
691       */
692      public void testDrainToWithActivePut() throws InterruptedException {
693          final LinkedTransferQueue q = populatedQueue(SIZE);
694 <        Thread t = new Thread(new Runnable() {
695 <            public void run() {
696 <                try {
777 <                    q.put(SIZE + 1);
778 <                } catch (Throwable ex) {
779 <                    threadUnexpectedException(ex);
780 <                }
694 >        Thread t = newStartedThread(new CheckedRunnable() {
695 >            public void realRun() {
696 >                q.put(SIZE + 1);
697              }});
782        t.start();
698          ArrayList l = new ArrayList();
699          q.drainTo(l);
700          assertTrue(l.size() >= SIZE);
701 <        for (int i = 0; i < SIZE; ++i) {
702 <            assertEquals(l.get(i), i);
703 <        }
789 <        t.join();
701 >        for (int i = 0; i < SIZE; ++i)
702 >            assertEquals(i, l.get(i));
703 >        awaitTermination(t);
704          assertTrue(q.size() + l.size() >= SIZE);
705      }
706  
707      /**
708 <     * drainTo(null, n) throws NullPointerException
795 <     */
796 <    public void testDrainToNullN() {
797 <        LinkedTransferQueue q = populatedQueue(SIZE);
798 <        try {
799 <            q.drainTo(null, 0);
800 <            shouldThrow();
801 <        } catch (NullPointerException success) {
802 <        }
803 <    }
804 <
805 <    /**
806 <     * drainTo(this, n) throws IllegalArgumentException
807 <     */
808 <    public void testDrainToSelfN() {
809 <        LinkedTransferQueue q = populatedQueue(SIZE);
810 <        try {
811 <            q.drainTo(q, 0);
812 <            shouldThrow();
813 <        } catch (IllegalArgumentException success) {
814 <        }
815 <    }
816 <
817 <    /**
818 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
708 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
709       */
710      public void testDrainToN() {
711          LinkedTransferQueue q = new LinkedTransferQueue();
# Line 826 | Line 716 | public class LinkedTransferQueueTest ext
716              ArrayList l = new ArrayList();
717              q.drainTo(l, i);
718              int k = (i < SIZE) ? i : SIZE;
719 <            assertEquals(l.size(), k);
720 <            assertEquals(q.size(), SIZE - k);
721 <            for (int j = 0; j < k; ++j) {
722 <                assertEquals(l.get(j), j);
723 <            }
834 <            while (q.poll() != null)
835 <                ;
719 >            assertEquals(k, l.size());
720 >            assertEquals(SIZE - k, q.size());
721 >            for (int j = 0; j < k; ++j)
722 >                assertEquals(j, l.get(j));
723 >            do {} while (q.poll() != null);
724          }
725      }
726  
727      /**
728 <     * poll and take should decrement the waiting consumer count
728 >     * timed poll() or take() increments the waiting consumer count;
729 >     * offer(e) decrements the waiting consumer count
730       */
731      public void testWaitingConsumer() throws InterruptedException {
732          final LinkedTransferQueue q = new LinkedTransferQueue();
733 <        final ConsumerObserver waiting = new ConsumerObserver();
734 <        new Thread(new Runnable() {
735 <            public void run() {
736 <                try {
737 <                    threadAssertTrue(q.hasWaitingConsumer());
738 <                    waiting.setWaitingConsumer(q.getWaitingConsumerCount());
739 <                    threadAssertTrue(q.offer(new Object()));
740 <                } catch (Throwable ex) {
741 <                    threadUnexpectedException(ex);
742 <                }
743 <            }}).start();
744 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
745 <        assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
733 >        assertEquals(0, q.getWaitingConsumerCount());
734 >        assertFalse(q.hasWaitingConsumer());
735 >        final CountDownLatch threadStarted = new CountDownLatch(1);
736 >
737 >        Thread t = newStartedThread(new CheckedRunnable() {
738 >            public void realRun() throws InterruptedException {
739 >                threadStarted.countDown();
740 >                long startTime = System.nanoTime();
741 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
742 >                assertEquals(0, q.getWaitingConsumerCount());
743 >                assertFalse(q.hasWaitingConsumer());
744 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
745 >            }});
746 >
747 >        threadStarted.await();
748 >        Callable<Boolean> oneConsumer
749 >            = new Callable<Boolean>() { public Boolean call() {
750 >                return q.hasWaitingConsumer()
751 >                && q.getWaitingConsumerCount() == 1; }};
752 >        waitForThreadToEnterWaitState(t, oneConsumer);
753 >
754 >        assertTrue(q.offer(one));
755 >        assertEquals(0, q.getWaitingConsumerCount());
756 >        assertFalse(q.hasWaitingConsumer());
757 >
758 >        awaitTermination(t);
759      }
760  
761      /**
# Line 864 | Line 766 | public class LinkedTransferQueueTest ext
766              LinkedTransferQueue q = new LinkedTransferQueue();
767              q.transfer(null);
768              shouldThrow();
769 <        } catch (NullPointerException ex) {
868 <        }
769 >        } catch (NullPointerException success) {}
770      }
771  
772      /**
773 <     * transfer attempts to insert into the queue then wait until that
774 <     * object is removed via take or poll.
773 >     * transfer waits until a poll occurs. The transferred element
774 >     * is returned by the associated poll.
775       */
776      public void testTransfer2() throws InterruptedException {
777 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
777 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
778 >        final CountDownLatch threadStarted = new CountDownLatch(1);
779  
780 <        new Thread(new Runnable() {
781 <            public void run() {
782 <                try {
783 <                    q.transfer(SIZE);
784 <                    threadAssertTrue(q.isEmpty());
785 <                } catch (Throwable ex) {
884 <                    threadUnexpectedException(ex);
885 <                }
886 <            }}).start();
780 >        Thread t = newStartedThread(new CheckedRunnable() {
781 >            public void realRun() throws InterruptedException {
782 >                threadStarted.countDown();
783 >                q.transfer(five);
784 >                checkEmpty(q);
785 >            }});
786  
787 <        Thread.sleep(SHORT_DELAY_MS);
788 <        assertEquals(1, q.size());
789 <        assertEquals(SIZE, (int) q.poll());
790 <        assertTrue(q.isEmpty());
787 >        threadStarted.await();
788 >        Callable<Boolean> oneElement
789 >            = new Callable<Boolean>() { public Boolean call() {
790 >                return !q.isEmpty() && q.size() == 1; }};
791 >        waitForThreadToEnterWaitState(t, oneElement);
792 >
793 >        assertSame(five, q.poll());
794 >        checkEmpty(q);
795 >        awaitTermination(t);
796      }
797  
798      /**
799 <     * transfer will attempt to transfer in fifo order and continue
896 <     * waiting if the element being transfered is not polled or taken
799 >     * transfer waits until a poll occurs, and then transfers in fifo order
800       */
801      public void testTransfer3() throws InterruptedException {
802 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
802 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
803  
804 <        Thread first =
805 <            new Thread(new Runnable() {
806 <                public void run() {
807 <                    try {
808 <                        Integer i = SIZE + 1;
809 <                        q.transfer(i);
907 <                        threadAssertTrue(!q.contains(i));
908 <                        threadAssertEquals(1, q.size());
909 <                    } catch (Throwable ex) {
910 <                        threadUnexpectedException(ex);
911 <                    }
912 <                }});
913 <        first.start();
804 >        Thread first = newStartedThread(new CheckedRunnable() {
805 >            public void realRun() throws InterruptedException {
806 >                q.transfer(four);
807 >                assertFalse(q.contains(four));
808 >                assertEquals(1, q.size());
809 >            }});
810  
811 <        Thread interruptedThread =
812 <            new Thread(new Runnable() {
813 <                public void run() {
814 <                    try {
815 <                        while (q.size() == 0)
816 <                            Thread.yield();
921 <                        q.transfer(SIZE);
922 <                        threadShouldThrow();
923 <                    } catch (InterruptedException success) {
924 <                    } catch (Throwable ex) {
925 <                        threadUnexpectedException(ex);
926 <                    }
811 >        Thread interruptedThread = newStartedThread(
812 >            new CheckedInterruptedRunnable() {
813 >                public void realRun() throws InterruptedException {
814 >                    while (q.isEmpty())
815 >                        Thread.yield();
816 >                    q.transfer(five);
817                  }});
928        interruptedThread.start();
818  
819          while (q.size() < 2)
820              Thread.yield();
821          assertEquals(2, q.size());
822 <        assertEquals(SIZE + 1, (int) q.poll());
822 >        assertSame(four, q.poll());
823          first.join();
824          assertEquals(1, q.size());
825          interruptedThread.interrupt();
826          interruptedThread.join();
827 <        assertEquals(0, q.size());
939 <        assertTrue(q.isEmpty());
827 >        checkEmpty(q);
828      }
829  
830      /**
831 <     * transfer will wait as long as a poll or take occurs if one does occur
832 <     * the waiting is finished and the thread that tries to poll/take
945 <     * wins in retrieving the element
831 >     * transfer waits until a poll occurs, at which point the polling
832 >     * thread returns the element
833       */
834      public void testTransfer4() throws InterruptedException {
835          final LinkedTransferQueue q = new LinkedTransferQueue();
836 <        new Thread(new Runnable() {
837 <            public void run() {
838 <                try {
839 <                    q.transfer(four);
840 <                    threadAssertFalse(q.contains(four));
841 <                    threadAssertEquals(three, q.poll());
842 <                } catch (Throwable ex) {
843 <                    threadUnexpectedException(ex);
844 <                }
845 <            }}).start();
846 <        Thread.sleep(MEDIUM_DELAY_MS);
836 >
837 >        Thread t = newStartedThread(new CheckedRunnable() {
838 >            public void realRun() throws InterruptedException {
839 >                q.transfer(four);
840 >                assertFalse(q.contains(four));
841 >                assertSame(three, q.poll());
842 >            }});
843 >
844 >        while (q.isEmpty())
845 >            Thread.yield();
846 >        assertFalse(q.isEmpty());
847 >        assertEquals(1, q.size());
848          assertTrue(q.offer(three));
849 <        assertEquals(four, q.poll());
849 >        assertSame(four, q.poll());
850 >        awaitTermination(t);
851 >    }
852 >
853 >    /**
854 >     * transfer waits until a take occurs. The transferred element
855 >     * is returned by the associated take.
856 >     */
857 >    public void testTransfer5() throws InterruptedException {
858 >        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
859 >
860 >        Thread t = newStartedThread(new CheckedRunnable() {
861 >            public void realRun() throws InterruptedException {
862 >                q.transfer(four);
863 >                checkEmpty(q);
864 >            }});
865 >
866 >        while (q.isEmpty())
867 >            Thread.yield();
868 >        assertFalse(q.isEmpty());
869 >        assertEquals(1, q.size());
870 >        assertSame(four, q.take());
871 >        checkEmpty(q);
872 >        awaitTermination(t);
873      }
874  
875      /**
876       * tryTransfer(null) throws NullPointerException
877       */
878      public void testTryTransfer1() {
879 +        final LinkedTransferQueue q = new LinkedTransferQueue();
880          try {
969            final LinkedTransferQueue q = new LinkedTransferQueue();
881              q.tryTransfer(null);
882 <            this.shouldThrow();
883 <        } catch (NullPointerException ex) {
973 <        }
882 >            shouldThrow();
883 >        } catch (NullPointerException success) {}
884      }
885  
886      /**
887       * tryTransfer returns false and does not enqueue if there are no
888       * consumers waiting to poll or take.
889       */
890 <    public void testTryTransfer2() {
890 >    public void testTryTransfer2() throws InterruptedException {
891          final LinkedTransferQueue q = new LinkedTransferQueue();
892          assertFalse(q.tryTransfer(new Object()));
893          assertFalse(q.hasWaitingConsumer());
894 <        assertTrue(q.isEmpty());
985 <        assertEquals(0, q.size());
894 >        checkEmpty(q);
895      }
896  
897      /**
# Line 992 | Line 901 | public class LinkedTransferQueueTest ext
901      public void testTryTransfer3() throws InterruptedException {
902          final Object hotPotato = new Object();
903          final LinkedTransferQueue q = new LinkedTransferQueue();
904 <        new Thread(new Runnable() {
905 <            public void run() {
906 <                try {
907 <                    while (! q.hasWaitingConsumer())
908 <                        Thread.yield();
909 <                    threadAssertTrue(q.hasWaitingConsumer());
910 <                    threadAssertTrue(q.isEmpty());
911 <                    threadAssertTrue(q.size() == 0);
912 <                    threadAssertTrue(q.tryTransfer(hotPotato));
913 <                } catch (Throwable ex) {
914 <                    threadUnexpectedException(ex);
915 <                }
916 <            }}).start();
917 <        assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato);
918 <        assertTrue(q.isEmpty());
904 >
905 >        Thread t = newStartedThread(new CheckedRunnable() {
906 >            public void realRun() {
907 >                while (! q.hasWaitingConsumer())
908 >                    Thread.yield();
909 >                assertTrue(q.hasWaitingConsumer());
910 >                checkEmpty(q);
911 >                assertTrue(q.tryTransfer(hotPotato));
912 >            }});
913 >
914 >        long startTime = System.nanoTime();
915 >        assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS));
916 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
917 >        checkEmpty(q);
918 >        awaitTermination(t);
919      }
920  
921      /**
# Line 1016 | Line 925 | public class LinkedTransferQueueTest ext
925      public void testTryTransfer4() throws InterruptedException {
926          final Object hotPotato = new Object();
927          final LinkedTransferQueue q = new LinkedTransferQueue();
928 <        new Thread(new Runnable() {
929 <            public void run() {
930 <                try {
931 <                    while (! q.hasWaitingConsumer())
932 <                        Thread.yield();
933 <                    threadAssertTrue(q.hasWaitingConsumer());
934 <                    threadAssertTrue(q.isEmpty());
935 <                    threadAssertTrue(q.size() == 0);
936 <                    threadAssertTrue(q.tryTransfer(hotPotato));
937 <                } catch (Throwable ex) {
938 <                    threadUnexpectedException(ex);
939 <                }
940 <            }}).start();
1032 <        assertTrue(q.take() == hotPotato);
1033 <        assertTrue(q.isEmpty());
928 >
929 >        Thread t = newStartedThread(new CheckedRunnable() {
930 >            public void realRun() {
931 >                while (! q.hasWaitingConsumer())
932 >                    Thread.yield();
933 >                assertTrue(q.hasWaitingConsumer());
934 >                checkEmpty(q);
935 >                assertTrue(q.tryTransfer(hotPotato));
936 >            }});
937 >
938 >        assertSame(q.take(), hotPotato);
939 >        checkEmpty(q);
940 >        awaitTermination(t);
941      }
942  
943      /**
944 <     * tryTransfer waits the amount given if interrupted, show an
1038 <     * interrupted exception
944 >     * tryTransfer blocks interruptibly if no takers
945       */
946      public void testTryTransfer5() throws InterruptedException {
947          final LinkedTransferQueue q = new LinkedTransferQueue();
948 <        Thread toInterrupt = new Thread(new Runnable() {
949 <            public void run() {
948 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
949 >        assertTrue(q.isEmpty());
950 >
951 >        Thread t = newStartedThread(new CheckedRunnable() {
952 >            public void realRun() throws InterruptedException {
953 >                long startTime = System.nanoTime();
954 >                Thread.currentThread().interrupt();
955                  try {
956 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
957 <                    threadShouldThrow();
958 <                } catch (InterruptedException success) {
959 <                } catch (Throwable ex) {
960 <                    threadUnexpectedException(ex);
961 <                }
956 >                    q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit());
957 >                    shouldThrow();
958 >                } catch (InterruptedException success) {}
959 >                assertFalse(Thread.interrupted());
960 >
961 >                pleaseInterrupt.countDown();
962 >                try {
963 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
964 >                    shouldThrow();
965 >                } catch (InterruptedException success) {}
966 >                assertFalse(Thread.interrupted());
967 >
968 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
969              }});
970 <        toInterrupt.start();
971 <        Thread.sleep(SMALL_DELAY_MS);
972 <        toInterrupt.interrupt();
970 >
971 >        await(pleaseInterrupt);
972 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
973 >        t.interrupt();
974 >        awaitTermination(t);
975 >        checkEmpty(q);
976      }
977  
978      /**
979 <     * tryTransfer gives up after the timeout and return false
979 >     * tryTransfer gives up after the timeout and returns false
980       */
981      public void testTryTransfer6() throws InterruptedException {
982          final LinkedTransferQueue q = new LinkedTransferQueue();
983 <        new Thread(new Runnable() {
984 <            public void run() {
985 <                try {
986 <                    threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
987 <                } catch (Throwable ex) {
988 <                    threadUnexpectedException(ex);
989 <                }
990 <            }}).start();
991 <        Thread.sleep(LONG_DELAY_MS);
992 <        assertTrue(q.isEmpty());
983 >
984 >        Thread t = newStartedThread(new CheckedRunnable() {
985 >            public void realRun() throws InterruptedException {
986 >                long startTime = System.nanoTime();
987 >                assertFalse(q.tryTransfer(new Object(),
988 >                                          timeoutMillis(), MILLISECONDS));
989 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
990 >                checkEmpty(q);
991 >            }});
992 >
993 >        awaitTermination(t);
994 >        checkEmpty(q);
995      }
996  
997      /**
# Line 1078 | Line 1001 | public class LinkedTransferQueueTest ext
1001      public void testTryTransfer7() throws InterruptedException {
1002          final LinkedTransferQueue q = new LinkedTransferQueue();
1003          assertTrue(q.offer(four));
1004 <        new Thread(new Runnable() {
1005 <            public void run() {
1006 <                try {
1007 <                    threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1008 <                    threadAssertTrue(q.isEmpty());
1009 <                } catch (Throwable ex) {
1010 <                    threadUnexpectedException(ex);
1011 <                }
1012 <            }}).start();
1013 <        Thread.sleep(SHORT_DELAY_MS);
1004 >
1005 >        Thread t = newStartedThread(new CheckedRunnable() {
1006 >            public void realRun() throws InterruptedException {
1007 >                long startTime = System.nanoTime();
1008 >                assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS));
1009 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1010 >                checkEmpty(q);
1011 >            }});
1012 >
1013 >        while (q.size() != 2)
1014 >            Thread.yield();
1015          assertEquals(2, q.size());
1016 <        assertEquals(four, q.poll());
1017 <        assertEquals(five, q.poll());
1018 <        assertTrue(q.isEmpty());
1016 >        assertSame(four, q.poll());
1017 >        assertSame(five, q.poll());
1018 >        checkEmpty(q);
1019 >        awaitTermination(t);
1020      }
1021  
1022      /**
1023 <     * tryTransfer attempts to enqueue into the q and fails returning
1024 <     * false not enqueueing and the successive poll is null
1023 >     * tryTransfer attempts to enqueue into the queue and fails
1024 >     * returning false not enqueueing and the successive poll is null
1025       */
1026      public void testTryTransfer8() throws InterruptedException {
1027          final LinkedTransferQueue q = new LinkedTransferQueue();
1028          assertTrue(q.offer(four));
1029          assertEquals(1, q.size());
1030 <        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1030 >        long startTime = System.nanoTime();
1031 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1032 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1033          assertEquals(1, q.size());
1034 <        assertEquals(four, q.poll());
1108 <        threadAssertTrue(q.isEmpty());
1034 >        assertSame(four, q.poll());
1035          assertNull(q.poll());
1036 +        checkEmpty(q);
1037      }
1038  
1039      private LinkedTransferQueue<Integer> populatedQueue(int n) {
1040 <        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1041 <        assertTrue(q.isEmpty());
1040 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
1041 >        checkEmpty(q);
1042          for (int i = 0; i < n; i++) {
1043              assertEquals(i, q.size());
1044              assertTrue(q.offer(i));
# Line 1121 | Line 1048 | public class LinkedTransferQueueTest ext
1048          return q;
1049      }
1050  
1051 <    private static class ConsumerObserver {
1052 <
1053 <        private int waitingConsumers;
1054 <
1055 <        private ConsumerObserver() {
1056 <        }
1057 <
1058 <        private void setWaitingConsumer(int i) {
1059 <            this.waitingConsumers = i;
1060 <        }
1061 <
1062 <        private int getWaitingConsumers() {
1136 <            return waitingConsumers;
1051 >    /**
1052 >     * remove(null), contains(null) always return false
1053 >     */
1054 >    public void testNeverContainsNull() {
1055 >        Collection<?>[] qs = {
1056 >            new LinkedTransferQueue<Object>(),
1057 >            populatedQueue(2),
1058 >        };
1059 >
1060 >        for (Collection<?> q : qs) {
1061 >            assertFalse(q.contains(null));
1062 >            assertFalse(q.remove(null));
1063          }
1064      }
1065   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines