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.4 by jsr166, Sat Aug 1 22:09:13 2009 UTC vs.
Revision 1.34 by jsr166, Fri Oct 29 07:01:51 2010 UTC

# Line 13 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
16 import java.util.ConcurrentModificationException;
16   import java.util.Iterator;
17 + import java.util.List;
18   import java.util.NoSuchElementException;
19   import java.util.concurrent.*;
20 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
22   import junit.framework.Test;
23   import junit.framework.TestSuite;
24  
25 + @SuppressWarnings({"unchecked", "rawtypes"})
26   public class LinkedTransferQueueTest extends JSR166TestCase {
27  
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());
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(LinkedTransferQueueTest.class);
39 >        return newTestSuite(LinkedTransferQueueTest.class,
40 >                            new Generic().testSuite());
41 >    }
42 >
43 >    void checkEmpty(BlockingQueue q) {
44 >        try {
45 >            assertTrue(q.isEmpty());
46 >            assertEquals(0, q.size());
47 >            assertNull(q.peek());
48 >            assertNull(q.poll());
49 >            assertNull(q.poll(0, MILLISECONDS));
50 >            assertEquals(q.toString(), "[]");
51 >            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
52 >            assertFalse(q.iterator().hasNext());
53 >            try {
54 >                q.element();
55 >                shouldThrow();
56 >            } catch (NoSuchElementException success) {}
57 >            try {
58 >                q.iterator().next();
59 >                shouldThrow();
60 >            } catch (NoSuchElementException success) {}
61 >            try {
62 >                q.remove();
63 >                shouldThrow();
64 >            } catch (NoSuchElementException success) {}
65 >        } catch (InterruptedException ie) {
66 >            threadUnexpectedException(ie);
67 >        }
68      }
69  
70      /**
71 <     * Constructor builds new queue with size being zero and empty being true
71 >     * Constructor builds new queue with size being zero and empty
72 >     * being true
73       */
74      public void testConstructor1() {
75          assertEquals(0, new LinkedTransferQueue().size());
# Line 39 | Line 77 | public class LinkedTransferQueueTest ext
77      }
78  
79      /**
80 <     * Initializing constructor with null collection throws NPE
80 >     * Initializing constructor with null collection throws
81 >     * NullPointerException
82       */
83      public void testConstructor2() {
84          try {
85              new LinkedTransferQueue(null);
86              shouldThrow();
87 <        } catch (NullPointerException success) {
49 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
91 <     * Initializing from Collection of null elements throws NPE
91 >     * Initializing from Collection of null elements throws
92 >     * NullPointerException
93       */
94      public void testConstructor3() {
95          try {
96              Integer[] ints = new Integer[SIZE];
97 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
97 >            new LinkedTransferQueue(Arrays.asList(ints));
98              shouldThrow();
99 <        } catch (NullPointerException success) {
61 <        }
99 >        } catch (NullPointerException success) {}
100      }
101  
102      /**
103       * Initializing constructor with a collection containing some null elements
104 <     * throws NPE
104 >     * throws NullPointerException
105       */
106      public void testConstructor4() {
107          try {
108              Integer[] ints = new Integer[SIZE];
109              for (int i = 0; i < SIZE - 1; ++i) {
110 <                ints[i] = new Integer(i);
110 >                ints[i] = i;
111              }
112 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
112 >            new LinkedTransferQueue(Arrays.asList(ints));
113              shouldThrow();
114 <        } catch (NullPointerException success) {
77 <        }
114 >        } catch (NullPointerException success) {}
115      }
116  
117      /**
118       * Queue contains all elements of the collection it is initialized by
119       */
120      public void testConstructor5() {
121 <        try {
122 <            Integer[] ints = new Integer[SIZE];
123 <            for (int i = 0; i < SIZE; ++i) {
124 <                ints[i] = new Integer(i);
125 <            }
126 <            LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints));
127 <            for (int i = 0; i < SIZE; ++i) {
128 <                assertEquals(ints[i], q.poll());
129 <            }
130 <        } finally {
121 >        Integer[] ints = new Integer[SIZE];
122 >        for (int i = 0; i < SIZE; ++i) {
123 >            ints[i] = i;
124 >        }
125 >        List intList = Arrays.asList(ints);
126 >        LinkedTransferQueue q
127 >            = new LinkedTransferQueue(intList);
128 >        assertEquals(q.size(), intList.size());
129 >        assertEquals(q.toString(), intList.toString());
130 >        assertTrue(Arrays.equals(q.toArray(),
131 >                                     intList.toArray()));
132 >        assertTrue(Arrays.equals(q.toArray(new Object[0]),
133 >                                 intList.toArray(new Object[0])));
134 >        assertTrue(Arrays.equals(q.toArray(new Object[SIZE]),
135 >                                 intList.toArray(new Object[SIZE])));
136 >        for (int i = 0; i < SIZE; ++i) {
137 >            assertEquals(ints[i], q.poll());
138          }
139      }
140  
141      /**
142 <     * Remaining capacity never decrease nor increase on add or remove
142 >     * remainingCapacity() always returns Integer.MAX_VALUE
143       */
144      public void testRemainingCapacity() {
145 <        LinkedTransferQueue q = populatedQueue(SIZE);
102 <        int remainingCapacity = q.remainingCapacity();
145 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
146          for (int i = 0; i < SIZE; ++i) {
147 <            assertEquals(remainingCapacity, q.remainingCapacity());
147 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
148              assertEquals(SIZE - i, q.size());
149              q.remove();
150          }
151          for (int i = 0; i < SIZE; ++i) {
152 <            assertEquals(remainingCapacity, q.remainingCapacity());
152 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
153              assertEquals(i, q.size());
154 <            q.add(new Integer(i));
154 >            q.add(i);
155          }
156      }
157  
158      /**
159 <     * offer(null) throws NPE
159 >     * offer(null) throws NullPointerException
160       */
161      public void testOfferNull() {
162          try {
163              LinkedTransferQueue q = new LinkedTransferQueue();
164              q.offer(null);
165              shouldThrow();
166 <        } catch (NullPointerException success) {
124 <        }
166 >        } catch (NullPointerException success) {}
167      }
168  
169      /**
170 <     * add(null) throws NPE
170 >     * add(null) throws NullPointerException
171       */
172      public void testAddNull() {
173          try {
174              LinkedTransferQueue q = new LinkedTransferQueue();
175              q.add(null);
176              shouldThrow();
177 <        } catch (NullPointerException success) {
136 <        }
177 >        } catch (NullPointerException success) {}
178      }
179  
180      /**
181 <     * addAll(null) throws NPE
181 >     * addAll(null) throws NullPointerException
182       */
183      public void testAddAll1() {
184          try {
185              LinkedTransferQueue q = new LinkedTransferQueue();
186              q.addAll(null);
187              shouldThrow();
188 <        } catch (NullPointerException success) {
148 <        }
188 >        } catch (NullPointerException success) {}
189      }
190  
191      /**
192 <     * addAll(this) throws IAE
192 >     * addAll(this) throws IllegalArgumentException
193       */
194      public void testAddAllSelf() {
195          try {
196              LinkedTransferQueue q = populatedQueue(SIZE);
197              q.addAll(q);
198              shouldThrow();
199 <        } catch (IllegalArgumentException success) {
160 <        }
199 >        } catch (IllegalArgumentException success) {}
200      }
201  
202      /**
203 <     * addAll of a collection with null elements throws NPE
203 >     * addAll of a collection with null elements throws NullPointerException
204       */
205      public void testAddAll2() {
206          try {
# Line 169 | Line 208 | public class LinkedTransferQueueTest ext
208              Integer[] ints = new Integer[SIZE];
209              q.addAll(Arrays.asList(ints));
210              shouldThrow();
211 <        } catch (NullPointerException success) {
173 <        }
211 >        } catch (NullPointerException success) {}
212      }
213  
214      /**
215 <     * addAll of a collection with any null elements throws NPE after
216 <     * possibly adding some elements
215 >     * addAll of a collection with any null elements throws
216 >     * NullPointerException after possibly adding some elements
217       */
218      public void testAddAll3() {
219          try {
220              LinkedTransferQueue q = new LinkedTransferQueue();
221              Integer[] ints = new Integer[SIZE];
222              for (int i = 0; i < SIZE - 1; ++i) {
223 <                ints[i] = new Integer(i);
223 >                ints[i] = i;
224              }
225              q.addAll(Arrays.asList(ints));
226              shouldThrow();
227 <        } catch (NullPointerException success) {
190 <        }
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
231       * Queue contains all elements, in traversal order, of successful addAll
232       */
233      public void testAddAll5() {
234 <        try {
235 <            Integer[] empty = new Integer[0];
236 <            Integer[] ints = new Integer[SIZE];
237 <            for (int i = 0; i < SIZE; ++i) {
238 <                ints[i] = new Integer(i);
239 <            }
240 <            LinkedTransferQueue q = new LinkedTransferQueue();
241 <            assertFalse(q.addAll(Arrays.asList(empty)));
242 <            assertTrue(q.addAll(Arrays.asList(ints)));
243 <            for (int i = 0; i < SIZE; ++i) {
207 <                assertEquals(ints[i], q.poll());
208 <            }
209 <        } finally {
234 >        Integer[] empty = new Integer[0];
235 >        Integer[] ints = new Integer[SIZE];
236 >        for (int i = 0; i < SIZE; ++i) {
237 >            ints[i] = i;
238 >        }
239 >        LinkedTransferQueue q = new LinkedTransferQueue();
240 >        assertFalse(q.addAll(Arrays.asList(empty)));
241 >        assertTrue(q.addAll(Arrays.asList(ints)));
242 >        for (int i = 0; i < SIZE; ++i) {
243 >            assertEquals(ints[i], q.poll());
244          }
245      }
246  
247      /**
248 <     * put(null) throws NPE
248 >     * put(null) throws NullPointerException
249       */
250 <    public void testPutNull() {
250 >    public void testPutNull() throws InterruptedException {
251          try {
252              LinkedTransferQueue q = new LinkedTransferQueue();
253              q.put(null);
254              shouldThrow();
255 <        } catch (NullPointerException success) {
222 <        } catch (Exception ie) {
223 <            unexpectedException();
224 <        }
255 >        } catch (NullPointerException success) {}
256      }
257  
258      /**
259       * all elements successfully put are contained
260       */
261      public void testPut() {
262 <        try {
263 <            LinkedTransferQueue q = new LinkedTransferQueue();
264 <            for (int i = 0; i < SIZE; ++i) {
265 <                Integer I = new Integer(i);
266 <                q.put(I);
236 <                assertTrue(q.contains(I));
237 <            }
238 <        } catch (Exception ie) {
239 <            unexpectedException();
262 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
263 >        for (int i = 0; i < SIZE; ++i) {
264 >            assertEquals(q.size(), i);
265 >            q.put(i);
266 >            assertTrue(q.contains(i));
267          }
268      }
269  
270      /**
271       * take retrieves elements in FIFO order
272       */
273 <    public void testTake() {
274 <        try {
275 <            LinkedTransferQueue q = populatedQueue(SIZE);
276 <            for (int i = 0; i < SIZE; ++i) {
250 <                assertEquals(i, ((Integer) q.take()).intValue());
251 <            }
252 <        } catch (InterruptedException e) {
253 <            unexpectedException();
273 >    public void testTake() throws InterruptedException {
274 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
275 >        for (int i = 0; i < SIZE; ++i) {
276 >            assertEquals(i, (int) q.take());
277          }
278      }
279  
280      /**
281 <     * take blocks interruptibly when empty
281 >     * take removes existing elements until empty, then blocks interruptibly
282       */
283 <    public void testTakeFromEmpty() {
284 <        final LinkedTransferQueue q = new LinkedTransferQueue();
285 <        Thread t = new Thread(new Runnable() {
286 <
287 <            public void run() {
288 <                try {
289 <                    q.take();
267 <                    threadShouldThrow();
268 <                } catch (InterruptedException success) {
283 >    public void testBlockingTake() throws InterruptedException {
284 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
285 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
286 >        Thread t = newStartedThread(new CheckedRunnable() {
287 >            public void realRun() throws InterruptedException {
288 >                for (int i = 0; i < SIZE; ++i) {
289 >                    assertEquals(i, (int) q.take());
290                  }
291 <            }
271 <        });
272 <        try {
273 <            t.start();
274 <            Thread.sleep(SHORT_DELAY_MS);
275 <            t.interrupt();
276 <            t.join();
277 <        } catch (Exception e) {
278 <            unexpectedException();
279 <        }
280 <    }
281 <
282 <    /**
283 <     * Take removes existing elements until empty, then blocks interruptibly
284 <     */
285 <    public void testBlockingTake() {
286 <        Thread t = new Thread(new Runnable() {
287 <
288 <            public void run() {
291 >                aboutToWait.countDown();
292                  try {
290                    LinkedTransferQueue q = populatedQueue(SIZE);
291                    for (int i = 0; i < SIZE; ++i) {
292                        assertEquals(i, ((Integer) q.take()).intValue());
293                    }
293                      q.take();
294 <                    threadShouldThrow();
295 <                } catch (InterruptedException success) {
296 <                }
297 <            }
298 <        });
299 <        t.start();
300 <        try {
301 <            Thread.sleep(SHORT_DELAY_MS);
302 <            t.interrupt();
304 <            t.join();
305 <        } catch (InterruptedException ie) {
306 <            unexpectedException();
307 <        }
294 >                    shouldThrow();
295 >                } catch (InterruptedException success) {}
296 >            }});
297 >
298 >        aboutToWait.await();
299 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
300 >        t.interrupt();
301 >        awaitTermination(t, MEDIUM_DELAY_MS);
302 >        checkEmpty(q);
303      }
304  
305      /**
306       * poll succeeds unless empty
307       */
308 <    public void testPoll() {
309 <        LinkedTransferQueue q = populatedQueue(SIZE);
308 >    public void testPoll() throws InterruptedException {
309 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
310          for (int i = 0; i < SIZE; ++i) {
311 <            assertEquals(i, ((Integer) q.poll()).intValue());
311 >            assertEquals(i, (int) q.poll());
312          }
313          assertNull(q.poll());
314 +        checkEmpty(q);
315      }
316  
317      /**
318 <     * timed pool with zero timeout succeeds when non-empty, else times out
318 >     * timed poll with zero timeout succeeds when non-empty, else times out
319       */
320 <    public void testTimedPoll0() {
321 <        try {
322 <            LinkedTransferQueue q = populatedQueue(SIZE);
323 <            for (int i = 0; i < SIZE; ++i) {
328 <                assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue());
329 <            }
330 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
331 <        } catch (InterruptedException e) {
332 <            unexpectedException();
320 >    public void testTimedPoll0() throws InterruptedException {
321 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
322 >        for (int i = 0; i < SIZE; ++i) {
323 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
324          }
325 +        assertNull(q.poll(0, MILLISECONDS));
326 +        checkEmpty(q);
327      }
328  
329      /**
330 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
330 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
331       */
332 <    public void testTimedPoll() {
333 <        try {
334 <            LinkedTransferQueue q = populatedQueue(SIZE);
335 <            for (int i = 0; i < SIZE; ++i) {
336 <                assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
337 <            }
338 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
339 <        } catch (InterruptedException e) {
340 <            unexpectedException();
341 <        }
332 >    public void testTimedPoll() throws InterruptedException {
333 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
334 >        for (int i = 0; i < SIZE; ++i) {
335 >            long t0 = System.nanoTime();
336 >            assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS));
337 >            assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
338 >        }
339 >        long t0 = System.nanoTime();
340 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
341 >        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
342 >        checkEmpty(q);
343      }
344  
345      /**
346       * Interrupted timed poll throws InterruptedException instead of
347       * returning timeout status
348       */
349 <    public void testInterruptedTimedPoll() {
350 <        Thread t = new Thread(new Runnable() {
351 <
352 <            public void run() {
353 <                try {
354 <                    LinkedTransferQueue q = populatedQueue(SIZE);
355 <                    for (int i = 0; i < SIZE; ++i) {
356 <                        threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
357 <                    }
364 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
365 <                } catch (InterruptedException success) {
349 >    public void testInterruptedTimedPoll() throws InterruptedException {
350 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
351 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
352 >        Thread t = newStartedThread(new CheckedRunnable() {
353 >            public void realRun() throws InterruptedException {
354 >                for (int i = 0; i < SIZE; ++i) {
355 >                    long t0 = System.nanoTime();
356 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
357 >                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
358                  }
359 <            }
360 <        });
361 <        t.start();
362 <        try {
363 <            Thread.sleep(SHORT_DELAY_MS);
364 <            t.interrupt();
365 <            t.join();
366 <        } catch (InterruptedException ie) {
367 <            unexpectedException();
368 <        }
359 >                aboutToWait.countDown();
360 >                try {
361 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
362 >                    shouldThrow();
363 >                } catch (InterruptedException success) {}
364 >            }});
365 >
366 >        aboutToWait.await();
367 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
368 >        t.interrupt();
369 >        awaitTermination(t, MEDIUM_DELAY_MS);
370 >        checkEmpty(q);
371      }
372  
373      /**
374 <     * timed poll before a delayed offer fails; after offer succeeds;
375 <     * on interruption throws
376 <     */
377 <    public void testTimedPollWithOffer() {
378 <        final LinkedTransferQueue q = new LinkedTransferQueue();
379 <        Thread t = new Thread(new Runnable() {
380 <
381 <            public void run() {
382 <                try {
383 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
384 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
385 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
392 <                    threadShouldThrow();
393 <                } catch (InterruptedException success) {
374 >     * timed poll after thread interrupted throws InterruptedException
375 >     * instead of returning timeout status
376 >     */
377 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
378 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
379 >        Thread t = newStartedThread(new CheckedRunnable() {
380 >            public void realRun() throws InterruptedException {
381 >                Thread.currentThread().interrupt();
382 >                for (int i = 0; i < SIZE; ++i) {
383 >                    long t0 = System.nanoTime();
384 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
385 >                    assertTrue(millisElapsedSince(t0) < SHORT_DELAY_MS);
386                  }
387 <            }
388 <        });
389 <        try {
390 <            t.start();
391 <            Thread.sleep(SMALL_DELAY_MS);
392 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
393 <            t.interrupt();
394 <            t.join();
403 <        } catch (Exception e) {
404 <            unexpectedException();
405 <        }
387 >                try {
388 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
389 >                    shouldThrow();
390 >                } catch (InterruptedException success) {}
391 >            }});
392 >
393 >        awaitTermination(t, MEDIUM_DELAY_MS);
394 >        checkEmpty(q);
395      }
396  
397      /**
398       * peek returns next element, or null if empty
399       */
400 <    public void testPeek() {
401 <        LinkedTransferQueue q = populatedQueue(SIZE);
400 >    public void testPeek() throws InterruptedException {
401 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
402          for (int i = 0; i < SIZE; ++i) {
403 <            assertEquals(i, ((Integer) q.peek()).intValue());
404 <            q.poll();
403 >            assertEquals(i, (int) q.peek());
404 >            assertEquals(i, (int) q.poll());
405              assertTrue(q.peek() == null ||
406 <                    i != ((Integer) q.peek()).intValue());
406 >                       i != (int) q.peek());
407          }
408          assertNull(q.peek());
409 +        checkEmpty(q);
410      }
411  
412      /**
413 <     * element returns next element, or throws NSEE if empty
413 >     * element returns next element, or throws NoSuchElementException if empty
414       */
415 <    public void testElement() {
416 <        LinkedTransferQueue q = populatedQueue(SIZE);
415 >    public void testElement() throws InterruptedException {
416 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
417          for (int i = 0; i < SIZE; ++i) {
418 <            assertEquals(i, ((Integer) q.element()).intValue());
419 <            q.poll();
418 >            assertEquals(i, (int) q.element());
419 >            assertEquals(i, (int) q.poll());
420          }
421          try {
422              q.element();
423              shouldThrow();
424 <        } catch (NoSuchElementException success) {
425 <        }
424 >        } catch (NoSuchElementException success) {}
425 >        checkEmpty(q);
426      }
427  
428      /**
429 <     * remove removes next element, or throws NSEE if empty
429 >     * remove removes next element, or throws NoSuchElementException if empty
430       */
431 <    public void testRemove() {
432 <        LinkedTransferQueue q = populatedQueue(SIZE);
431 >    public void testRemove() throws InterruptedException {
432 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
433          for (int i = 0; i < SIZE; ++i) {
434 <            assertEquals(i, ((Integer) q.remove()).intValue());
434 >            assertEquals(i, (int) q.remove());
435          }
436          try {
437              q.remove();
438              shouldThrow();
439 <        } catch (NoSuchElementException success) {
440 <        }
439 >        } catch (NoSuchElementException success) {}
440 >        checkEmpty(q);
441      }
442  
443      /**
444       * remove(x) removes x and returns true if present
445       */
446 <    public void testRemoveElement() {
446 >    public void testRemoveElement() throws InterruptedException {
447          LinkedTransferQueue q = populatedQueue(SIZE);
448          for (int i = 1; i < SIZE; i += 2) {
449 <            assertTrue(q.remove(new Integer(i)));
449 >            assertTrue(q.remove(i));
450          }
451          for (int i = 0; i < SIZE; i += 2) {
452 <            assertTrue(q.remove(new Integer(i)));
453 <            assertFalse(q.remove(new Integer(i + 1)));
452 >            assertTrue(q.remove(i));
453 >            assertFalse(q.remove(i + 1));
454          }
455 <        assertTrue(q.isEmpty());
455 >        checkEmpty(q);
456      }
457  
458      /**
459       * An add following remove(x) succeeds
460       */
461 <    public void testRemoveElementAndAdd() {
462 <        try {
463 <            LinkedTransferQueue q = new LinkedTransferQueue();
464 <            assertTrue(q.add(new Integer(1)));
465 <            assertTrue(q.add(new Integer(2)));
466 <            assertTrue(q.remove(new Integer(1)));
467 <            assertTrue(q.remove(new Integer(2)));
468 <            assertTrue(q.add(new Integer(3)));
479 <            assertTrue(q.take() != null);
480 <        } catch (Exception e) {
481 <            unexpectedException();
482 <        }
461 >    public void testRemoveElementAndAdd() throws InterruptedException {
462 >        LinkedTransferQueue q = new LinkedTransferQueue();
463 >        assertTrue(q.add(one));
464 >        assertTrue(q.add(two));
465 >        assertTrue(q.remove(one));
466 >        assertTrue(q.remove(two));
467 >        assertTrue(q.add(three));
468 >        assertSame(q.take(), three);
469      }
470  
471      /**
472       * contains(x) reports true when elements added but not yet removed
473       */
474      public void testContains() {
475 <        LinkedTransferQueue q = populatedQueue(SIZE);
475 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
476          for (int i = 0; i < SIZE; ++i) {
477 <            assertTrue(q.contains(new Integer(i)));
478 <            q.poll();
479 <            assertFalse(q.contains(new Integer(i)));
477 >            assertTrue(q.contains(i));
478 >            assertEquals(i, (int) q.poll());
479 >            assertFalse(q.contains(i));
480          }
481      }
482  
483      /**
484       * clear removes all elements
485       */
486 <    public void testClear() {
486 >    public void testClear() throws InterruptedException {
487          LinkedTransferQueue q = populatedQueue(SIZE);
502        int remainingCapacity = q.remainingCapacity();
488          q.clear();
489 <        assertTrue(q.isEmpty());
490 <        assertEquals(0, q.size());
506 <        assertEquals(remainingCapacity, q.remainingCapacity());
489 >        checkEmpty(q);
490 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
491          q.add(one);
492          assertFalse(q.isEmpty());
493 +        assertEquals(1, q.size());
494          assertTrue(q.contains(one));
495          q.clear();
496 <        assertTrue(q.isEmpty());
496 >        checkEmpty(q);
497      }
498  
499      /**
500       * containsAll(c) is true when c contains a subset of elements
501       */
502      public void testContainsAll() {
503 <        LinkedTransferQueue q = populatedQueue(SIZE);
504 <        LinkedTransferQueue p = new LinkedTransferQueue();
503 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
504 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
505          for (int i = 0; i < SIZE; ++i) {
506              assertTrue(q.containsAll(p));
507              assertFalse(p.containsAll(q));
508 <            p.add(new Integer(i));
508 >            p.add(i);
509          }
510          assertTrue(p.containsAll(q));
511      }
512  
513      /**
514 <     * retainAll(c) retains only those elements of c and reports true if changed
514 >     * retainAll(c) retains only those elements of c and reports true
515 >     * if changed
516       */
517      public void testRetainAll() {
518          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 545 | Line 531 | public class LinkedTransferQueueTest ext
531      }
532  
533      /**
534 <     * removeAll(c) removes only those elements of c and reports true if changed
534 >     * removeAll(c) removes only those elements of c and reports true
535 >     * if changed
536       */
537      public void testRemoveAll() {
538          for (int i = 1; i < SIZE; ++i) {
# Line 554 | Line 541 | public class LinkedTransferQueueTest ext
541              assertTrue(q.removeAll(p));
542              assertEquals(SIZE - i, q.size());
543              for (int j = 0; j < i; ++j) {
544 <                Integer I = (Integer) (p.remove());
558 <                assertFalse(q.contains(I));
544 >                assertFalse(q.contains(p.remove()));
545              }
546          }
547      }
548  
549      /**
550 <     * toArray contains all elements
550 >     * toArray() contains all elements
551       */
552 <    public void testToArray() {
552 >    public void testToArray() throws InterruptedException {
553          LinkedTransferQueue q = populatedQueue(SIZE);
554          Object[] o = q.toArray();
555 <        try {
556 <            for (int i = 0; i < o.length; i++) {
571 <                assertEquals(o[i], q.take());
572 <            }
573 <        } catch (InterruptedException e) {
574 <            unexpectedException();
555 >        for (int i = 0; i < o.length; i++) {
556 >            assertEquals(o[i], q.take());
557          }
558      }
559  
560      /**
561       * toArray(a) contains all elements
562       */
563 <    public void testToArray2() {
564 <        LinkedTransferQueue q = populatedQueue(SIZE);
563 >    public void testToArray2() throws InterruptedException {
564 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
565          Integer[] ints = new Integer[SIZE];
566 <        ints = (Integer[]) q.toArray(ints);
567 <        try {
568 <            for (int i = 0; i < ints.length; i++) {
587 <                assertEquals(ints[i], q.take());
588 <            }
589 <        } catch (InterruptedException e) {
590 <            unexpectedException();
566 >        ints = q.toArray(ints);
567 >        for (int i = 0; i < ints.length; i++) {
568 >            assertEquals(ints[i], q.take());
569          }
570      }
571  
572      /**
573 <     * toArray(null) throws NPE
573 >     * toArray(null) throws NullPointerException
574       */
575      public void testToArray_BadArg() {
576 +        LinkedTransferQueue q = populatedQueue(SIZE);
577          try {
599            LinkedTransferQueue q = populatedQueue(SIZE);
578              Object o[] = q.toArray(null);
579              shouldThrow();
580 <        } catch (NullPointerException success) {
603 <        }
580 >        } catch (NullPointerException success) {}
581      }
582  
583      /**
584 <     * toArray with incompatible array type throws CCE
584 >     * toArray(incompatible array type) throws CCE
585       */
586      public void testToArray1_BadArg() {
587 +        LinkedTransferQueue q = populatedQueue(SIZE);
588          try {
611            LinkedTransferQueue q = populatedQueue(SIZE);
589              Object o[] = q.toArray(new String[10]);
590              shouldThrow();
591 <        } catch (ArrayStoreException success) {
615 <        }
591 >        } catch (ArrayStoreException success) {}
592      }
593  
594      /**
595       * iterator iterates through all elements
596       */
597 <    public void testIterator() {
597 >    public void testIterator() throws InterruptedException {
598          LinkedTransferQueue q = populatedQueue(SIZE);
599          Iterator it = q.iterator();
600 <        try {
601 <            while (it.hasNext()) {
602 <                assertEquals(it.next(), q.take());
627 <            }
628 <        } catch (InterruptedException e) {
629 <            unexpectedException();
600 >        int i = 0;
601 >        while (it.hasNext()) {
602 >            assertEquals(it.next(), i++);
603          }
604 +        assertEquals(i, SIZE);
605      }
606  
607      /**
608 <     * iterator.remove removes current element
608 >     * iterator.remove() removes current element
609       */
610      public void testIteratorRemove() {
611          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 644 | Line 618 | public class LinkedTransferQueueTest ext
618          it.remove();
619  
620          it = q.iterator();
621 <        assertEquals(it.next(), one);
622 <        assertEquals(it.next(), three);
621 >        assertSame(it.next(), one);
622 >        assertSame(it.next(), three);
623          assertFalse(it.hasNext());
624      }
625  
# Line 653 | Line 627 | public class LinkedTransferQueueTest ext
627       * iterator ordering is FIFO
628       */
629      public void testIteratorOrdering() {
630 <        final LinkedTransferQueue q = new LinkedTransferQueue();
631 <        int remainingCapacity = q.remainingCapacity();
630 >        final LinkedTransferQueue<Integer> q
631 >            = new LinkedTransferQueue<Integer>();
632 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
633          q.add(one);
634          q.add(two);
635          q.add(three);
636 <        assertEquals(remainingCapacity, q.remainingCapacity());
636 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
637          int k = 0;
638 <        for (Iterator it = q.iterator(); it.hasNext();) {
639 <            int i = ((Integer) (it.next())).intValue();
665 <            assertEquals(++k, i);
638 >        for (Integer n : q) {
639 >            assertEquals(++k, (int) n);
640          }
641          assertEquals(3, k);
642      }
# Line 675 | Line 649 | public class LinkedTransferQueueTest ext
649          q.add(one);
650          q.add(two);
651          q.add(three);
652 <        try {
653 <            for (Iterator it = q.iterator(); it.hasNext();) {
654 <                q.remove();
681 <                it.next();
682 <            }
683 <        } catch (ConcurrentModificationException e) {
684 <            unexpectedException();
652 >        for (Iterator it = q.iterator(); it.hasNext();) {
653 >            q.remove();
654 >            it.next();
655          }
656          assertEquals(0, q.size());
657      }
# Line 702 | Line 672 | public class LinkedTransferQueueTest ext
672       */
673      public void testOfferInExecutor() {
674          final LinkedTransferQueue q = new LinkedTransferQueue();
675 <        q.add(one);
706 <        q.add(two);
675 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
676          ExecutorService executor = Executors.newFixedThreadPool(2);
708        executor.execute(new Runnable() {
709
710            public void run() {
711                try {
712                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
713                } catch (Exception e) {
714                    threadUnexpectedException();
715                }
716            }
717        });
718
719        executor.execute(new Runnable() {
677  
678 <            public void run() {
679 <                try {
680 <                    Thread.sleep(SMALL_DELAY_MS);
681 <                    threadAssertEquals(one, q.take());
682 <                } catch (InterruptedException e) {
683 <                    threadUnexpectedException();
684 <                }
685 <            }
686 <        });
678 >        executor.execute(new CheckedRunnable() {
679 >            public void realRun() throws InterruptedException {
680 >                threadsStarted.countDown();
681 >                threadsStarted.await();
682 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
683 >            }});
684 >
685 >        executor.execute(new CheckedRunnable() {
686 >            public void realRun() throws InterruptedException {
687 >                threadsStarted.countDown();
688 >                threadsStarted.await();
689 >                assertSame(one, q.take());
690 >                checkEmpty(q);
691 >            }});
692  
693          joinPool(executor);
694      }
695  
696      /**
697 <     * poll retrieves elements across Executor threads
697 >     * timed poll retrieves elements across Executor threads
698       */
699      public void testPollInExecutor() {
700          final LinkedTransferQueue q = new LinkedTransferQueue();
701 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
702          ExecutorService executor = Executors.newFixedThreadPool(2);
740        executor.execute(new Runnable() {
741
742            public void run() {
743                threadAssertNull(q.poll());
744                try {
745                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
746                    threadAssertTrue(q.isEmpty());
747                } catch (InterruptedException e) {
748                    threadUnexpectedException();
749                }
750            }
751        });
703  
704 <        executor.execute(new Runnable() {
705 <
706 <            public void run() {
707 <                try {
708 <                    Thread.sleep(SMALL_DELAY_MS);
709 <                    q.put(one);
710 <                } catch (InterruptedException e) {
711 <                    threadUnexpectedException();
712 <                }
713 <            }
714 <        });
704 >        executor.execute(new CheckedRunnable() {
705 >            public void realRun() throws InterruptedException {
706 >                assertNull(q.poll());
707 >                threadsStarted.countDown();
708 >                threadsStarted.await();
709 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
710 >                checkEmpty(q);
711 >            }});
712 >
713 >        executor.execute(new CheckedRunnable() {
714 >            public void realRun() throws InterruptedException {
715 >                threadsStarted.countDown();
716 >                threadsStarted.await();
717 >                q.put(one);
718 >            }});
719  
720          joinPool(executor);
721      }
# Line 768 | Line 723 | public class LinkedTransferQueueTest ext
723      /**
724       * A deserialized serialized queue has same elements in same order
725       */
726 <    public void testSerialization() {
726 >    public void testSerialization() throws Exception {
727          LinkedTransferQueue q = populatedQueue(SIZE);
728  
729 <        try {
730 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
731 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
732 <            out.writeObject(q);
733 <            out.close();
734 <
735 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
736 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
737 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
738 <
739 <            assertEquals(q.size(), r.size());
740 <            while (!q.isEmpty()) {
741 <                assertEquals(q.remove(), r.remove());
742 <            }
743 <        } catch (Exception e) {
744 <            unexpectedException();
729 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
730 >        ObjectOutputStream out
731 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
732 >        out.writeObject(q);
733 >        out.close();
734 >
735 >        ByteArrayInputStream bin
736 >            = new ByteArrayInputStream(bout.toByteArray());
737 >        ObjectInputStream in
738 >            = new ObjectInputStream(new BufferedInputStream(bin));
739 >        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
740 >
741 >        assertEquals(q.size(), r.size());
742 >        assertEquals(q.toString(), r.toString());
743 >        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
744 >        while (!q.isEmpty()) {
745 >            assertEquals(q.remove(), r.remove());
746          }
747      }
748  
749      /**
750 <     * drainTo(null) throws NPE
750 >     * drainTo(null) throws NullPointerException
751       */
752      public void testDrainToNull() {
753          LinkedTransferQueue q = populatedQueue(SIZE);
754          try {
755              q.drainTo(null);
756              shouldThrow();
757 <        } catch (NullPointerException success) {
802 <        }
757 >        } catch (NullPointerException success) {}
758      }
759  
760      /**
761 <     * drainTo(this) throws IAE
761 >     * drainTo(this) throws IllegalArgumentException
762       */
763      public void testDrainToSelf() {
764          LinkedTransferQueue q = populatedQueue(SIZE);
765          try {
766              q.drainTo(q);
767              shouldThrow();
768 <        } catch (IllegalArgumentException success) {
814 <        }
768 >        } catch (IllegalArgumentException success) {}
769      }
770  
771      /**
# Line 824 | Line 778 | public class LinkedTransferQueueTest ext
778          assertEquals(q.size(), 0);
779          assertEquals(l.size(), SIZE);
780          for (int i = 0; i < SIZE; ++i) {
781 <            assertEquals(l.get(i), new Integer(i));
781 >            assertEquals(l.get(i), i);
782          }
783          q.add(zero);
784          q.add(one);
# Line 836 | Line 790 | public class LinkedTransferQueueTest ext
790          assertEquals(q.size(), 0);
791          assertEquals(l.size(), 2);
792          for (int i = 0; i < 2; ++i) {
793 <            assertEquals(l.get(i), new Integer(i));
793 >            assertEquals(l.get(i), i);
794          }
795      }
796  
797      /**
798 <     * drainTo empties full queue, unblocking a waiting put.
798 >     * drainTo(c) empties full queue, unblocking a waiting put.
799       */
800 <    public void testDrainToWithActivePut() {
800 >    public void testDrainToWithActivePut() throws InterruptedException {
801          final LinkedTransferQueue q = populatedQueue(SIZE);
802 <        Thread t = new Thread(new Runnable() {
803 <
804 <            public void run() {
805 <                try {
806 <                    q.put(new Integer(SIZE + 1));
807 <                } catch (Exception ie) {
808 <                    threadUnexpectedException();
809 <                }
810 <            }
857 <        });
858 <        try {
859 <            t.start();
860 <            ArrayList l = new ArrayList();
861 <            q.drainTo(l);
862 <            assertTrue(l.size() >= SIZE);
863 <            for (int i = 0; i < SIZE; ++i) {
864 <                assertEquals(l.get(i), new Integer(i));
865 <            }
866 <            t.join();
867 <            assertTrue(q.size() + l.size() >= SIZE);
868 <        } catch (Exception e) {
869 <            unexpectedException();
802 >        Thread t = newStartedThread(new CheckedRunnable() {
803 >            public void realRun() {
804 >                q.put(SIZE + 1);
805 >            }});
806 >        ArrayList l = new ArrayList();
807 >        q.drainTo(l);
808 >        assertTrue(l.size() >= SIZE);
809 >        for (int i = 0; i < SIZE; ++i) {
810 >            assertEquals(l.get(i), i);
811          }
812 +        awaitTermination(t, MEDIUM_DELAY_MS);
813 +        assertTrue(q.size() + l.size() >= SIZE);
814      }
815  
816      /**
817 <     * drainTo(null, n) throws NPE
817 >     * drainTo(null, n) throws NullPointerException
818       */
819      public void testDrainToNullN() {
820          LinkedTransferQueue q = populatedQueue(SIZE);
821          try {
822 <            q.drainTo(null, 0);
822 >            q.drainTo(null, SIZE);
823              shouldThrow();
824 <        } catch (NullPointerException success) {
882 <        }
824 >        } catch (NullPointerException success) {}
825      }
826  
827      /**
828 <     * drainTo(this, n) throws IAE
828 >     * drainTo(this, n) throws IllegalArgumentException
829       */
830      public void testDrainToSelfN() {
831          LinkedTransferQueue q = populatedQueue(SIZE);
832          try {
833 <            q.drainTo(q, 0);
833 >            q.drainTo(q, SIZE);
834              shouldThrow();
835 <        } catch (IllegalArgumentException success) {
894 <        }
835 >        } catch (IllegalArgumentException success) {}
836      }
837  
838      /**
839 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
839 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
840       */
841      public void testDrainToN() {
842          LinkedTransferQueue q = new LinkedTransferQueue();
843          for (int i = 0; i < SIZE + 2; ++i) {
844              for (int j = 0; j < SIZE; j++) {
845 <                assertTrue(q.offer(new Integer(j)));
845 >                assertTrue(q.offer(j));
846              }
847              ArrayList l = new ArrayList();
848              q.drainTo(l, i);
# Line 909 | Line 850 | public class LinkedTransferQueueTest ext
850              assertEquals(l.size(), k);
851              assertEquals(q.size(), SIZE - k);
852              for (int j = 0; j < k; ++j) {
853 <                assertEquals(l.get(j), new Integer(j));
853 >                assertEquals(l.get(j), j);
854              }
855 <            while (q.poll() != null);
855 >            while (q.poll() != null)
856 >                ;
857          }
858      }
859  
860      /**
861 <     * poll and take should decrement the waiting consumer count
861 >     * timed poll() or take() increments the waiting consumer count;
862 >     * offer(e) decrements the waiting consumer count
863       */
864 <    public void testWaitingConsumer() {
865 <        try {
866 <            final LinkedTransferQueue q = new LinkedTransferQueue();
867 <            final ConsumerObserver waiting = new ConsumerObserver();
868 <            new Thread(new Runnable() {
864 >    public void testWaitingConsumer() throws InterruptedException {
865 >        final LinkedTransferQueue q = new LinkedTransferQueue();
866 >        assertEquals(q.getWaitingConsumerCount(), 0);
867 >        assertFalse(q.hasWaitingConsumer());
868 >        final CountDownLatch threadStarted = new CountDownLatch(1);
869  
870 <                public void run() {
871 <                    try {
872 <                        threadAssertTrue(q.hasWaitingConsumer());
873 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
874 <                        threadAssertTrue(q.offer(new Object()));
875 <                    } catch (Exception ex) {
876 <                        threadUnexpectedException();
934 <                    }
870 >        Thread t = newStartedThread(new CheckedRunnable() {
871 >            public void realRun() throws InterruptedException {
872 >                threadStarted.countDown();
873 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
874 >                assertEquals(q.getWaitingConsumerCount(), 0);
875 >                assertFalse(q.hasWaitingConsumer());
876 >            }});
877  
878 <                }
879 <            }).start();
880 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
881 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
882 <        } catch (Exception ex) {
883 <            this.unexpectedException();
884 <        }
878 >        threadStarted.await();
879 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
880 >        assertEquals(q.getWaitingConsumerCount(), 1);
881 >        assertTrue(q.hasWaitingConsumer());
882 >
883 >        assertTrue(q.offer(one));
884 >        assertEquals(q.getWaitingConsumerCount(), 0);
885 >        assertFalse(q.hasWaitingConsumer());
886 >
887 >        awaitTermination(t, MEDIUM_DELAY_MS);
888      }
889  
890      /**
891 <     * Inserts null into transfer throws NPE
891 >     * transfer(null) throws NullPointerException
892       */
893 <    public void testTransfer1() {
893 >    public void testTransfer1() throws InterruptedException {
894          try {
895              LinkedTransferQueue q = new LinkedTransferQueue();
896              q.transfer(null);
897              shouldThrow();
898 <        } catch (NullPointerException ex) {
954 <        } catch (Exception ex) {
955 <            this.unexpectedException();
956 <        }
898 >        } catch (NullPointerException success) {}
899      }
900  
901      /**
902 <     * transfer attempts to insert into the queue then wait until that
903 <     * object is removed via take or poll.
902 >     * transfer waits until a poll occurs. The transfered element
903 >     * is returned by this associated poll.
904       */
905 <    public void testTransfer2() {
906 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
907 <        new Thread(new Runnable() {
905 >    public void testTransfer2() throws InterruptedException {
906 >        final LinkedTransferQueue<Integer> q
907 >            = new LinkedTransferQueue<Integer>();
908 >        final CountDownLatch threadStarted = new CountDownLatch(1);
909  
910 <            public void run() {
911 <                try {
912 <                    q.transfer(new Integer(SIZE));
913 <                    threadAssertTrue(q.isEmpty());
914 <                } catch (Exception ex) {
915 <                    threadUnexpectedException();
973 <                }
974 <            }
975 <        }).start();
910 >        Thread t = newStartedThread(new CheckedRunnable() {
911 >            public void realRun() throws InterruptedException {
912 >                threadStarted.countDown();
913 >                q.transfer(five);
914 >                checkEmpty(q);
915 >            }});
916  
917 <        try {
918 <            Thread.sleep(SHORT_DELAY_MS);
919 <            assertEquals(1, q.size());
920 <            q.poll();
921 <            assertTrue(q.isEmpty());
922 <        } catch (Exception ex) {
983 <            this.unexpectedException();
984 <        }
917 >        threadStarted.await();
918 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
919 >        assertEquals(1, q.size());
920 >        assertSame(five, q.poll());
921 >        checkEmpty(q);
922 >        awaitTermination(t, MEDIUM_DELAY_MS);
923      }
924  
925      /**
926 <     * transfer will attempt to transfer in fifo order and continue
989 <     * waiting if the element being transfered is not polled or taken
926 >     * transfer waits until a poll occurs, and then transfers in fifo order
927       */
928 <    public void testTransfer3() {
929 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
930 <        new Thread(new Runnable() {
931 <                public void run() {
932 <                    try {
933 <                        Integer i;
934 <                        q.transfer((i = new Integer(SIZE + 1)));
935 <                        threadAssertTrue(!q.contains(i));
936 <                        threadAssertEquals(1, q.size());
937 <                    } catch (Exception ex) {
938 <                        threadUnexpectedException();
939 <                    }
940 <                }
941 <            }).start();
942 <        Thread interruptedThread =
943 <            new Thread(new Runnable() {
944 <                    public void run() {
945 <                        try {
946 <                            q.transfer(new Integer(SIZE));
947 <                            threadShouldThrow();
948 <                        } catch (InterruptedException ex) {
949 <                        }
950 <                    }
951 <                });
952 <        interruptedThread.start();
953 <        try {
954 <            Thread.sleep(LONG_DELAY_MS);
955 <            assertEquals(2, q.size());
1019 <            q.poll();
1020 <            Thread.sleep(LONG_DELAY_MS);
1021 <            interruptedThread.interrupt();
1022 <            assertEquals(1, q.size());
1023 <        } catch (Exception ex) {
1024 <            this.unexpectedException();
1025 <        }
928 >    public void testTransfer3() throws InterruptedException {
929 >        final LinkedTransferQueue<Integer> q
930 >            = new LinkedTransferQueue<Integer>();
931 >
932 >        Thread first = newStartedThread(new CheckedRunnable() {
933 >            public void realRun() throws InterruptedException {
934 >                q.transfer(four);
935 >                assertTrue(!q.contains(four));
936 >                assertEquals(1, q.size());
937 >            }});
938 >
939 >        Thread interruptedThread = newStartedThread(
940 >            new CheckedInterruptedRunnable() {
941 >                public void realRun() throws InterruptedException {
942 >                    while (q.isEmpty())
943 >                        Thread.yield();
944 >                    q.transfer(five);
945 >                }});
946 >
947 >        while (q.size() < 2)
948 >            Thread.yield();
949 >        assertEquals(2, q.size());
950 >        assertSame(four, q.poll());
951 >        first.join();
952 >        assertEquals(1, q.size());
953 >        interruptedThread.interrupt();
954 >        interruptedThread.join();
955 >        checkEmpty(q);
956      }
957  
958      /**
959 <     * transfer will wait as long as a poll or take occurs if one does occur
960 <     * the waiting is finished and the thread that tries to poll/take
1031 <     * wins in retrieving the element
959 >     * transfer waits until a poll occurs, at which point the polling
960 >     * thread returns the element
961       */
962 <    public void testTransfer4() {
962 >    public void testTransfer4() throws InterruptedException {
963          final LinkedTransferQueue q = new LinkedTransferQueue();
1035        new Thread(new Runnable() {
964  
965 <            public void run() {
966 <                try {
967 <                    q.transfer(new Integer(four));
968 <                    threadAssertFalse(q.contains(new Integer(four)));
969 <                    threadAssertEquals(new Integer(three), q.poll());
970 <                } catch (Exception ex) {
971 <                    threadUnexpectedException();
972 <                }
973 <            }
974 <        }).start();
975 <        try {
976 <            Thread.sleep(MEDIUM_DELAY_MS);
977 <            assertTrue(q.offer(three));
978 <            assertEquals(new Integer(four), q.poll());
979 <        } catch (Exception ex) {
980 <            this.unexpectedException();
981 <        }
965 >        Thread t = newStartedThread(new CheckedRunnable() {
966 >            public void realRun() throws InterruptedException {
967 >                q.transfer(four);
968 >                assertFalse(q.contains(four));
969 >                assertSame(three, q.poll());
970 >            }});
971 >
972 >        while (q.isEmpty())
973 >            Thread.yield();
974 >        assertFalse(q.isEmpty());
975 >        assertEquals(1, q.size());
976 >        assertTrue(q.offer(three));
977 >        assertSame(four, q.poll());
978 >        awaitTermination(t, MEDIUM_DELAY_MS);
979 >    }
980 >
981 >    /**
982 >     * transfer waits until a take occurs. The transfered element
983 >     * is returned by this associated take.
984 >     */
985 >    public void testTransfer5() throws InterruptedException {
986 >        final LinkedTransferQueue<Integer> q
987 >            = new LinkedTransferQueue<Integer>();
988 >
989 >        Thread t = newStartedThread(new CheckedRunnable() {
990 >            public void realRun() throws InterruptedException {
991 >                q.transfer(four);
992 >                checkEmpty(q);
993 >            }});
994 >
995 >        while (q.isEmpty())
996 >            Thread.yield();
997 >        assertFalse(q.isEmpty());
998 >        assertEquals(1, q.size());
999 >        assertSame(four, q.take());
1000 >        checkEmpty(q);
1001 >        awaitTermination(t, MEDIUM_DELAY_MS);
1002      }
1003  
1004      /**
1005 <     * Insert null into tryTransfer throws NPE
1005 >     * tryTransfer(null) throws NullPointerException
1006       */
1007      public void testTryTransfer1() {
1008          try {
1009              final LinkedTransferQueue q = new LinkedTransferQueue();
1010              q.tryTransfer(null);
1011 <            this.shouldThrow();
1012 <        } catch (NullPointerException ex) {
1065 <        } catch (Exception ex) {
1066 <            this.unexpectedException();
1067 <        }
1011 >            shouldThrow();
1012 >        } catch (NullPointerException success) {}
1013      }
1014  
1015      /**
1016 <     * tryTransfer returns false and does not enqueue if there are no consumers
1017 <     * waiting to poll or take.
1016 >     * tryTransfer returns false and does not enqueue if there are no
1017 >     * consumers waiting to poll or take.
1018       */
1019 <    public void testTryTransfer2() {
1020 <        try {
1021 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1022 <            assertFalse(q.tryTransfer(new Object()));
1023 <            assertEquals(0, q.size());
1079 <        } catch (Exception ex) {
1080 <            this.unexpectedException();
1081 <        }
1019 >    public void testTryTransfer2() throws InterruptedException {
1020 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1021 >        assertFalse(q.tryTransfer(new Object()));
1022 >        assertFalse(q.hasWaitingConsumer());
1023 >        checkEmpty(q);
1024      }
1025  
1026      /**
1027 <     * if there is a consumer waiting poll or take tryTransfer returns
1028 <     * true while enqueueing object
1027 >     * If there is a consumer waiting in timed poll, tryTransfer
1028 >     * returns true while successfully transfering object.
1029       */
1030 <    public void testTryTransfer3() {
1031 <        try {
1032 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1091 <            new Thread(new Runnable() {
1030 >    public void testTryTransfer3() throws InterruptedException {
1031 >        final Object hotPotato = new Object();
1032 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1033  
1034 <                public void run() {
1035 <                    try {
1036 <                        threadAssertTrue(q.hasWaitingConsumer());
1037 <                        threadAssertTrue(q.tryTransfer(new Object()));
1038 <                    } catch (Exception ex) {
1039 <                        threadUnexpectedException();
1040 <                    }
1034 >        Thread t = newStartedThread(new CheckedRunnable() {
1035 >            public void realRun() {
1036 >                while (! q.hasWaitingConsumer())
1037 >                    Thread.yield();
1038 >                assertTrue(q.hasWaitingConsumer());
1039 >                checkEmpty(q);
1040 >                assertTrue(q.tryTransfer(hotPotato));
1041 >            }});
1042 >
1043 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1044 >        checkEmpty(q);
1045 >        awaitTermination(t, MEDIUM_DELAY_MS);
1046 >    }
1047  
1048 <                }
1049 <            }).start();
1050 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
1051 <            assertTrue(q.isEmpty());
1052 <        } catch (Exception ex) {
1053 <            this.unexpectedException();
1054 <        }
1048 >    /**
1049 >     * If there is a consumer waiting in take, tryTransfer returns
1050 >     * true while successfully transfering object.
1051 >     */
1052 >    public void testTryTransfer4() throws InterruptedException {
1053 >        final Object hotPotato = new Object();
1054 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1055 >
1056 >        Thread t = newStartedThread(new CheckedRunnable() {
1057 >            public void realRun() {
1058 >                while (! q.hasWaitingConsumer())
1059 >                    Thread.yield();
1060 >                assertTrue(q.hasWaitingConsumer());
1061 >                checkEmpty(q);
1062 >                assertTrue(q.tryTransfer(hotPotato));
1063 >            }});
1064 >
1065 >        assertSame(q.take(), hotPotato);
1066 >        checkEmpty(q);
1067 >        awaitTermination(t, MEDIUM_DELAY_MS);
1068      }
1069  
1070      /**
1071 <     * tryTransfer waits the amount given if interrupted, show an
1072 <     * interrupted exception
1071 >     * tryTransfer waits the amount given if interrupted, and
1072 >     * throws interrupted exception
1073       */
1074 <    public void testTryTransfer4() {
1074 >    public void testTryTransfer5() throws InterruptedException {
1075          final LinkedTransferQueue q = new LinkedTransferQueue();
1076 <        Thread toInterrupt = new Thread(new Runnable() {
1076 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1077  
1078 <            public void run() {
1078 >        Thread t = newStartedThread(new CheckedRunnable() {
1079 >            public void realRun() throws InterruptedException {
1080 >                long t0 = System.nanoTime();
1081 >                threadStarted.countDown();
1082                  try {
1083 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1084 <                    threadShouldThrow();
1085 <                } catch (InterruptedException ex) {
1086 <                }
1087 <            }
1088 <        });
1089 <        try {
1090 <            toInterrupt.start();
1091 <            Thread.sleep(SMALL_DELAY_MS);
1092 <            toInterrupt.interrupt();
1093 <        } catch (Exception ex) {
1131 <            this.unexpectedException();
1132 <        }
1083 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1084 >                    shouldThrow();
1085 >                } catch (InterruptedException success) {}
1086 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1087 >            }});
1088 >
1089 >        threadStarted.await();
1090 >        Thread.sleep(SHORT_DELAY_MS);
1091 >        t.interrupt();
1092 >        awaitTermination(t, MEDIUM_DELAY_MS);
1093 >        checkEmpty(q);
1094      }
1095  
1096      /**
1097 <     * tryTransfer gives up after the timeout and return false
1097 >     * tryTransfer gives up after the timeout and returns false
1098       */
1099 <    public void testTryTransfer5() {
1099 >    public void testTryTransfer6() throws InterruptedException {
1100          final LinkedTransferQueue q = new LinkedTransferQueue();
1140        try {
1141            new Thread(new Runnable() {
1101  
1102 <                public void run() {
1103 <                    try {
1104 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1105 <                    } catch (InterruptedException ex) {
1106 <                        threadUnexpectedException();
1107 <                    }
1108 <                }
1109 <            }).start();
1110 <            Thread.sleep(LONG_DELAY_MS);
1111 <            assertTrue(q.isEmpty());
1112 <        } catch (Exception ex) {
1154 <            this.unexpectedException();
1155 <        }
1102 >        Thread t = newStartedThread(new CheckedRunnable() {
1103 >            public void realRun() throws InterruptedException {
1104 >                long t0 = System.nanoTime();
1105 >                assertFalse(q.tryTransfer(new Object(),
1106 >                                          SHORT_DELAY_MS, MILLISECONDS));
1107 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1108 >                checkEmpty(q);
1109 >            }});
1110 >
1111 >        awaitTermination(t, MEDIUM_DELAY_MS);
1112 >        checkEmpty(q);
1113      }
1114  
1115      /**
1116       * tryTransfer waits for any elements previously in to be removed
1117       * before transfering to a poll or take
1118       */
1119 <    public void testTryTransfer6() {
1119 >    public void testTryTransfer7() throws InterruptedException {
1120          final LinkedTransferQueue q = new LinkedTransferQueue();
1121 <        q.offer(new Integer(four));
1165 <        new Thread(new Runnable() {
1121 >        assertTrue(q.offer(four));
1122  
1123 <            public void run() {
1124 <                try {
1125 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1126 <                    threadAssertTrue(q.isEmpty());
1127 <                } catch (InterruptedException ex) {
1128 <                    threadUnexpectedException();
1129 <                }
1130 <            }
1131 <        }).start();
1132 <        try {
1133 <            Thread.sleep(SHORT_DELAY_MS);
1134 <            assertEquals(2, q.size());
1135 <            assertEquals(new Integer(four), q.poll());
1180 <            assertEquals(new Integer(five), q.poll());
1181 <            assertTrue(q.isEmpty());
1182 <        } catch (Exception ex) {
1183 <            this.unexpectedException();
1184 <        }
1123 >        Thread t = newStartedThread(new CheckedRunnable() {
1124 >            public void realRun() throws InterruptedException {
1125 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1126 >                checkEmpty(q);
1127 >            }});
1128 >
1129 >        while (q.size() != 2)
1130 >            Thread.yield();
1131 >        assertEquals(2, q.size());
1132 >        assertSame(four, q.poll());
1133 >        assertSame(five, q.poll());
1134 >        checkEmpty(q);
1135 >        awaitTermination(t, MEDIUM_DELAY_MS);
1136      }
1137  
1138      /**
1139 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1140 <     * enqueueing and the successive poll is null
1139 >     * tryTransfer attempts to enqueue into the q and fails returning
1140 >     * false not enqueueing and the successive poll is null
1141       */
1142 <    public void testTryTransfer7() {
1142 >    public void testTryTransfer8() throws InterruptedException {
1143          final LinkedTransferQueue q = new LinkedTransferQueue();
1144 <        q.offer(new Integer(four));
1145 <        new Thread(new Runnable() {
1146 <
1147 <            public void run() {
1148 <                try {
1149 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1150 <                    threadAssertTrue(q.isEmpty());
1151 <                } catch (InterruptedException ex) {
1152 <                    threadUnexpectedException();
1202 <                }
1203 <            }
1204 <        }).start();
1205 <        try {
1206 <            assertEquals(1, q.size());
1207 <            assertEquals(new Integer(four), q.poll());
1208 <            Thread.sleep(MEDIUM_DELAY_MS);
1209 <            assertNull(q.poll());
1210 <        } catch (Exception ex) {
1211 <            this.unexpectedException();
1212 <        }
1144 >        assertTrue(q.offer(four));
1145 >        assertEquals(1, q.size());
1146 >        long t0 = System.nanoTime();
1147 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1148 >        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1149 >        assertEquals(1, q.size());
1150 >        assertSame(four, q.poll());
1151 >        assertNull(q.poll());
1152 >        checkEmpty(q);
1153      }
1154  
1155 <    private LinkedTransferQueue populatedQueue(
1156 <            int n) {
1157 <        LinkedTransferQueue q = new LinkedTransferQueue();
1158 <        assertTrue(q.isEmpty());
1159 <        int remainingCapacity = q.remainingCapacity();
1220 <        for (int i = 0; i <
1221 <                n; i++) {
1155 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1156 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1157 >        checkEmpty(q);
1158 >        for (int i = 0; i < n; i++) {
1159 >            assertEquals(i, q.size());
1160              assertTrue(q.offer(i));
1161 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1162          }
1224
1163          assertFalse(q.isEmpty());
1226        assertEquals(remainingCapacity, q.remainingCapacity());
1227        assertEquals(n, q.size());
1164          return q;
1165      }
1230
1231    private static class ConsumerObserver {
1232
1233        private int waitingConsumers;
1234
1235        private ConsumerObserver() {
1236        }
1237
1238        private void setWaitingConsumer(int i) {
1239            this.waitingConsumers = i;
1240        }
1241
1242        private int getWaitingConsumers() {
1243            return waitingConsumers;
1244        }
1245    }
1166   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines