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.2 by jsr166, Fri Jul 31 23:37:31 2009 UTC vs.
Revision 1.62 by jsr166, Sun Jun 14 20:58:14 2015 UTC

# Line 1 | Line 1
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;
11 < import java.io.ByteArrayInputStream;
12 < import java.io.ByteArrayOutputStream;
13 < import java.io.ObjectInputStream;
14 < 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.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 class Generic extends BlockingQueueTest {
36 +        protected BlockingQueue emptyCollection() {
37 +            return new LinkedTransferQueue();
38 +        }
39 +    }
40  
41      public static void main(String[] args) {
42 <        junit.textui.TestRunner.run(suite());
42 >        main(suite(), args);
43      }
44  
45      public static Test suite() {
46 <        return new TestSuite(LinkedTransferQueueTest.class);
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
51 >    /**
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());
57          assertTrue(new LinkedTransferQueue().isEmpty());
58      }
59  
60 <    /*
61 <     * Initizialing constructor with null collection throws NPE
60 >    /**
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) {
50 <        }
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
72 <     * Initializing from Collection of null elements throws NPE
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];
59 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
78 >            new LinkedTransferQueue(elements);
79              shouldThrow();
80 <        } catch (NullPointerException success) {
62 <        }
80 >        } catch (NullPointerException success) {}
81      }
82 <    /*
82 >
83 >    /**
84       * Initializing constructor with a collection containing some null elements
85 <     * throws NPE
85 >     * throws NullPointerException
86       */
68
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];
72 <            for (int i = 0; i < SIZE - 1; ++i) {
73 <                ints[i] = new Integer(i);
74 <            }
75 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
93 >            new LinkedTransferQueue(elements);
94              shouldThrow();
95 <        } catch (NullPointerException success) {
78 <        }
95 >        } catch (NullPointerException success) {}
96      }
97  
98 <    /*
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] = new Integer(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 q = populatedQueue(SIZE);
103 <        int remainingCapacity = q.remainingCapacity();
126 >        BlockingQueue q = populatedQueue(SIZE);
127          for (int i = 0; i < SIZE; ++i) {
128 <            assertEquals(remainingCapacity, q.remainingCapacity());
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(remainingCapacity, q.remainingCapacity());
133 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
134              assertEquals(i, q.size());
135 <            q.add(new Integer(i));
113 <        }
114 <    }
115 <
116 <    /**
117 <     * offer(null) throws NPE
118 <     */
119 <    public void testOfferNull() {
120 <        try {
121 <            LinkedTransferQueue q = new LinkedTransferQueue();
122 <            q.offer(null);
123 <            shouldThrow();
124 <        } catch (NullPointerException success) {
135 >            assertTrue(q.add(i));
136          }
137      }
138  
139      /**
140 <     * add(null) throws NPE
130 <     */
131 <    public void testAddNull() {
132 <        try {
133 <            LinkedTransferQueue q = new LinkedTransferQueue();
134 <            q.add(null);
135 <            shouldThrow();
136 <        } catch (NullPointerException success) {
137 <        }
138 <    }
139 <
140 <    /**
141 <     * addAll(null) throws NPE
142 <     */
143 <    public void testAddAll1() {
144 <        try {
145 <            LinkedTransferQueue q = new LinkedTransferQueue();
146 <            q.addAll(null);
147 <            shouldThrow();
148 <        } catch (NullPointerException success) {
149 <        }
150 <    }
151 <
152 <    /**
153 <     * addAll(this) throws IAE
140 >     * addAll(this) throws IllegalArgumentException
141       */
142      public void testAddAllSelf() {
143 +        LinkedTransferQueue q = populatedQueue(SIZE);
144          try {
157            LinkedTransferQueue q = populatedQueue(SIZE);
145              q.addAll(q);
146              shouldThrow();
147 <        } catch (IllegalArgumentException success) {
161 <        }
162 <    }
163 <
164 <    /**
165 <     * addAll of a collection with null elements throws NPE
166 <     */
167 <    public void testAddAll2() {
168 <        try {
169 <            LinkedTransferQueue q = new LinkedTransferQueue();
170 <            Integer[] ints = new Integer[SIZE];
171 <            q.addAll(Arrays.asList(ints));
172 <            shouldThrow();
173 <        } catch (NullPointerException success) {
174 <        }
147 >        } catch (IllegalArgumentException success) {}
148      }
149  
150      /**
151 <     * addAll of a collection with any null elements throws NPE 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 {
183            LinkedTransferQueue q = new LinkedTransferQueue();
184            Integer[] ints = new Integer[SIZE];
185            for (int i = 0; i < SIZE - 1; ++i) {
186                ints[i] = new Integer(i);
187            }
160              q.addAll(Arrays.asList(ints));
161              shouldThrow();
162 <        } catch (NullPointerException success) {
191 <        }
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
166       * Queue contains all elements, in traversal order, of successful addAll
167       */
168      public void testAddAll5() {
169 <        try {
170 <            Integer[] empty = new Integer[0];
171 <            Integer[] ints = new Integer[SIZE];
172 <            for (int i = 0; i < SIZE; ++i) {
202 <                ints[i] = new Integer(i);
203 <            }
204 <            LinkedTransferQueue q = new LinkedTransferQueue();
205 <            assertFalse(q.addAll(Arrays.asList(empty)));
206 <            assertTrue(q.addAll(Arrays.asList(ints)));
207 <            for (int i = 0; i < SIZE; ++i) {
208 <                assertEquals(ints[i], q.poll());
209 <            }
210 <        } finally {
169 >        Integer[] empty = new Integer[0];
170 >        Integer[] ints = new Integer[SIZE];
171 >        for (int i = 0; i < SIZE; ++i) {
172 >            ints[i] = i;
173          }
174 <    }
175 <
176 <    /**
177 <     * put(null) throws NPE
178 <     */
217 <    public void testPutNull() {
218 <        try {
219 <            LinkedTransferQueue q = new LinkedTransferQueue();
220 <            q.put(null);
221 <            shouldThrow();
222 <        } catch (NullPointerException success) {
223 <        } catch (Exception ie) {
224 <            unexpectedException();
174 >        LinkedTransferQueue q = new LinkedTransferQueue();
175 >        assertFalse(q.addAll(Arrays.asList(empty)));
176 >        assertTrue(q.addAll(Arrays.asList(ints)));
177 >        for (int i = 0; i < SIZE; ++i) {
178 >            assertEquals(ints[i], q.poll());
179          }
180      }
181  
# Line 229 | Line 183 | public class LinkedTransferQueueTest ext
183       * all elements successfully put are contained
184       */
185      public void testPut() {
186 <        try {
187 <            LinkedTransferQueue q = new LinkedTransferQueue();
188 <            for (int i = 0; i < SIZE; ++i) {
189 <                Integer I = new Integer(i);
190 <                q.put(I);
237 <                assertTrue(q.contains(I));
238 <            }
239 <        } catch (Exception ie) {
240 <            unexpectedException();
186 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
187 >        for (int i = 0; i < SIZE; ++i) {
188 >            assertEquals(i, q.size());
189 >            q.put(i);
190 >            assertTrue(q.contains(i));
191          }
192      }
193  
194      /**
195       * take retrieves elements in FIFO order
196       */
197 <    public void testTake() {
198 <        try {
199 <            LinkedTransferQueue q = populatedQueue(SIZE);
200 <            for (int i = 0; i < SIZE; ++i) {
251 <                assertEquals(i, ((Integer) q.take()).intValue());
252 <            }
253 <        } catch (InterruptedException e) {
254 <            unexpectedException();
197 >    public void testTake() throws InterruptedException {
198 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
199 >        for (int i = 0; i < SIZE; ++i) {
200 >            assertEquals(i, (int) q.take());
201          }
202      }
203  
204      /**
205 <     * take blocks interruptibly when empty
205 >     * take removes existing elements until empty, then blocks interruptibly
206       */
207 <    public void testTakeFromEmpty() {
208 <        final LinkedTransferQueue q = new LinkedTransferQueue();
209 <        Thread t = new Thread(new Runnable() {
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) {
213 >                    assertEquals(i, q.take());
214 >                }
215  
216 <            public void run() {
216 >                Thread.currentThread().interrupt();
217                  try {
218                      q.take();
219 <                    threadShouldThrow();
220 <                } catch (InterruptedException success) {
221 <                }
271 <            }
272 <        });
273 <        try {
274 <            t.start();
275 <            Thread.sleep(SHORT_DELAY_MS);
276 <            t.interrupt();
277 <            t.join();
278 <        } catch (Exception e) {
279 <            unexpectedException();
280 <        }
281 <    }
282 <    /*
283 <     * Take removes existing elements until empty, then blocks interruptibly
284 <     */
219 >                    shouldThrow();
220 >                } catch (InterruptedException success) {}
221 >                assertFalse(Thread.interrupted());
222  
223 <    public void testBlockingTake() {
287 <        Thread t = new Thread(new Runnable() {
288 <
289 <            public void run() {
223 >                pleaseInterrupt.countDown();
224                  try {
291                    LinkedTransferQueue q = populatedQueue(SIZE);
292                    for (int i = 0; i < SIZE; ++i) {
293                        assertEquals(i, ((Integer) q.take()).intValue());
294                    }
225                      q.take();
226 <                    threadShouldThrow();
227 <                } catch (InterruptedException success) {
228 <                }
229 <            }
230 <        });
231 <        t.start();
232 <        try {
233 <            Thread.sleep(SHORT_DELAY_MS);
234 <            t.interrupt();
305 <            t.join();
306 <        } catch (InterruptedException ie) {
307 <            unexpectedException();
308 <        }
226 >                    shouldThrow();
227 >                } catch (InterruptedException success) {}
228 >                assertFalse(Thread.interrupted());
229 >            }});
230 >
231 >        await(pleaseInterrupt);
232 >        assertThreadStaysAlive(t);
233 >        t.interrupt();
234 >        awaitTermination(t);
235      }
236  
237      /**
238       * poll succeeds unless empty
239       */
240 <    public void testPoll() {
241 <        LinkedTransferQueue q = populatedQueue(SIZE);
240 >    public void testPoll() throws InterruptedException {
241 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
242          for (int i = 0; i < SIZE; ++i) {
243 <            assertEquals(i, ((Integer) q.poll()).intValue());
243 >            assertEquals(i, (int) q.poll());
244          }
245          assertNull(q.poll());
246 +        checkEmpty(q);
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() {
253 <        try {
254 <            LinkedTransferQueue q = populatedQueue(SIZE);
255 <            for (int i = 0; i < SIZE; ++i) {
329 <                assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue());
330 <            }
331 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
332 <        } catch (InterruptedException e) {
333 <            unexpectedException();
252 >    public void testTimedPoll0() throws InterruptedException {
253 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
254 >        for (int i = 0; i < SIZE; ++i) {
255 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
256          }
257 +        assertNull(q.poll(0, MILLISECONDS));
258 +        checkEmpty(q);
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() {
265 <        try {
266 <            LinkedTransferQueue q = populatedQueue(SIZE);
267 <            for (int i = 0; i < SIZE; ++i) {
268 <                assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
269 <            }
270 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
271 <        } catch (InterruptedException e) {
272 <            unexpectedException();
273 <        }
264 >    public void testTimedPoll() throws InterruptedException {
265 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
266 >        for (int i = 0; i < SIZE; ++i) {
267 >            long startTime = System.nanoTime();
268 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
269 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
270 >        }
271 >        long startTime = System.nanoTime();
272 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
273 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
274 >        checkEmpty(q);
275      }
276  
277      /**
278       * Interrupted timed poll throws InterruptedException instead of
279       * returning timeout status
280       */
281 <    public void testInterruptedTimedPoll() {
282 <        Thread t = new Thread(new Runnable() {
283 <
284 <            public void run() {
281 >    public void testInterruptedTimedPoll() throws InterruptedException {
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 >                for (int i = 0; i < SIZE; ++i) {
287 >                    long t0 = System.nanoTime();
288 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
289 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
290 >                }
291 >                long t0 = System.nanoTime();
292 >                aboutToWait.countDown();
293                  try {
294 <                    LinkedTransferQueue q = populatedQueue(SIZE);
295 <                    for (int i = 0; i < SIZE; ++i) {
363 <                        threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
364 <                    }
365 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
294 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
295 >                    shouldThrow();
296                  } catch (InterruptedException success) {
297 +                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
298                  }
299 <            }
300 <        });
301 <        t.start();
302 <        try {
303 <            Thread.sleep(SHORT_DELAY_MS);
304 <            t.interrupt();
305 <            t.join();
375 <        } catch (InterruptedException ie) {
376 <            unexpectedException();
377 <        }
299 >            }});
300 >
301 >        aboutToWait.await();
302 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
303 >        t.interrupt();
304 >        awaitTermination(t, MEDIUM_DELAY_MS);
305 >        checkEmpty(q);
306      }
307  
308      /**
309 <     *  timed poll before a delayed offer fails; after offer succeeds;
310 <     *  on interruption throws
311 <     */
312 <    public void testTimedPollWithOffer() {
313 <        final LinkedTransferQueue q = new LinkedTransferQueue();
314 <        Thread t = new Thread(new Runnable() {
315 <
316 <            public void run() {
317 <                try {
318 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
319 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
320 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
393 <                    threadShouldThrow();
394 <                } catch (InterruptedException success) {
309 >     * timed poll after thread interrupted throws InterruptedException
310 >     * instead of returning timeout status
311 >     */
312 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
313 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
314 >        Thread t = newStartedThread(new CheckedRunnable() {
315 >            public void realRun() throws InterruptedException {
316 >                Thread.currentThread().interrupt();
317 >                for (int i = 0; i < SIZE; ++i) {
318 >                    long t0 = System.nanoTime();
319 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
320 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
321                  }
322 <            }
323 <        });
324 <        try {
325 <            t.start();
326 <            Thread.sleep(SMALL_DELAY_MS);
327 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
328 <            t.interrupt();
329 <            t.join();
404 <        } catch (Exception e) {
405 <            unexpectedException();
406 <        }
322 >                try {
323 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
324 >                    shouldThrow();
325 >                } catch (InterruptedException success) {}
326 >            }});
327 >
328 >        awaitTermination(t, MEDIUM_DELAY_MS);
329 >        checkEmpty(q);
330      }
331  
332      /**
333       * peek returns next element, or null if empty
334       */
335 <    public void testPeek() {
336 <        LinkedTransferQueue q = populatedQueue(SIZE);
335 >    public void testPeek() throws InterruptedException {
336 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
337          for (int i = 0; i < SIZE; ++i) {
338 <            assertEquals(i, ((Integer) q.peek()).intValue());
339 <            q.poll();
338 >            assertEquals(i, (int) q.peek());
339 >            assertEquals(i, (int) q.poll());
340              assertTrue(q.peek() == null ||
341 <                    i != ((Integer) q.peek()).intValue());
341 >                       i != (int) q.peek());
342          }
343          assertNull(q.peek());
344 +        checkEmpty(q);
345      }
346  
347      /**
348 <     * element returns next element, or throws NSEE if empty
348 >     * element returns next element, or throws NoSuchElementException if empty
349       */
350 <    public void testElement() {
351 <        LinkedTransferQueue q = populatedQueue(SIZE);
350 >    public void testElement() throws InterruptedException {
351 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
352          for (int i = 0; i < SIZE; ++i) {
353 <            assertEquals(i, ((Integer) q.element()).intValue());
354 <            q.poll();
353 >            assertEquals(i, (int) q.element());
354 >            assertEquals(i, (int) q.poll());
355          }
356          try {
357              q.element();
358              shouldThrow();
359 <        } catch (NoSuchElementException success) {
360 <        }
359 >        } catch (NoSuchElementException success) {}
360 >        checkEmpty(q);
361      }
362  
363      /**
364 <     * remove removes next element, or throws NSEE if empty
364 >     * remove removes next element, or throws NoSuchElementException if empty
365       */
366 <    public void testRemove() {
367 <        LinkedTransferQueue q = populatedQueue(SIZE);
366 >    public void testRemove() throws InterruptedException {
367 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
368          for (int i = 0; i < SIZE; ++i) {
369 <            assertEquals(i, ((Integer) q.remove()).intValue());
369 >            assertEquals(i, (int) q.remove());
370          }
371          try {
372              q.remove();
373              shouldThrow();
374 <        } catch (NoSuchElementException success) {
375 <        }
452 <    }
453 <
454 <    /**
455 <     * remove(x) removes x and returns true if present
456 <     */
457 <    public void testRemoveElement() {
458 <        LinkedTransferQueue q = populatedQueue(SIZE);
459 <        for (int i = 1; i < SIZE; i += 2) {
460 <            assertTrue(q.remove(new Integer(i)));
461 <        }
462 <        for (int i = 0; i < SIZE; i += 2) {
463 <            assertTrue(q.remove(new Integer(i)));
464 <            assertFalse(q.remove(new Integer(i + 1)));
465 <        }
466 <        assertTrue(q.isEmpty());
374 >        } catch (NoSuchElementException success) {}
375 >        checkEmpty(q);
376      }
377  
378      /**
379       * An add following remove(x) succeeds
380       */
381 <    public void testRemoveElementAndAdd() {
382 <        try {
383 <            LinkedTransferQueue q = new LinkedTransferQueue();
384 <            assertTrue(q.add(new Integer(1)));
385 <            assertTrue(q.add(new Integer(2)));
386 <            assertTrue(q.remove(new Integer(1)));
387 <            assertTrue(q.remove(new Integer(2)));
388 <            assertTrue(q.add(new Integer(3)));
480 <            assertTrue(q.take() != null);
481 <        } catch (Exception e) {
482 <            unexpectedException();
483 <        }
381 >    public void testRemoveElementAndAdd() throws InterruptedException {
382 >        LinkedTransferQueue q = new LinkedTransferQueue();
383 >        assertTrue(q.add(one));
384 >        assertTrue(q.add(two));
385 >        assertTrue(q.remove(one));
386 >        assertTrue(q.remove(two));
387 >        assertTrue(q.add(three));
388 >        assertSame(q.take(), three);
389      }
390  
391      /**
392       * contains(x) reports true when elements added but not yet removed
393       */
394      public void testContains() {
395 <        LinkedTransferQueue q = populatedQueue(SIZE);
395 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
396          for (int i = 0; i < SIZE; ++i) {
397 <            assertTrue(q.contains(new Integer(i)));
398 <            q.poll();
399 <            assertFalse(q.contains(new Integer(i)));
397 >            assertTrue(q.contains(i));
398 >            assertEquals(i, (int) q.poll());
399 >            assertFalse(q.contains(i));
400          }
401      }
402  
403      /**
404       * clear removes all elements
405       */
406 <    public void testClear() {
406 >    public void testClear() throws InterruptedException {
407          LinkedTransferQueue q = populatedQueue(SIZE);
503        int remainingCapacity = q.remainingCapacity();
408          q.clear();
409 <        assertTrue(q.isEmpty());
410 <        assertEquals(0, q.size());
507 <        assertEquals(remainingCapacity, q.remainingCapacity());
409 >        checkEmpty(q);
410 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
411          q.add(one);
412          assertFalse(q.isEmpty());
413 +        assertEquals(1, q.size());
414          assertTrue(q.contains(one));
415          q.clear();
416 <        assertTrue(q.isEmpty());
416 >        checkEmpty(q);
417      }
418  
419      /**
420       * containsAll(c) is true when c contains a subset of elements
421       */
422      public void testContainsAll() {
423 <        LinkedTransferQueue q = populatedQueue(SIZE);
424 <        LinkedTransferQueue p = new LinkedTransferQueue();
423 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
424 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
425          for (int i = 0; i < SIZE; ++i) {
426              assertTrue(q.containsAll(p));
427              assertFalse(p.containsAll(q));
428 <            p.add(new Integer(i));
428 >            p.add(i);
429          }
430          assertTrue(p.containsAll(q));
431      }
432  
433      /**
434 <     * retainAll(c) retains only those elements of c and reports true if changed
434 >     * retainAll(c) retains only those elements of c and reports true
435 >     * if changed
436       */
437      public void testRetainAll() {
438          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 546 | Line 451 | public class LinkedTransferQueueTest ext
451      }
452  
453      /**
454 <     * removeAll(c) removes only those elements of c and reports true if changed
454 >     * removeAll(c) removes only those elements of c and reports true
455 >     * if changed
456       */
457      public void testRemoveAll() {
458          for (int i = 1; i < SIZE; ++i) {
# Line 555 | Line 461 | public class LinkedTransferQueueTest ext
461              assertTrue(q.removeAll(p));
462              assertEquals(SIZE - i, q.size());
463              for (int j = 0; j < i; ++j) {
464 <                Integer I = (Integer) (p.remove());
559 <                assertFalse(q.contains(I));
464 >                assertFalse(q.contains(p.remove()));
465              }
466          }
467      }
468  
469      /**
470 <     * toArray contains all elements
470 >     * toArray() contains all elements in FIFO order
471       */
472      public void testToArray() {
473          LinkedTransferQueue q = populatedQueue(SIZE);
474          Object[] o = q.toArray();
475 <        try {
476 <            for (int i = 0; i < o.length; i++) {
572 <                assertEquals(o[i], q.take());
573 <            }
574 <        } catch (InterruptedException e) {
575 <            unexpectedException();
475 >        for (int i = 0; i < o.length; i++) {
476 >            assertSame(o[i], q.poll());
477          }
478      }
479  
480      /**
481 <     * toArray(a) contains all elements
481 >     * toArray(a) contains all elements in FIFO order
482       */
483      public void testToArray2() {
484 <        LinkedTransferQueue q = populatedQueue(SIZE);
484 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
485          Integer[] ints = new Integer[SIZE];
486 <        ints = (Integer[]) q.toArray(ints);
487 <        try {
488 <            for (int i = 0; i < ints.length; i++) {
489 <                assertEquals(ints[i], q.take());
589 <            }
590 <        } catch (InterruptedException e) {
591 <            unexpectedException();
592 <        }
593 <    }
594 <
595 <    /**
596 <     * toArray(null) throws NPE
597 <     */
598 <    public void testToArray_BadArg() {
599 <        try {
600 <            LinkedTransferQueue q = populatedQueue(SIZE);
601 <            Object o[] = q.toArray(null);
602 <            shouldThrow();
603 <        } catch (NullPointerException success) {
486 >        Integer[] array = q.toArray(ints);
487 >        assertSame(ints, array);
488 >        for (int i = 0; i < ints.length; i++) {
489 >            assertSame(ints[i], q.poll());
490          }
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);
613 <            Object o[] = q.toArray(new String[10]);
499 >            q.toArray(new String[10]);
500              shouldThrow();
501 <        } catch (ArrayStoreException success) {
616 <        }
501 >        } catch (ArrayStoreException success) {}
502      }
503  
504      /**
505       * iterator iterates through all elements
506       */
507 <    public void testIterator() {
507 >    public void testIterator() throws InterruptedException {
508          LinkedTransferQueue q = populatedQueue(SIZE);
509          Iterator it = q.iterator();
510 <        try {
511 <            while (it.hasNext()) {
512 <                assertEquals(it.next(), q.take());
513 <            }
514 <        } catch (InterruptedException e) {
515 <            unexpectedException();
516 <        }
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 >        assertEquals(i, SIZE);
520 >        assertIteratorExhausted(it);
521 >    }
522 >
523 >    /**
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
531 >     * iterator.remove() removes current element
532       */
533      public void testIteratorRemove() {
534          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 645 | 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 654 | Line 550 | public class LinkedTransferQueueTest ext
550       * iterator ordering is FIFO
551       */
552      public void testIteratorOrdering() {
553 <        final LinkedTransferQueue q = new LinkedTransferQueue();
554 <        int remainingCapacity = q.remainingCapacity();
553 >        final LinkedTransferQueue<Integer> q
554 >            = new LinkedTransferQueue<Integer>();
555 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
556          q.add(one);
557          q.add(two);
558          q.add(three);
559 <        assertEquals(remainingCapacity, q.remainingCapacity());
559 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
560          int k = 0;
561 <        for (Iterator it = q.iterator(); it.hasNext();) {
562 <            int i = ((Integer) (it.next())).intValue();
666 <            assertEquals(++k, i);
561 >        for (Integer n : q) {
562 >            assertEquals(++k, (int) n);
563          }
564          assertEquals(3, k);
565      }
# Line 676 | Line 572 | public class LinkedTransferQueueTest ext
572          q.add(one);
573          q.add(two);
574          q.add(three);
575 <        try {
576 <            for (Iterator it = q.iterator(); it.hasNext();) {
577 <                q.remove();
682 <                it.next();
683 <            }
684 <        } catch (ConcurrentModificationException e) {
685 <            unexpectedException();
575 >        for (Iterator it = q.iterator(); it.hasNext();) {
576 >            q.remove();
577 >            it.next();
578          }
579          assertEquals(0, q.size());
580      }
# Line 694 | Line 586 | public class LinkedTransferQueueTest ext
586          LinkedTransferQueue q = populatedQueue(SIZE);
587          String s = q.toString();
588          for (int i = 0; i < SIZE; ++i) {
589 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
589 >            assertTrue(s.contains(String.valueOf(i)));
590          }
591      }
592  
# Line 703 | Line 595 | public class LinkedTransferQueueTest ext
595       */
596      public void testOfferInExecutor() {
597          final LinkedTransferQueue q = new LinkedTransferQueue();
598 <        q.add(one);
707 <        q.add(two);
598 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
599          ExecutorService executor = Executors.newFixedThreadPool(2);
709        executor.execute(new Runnable() {
600  
601 <            public void run() {
602 <                try {
603 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
604 <                } catch (Exception e) {
605 <                    threadUnexpectedException();
606 <                }
607 <            }
608 <        });
609 <
610 <        executor.execute(new Runnable() {
611 <
612 <            public void run() {
723 <                try {
724 <                    Thread.sleep(SMALL_DELAY_MS);
725 <                    threadAssertEquals(one, q.take());
726 <                } catch (InterruptedException e) {
727 <                    threadUnexpectedException();
728 <                }
729 <            }
730 <        });
601 >        executor.execute(new CheckedRunnable() {
602 >            public void realRun() throws InterruptedException {
603 >                threadsStarted.await();
604 >                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
605 >            }});
606 >
607 >        executor.execute(new CheckedRunnable() {
608 >            public void realRun() throws InterruptedException {
609 >                threadsStarted.await();
610 >                assertSame(one, q.take());
611 >                checkEmpty(q);
612 >            }});
613  
614          joinPool(executor);
615      }
616  
617      /**
618 <     * poll retrieves elements across Executor threads
618 >     * timed poll retrieves elements across Executor threads
619       */
620      public void testPollInExecutor() {
621          final LinkedTransferQueue q = new LinkedTransferQueue();
622 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
623          ExecutorService executor = Executors.newFixedThreadPool(2);
741        executor.execute(new Runnable() {
624  
625 <            public void run() {
626 <                threadAssertNull(q.poll());
627 <                try {
628 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
629 <                    threadAssertTrue(q.isEmpty());
630 <                } catch (InterruptedException e) {
631 <                    threadUnexpectedException();
632 <                }
633 <            }
634 <        });
635 <
636 <        executor.execute(new Runnable() {
637 <
756 <            public void run() {
757 <                try {
758 <                    Thread.sleep(SMALL_DELAY_MS);
759 <                    q.put(one);
760 <                } catch (InterruptedException e) {
761 <                    threadUnexpectedException();
762 <                }
763 <            }
764 <        });
625 >        executor.execute(new CheckedRunnable() {
626 >            public void realRun() throws InterruptedException {
627 >                assertNull(q.poll());
628 >                threadsStarted.await();
629 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
630 >                checkEmpty(q);
631 >            }});
632 >
633 >        executor.execute(new CheckedRunnable() {
634 >            public void realRun() throws InterruptedException {
635 >                threadsStarted.await();
636 >                q.put(one);
637 >            }});
638  
639          joinPool(executor);
640      }
# Line 769 | Line 642 | public class LinkedTransferQueueTest ext
642      /**
643       * A deserialized serialized queue has same elements in same order
644       */
645 <    public void testSerialization() {
646 <        LinkedTransferQueue q = populatedQueue(SIZE);
647 <
648 <        try {
649 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
650 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
651 <            out.writeObject(q);
652 <            out.close();
653 <
654 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
655 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
783 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
784 <
785 <            assertEquals(q.size(), r.size());
786 <            while (!q.isEmpty()) {
787 <                assertEquals(q.remove(), r.remove());
788 <            }
789 <        } catch (Exception e) {
790 <            unexpectedException();
791 <        }
792 <    }
793 <
794 <    /**
795 <     * drainTo(null) throws NPE
796 <     */
797 <    public void testDrainToNull() {
798 <        LinkedTransferQueue q = populatedQueue(SIZE);
799 <        try {
800 <            q.drainTo(null);
801 <            shouldThrow();
802 <        } catch (NullPointerException success) {
803 <        }
804 <    }
805 <
806 <    /**
807 <     * drainTo(this) throws IAE
808 <     */
809 <    public void testDrainToSelf() {
810 <        LinkedTransferQueue q = populatedQueue(SIZE);
811 <        try {
812 <            q.drainTo(q);
813 <            shouldThrow();
814 <        } catch (IllegalArgumentException success) {
645 >    public void testSerialization() throws Exception {
646 >        Queue x = populatedQueue(SIZE);
647 >        Queue y = serialClone(x);
648 >
649 >        assertNotSame(y, x);
650 >        assertEquals(x.size(), y.size());
651 >        assertEquals(x.toString(), y.toString());
652 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
653 >        while (!x.isEmpty()) {
654 >            assertFalse(y.isEmpty());
655 >            assertEquals(x.remove(), y.remove());
656          }
657 +        assertTrue(y.isEmpty());
658      }
659  
660      /**
# Line 822 | Line 664 | public class LinkedTransferQueueTest ext
664          LinkedTransferQueue q = populatedQueue(SIZE);
665          ArrayList l = new ArrayList();
666          q.drainTo(l);
667 <        assertEquals(q.size(), 0);
668 <        assertEquals(l.size(), SIZE);
667 >        assertEquals(0, q.size());
668 >        assertEquals(SIZE, l.size());
669          for (int i = 0; i < SIZE; ++i) {
670 <            assertEquals(l.get(i), new Integer(i));
670 >            assertEquals(i, l.get(i));
671          }
672          q.add(zero);
673          q.add(one);
# Line 834 | Line 676 | public class LinkedTransferQueueTest ext
676          assertTrue(q.contains(one));
677          l.clear();
678          q.drainTo(l);
679 <        assertEquals(q.size(), 0);
680 <        assertEquals(l.size(), 2);
679 >        assertEquals(0, q.size());
680 >        assertEquals(2, l.size());
681          for (int i = 0; i < 2; ++i) {
682 <            assertEquals(l.get(i), new Integer(i));
682 >            assertEquals(i, l.get(i));
683          }
684      }
685  
686      /**
687 <     * drainTo empties full queue, unblocking a waiting put.
687 >     * drainTo(c) empties full queue, unblocking a waiting put.
688       */
689 <    public void testDrainToWithActivePut() {
689 >    public void testDrainToWithActivePut() throws InterruptedException {
690          final LinkedTransferQueue q = populatedQueue(SIZE);
691 <        Thread t = new Thread(new Runnable() {
692 <
693 <            public void run() {
694 <                try {
695 <                    q.put(new Integer(SIZE + 1));
696 <                } catch (Exception ie) {
697 <                    threadUnexpectedException();
698 <                }
699 <            }
700 <        });
701 <        try {
860 <            t.start();
861 <            ArrayList l = new ArrayList();
862 <            q.drainTo(l);
863 <            assertTrue(l.size() >= SIZE);
864 <            for (int i = 0; i < SIZE; ++i) {
865 <                assertEquals(l.get(i), new Integer(i));
866 <            }
867 <            t.join();
868 <            assertTrue(q.size() + l.size() >= SIZE);
869 <        } catch (Exception e) {
870 <            unexpectedException();
871 <        }
872 <    }
873 <
874 <    /**
875 <     * drainTo(null, n) throws NPE
876 <     */
877 <    public void testDrainToNullN() {
878 <        LinkedTransferQueue q = populatedQueue(SIZE);
879 <        try {
880 <            q.drainTo(null, 0);
881 <            shouldThrow();
882 <        } catch (NullPointerException success) {
883 <        }
884 <    }
885 <
886 <    /**
887 <     * drainTo(this, n) throws IAE
888 <     */
889 <    public void testDrainToSelfN() {
890 <        LinkedTransferQueue q = populatedQueue(SIZE);
891 <        try {
892 <            q.drainTo(q, 0);
893 <            shouldThrow();
894 <        } catch (IllegalArgumentException success) {
895 <        }
691 >        Thread t = newStartedThread(new CheckedRunnable() {
692 >            public void realRun() {
693 >                q.put(SIZE + 1);
694 >            }});
695 >        ArrayList l = new ArrayList();
696 >        q.drainTo(l);
697 >        assertTrue(l.size() >= SIZE);
698 >        for (int i = 0; i < SIZE; ++i)
699 >            assertEquals(i, l.get(i));
700 >        awaitTermination(t, MEDIUM_DELAY_MS);
701 >        assertTrue(q.size() + l.size() >= SIZE);
702      }
703  
704      /**
705 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
705 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
706       */
707      public void testDrainToN() {
708          LinkedTransferQueue q = new LinkedTransferQueue();
709          for (int i = 0; i < SIZE + 2; ++i) {
710              for (int j = 0; j < SIZE; j++) {
711 <                assertTrue(q.offer(new Integer(j)));
711 >                assertTrue(q.offer(j));
712              }
713              ArrayList l = new ArrayList();
714              q.drainTo(l, i);
715              int k = (i < SIZE) ? i : SIZE;
716 <            assertEquals(l.size(), k);
717 <            assertEquals(q.size(), SIZE - k);
718 <            for (int j = 0; j < k; ++j) {
719 <                assertEquals(l.get(j), new Integer(j));
720 <            }
915 <            while (q.poll() != null);
716 >            assertEquals(k, l.size());
717 >            assertEquals(SIZE - k, q.size());
718 >            for (int j = 0; j < k; ++j)
719 >                assertEquals(j, l.get(j));
720 >            do {} while (q.poll() != null);
721          }
722      }
723  
724 <    /*
725 <     * poll and take should decrement the waiting consumer count
724 >    /**
725 >     * timed poll() or take() increments the waiting consumer count;
726 >     * offer(e) decrements the waiting consumer count
727       */
728 <    public void testWaitingConsumer() {
729 <        try {
730 <            final LinkedTransferQueue q = new LinkedTransferQueue();
731 <            final ConsumerObserver waiting = new ConsumerObserver();
732 <            new Thread(new Runnable() {
927 <
928 <                public void run() {
929 <                    try {
930 <                        threadAssertTrue(q.hasWaitingConsumer());
931 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
932 <                        threadAssertTrue(q.offer(new Object()));
933 <                    } catch (Exception ex) {
934 <                        threadUnexpectedException();
935 <                    }
728 >    public void testWaitingConsumer() throws InterruptedException {
729 >        final LinkedTransferQueue q = new LinkedTransferQueue();
730 >        assertEquals(0, q.getWaitingConsumerCount());
731 >        assertFalse(q.hasWaitingConsumer());
732 >        final CountDownLatch threadStarted = new CountDownLatch(1);
733  
734 <                }
735 <            }).start();
736 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
737 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
738 <        } catch (Exception ex) {
739 <            this.unexpectedException();
740 <        }
734 >        Thread t = newStartedThread(new CheckedRunnable() {
735 >            public void realRun() throws InterruptedException {
736 >                threadStarted.countDown();
737 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
738 >                assertEquals(0, q.getWaitingConsumerCount());
739 >                assertFalse(q.hasWaitingConsumer());
740 >            }});
741 >
742 >        threadStarted.await();
743 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
744 >        assertEquals(1, q.getWaitingConsumerCount());
745 >        assertTrue(q.hasWaitingConsumer());
746 >
747 >        assertTrue(q.offer(one));
748 >        assertEquals(0, q.getWaitingConsumerCount());
749 >        assertFalse(q.hasWaitingConsumer());
750 >
751 >        awaitTermination(t, MEDIUM_DELAY_MS);
752      }
945    /*
946     * Inserts null into transfer throws NPE
947     */
753  
754 <    public void testTransfer1() {
754 >    /**
755 >     * transfer(null) throws NullPointerException
756 >     */
757 >    public void testTransfer1() throws InterruptedException {
758          try {
759              LinkedTransferQueue q = new LinkedTransferQueue();
760              q.transfer(null);
761              shouldThrow();
762 <        } catch (NullPointerException ex) {
955 <        } catch (Exception ex) {
956 <            this.unexpectedException();
957 <        }
762 >        } catch (NullPointerException success) {}
763      }
764  
765 <    /*
766 <     * transfer attempts to insert into the queue then wait until that
767 <     * object is removed via take or poll.
765 >    /**
766 >     * transfer waits until a poll occurs. The transfered element
767 >     * is returned by this associated poll.
768       */
769 <    public void testTransfer2() {
770 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
771 <        new Thread(new Runnable() {
769 >    public void testTransfer2() throws InterruptedException {
770 >        final LinkedTransferQueue<Integer> q
771 >            = new LinkedTransferQueue<Integer>();
772 >        final CountDownLatch threadStarted = new CountDownLatch(1);
773  
774 <            public void run() {
775 <                try {
776 <                    q.transfer(new Integer(SIZE));
777 <                    threadAssertTrue(q.isEmpty());
778 <                } catch (Exception ex) {
779 <                    threadUnexpectedException();
974 <                }
975 <            }
976 <        }).start();
774 >        Thread t = newStartedThread(new CheckedRunnable() {
775 >            public void realRun() throws InterruptedException {
776 >                threadStarted.countDown();
777 >                q.transfer(five);
778 >                checkEmpty(q);
779 >            }});
780  
781 <        try {
782 <            Thread.sleep(SHORT_DELAY_MS);
783 <            assertEquals(1, q.size());
784 <            q.poll();
785 <            assertTrue(q.isEmpty());
786 <        } catch (Exception ex) {
984 <            this.unexpectedException();
985 <        }
986 <    }
987 <    /*
988 <     * transfer will attempt to transfer in fifo order and continue waiting if
989 <     * the element being transfered is not polled or taken
990 <     */
991 <
992 <    public void testTransfer3() {
993 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
994 <        new Thread(new Runnable() {
995 <                public void run() {
996 <                    try {
997 <                        Integer i;
998 <                        q.transfer((i = new Integer(SIZE + 1)));
999 <                        threadAssertTrue(!q.contains(i));
1000 <                        threadAssertEquals(1, q.size());
1001 <                    } catch (Exception ex) {
1002 <                        threadUnexpectedException();
1003 <                    }
1004 <                }
1005 <            }).start();
1006 <        Thread interruptedThread =
1007 <            new Thread(new Runnable() {
1008 <                    public void run() {
1009 <                        try {
1010 <                            q.transfer(new Integer(SIZE));
1011 <                            threadShouldThrow();
1012 <                        } catch (InterruptedException ex) {
1013 <                        }
1014 <                    }
1015 <                });
1016 <        interruptedThread.start();
1017 <        try {
1018 <            Thread.sleep(LONG_DELAY_MS);
1019 <            assertEquals(2, q.size());
1020 <            q.poll();
1021 <            Thread.sleep(LONG_DELAY_MS);
1022 <            interruptedThread.interrupt();
1023 <            assertEquals(1, q.size());
1024 <        } catch (Exception ex) {
1025 <            this.unexpectedException();
1026 <        }
781 >        threadStarted.await();
782 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
783 >        assertEquals(1, q.size());
784 >        assertSame(five, q.poll());
785 >        checkEmpty(q);
786 >        awaitTermination(t, MEDIUM_DELAY_MS);
787      }
788  
789      /**
790 <     * transfer will wait as long as a poll or take occurs if one does occur
1031 <     * the waiting is finished and the thread that tries to poll/take
1032 <     * wins in retrieving the element
790 >     * transfer waits until a poll occurs, and then transfers in fifo order
791       */
792 <    public void testTransfer4() {
792 >    public void testTransfer3() throws InterruptedException {
793 >        final LinkedTransferQueue<Integer> q
794 >            = new LinkedTransferQueue<Integer>();
795 >
796 >        Thread first = newStartedThread(new CheckedRunnable() {
797 >            public void realRun() throws InterruptedException {
798 >                q.transfer(four);
799 >                assertTrue(!q.contains(four));
800 >                assertEquals(1, q.size());
801 >            }});
802 >
803 >        Thread interruptedThread = newStartedThread(
804 >            new CheckedInterruptedRunnable() {
805 >                public void realRun() throws InterruptedException {
806 >                    while (q.isEmpty())
807 >                        Thread.yield();
808 >                    q.transfer(five);
809 >                }});
810 >
811 >        while (q.size() < 2)
812 >            Thread.yield();
813 >        assertEquals(2, q.size());
814 >        assertSame(four, q.poll());
815 >        first.join();
816 >        assertEquals(1, q.size());
817 >        interruptedThread.interrupt();
818 >        interruptedThread.join();
819 >        checkEmpty(q);
820 >    }
821 >
822 >    /**
823 >     * transfer waits until a poll occurs, at which point the polling
824 >     * thread returns the element
825 >     */
826 >    public void testTransfer4() throws InterruptedException {
827          final LinkedTransferQueue q = new LinkedTransferQueue();
1036        new Thread(new Runnable() {
828  
829 <            public void run() {
830 <                try {
831 <                    q.transfer(new Integer(four));
832 <                    threadAssertFalse(q.contains(new Integer(four)));
833 <                    threadAssertEquals(new Integer(three), q.poll());
834 <                } catch (Exception ex) {
835 <                    threadUnexpectedException();
836 <                }
837 <            }
838 <        }).start();
839 <        try {
840 <            Thread.sleep(MEDIUM_DELAY_MS);
841 <            assertTrue(q.offer(three));
842 <            assertEquals(new Integer(four), q.poll());
1052 <        } catch (Exception ex) {
1053 <            this.unexpectedException();
1054 <        }
829 >        Thread t = newStartedThread(new CheckedRunnable() {
830 >            public void realRun() throws InterruptedException {
831 >                q.transfer(four);
832 >                assertFalse(q.contains(four));
833 >                assertSame(three, q.poll());
834 >            }});
835 >
836 >        while (q.isEmpty())
837 >            Thread.yield();
838 >        assertFalse(q.isEmpty());
839 >        assertEquals(1, q.size());
840 >        assertTrue(q.offer(three));
841 >        assertSame(four, q.poll());
842 >        awaitTermination(t, MEDIUM_DELAY_MS);
843      }
844 <    /*
845 <     * Insert null into trTransfer throws NPE
844 >
845 >    /**
846 >     * transfer waits until a take occurs. The transfered element
847 >     * is returned by this associated take.
848       */
849 +    public void testTransfer5() throws InterruptedException {
850 +        final LinkedTransferQueue<Integer> q
851 +            = new LinkedTransferQueue<Integer>();
852 +
853 +        Thread t = newStartedThread(new CheckedRunnable() {
854 +            public void realRun() throws InterruptedException {
855 +                q.transfer(four);
856 +                checkEmpty(q);
857 +            }});
858  
859 +        while (q.isEmpty())
860 +            Thread.yield();
861 +        assertFalse(q.isEmpty());
862 +        assertEquals(1, q.size());
863 +        assertSame(four, q.take());
864 +        checkEmpty(q);
865 +        awaitTermination(t, MEDIUM_DELAY_MS);
866 +    }
867 +
868 +    /**
869 +     * tryTransfer(null) throws NullPointerException
870 +     */
871      public void testTryTransfer1() {
872 +        final LinkedTransferQueue q = new LinkedTransferQueue();
873          try {
1062            final LinkedTransferQueue q = new LinkedTransferQueue();
874              q.tryTransfer(null);
875 <            this.shouldThrow();
876 <        } catch (NullPointerException ex) {
1066 <        } catch (Exception ex) {
1067 <            this.unexpectedException();
1068 <        }
875 >            shouldThrow();
876 >        } catch (NullPointerException success) {}
877      }
1070    /*
1071     * tryTransfer returns false and does not enqueue if there are no consumers
1072     * waiting to poll or take.
1073     */
878  
879 <    public void testTryTransfer2() {
880 <        try {
881 <            final LinkedTransferQueue q = new LinkedTransferQueue();
882 <            assertFalse(q.tryTransfer(new Object()));
883 <            assertEquals(0, q.size());
884 <        } catch (Exception ex) {
885 <            this.unexpectedException();
886 <        }
879 >    /**
880 >     * tryTransfer returns false and does not enqueue if there are no
881 >     * consumers waiting to poll or take.
882 >     */
883 >    public void testTryTransfer2() throws InterruptedException {
884 >        final LinkedTransferQueue q = new LinkedTransferQueue();
885 >        assertFalse(q.tryTransfer(new Object()));
886 >        assertFalse(q.hasWaitingConsumer());
887 >        checkEmpty(q);
888      }
889 <    /*
890 <     * if there is a consumer waiting poll or take tryTransfer returns
891 <     * true while enqueueing object
889 >
890 >    /**
891 >     * If there is a consumer waiting in timed poll, tryTransfer
892 >     * returns true while successfully transfering object.
893       */
894 +    public void testTryTransfer3() throws InterruptedException {
895 +        final Object hotPotato = new Object();
896 +        final LinkedTransferQueue q = new LinkedTransferQueue();
897  
898 <    public void testTryTransfer3() {
899 <        try {
900 <            final LinkedTransferQueue q = new LinkedTransferQueue();
901 <            new Thread(new Runnable() {
898 >        Thread t = newStartedThread(new CheckedRunnable() {
899 >            public void realRun() {
900 >                while (! q.hasWaitingConsumer())
901 >                    Thread.yield();
902 >                assertTrue(q.hasWaitingConsumer());
903 >                checkEmpty(q);
904 >                assertTrue(q.tryTransfer(hotPotato));
905 >            }});
906 >
907 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
908 >        checkEmpty(q);
909 >        awaitTermination(t, MEDIUM_DELAY_MS);
910 >    }
911  
912 <                public void run() {
913 <                    try {
914 <                        threadAssertTrue(q.hasWaitingConsumer());
915 <                        threadAssertTrue(q.tryTransfer(new Object()));
916 <                    } catch (Exception ex) {
917 <                        threadUnexpectedException();
918 <                    }
912 >    /**
913 >     * If there is a consumer waiting in take, tryTransfer returns
914 >     * true while successfully transfering object.
915 >     */
916 >    public void testTryTransfer4() throws InterruptedException {
917 >        final Object hotPotato = new Object();
918 >        final LinkedTransferQueue q = new LinkedTransferQueue();
919  
920 <                }
921 <            }).start();
922 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
923 <            assertTrue(q.isEmpty());
924 <        } catch (Exception ex) {
925 <            this.unexpectedException();
926 <        }
920 >        Thread t = newStartedThread(new CheckedRunnable() {
921 >            public void realRun() {
922 >                while (! q.hasWaitingConsumer())
923 >                    Thread.yield();
924 >                assertTrue(q.hasWaitingConsumer());
925 >                checkEmpty(q);
926 >                assertTrue(q.tryTransfer(hotPotato));
927 >            }});
928 >
929 >        assertSame(q.take(), hotPotato);
930 >        checkEmpty(q);
931 >        awaitTermination(t, MEDIUM_DELAY_MS);
932      }
933  
934 <    /*
935 <     * tryTransfer waits the amount given if interrupted, show an
1113 <     * interrupted exception
934 >    /**
935 >     * tryTransfer blocks interruptibly if no takers
936       */
937 <    public void testTryTransfer4() {
937 >    public void testTryTransfer5() throws InterruptedException {
938          final LinkedTransferQueue q = new LinkedTransferQueue();
939 <        Thread toInterrupt = new Thread(new Runnable() {
939 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
940 >        assertTrue(q.isEmpty());
941  
942 <            public void run() {
942 >        Thread t = newStartedThread(new CheckedRunnable() {
943 >            public void realRun() throws InterruptedException {
944 >                Thread.currentThread().interrupt();
945                  try {
946 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
947 <                    threadShouldThrow();
948 <                } catch (InterruptedException ex) {
949 <                }
950 <            }
951 <        });
952 <        try {
953 <            toInterrupt.start();
954 <            Thread.sleep(SMALL_DELAY_MS);
955 <            toInterrupt.interrupt();
956 <        } catch (Exception ex) {
957 <            this.unexpectedException();
958 <        }
946 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
947 >                    shouldThrow();
948 >                } catch (InterruptedException success) {}
949 >                assertFalse(Thread.interrupted());
950 >
951 >                pleaseInterrupt.countDown();
952 >                try {
953 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
954 >                    shouldThrow();
955 >                } catch (InterruptedException success) {}
956 >                assertFalse(Thread.interrupted());
957 >            }});
958 >
959 >        await(pleaseInterrupt);
960 >        assertThreadStaysAlive(t);
961 >        t.interrupt();
962 >        awaitTermination(t);
963 >        checkEmpty(q);
964      }
965  
966 <    /*
967 <     * tryTransfer gives up after the timeout and return false
966 >    /**
967 >     * tryTransfer gives up after the timeout and returns false
968       */
969 <    public void testTryTransfer5() {
969 >    public void testTryTransfer6() throws InterruptedException {
970          final LinkedTransferQueue q = new LinkedTransferQueue();
1141        try {
1142            new Thread(new Runnable() {
971  
972 <                public void run() {
973 <                    try {
974 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
975 <                    } catch (InterruptedException ex) {
976 <                        threadUnexpectedException();
977 <                    }
978 <                }
979 <            }).start();
980 <            Thread.sleep(LONG_DELAY_MS);
981 <            assertTrue(q.isEmpty());
982 <        } catch (Exception ex) {
1155 <            this.unexpectedException();
1156 <        }
972 >        Thread t = newStartedThread(new CheckedRunnable() {
973 >            public void realRun() throws InterruptedException {
974 >                long t0 = System.nanoTime();
975 >                assertFalse(q.tryTransfer(new Object(),
976 >                                          timeoutMillis(), MILLISECONDS));
977 >                assertTrue(millisElapsedSince(t0) >= timeoutMillis());
978 >                checkEmpty(q);
979 >            }});
980 >
981 >        awaitTermination(t);
982 >        checkEmpty(q);
983      }
984  
985 <    /*
985 >    /**
986       * tryTransfer waits for any elements previously in to be removed
987       * before transfering to a poll or take
988       */
989 <    public void testTryTransfer6() {
989 >    public void testTryTransfer7() throws InterruptedException {
990          final LinkedTransferQueue q = new LinkedTransferQueue();
991 <        q.offer(new Integer(four));
1166 <        new Thread(new Runnable() {
991 >        assertTrue(q.offer(four));
992  
993 <            public void run() {
994 <                try {
995 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
996 <                    threadAssertTrue(q.isEmpty());
997 <                } catch (InterruptedException ex) {
998 <                    threadUnexpectedException();
999 <                }
1000 <            }
1001 <        }).start();
1002 <        try {
1003 <            Thread.sleep(SHORT_DELAY_MS);
1004 <            assertEquals(2, q.size());
1005 <            assertEquals(new Integer(four), q.poll());
1181 <            assertEquals(new Integer(five), q.poll());
1182 <            assertTrue(q.isEmpty());
1183 <        } catch (Exception ex) {
1184 <            this.unexpectedException();
1185 <        }
993 >        Thread t = newStartedThread(new CheckedRunnable() {
994 >            public void realRun() throws InterruptedException {
995 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
996 >                checkEmpty(q);
997 >            }});
998 >
999 >        while (q.size() != 2)
1000 >            Thread.yield();
1001 >        assertEquals(2, q.size());
1002 >        assertSame(four, q.poll());
1003 >        assertSame(five, q.poll());
1004 >        checkEmpty(q);
1005 >        awaitTermination(t, MEDIUM_DELAY_MS);
1006      }
1007  
1008 <    /*
1009 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1010 <     * enqueueing and the successing poll is null
1008 >    /**
1009 >     * tryTransfer attempts to enqueue into the queue and fails
1010 >     * returning false not enqueueing and the successive poll is null
1011       */
1012 <    public void testTryTransfer7() {
1012 >    public void testTryTransfer8() throws InterruptedException {
1013          final LinkedTransferQueue q = new LinkedTransferQueue();
1014 <        q.offer(new Integer(four));
1015 <        new Thread(new Runnable() {
1016 <
1017 <            public void run() {
1018 <                try {
1019 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1020 <                    threadAssertTrue(q.isEmpty());
1021 <                } catch (InterruptedException ex) {
1022 <                    threadUnexpectedException();
1203 <                }
1204 <            }
1205 <        }).start();
1206 <        try {
1207 <            assertEquals(1, q.size());
1208 <            assertEquals(new Integer(four), q.poll());
1209 <            Thread.sleep(MEDIUM_DELAY_MS);
1210 <            assertNull(q.poll());
1211 <        } catch (Exception ex) {
1212 <            this.unexpectedException();
1213 <        }
1014 >        assertTrue(q.offer(four));
1015 >        assertEquals(1, q.size());
1016 >        long t0 = System.nanoTime();
1017 >        assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS));
1018 >        assertTrue(millisElapsedSince(t0) >= timeoutMillis());
1019 >        assertEquals(1, q.size());
1020 >        assertSame(four, q.poll());
1021 >        assertNull(q.poll());
1022 >        checkEmpty(q);
1023      }
1024  
1025 <    private LinkedTransferQueue populatedQueue(
1026 <            int n) {
1027 <        LinkedTransferQueue q = new LinkedTransferQueue();
1028 <        assertTrue(q.isEmpty());
1029 <        int remainingCapacity = q.remainingCapacity();
1221 <        for (int i = 0; i <
1222 <                n; i++) {
1025 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1026 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1027 >        checkEmpty(q);
1028 >        for (int i = 0; i < n; i++) {
1029 >            assertEquals(i, q.size());
1030              assertTrue(q.offer(i));
1031 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1032          }
1225
1033          assertFalse(q.isEmpty());
1227        assertEquals(remainingCapacity, q.remainingCapacity());
1228        assertEquals(n, q.size());
1034          return q;
1035      }
1036  
1037 <    private static class ConsumerObserver {
1038 <
1039 <        private int waitingConsumers;
1040 <
1041 <        private ConsumerObserver() {
1042 <        }
1043 <
1044 <        private void setWaitingConsumer(int i) {
1045 <            this.waitingConsumers = i;
1046 <        }
1047 <
1048 <        private int getWaitingConsumers() {
1244 <            return waitingConsumers;
1037 >    /**
1038 >     * remove(null), contains(null) always return false
1039 >     */
1040 >    public void testNeverContainsNull() {
1041 >        Collection<?>[] qs = {
1042 >            new LinkedTransferQueue<Object>(),
1043 >            populatedQueue(2),
1044 >        };
1045 >
1046 >        for (Collection<?> q : qs) {
1047 >            assertFalse(q.contains(null));
1048 >            assertFalse(q.remove(null));
1049          }
1050      }
1051   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines