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.42 by jsr166, Thu Nov 18 20:21:53 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() {
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) < SMALL_DELAY_MS);
358 >                }
359 >                long t0 = System.nanoTime();
360 >                aboutToWait.countDown();
361                  try {
362 <                    LinkedTransferQueue q = populatedQueue(SIZE);
363 <                    for (int i = 0; i < SIZE; ++i) {
362 <                        threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
363 <                    }
364 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
362 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
363 >                    shouldThrow();
364                  } catch (InterruptedException success) {
365 +                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
366                  }
367 <            }
368 <        });
369 <        t.start();
370 <        try {
371 <            Thread.sleep(SHORT_DELAY_MS);
372 <            t.interrupt();
373 <            t.join();
374 <        } catch (InterruptedException ie) {
375 <            unexpectedException();
376 <        }
367 >            }});
368 >
369 >        aboutToWait.await();
370 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
371 >        t.interrupt();
372 >        awaitTermination(t, MEDIUM_DELAY_MS);
373 >        checkEmpty(q);
374      }
375  
376      /**
377 <     * timed poll before a delayed offer fails; after offer succeeds;
378 <     * on interruption throws
379 <     */
380 <    public void testTimedPollWithOffer() {
381 <        final LinkedTransferQueue q = new LinkedTransferQueue();
382 <        Thread t = new Thread(new Runnable() {
383 <
384 <            public void run() {
385 <                try {
386 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
387 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
388 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
392 <                    threadShouldThrow();
393 <                } catch (InterruptedException success) {
377 >     * timed poll after thread interrupted throws InterruptedException
378 >     * instead of returning timeout status
379 >     */
380 >    public void testTimedPollAfterInterrupt() throws InterruptedException {
381 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
382 >        Thread t = newStartedThread(new CheckedRunnable() {
383 >            public void realRun() throws InterruptedException {
384 >                Thread.currentThread().interrupt();
385 >                for (int i = 0; i < SIZE; ++i) {
386 >                    long t0 = System.nanoTime();
387 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
388 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
389                  }
390 <            }
391 <        });
392 <        try {
393 <            t.start();
394 <            Thread.sleep(SMALL_DELAY_MS);
395 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396 <            t.interrupt();
397 <            t.join();
403 <        } catch (Exception e) {
404 <            unexpectedException();
405 <        }
390 >                try {
391 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
392 >                    shouldThrow();
393 >                } catch (InterruptedException success) {}
394 >            }});
395 >
396 >        awaitTermination(t, MEDIUM_DELAY_MS);
397 >        checkEmpty(q);
398      }
399  
400      /**
401       * peek returns next element, or null if empty
402       */
403 <    public void testPeek() {
404 <        LinkedTransferQueue q = populatedQueue(SIZE);
403 >    public void testPeek() throws InterruptedException {
404 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
405          for (int i = 0; i < SIZE; ++i) {
406 <            assertEquals(i, ((Integer) q.peek()).intValue());
407 <            q.poll();
406 >            assertEquals(i, (int) q.peek());
407 >            assertEquals(i, (int) q.poll());
408              assertTrue(q.peek() == null ||
409 <                    i != ((Integer) q.peek()).intValue());
409 >                       i != (int) q.peek());
410          }
411          assertNull(q.peek());
412 +        checkEmpty(q);
413      }
414  
415      /**
416 <     * element returns next element, or throws NSEE if empty
416 >     * element returns next element, or throws NoSuchElementException if empty
417       */
418 <    public void testElement() {
419 <        LinkedTransferQueue q = populatedQueue(SIZE);
418 >    public void testElement() throws InterruptedException {
419 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer) q.element()).intValue());
422 <            q.poll();
421 >            assertEquals(i, (int) q.element());
422 >            assertEquals(i, (int) q.poll());
423          }
424          try {
425              q.element();
426              shouldThrow();
427 <        } catch (NoSuchElementException success) {
428 <        }
427 >        } catch (NoSuchElementException success) {}
428 >        checkEmpty(q);
429      }
430  
431      /**
432 <     * remove removes next element, or throws NSEE if empty
432 >     * remove removes next element, or throws NoSuchElementException if empty
433       */
434 <    public void testRemove() {
435 <        LinkedTransferQueue q = populatedQueue(SIZE);
434 >    public void testRemove() throws InterruptedException {
435 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
436          for (int i = 0; i < SIZE; ++i) {
437 <            assertEquals(i, ((Integer) q.remove()).intValue());
437 >            assertEquals(i, (int) q.remove());
438          }
439          try {
440              q.remove();
441              shouldThrow();
442 <        } catch (NoSuchElementException success) {
443 <        }
442 >        } catch (NoSuchElementException success) {}
443 >        checkEmpty(q);
444      }
445  
446      /**
447       * remove(x) removes x and returns true if present
448       */
449 <    public void testRemoveElement() {
449 >    public void testRemoveElement() throws InterruptedException {
450          LinkedTransferQueue q = populatedQueue(SIZE);
451 <        for (int i = 1; i < SIZE; i += 2) {
452 <            assertTrue(q.remove(new Integer(i)));
453 <        }
454 <        for (int i = 0; i < SIZE; i += 2) {
455 <            assertTrue(q.remove(new Integer(i)));
456 <            assertFalse(q.remove(new Integer(i + 1)));
451 >        for (int i = 1; i < SIZE; i+=2) {
452 >            assertTrue(q.contains(i));
453 >            assertTrue(q.remove(i));
454 >            assertFalse(q.contains(i));
455 >            assertTrue(q.contains(i-1));
456 >        }
457 >        for (int i = 0; i < SIZE; i+=2) {
458 >            assertTrue(q.contains(i));
459 >            assertTrue(q.remove(i));
460 >            assertFalse(q.contains(i));
461 >            assertFalse(q.remove(i+1));
462 >            assertFalse(q.contains(i+1));
463          }
464 <        assertTrue(q.isEmpty());
464 >        checkEmpty(q);
465      }
466  
467      /**
468       * An add following remove(x) succeeds
469       */
470 <    public void testRemoveElementAndAdd() {
471 <        try {
472 <            LinkedTransferQueue q = new LinkedTransferQueue();
473 <            assertTrue(q.add(new Integer(1)));
474 <            assertTrue(q.add(new Integer(2)));
475 <            assertTrue(q.remove(new Integer(1)));
476 <            assertTrue(q.remove(new Integer(2)));
477 <            assertTrue(q.add(new Integer(3)));
479 <            assertTrue(q.take() != null);
480 <        } catch (Exception e) {
481 <            unexpectedException();
482 <        }
470 >    public void testRemoveElementAndAdd() throws InterruptedException {
471 >        LinkedTransferQueue q = new LinkedTransferQueue();
472 >        assertTrue(q.add(one));
473 >        assertTrue(q.add(two));
474 >        assertTrue(q.remove(one));
475 >        assertTrue(q.remove(two));
476 >        assertTrue(q.add(three));
477 >        assertSame(q.take(), three);
478      }
479  
480      /**
481       * contains(x) reports true when elements added but not yet removed
482       */
483      public void testContains() {
484 <        LinkedTransferQueue q = populatedQueue(SIZE);
484 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
485          for (int i = 0; i < SIZE; ++i) {
486 <            assertTrue(q.contains(new Integer(i)));
487 <            q.poll();
488 <            assertFalse(q.contains(new Integer(i)));
486 >            assertTrue(q.contains(i));
487 >            assertEquals(i, (int) q.poll());
488 >            assertFalse(q.contains(i));
489          }
490      }
491  
492      /**
493       * clear removes all elements
494       */
495 <    public void testClear() {
495 >    public void testClear() throws InterruptedException {
496          LinkedTransferQueue q = populatedQueue(SIZE);
502        int remainingCapacity = q.remainingCapacity();
497          q.clear();
498 <        assertTrue(q.isEmpty());
499 <        assertEquals(0, q.size());
506 <        assertEquals(remainingCapacity, q.remainingCapacity());
498 >        checkEmpty(q);
499 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
500          q.add(one);
501          assertFalse(q.isEmpty());
502 +        assertEquals(1, q.size());
503          assertTrue(q.contains(one));
504          q.clear();
505 <        assertTrue(q.isEmpty());
505 >        checkEmpty(q);
506      }
507  
508      /**
509       * containsAll(c) is true when c contains a subset of elements
510       */
511      public void testContainsAll() {
512 <        LinkedTransferQueue q = populatedQueue(SIZE);
513 <        LinkedTransferQueue p = new LinkedTransferQueue();
512 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
513 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
514          for (int i = 0; i < SIZE; ++i) {
515              assertTrue(q.containsAll(p));
516              assertFalse(p.containsAll(q));
517 <            p.add(new Integer(i));
517 >            p.add(i);
518          }
519          assertTrue(p.containsAll(q));
520      }
521  
522      /**
523 <     * retainAll(c) retains only those elements of c and reports true if changed
523 >     * retainAll(c) retains only those elements of c and reports true
524 >     * if changed
525       */
526      public void testRetainAll() {
527          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 545 | Line 540 | public class LinkedTransferQueueTest ext
540      }
541  
542      /**
543 <     * removeAll(c) removes only those elements of c and reports true if changed
543 >     * removeAll(c) removes only those elements of c and reports true
544 >     * if changed
545       */
546      public void testRemoveAll() {
547          for (int i = 1; i < SIZE; ++i) {
# Line 554 | Line 550 | public class LinkedTransferQueueTest ext
550              assertTrue(q.removeAll(p));
551              assertEquals(SIZE - i, q.size());
552              for (int j = 0; j < i; ++j) {
553 <                Integer I = (Integer) (p.remove());
558 <                assertFalse(q.contains(I));
553 >                assertFalse(q.contains(p.remove()));
554              }
555          }
556      }
557  
558      /**
559 <     * toArray contains all elements
559 >     * toArray() contains all elements in FIFO order
560       */
561      public void testToArray() {
562          LinkedTransferQueue q = populatedQueue(SIZE);
563          Object[] o = q.toArray();
564 <        try {
565 <            for (int i = 0; i < o.length; i++) {
571 <                assertEquals(o[i], q.take());
572 <            }
573 <        } catch (InterruptedException e) {
574 <            unexpectedException();
564 >        for (int i = 0; i < o.length; i++) {
565 >            assertSame(o[i], q.poll());
566          }
567      }
568  
569      /**
570 <     * toArray(a) contains all elements
570 >     * toArray(a) contains all elements in FIFO order
571       */
572      public void testToArray2() {
573 <        LinkedTransferQueue q = populatedQueue(SIZE);
573 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
574          Integer[] ints = new Integer[SIZE];
575 <        ints = (Integer[]) q.toArray(ints);
576 <        try {
577 <            for (int i = 0; i < ints.length; i++) {
578 <                assertEquals(ints[i], q.take());
588 <            }
589 <        } catch (InterruptedException e) {
590 <            unexpectedException();
575 >        Integer[] array = q.toArray(ints);
576 >        assertSame(ints, array);
577 >        for (int i = 0; i < ints.length; i++) {
578 >            assertSame(ints[i], q.poll());
579          }
580      }
581  
582      /**
583 <     * toArray(null) throws NPE
583 >     * toArray(null) throws NullPointerException
584       */
585 <    public void testToArray_BadArg() {
585 >    public void testToArray_NullArg() {
586 >        LinkedTransferQueue q = populatedQueue(SIZE);
587          try {
588 <            LinkedTransferQueue q = populatedQueue(SIZE);
600 <            Object o[] = q.toArray(null);
588 >            q.toArray(null);
589              shouldThrow();
590 <        } catch (NullPointerException success) {
603 <        }
590 >        } catch (NullPointerException success) {}
591      }
592  
593      /**
594 <     * toArray with incompatible array type throws CCE
594 >     * toArray(incompatible array type) throws ArrayStoreException
595       */
596      public void testToArray1_BadArg() {
597 +        LinkedTransferQueue q = populatedQueue(SIZE);
598          try {
599 <            LinkedTransferQueue q = populatedQueue(SIZE);
612 <            Object o[] = q.toArray(new String[10]);
599 >            q.toArray(new String[10]);
600              shouldThrow();
601 <        } catch (ArrayStoreException success) {
615 <        }
601 >        } catch (ArrayStoreException success) {}
602      }
603  
604      /**
605       * iterator iterates through all elements
606       */
607 <    public void testIterator() {
607 >    public void testIterator() throws InterruptedException {
608          LinkedTransferQueue q = populatedQueue(SIZE);
609          Iterator it = q.iterator();
610 <        try {
611 <            while (it.hasNext()) {
612 <                assertEquals(it.next(), q.take());
627 <            }
628 <        } catch (InterruptedException e) {
629 <            unexpectedException();
610 >        int i = 0;
611 >        while (it.hasNext()) {
612 >            assertEquals(it.next(), i++);
613          }
614 +        assertEquals(i, SIZE);
615      }
616  
617      /**
618 <     * iterator.remove removes current element
618 >     * iterator.remove() removes current element
619       */
620      public void testIteratorRemove() {
621          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 644 | Line 628 | public class LinkedTransferQueueTest ext
628          it.remove();
629  
630          it = q.iterator();
631 <        assertEquals(it.next(), one);
632 <        assertEquals(it.next(), three);
631 >        assertSame(it.next(), one);
632 >        assertSame(it.next(), three);
633          assertFalse(it.hasNext());
634      }
635  
# Line 653 | Line 637 | public class LinkedTransferQueueTest ext
637       * iterator ordering is FIFO
638       */
639      public void testIteratorOrdering() {
640 <        final LinkedTransferQueue q = new LinkedTransferQueue();
641 <        int remainingCapacity = q.remainingCapacity();
640 >        final LinkedTransferQueue<Integer> q
641 >            = new LinkedTransferQueue<Integer>();
642 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
643          q.add(one);
644          q.add(two);
645          q.add(three);
646 <        assertEquals(remainingCapacity, q.remainingCapacity());
646 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
647          int k = 0;
648 <        for (Iterator it = q.iterator(); it.hasNext();) {
649 <            int i = ((Integer) (it.next())).intValue();
665 <            assertEquals(++k, i);
648 >        for (Integer n : q) {
649 >            assertEquals(++k, (int) n);
650          }
651          assertEquals(3, k);
652      }
# Line 675 | Line 659 | public class LinkedTransferQueueTest ext
659          q.add(one);
660          q.add(two);
661          q.add(three);
662 <        try {
663 <            for (Iterator it = q.iterator(); it.hasNext();) {
664 <                q.remove();
681 <                it.next();
682 <            }
683 <        } catch (ConcurrentModificationException e) {
684 <            unexpectedException();
662 >        for (Iterator it = q.iterator(); it.hasNext();) {
663 >            q.remove();
664 >            it.next();
665          }
666          assertEquals(0, q.size());
667      }
# Line 702 | Line 682 | public class LinkedTransferQueueTest ext
682       */
683      public void testOfferInExecutor() {
684          final LinkedTransferQueue q = new LinkedTransferQueue();
685 <        q.add(one);
706 <        q.add(two);
685 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
686          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        });
687  
688 <        executor.execute(new Runnable() {
689 <
690 <            public void run() {
691 <                try {
692 <                    Thread.sleep(SMALL_DELAY_MS);
693 <                    threadAssertEquals(one, q.take());
694 <                } catch (InterruptedException e) {
695 <                    threadUnexpectedException();
696 <                }
697 <            }
698 <        });
688 >        executor.execute(new CheckedRunnable() {
689 >            public void realRun() throws InterruptedException {
690 >                threadsStarted.countDown();
691 >                threadsStarted.await();
692 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
693 >            }});
694 >
695 >        executor.execute(new CheckedRunnable() {
696 >            public void realRun() throws InterruptedException {
697 >                threadsStarted.countDown();
698 >                threadsStarted.await();
699 >                assertSame(one, q.take());
700 >                checkEmpty(q);
701 >            }});
702  
703          joinPool(executor);
704      }
705  
706      /**
707 <     * poll retrieves elements across Executor threads
707 >     * timed poll retrieves elements across Executor threads
708       */
709      public void testPollInExecutor() {
710          final LinkedTransferQueue q = new LinkedTransferQueue();
711 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
712          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        });
713  
714 <        executor.execute(new Runnable() {
715 <
716 <            public void run() {
717 <                try {
718 <                    Thread.sleep(SMALL_DELAY_MS);
719 <                    q.put(one);
720 <                } catch (InterruptedException e) {
721 <                    threadUnexpectedException();
722 <                }
723 <            }
724 <        });
714 >        executor.execute(new CheckedRunnable() {
715 >            public void realRun() throws InterruptedException {
716 >                assertNull(q.poll());
717 >                threadsStarted.countDown();
718 >                threadsStarted.await();
719 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
720 >                checkEmpty(q);
721 >            }});
722 >
723 >        executor.execute(new CheckedRunnable() {
724 >            public void realRun() throws InterruptedException {
725 >                threadsStarted.countDown();
726 >                threadsStarted.await();
727 >                q.put(one);
728 >            }});
729  
730          joinPool(executor);
731      }
# Line 768 | Line 733 | public class LinkedTransferQueueTest ext
733      /**
734       * A deserialized serialized queue has same elements in same order
735       */
736 <    public void testSerialization() {
736 >    public void testSerialization() throws Exception {
737          LinkedTransferQueue q = populatedQueue(SIZE);
738  
739 <        try {
740 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
741 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
742 <            out.writeObject(q);
743 <            out.close();
744 <
745 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
746 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
747 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
748 <
749 <            assertEquals(q.size(), r.size());
750 <            while (!q.isEmpty()) {
751 <                assertEquals(q.remove(), r.remove());
752 <            }
753 <        } catch (Exception e) {
754 <            unexpectedException();
739 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
740 >        ObjectOutputStream out
741 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
742 >        out.writeObject(q);
743 >        out.close();
744 >
745 >        ByteArrayInputStream bin
746 >            = new ByteArrayInputStream(bout.toByteArray());
747 >        ObjectInputStream in
748 >            = new ObjectInputStream(new BufferedInputStream(bin));
749 >        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
750 >
751 >        assertEquals(q.size(), r.size());
752 >        assertEquals(q.toString(), r.toString());
753 >        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
754 >        while (!q.isEmpty()) {
755 >            assertEquals(q.remove(), r.remove());
756          }
757      }
758  
759      /**
760 <     * drainTo(null) throws NPE
760 >     * drainTo(null) throws NullPointerException
761       */
762      public void testDrainToNull() {
763          LinkedTransferQueue q = populatedQueue(SIZE);
764          try {
765              q.drainTo(null);
766              shouldThrow();
767 <        } catch (NullPointerException success) {
802 <        }
767 >        } catch (NullPointerException success) {}
768      }
769  
770      /**
771 <     * drainTo(this) throws IAE
771 >     * drainTo(this) throws IllegalArgumentException
772       */
773      public void testDrainToSelf() {
774          LinkedTransferQueue q = populatedQueue(SIZE);
775          try {
776              q.drainTo(q);
777              shouldThrow();
778 <        } catch (IllegalArgumentException success) {
814 <        }
778 >        } catch (IllegalArgumentException success) {}
779      }
780  
781      /**
# Line 824 | Line 788 | public class LinkedTransferQueueTest ext
788          assertEquals(q.size(), 0);
789          assertEquals(l.size(), SIZE);
790          for (int i = 0; i < SIZE; ++i) {
791 <            assertEquals(l.get(i), new Integer(i));
791 >            assertEquals(l.get(i), i);
792          }
793          q.add(zero);
794          q.add(one);
# Line 836 | Line 800 | public class LinkedTransferQueueTest ext
800          assertEquals(q.size(), 0);
801          assertEquals(l.size(), 2);
802          for (int i = 0; i < 2; ++i) {
803 <            assertEquals(l.get(i), new Integer(i));
803 >            assertEquals(l.get(i), i);
804          }
805      }
806  
807      /**
808 <     * drainTo empties full queue, unblocking a waiting put.
808 >     * drainTo(c) empties full queue, unblocking a waiting put.
809       */
810 <    public void testDrainToWithActivePut() {
810 >    public void testDrainToWithActivePut() throws InterruptedException {
811          final LinkedTransferQueue q = populatedQueue(SIZE);
812 <        Thread t = new Thread(new Runnable() {
813 <
814 <            public void run() {
815 <                try {
816 <                    q.put(new Integer(SIZE + 1));
817 <                } catch (Exception ie) {
818 <                    threadUnexpectedException();
819 <                }
820 <            }
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();
812 >        Thread t = newStartedThread(new CheckedRunnable() {
813 >            public void realRun() {
814 >                q.put(SIZE + 1);
815 >            }});
816 >        ArrayList l = new ArrayList();
817 >        q.drainTo(l);
818 >        assertTrue(l.size() >= SIZE);
819 >        for (int i = 0; i < SIZE; ++i) {
820 >            assertEquals(l.get(i), i);
821          }
822 +        awaitTermination(t, MEDIUM_DELAY_MS);
823 +        assertTrue(q.size() + l.size() >= SIZE);
824      }
825  
826      /**
827 <     * drainTo(null, n) throws NPE
827 >     * drainTo(null, n) throws NullPointerException
828       */
829      public void testDrainToNullN() {
830          LinkedTransferQueue q = populatedQueue(SIZE);
831          try {
832 <            q.drainTo(null, 0);
832 >            q.drainTo(null, SIZE);
833              shouldThrow();
834 <        } catch (NullPointerException success) {
882 <        }
834 >        } catch (NullPointerException success) {}
835      }
836  
837      /**
838 <     * drainTo(this, n) throws IAE
838 >     * drainTo(this, n) throws IllegalArgumentException
839       */
840      public void testDrainToSelfN() {
841          LinkedTransferQueue q = populatedQueue(SIZE);
842          try {
843 <            q.drainTo(q, 0);
843 >            q.drainTo(q, SIZE);
844              shouldThrow();
845 <        } catch (IllegalArgumentException success) {
894 <        }
845 >        } catch (IllegalArgumentException success) {}
846      }
847  
848      /**
849 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
849 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
850       */
851      public void testDrainToN() {
852          LinkedTransferQueue q = new LinkedTransferQueue();
853          for (int i = 0; i < SIZE + 2; ++i) {
854              for (int j = 0; j < SIZE; j++) {
855 <                assertTrue(q.offer(new Integer(j)));
855 >                assertTrue(q.offer(j));
856              }
857              ArrayList l = new ArrayList();
858              q.drainTo(l, i);
# Line 909 | Line 860 | public class LinkedTransferQueueTest ext
860              assertEquals(l.size(), k);
861              assertEquals(q.size(), SIZE - k);
862              for (int j = 0; j < k; ++j) {
863 <                assertEquals(l.get(j), new Integer(j));
863 >                assertEquals(l.get(j), j);
864              }
865 <            while (q.poll() != null);
865 >            while (q.poll() != null)
866 >                ;
867          }
868      }
869  
870      /**
871 <     * poll and take should decrement the waiting consumer count
871 >     * timed poll() or take() increments the waiting consumer count;
872 >     * offer(e) decrements the waiting consumer count
873       */
874 <    public void testWaitingConsumer() {
875 <        try {
876 <            final LinkedTransferQueue q = new LinkedTransferQueue();
877 <            final ConsumerObserver waiting = new ConsumerObserver();
878 <            new Thread(new Runnable() {
874 >    public void testWaitingConsumer() throws InterruptedException {
875 >        final LinkedTransferQueue q = new LinkedTransferQueue();
876 >        assertEquals(q.getWaitingConsumerCount(), 0);
877 >        assertFalse(q.hasWaitingConsumer());
878 >        final CountDownLatch threadStarted = new CountDownLatch(1);
879  
880 <                public void run() {
881 <                    try {
882 <                        threadAssertTrue(q.hasWaitingConsumer());
883 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
884 <                        threadAssertTrue(q.offer(new Object()));
885 <                    } catch (Exception ex) {
886 <                        threadUnexpectedException();
934 <                    }
880 >        Thread t = newStartedThread(new CheckedRunnable() {
881 >            public void realRun() throws InterruptedException {
882 >                threadStarted.countDown();
883 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
884 >                assertEquals(q.getWaitingConsumerCount(), 0);
885 >                assertFalse(q.hasWaitingConsumer());
886 >            }});
887  
888 <                }
889 <            }).start();
890 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
891 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
892 <        } catch (Exception ex) {
893 <            this.unexpectedException();
894 <        }
888 >        threadStarted.await();
889 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
890 >        assertEquals(q.getWaitingConsumerCount(), 1);
891 >        assertTrue(q.hasWaitingConsumer());
892 >
893 >        assertTrue(q.offer(one));
894 >        assertEquals(q.getWaitingConsumerCount(), 0);
895 >        assertFalse(q.hasWaitingConsumer());
896 >
897 >        awaitTermination(t, MEDIUM_DELAY_MS);
898      }
899  
900      /**
901 <     * Inserts null into transfer throws NPE
901 >     * transfer(null) throws NullPointerException
902       */
903 <    public void testTransfer1() {
903 >    public void testTransfer1() throws InterruptedException {
904          try {
905              LinkedTransferQueue q = new LinkedTransferQueue();
906              q.transfer(null);
907              shouldThrow();
908 <        } catch (NullPointerException ex) {
954 <        } catch (Exception ex) {
955 <            this.unexpectedException();
956 <        }
908 >        } catch (NullPointerException success) {}
909      }
910  
911      /**
912 <     * transfer attempts to insert into the queue then wait until that
913 <     * object is removed via take or poll.
912 >     * transfer waits until a poll occurs. The transfered element
913 >     * is returned by this associated poll.
914       */
915 <    public void testTransfer2() {
916 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
917 <        new Thread(new Runnable() {
915 >    public void testTransfer2() throws InterruptedException {
916 >        final LinkedTransferQueue<Integer> q
917 >            = new LinkedTransferQueue<Integer>();
918 >        final CountDownLatch threadStarted = new CountDownLatch(1);
919  
920 <            public void run() {
921 <                try {
922 <                    q.transfer(new Integer(SIZE));
923 <                    threadAssertTrue(q.isEmpty());
924 <                } catch (Exception ex) {
925 <                    threadUnexpectedException();
973 <                }
974 <            }
975 <        }).start();
920 >        Thread t = newStartedThread(new CheckedRunnable() {
921 >            public void realRun() throws InterruptedException {
922 >                threadStarted.countDown();
923 >                q.transfer(five);
924 >                checkEmpty(q);
925 >            }});
926  
927 <        try {
928 <            Thread.sleep(SHORT_DELAY_MS);
929 <            assertEquals(1, q.size());
930 <            q.poll();
931 <            assertTrue(q.isEmpty());
932 <        } catch (Exception ex) {
983 <            this.unexpectedException();
984 <        }
927 >        threadStarted.await();
928 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
929 >        assertEquals(1, q.size());
930 >        assertSame(five, q.poll());
931 >        checkEmpty(q);
932 >        awaitTermination(t, MEDIUM_DELAY_MS);
933      }
934  
935      /**
936 <     * transfer will attempt to transfer in fifo order and continue
989 <     * waiting if the element being transfered is not polled or taken
936 >     * transfer waits until a poll occurs, and then transfers in fifo order
937       */
938 <    public void testTransfer3() {
939 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
940 <        new Thread(new Runnable() {
941 <                public void run() {
942 <                    try {
943 <                        Integer i;
944 <                        q.transfer((i = new Integer(SIZE + 1)));
945 <                        threadAssertTrue(!q.contains(i));
946 <                        threadAssertEquals(1, q.size());
947 <                    } catch (Exception ex) {
948 <                        threadUnexpectedException();
949 <                    }
950 <                }
951 <            }).start();
952 <        Thread interruptedThread =
953 <            new Thread(new Runnable() {
954 <                    public void run() {
955 <                        try {
956 <                            q.transfer(new Integer(SIZE));
957 <                            threadShouldThrow();
958 <                        } catch (InterruptedException ex) {
959 <                        }
960 <                    }
961 <                });
962 <        interruptedThread.start();
963 <        try {
964 <            Thread.sleep(LONG_DELAY_MS);
965 <            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 <        }
938 >    public void testTransfer3() throws InterruptedException {
939 >        final LinkedTransferQueue<Integer> q
940 >            = new LinkedTransferQueue<Integer>();
941 >
942 >        Thread first = newStartedThread(new CheckedRunnable() {
943 >            public void realRun() throws InterruptedException {
944 >                q.transfer(four);
945 >                assertTrue(!q.contains(four));
946 >                assertEquals(1, q.size());
947 >            }});
948 >
949 >        Thread interruptedThread = newStartedThread(
950 >            new CheckedInterruptedRunnable() {
951 >                public void realRun() throws InterruptedException {
952 >                    while (q.isEmpty())
953 >                        Thread.yield();
954 >                    q.transfer(five);
955 >                }});
956 >
957 >        while (q.size() < 2)
958 >            Thread.yield();
959 >        assertEquals(2, q.size());
960 >        assertSame(four, q.poll());
961 >        first.join();
962 >        assertEquals(1, q.size());
963 >        interruptedThread.interrupt();
964 >        interruptedThread.join();
965 >        checkEmpty(q);
966      }
967  
968      /**
969 <     * transfer will wait as long as a poll or take occurs if one does occur
970 <     * the waiting is finished and the thread that tries to poll/take
1031 <     * wins in retrieving the element
969 >     * transfer waits until a poll occurs, at which point the polling
970 >     * thread returns the element
971       */
972 <    public void testTransfer4() {
972 >    public void testTransfer4() throws InterruptedException {
973          final LinkedTransferQueue q = new LinkedTransferQueue();
1035        new Thread(new Runnable() {
974  
975 <            public void run() {
976 <                try {
977 <                    q.transfer(new Integer(four));
978 <                    threadAssertFalse(q.contains(new Integer(four)));
979 <                    threadAssertEquals(new Integer(three), q.poll());
980 <                } catch (Exception ex) {
981 <                    threadUnexpectedException();
982 <                }
983 <            }
984 <        }).start();
985 <        try {
986 <            Thread.sleep(MEDIUM_DELAY_MS);
987 <            assertTrue(q.offer(three));
988 <            assertEquals(new Integer(four), q.poll());
1051 <        } catch (Exception ex) {
1052 <            this.unexpectedException();
1053 <        }
975 >        Thread t = newStartedThread(new CheckedRunnable() {
976 >            public void realRun() throws InterruptedException {
977 >                q.transfer(four);
978 >                assertFalse(q.contains(four));
979 >                assertSame(three, q.poll());
980 >            }});
981 >
982 >        while (q.isEmpty())
983 >            Thread.yield();
984 >        assertFalse(q.isEmpty());
985 >        assertEquals(1, q.size());
986 >        assertTrue(q.offer(three));
987 >        assertSame(four, q.poll());
988 >        awaitTermination(t, MEDIUM_DELAY_MS);
989      }
990  
991      /**
992 <     * Insert null into tryTransfer throws NPE
992 >     * transfer waits until a take occurs. The transfered element
993 >     * is returned by this associated take.
994 >     */
995 >    public void testTransfer5() throws InterruptedException {
996 >        final LinkedTransferQueue<Integer> q
997 >            = new LinkedTransferQueue<Integer>();
998 >
999 >        Thread t = newStartedThread(new CheckedRunnable() {
1000 >            public void realRun() throws InterruptedException {
1001 >                q.transfer(four);
1002 >                checkEmpty(q);
1003 >            }});
1004 >
1005 >        while (q.isEmpty())
1006 >            Thread.yield();
1007 >        assertFalse(q.isEmpty());
1008 >        assertEquals(1, q.size());
1009 >        assertSame(four, q.take());
1010 >        checkEmpty(q);
1011 >        awaitTermination(t, MEDIUM_DELAY_MS);
1012 >    }
1013 >
1014 >    /**
1015 >     * tryTransfer(null) throws NullPointerException
1016       */
1017      public void testTryTransfer1() {
1018          try {
1019              final LinkedTransferQueue q = new LinkedTransferQueue();
1020              q.tryTransfer(null);
1021 <            this.shouldThrow();
1022 <        } catch (NullPointerException ex) {
1065 <        } catch (Exception ex) {
1066 <            this.unexpectedException();
1067 <        }
1021 >            shouldThrow();
1022 >        } catch (NullPointerException success) {}
1023      }
1024  
1025      /**
1026 <     * tryTransfer returns false and does not enqueue if there are no consumers
1027 <     * waiting to poll or take.
1026 >     * tryTransfer returns false and does not enqueue if there are no
1027 >     * consumers waiting to poll or take.
1028       */
1029 <    public void testTryTransfer2() {
1030 <        try {
1031 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1032 <            assertFalse(q.tryTransfer(new Object()));
1033 <            assertEquals(0, q.size());
1079 <        } catch (Exception ex) {
1080 <            this.unexpectedException();
1081 <        }
1029 >    public void testTryTransfer2() throws InterruptedException {
1030 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1031 >        assertFalse(q.tryTransfer(new Object()));
1032 >        assertFalse(q.hasWaitingConsumer());
1033 >        checkEmpty(q);
1034      }
1035  
1036      /**
1037 <     * if there is a consumer waiting poll or take tryTransfer returns
1038 <     * true while enqueueing object
1037 >     * If there is a consumer waiting in timed poll, tryTransfer
1038 >     * returns true while successfully transfering object.
1039       */
1040 <    public void testTryTransfer3() {
1041 <        try {
1042 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1091 <            new Thread(new Runnable() {
1040 >    public void testTryTransfer3() throws InterruptedException {
1041 >        final Object hotPotato = new Object();
1042 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1043  
1044 <                public void run() {
1045 <                    try {
1046 <                        threadAssertTrue(q.hasWaitingConsumer());
1047 <                        threadAssertTrue(q.tryTransfer(new Object()));
1048 <                    } catch (Exception ex) {
1049 <                        threadUnexpectedException();
1050 <                    }
1044 >        Thread t = newStartedThread(new CheckedRunnable() {
1045 >            public void realRun() {
1046 >                while (! q.hasWaitingConsumer())
1047 >                    Thread.yield();
1048 >                assertTrue(q.hasWaitingConsumer());
1049 >                checkEmpty(q);
1050 >                assertTrue(q.tryTransfer(hotPotato));
1051 >            }});
1052 >
1053 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1054 >        checkEmpty(q);
1055 >        awaitTermination(t, MEDIUM_DELAY_MS);
1056 >    }
1057  
1058 <                }
1059 <            }).start();
1060 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
1061 <            assertTrue(q.isEmpty());
1062 <        } catch (Exception ex) {
1063 <            this.unexpectedException();
1064 <        }
1058 >    /**
1059 >     * If there is a consumer waiting in take, tryTransfer returns
1060 >     * true while successfully transfering object.
1061 >     */
1062 >    public void testTryTransfer4() throws InterruptedException {
1063 >        final Object hotPotato = new Object();
1064 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1065 >
1066 >        Thread t = newStartedThread(new CheckedRunnable() {
1067 >            public void realRun() {
1068 >                while (! q.hasWaitingConsumer())
1069 >                    Thread.yield();
1070 >                assertTrue(q.hasWaitingConsumer());
1071 >                checkEmpty(q);
1072 >                assertTrue(q.tryTransfer(hotPotato));
1073 >            }});
1074 >
1075 >        assertSame(q.take(), hotPotato);
1076 >        checkEmpty(q);
1077 >        awaitTermination(t, MEDIUM_DELAY_MS);
1078      }
1079  
1080      /**
1081 <     * tryTransfer waits the amount given if interrupted, show an
1082 <     * interrupted exception
1081 >     * tryTransfer waits the amount given, and throws
1082 >     * InterruptedException when interrupted.
1083       */
1084 <    public void testTryTransfer4() {
1084 >    public void testTryTransfer5() throws InterruptedException {
1085          final LinkedTransferQueue q = new LinkedTransferQueue();
1086 <        Thread toInterrupt = new Thread(new Runnable() {
1086 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1087 >        assertTrue(q.isEmpty());
1088  
1089 <            public void run() {
1089 >        Thread t = newStartedThread(new CheckedRunnable() {
1090 >            public void realRun() throws InterruptedException {
1091 >                long t0 = System.nanoTime();
1092 >                threadStarted.countDown();
1093                  try {
1094 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1095 <                    threadShouldThrow();
1096 <                } catch (InterruptedException ex) {
1097 <                }
1098 <            }
1099 <        });
1100 <        try {
1101 <            toInterrupt.start();
1102 <            Thread.sleep(SMALL_DELAY_MS);
1103 <            toInterrupt.interrupt();
1104 <        } catch (Exception ex) {
1105 <            this.unexpectedException();
1106 <        }
1094 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1095 >                    shouldThrow();
1096 >                } catch (InterruptedException success) {}
1097 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1098 >                assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
1099 >            }});
1100 >
1101 >        threadStarted.await();
1102 >        while (q.isEmpty())
1103 >            Thread.yield();
1104 >        Thread.sleep(SHORT_DELAY_MS);
1105 >        t.interrupt();
1106 >        awaitTermination(t, MEDIUM_DELAY_MS);
1107 >        checkEmpty(q);
1108      }
1109  
1110      /**
1111 <     * tryTransfer gives up after the timeout and return false
1111 >     * tryTransfer gives up after the timeout and returns false
1112       */
1113 <    public void testTryTransfer5() {
1113 >    public void testTryTransfer6() throws InterruptedException {
1114          final LinkedTransferQueue q = new LinkedTransferQueue();
1140        try {
1141            new Thread(new Runnable() {
1115  
1116 <                public void run() {
1117 <                    try {
1118 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1119 <                    } catch (InterruptedException ex) {
1120 <                        threadUnexpectedException();
1121 <                    }
1122 <                }
1123 <            }).start();
1124 <            Thread.sleep(LONG_DELAY_MS);
1125 <            assertTrue(q.isEmpty());
1126 <        } catch (Exception ex) {
1154 <            this.unexpectedException();
1155 <        }
1116 >        Thread t = newStartedThread(new CheckedRunnable() {
1117 >            public void realRun() throws InterruptedException {
1118 >                long t0 = System.nanoTime();
1119 >                assertFalse(q.tryTransfer(new Object(),
1120 >                                          SHORT_DELAY_MS, MILLISECONDS));
1121 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1122 >                checkEmpty(q);
1123 >            }});
1124 >
1125 >        awaitTermination(t, MEDIUM_DELAY_MS);
1126 >        checkEmpty(q);
1127      }
1128  
1129      /**
1130       * tryTransfer waits for any elements previously in to be removed
1131       * before transfering to a poll or take
1132       */
1133 <    public void testTryTransfer6() {
1133 >    public void testTryTransfer7() throws InterruptedException {
1134          final LinkedTransferQueue q = new LinkedTransferQueue();
1135 <        q.offer(new Integer(four));
1165 <        new Thread(new Runnable() {
1135 >        assertTrue(q.offer(four));
1136  
1137 <            public void run() {
1138 <                try {
1139 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1140 <                    threadAssertTrue(q.isEmpty());
1141 <                } catch (InterruptedException ex) {
1142 <                    threadUnexpectedException();
1143 <                }
1144 <            }
1145 <        }).start();
1146 <        try {
1147 <            Thread.sleep(SHORT_DELAY_MS);
1148 <            assertEquals(2, q.size());
1149 <            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 <        }
1137 >        Thread t = newStartedThread(new CheckedRunnable() {
1138 >            public void realRun() throws InterruptedException {
1139 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1140 >                checkEmpty(q);
1141 >            }});
1142 >
1143 >        while (q.size() != 2)
1144 >            Thread.yield();
1145 >        assertEquals(2, q.size());
1146 >        assertSame(four, q.poll());
1147 >        assertSame(five, q.poll());
1148 >        checkEmpty(q);
1149 >        awaitTermination(t, MEDIUM_DELAY_MS);
1150      }
1151  
1152      /**
1153 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1154 <     * enqueueing and the successive poll is null
1153 >     * tryTransfer attempts to enqueue into the queue and fails
1154 >     * returning false not enqueueing and the successive poll is null
1155       */
1156 <    public void testTryTransfer7() {
1156 >    public void testTryTransfer8() throws InterruptedException {
1157          final LinkedTransferQueue q = new LinkedTransferQueue();
1158 <        q.offer(new Integer(four));
1159 <        new Thread(new Runnable() {
1160 <
1161 <            public void run() {
1162 <                try {
1163 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1164 <                    threadAssertTrue(q.isEmpty());
1165 <                } catch (InterruptedException ex) {
1166 <                    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 <        }
1158 >        assertTrue(q.offer(four));
1159 >        assertEquals(1, q.size());
1160 >        long t0 = System.nanoTime();
1161 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1162 >        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1163 >        assertEquals(1, q.size());
1164 >        assertSame(four, q.poll());
1165 >        assertNull(q.poll());
1166 >        checkEmpty(q);
1167      }
1168  
1169 <    private LinkedTransferQueue populatedQueue(
1170 <            int n) {
1171 <        LinkedTransferQueue q = new LinkedTransferQueue();
1172 <        assertTrue(q.isEmpty());
1173 <        int remainingCapacity = q.remainingCapacity();
1220 <        for (int i = 0; i <
1221 <                n; i++) {
1169 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1170 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1171 >        checkEmpty(q);
1172 >        for (int i = 0; i < n; i++) {
1173 >            assertEquals(i, q.size());
1174              assertTrue(q.offer(i));
1175 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1176          }
1224
1177          assertFalse(q.isEmpty());
1226        assertEquals(remainingCapacity, q.remainingCapacity());
1227        assertEquals(n, q.size());
1178          return q;
1179      }
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    }
1180   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines