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.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.38 by jsr166, Thu Nov 4 01:04:54 2010 UTC

# Line 1 | Line 1
1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
# Line 14 | Line 13 | import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.ArrayList;
15   import java.util.Arrays;
17 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
70 >    /**
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());
76          assertTrue(new LinkedTransferQueue().isEmpty());
77      }
78  
79 <    /*
80 <     * Initizialing constructor with null collection throws NPE
79 >    /**
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) {
50 <        }
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) {
62 <        }
99 >        } catch (NullPointerException success) {}
100      }
101 <    /*
101 >
102 >    /**
103       * Initializing constructor with a collection containing some null elements
104 <     * throws NPE
104 >     * throws NullPointerException
105       */
68
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) {
78 <        }
114 >        } catch (NullPointerException success) {}
115      }
116  
117 <    /*
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);
103 <        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) {
125 <        }
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) {
137 <        }
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) {
149 <        }
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) {
161 <        }
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 170 | Line 208 | public class LinkedTransferQueueTest ext
208              Integer[] ints = new Integer[SIZE];
209              q.addAll(Arrays.asList(ints));
210              shouldThrow();
211 <        } catch (NullPointerException success) {
174 <        }
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) {
191 <        }
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) {
208 <                assertEquals(ints[i], q.poll());
209 <            }
210 <        } 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) {
223 <        } catch (Exception ie) {
224 <            unexpectedException();
225 <        }
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);
237 <                assertTrue(q.contains(I));
238 <            }
239 <        } catch (Exception ie) {
240 <            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) {
251 <                assertEquals(i, ((Integer) q.take()).intValue());
252 <            }
253 <        } catch (InterruptedException e) {
254 <            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();
268 <                    threadShouldThrow();
269 <                } 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 <            }
272 <        });
273 <        try {
274 <            t.start();
275 <            Thread.sleep(SHORT_DELAY_MS);
276 <            t.interrupt();
277 <            t.join();
278 <        } catch (Exception e) {
279 <            unexpectedException();
280 <        }
281 <    }
282 <    /*
283 <     * Take removes existing elements until empty, then blocks interruptibly
284 <     */
285 <
286 <    public void testBlockingTake() {
287 <        Thread t = new Thread(new Runnable() {
288 <
289 <            public void run() {
291 >                aboutToWait.countDown();
292                  try {
291                    LinkedTransferQueue q = populatedQueue(SIZE);
292                    for (int i = 0; i < SIZE; ++i) {
293                        assertEquals(i, ((Integer) q.take()).intValue());
294                    }
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();
305 <            t.join();
306 <        } catch (InterruptedException ie) {
307 <            unexpectedException();
308 <        }
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) {
329 <                assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue());
330 <            }
331 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
332 <        } catch (InterruptedException e) {
333 <            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) {
363 <                        threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
364 <                    }
365 <                    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();
375 <        } catch (InterruptedException ie) {
376 <            unexpectedException();
377 <        }
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);
393 <                    threadShouldThrow();
394 <                } 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();
404 <        } catch (Exception e) {
405 <            unexpectedException();
406 <        }
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)));
452 >            assertTrue(q.remove(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)));
455 >            assertTrue(q.remove(i));
456 >            assertFalse(q.remove(i + 1));
457          }
458 <        assertTrue(q.isEmpty());
458 >        checkEmpty(q);
459      }
460  
461      /**
462       * An add following remove(x) succeeds
463       */
464 <    public void testRemoveElementAndAdd() {
465 <        try {
466 <            LinkedTransferQueue q = new LinkedTransferQueue();
467 <            assertTrue(q.add(new Integer(1)));
468 <            assertTrue(q.add(new Integer(2)));
469 <            assertTrue(q.remove(new Integer(1)));
470 <            assertTrue(q.remove(new Integer(2)));
471 <            assertTrue(q.add(new Integer(3)));
480 <            assertTrue(q.take() != null);
481 <        } catch (Exception e) {
482 <            unexpectedException();
483 <        }
464 >    public void testRemoveElementAndAdd() throws InterruptedException {
465 >        LinkedTransferQueue q = new LinkedTransferQueue();
466 >        assertTrue(q.add(one));
467 >        assertTrue(q.add(two));
468 >        assertTrue(q.remove(one));
469 >        assertTrue(q.remove(two));
470 >        assertTrue(q.add(three));
471 >        assertSame(q.take(), three);
472      }
473  
474      /**
475       * contains(x) reports true when elements added but not yet removed
476       */
477      public void testContains() {
478 <        LinkedTransferQueue q = populatedQueue(SIZE);
478 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480 <            assertTrue(q.contains(new Integer(i)));
481 <            q.poll();
482 <            assertFalse(q.contains(new Integer(i)));
480 >            assertTrue(q.contains(i));
481 >            assertEquals(i, (int) q.poll());
482 >            assertFalse(q.contains(i));
483          }
484      }
485  
486      /**
487       * clear removes all elements
488       */
489 <    public void testClear() {
489 >    public void testClear() throws InterruptedException {
490          LinkedTransferQueue q = populatedQueue(SIZE);
503        int remainingCapacity = q.remainingCapacity();
491          q.clear();
492 <        assertTrue(q.isEmpty());
493 <        assertEquals(0, q.size());
507 <        assertEquals(remainingCapacity, q.remainingCapacity());
492 >        checkEmpty(q);
493 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
494          q.add(one);
495          assertFalse(q.isEmpty());
496 +        assertEquals(1, q.size());
497          assertTrue(q.contains(one));
498          q.clear();
499 <        assertTrue(q.isEmpty());
499 >        checkEmpty(q);
500      }
501  
502      /**
503       * containsAll(c) is true when c contains a subset of elements
504       */
505      public void testContainsAll() {
506 <        LinkedTransferQueue q = populatedQueue(SIZE);
507 <        LinkedTransferQueue p = new LinkedTransferQueue();
506 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
507 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
508          for (int i = 0; i < SIZE; ++i) {
509              assertTrue(q.containsAll(p));
510              assertFalse(p.containsAll(q));
511 <            p.add(new Integer(i));
511 >            p.add(i);
512          }
513          assertTrue(p.containsAll(q));
514      }
515  
516      /**
517 <     * retainAll(c) retains only those elements of c and reports true if changed
517 >     * retainAll(c) retains only those elements of c and reports true
518 >     * if changed
519       */
520      public void testRetainAll() {
521          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 546 | Line 534 | public class LinkedTransferQueueTest ext
534      }
535  
536      /**
537 <     * removeAll(c) removes only those elements of c and reports true if changed
537 >     * removeAll(c) removes only those elements of c and reports true
538 >     * if changed
539       */
540      public void testRemoveAll() {
541          for (int i = 1; i < SIZE; ++i) {
# Line 555 | Line 544 | public class LinkedTransferQueueTest ext
544              assertTrue(q.removeAll(p));
545              assertEquals(SIZE - i, q.size());
546              for (int j = 0; j < i; ++j) {
547 <                Integer I = (Integer) (p.remove());
559 <                assertFalse(q.contains(I));
547 >                assertFalse(q.contains(p.remove()));
548              }
549          }
550      }
551  
552      /**
553 <     * toArray contains all elements
553 >     * toArray() contains all elements in FIFO order
554       */
555      public void testToArray() {
556          LinkedTransferQueue q = populatedQueue(SIZE);
557          Object[] o = q.toArray();
558 <        try {
559 <            for (int i = 0; i < o.length; i++) {
572 <                assertEquals(o[i], q.take());
573 <            }
574 <        } catch (InterruptedException e) {
575 <            unexpectedException();
558 >        for (int i = 0; i < o.length; i++) {
559 >            assertSame(o[i], q.poll());
560          }
561      }
562  
563      /**
564 <     * toArray(a) contains all elements
564 >     * toArray(a) contains all elements in FIFO order
565       */
566      public void testToArray2() {
567 <        LinkedTransferQueue q = populatedQueue(SIZE);
567 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
568          Integer[] ints = new Integer[SIZE];
569 <        ints = (Integer[]) q.toArray(ints);
570 <        try {
571 <            for (int i = 0; i < ints.length; i++) {
588 <                assertEquals(ints[i], q.take());
589 <            }
590 <        } catch (InterruptedException e) {
591 <            unexpectedException();
569 >        assertSame(ints, q.toArray(ints));
570 >        for (int i = 0; i < ints.length; i++) {
571 >            assertSame(ints[i], q.poll());
572          }
573      }
574  
575      /**
576 <     * toArray(null) throws NPE
576 >     * toArray(null) throws NullPointerException
577       */
578 <    public void testToArray_BadArg() {
578 >    public void testToArray_NullArg() {
579 >        LinkedTransferQueue q = populatedQueue(SIZE);
580          try {
581 <            LinkedTransferQueue q = populatedQueue(SIZE);
601 <            Object o[] = q.toArray(null);
581 >            q.toArray(null);
582              shouldThrow();
583 <        } catch (NullPointerException success) {
604 <        }
583 >        } catch (NullPointerException success) {}
584      }
585  
586      /**
587 <     * toArray with incompatible array type throws CCE
587 >     * toArray(incompatible array type) throws ArrayStoreException
588       */
589      public void testToArray1_BadArg() {
590 +        LinkedTransferQueue q = populatedQueue(SIZE);
591          try {
592 <            LinkedTransferQueue q = populatedQueue(SIZE);
613 <            Object o[] = q.toArray(new String[10]);
592 >            q.toArray(new String[10]);
593              shouldThrow();
594 <        } catch (ArrayStoreException success) {
616 <        }
594 >        } catch (ArrayStoreException success) {}
595      }
596  
597      /**
598       * iterator iterates through all elements
599       */
600 <    public void testIterator() {
600 >    public void testIterator() throws InterruptedException {
601          LinkedTransferQueue q = populatedQueue(SIZE);
602          Iterator it = q.iterator();
603 <        try {
604 <            while (it.hasNext()) {
605 <                assertEquals(it.next(), q.take());
628 <            }
629 <        } catch (InterruptedException e) {
630 <            unexpectedException();
603 >        int i = 0;
604 >        while (it.hasNext()) {
605 >            assertEquals(it.next(), i++);
606          }
607 +        assertEquals(i, SIZE);
608      }
609  
610      /**
611 <     * iterator.remove removes current element
611 >     * iterator.remove() removes current element
612       */
613      public void testIteratorRemove() {
614          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 645 | Line 621 | public class LinkedTransferQueueTest ext
621          it.remove();
622  
623          it = q.iterator();
624 <        assertEquals(it.next(), one);
625 <        assertEquals(it.next(), three);
624 >        assertSame(it.next(), one);
625 >        assertSame(it.next(), three);
626          assertFalse(it.hasNext());
627      }
628  
# Line 654 | Line 630 | public class LinkedTransferQueueTest ext
630       * iterator ordering is FIFO
631       */
632      public void testIteratorOrdering() {
633 <        final LinkedTransferQueue q = new LinkedTransferQueue();
634 <        int remainingCapacity = q.remainingCapacity();
633 >        final LinkedTransferQueue<Integer> q
634 >            = new LinkedTransferQueue<Integer>();
635 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
636          q.add(one);
637          q.add(two);
638          q.add(three);
639 <        assertEquals(remainingCapacity, q.remainingCapacity());
639 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
640          int k = 0;
641 <        for (Iterator it = q.iterator(); it.hasNext();) {
642 <            int i = ((Integer) (it.next())).intValue();
666 <            assertEquals(++k, i);
641 >        for (Integer n : q) {
642 >            assertEquals(++k, (int) n);
643          }
644          assertEquals(3, k);
645      }
# Line 676 | Line 652 | public class LinkedTransferQueueTest ext
652          q.add(one);
653          q.add(two);
654          q.add(three);
655 <        try {
656 <            for (Iterator it = q.iterator(); it.hasNext();) {
657 <                q.remove();
682 <                it.next();
683 <            }
684 <        } catch (ConcurrentModificationException e) {
685 <            unexpectedException();
655 >        for (Iterator it = q.iterator(); it.hasNext();) {
656 >            q.remove();
657 >            it.next();
658          }
659          assertEquals(0, q.size());
660      }
# Line 703 | Line 675 | public class LinkedTransferQueueTest ext
675       */
676      public void testOfferInExecutor() {
677          final LinkedTransferQueue q = new LinkedTransferQueue();
678 <        q.add(one);
707 <        q.add(two);
678 >        final CountDownLatch threadsStarted = new CountDownLatch(2);
679          ExecutorService executor = Executors.newFixedThreadPool(2);
709        executor.execute(new Runnable() {
680  
681 <            public void run() {
682 <                try {
683 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
684 <                } catch (Exception e) {
685 <                    threadUnexpectedException();
686 <                }
687 <            }
688 <        });
689 <
690 <        executor.execute(new Runnable() {
691 <
692 <            public void run() {
693 <                try {
694 <                    Thread.sleep(SMALL_DELAY_MS);
725 <                    threadAssertEquals(one, q.take());
726 <                } catch (InterruptedException e) {
727 <                    threadUnexpectedException();
728 <                }
729 <            }
730 <        });
681 >        executor.execute(new CheckedRunnable() {
682 >            public void realRun() throws InterruptedException {
683 >                threadsStarted.countDown();
684 >                threadsStarted.await();
685 >                assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS));
686 >            }});
687 >
688 >        executor.execute(new CheckedRunnable() {
689 >            public void realRun() throws InterruptedException {
690 >                threadsStarted.countDown();
691 >                threadsStarted.await();
692 >                assertSame(one, q.take());
693 >                checkEmpty(q);
694 >            }});
695  
696          joinPool(executor);
697      }
698  
699      /**
700 <     * poll retrieves elements across Executor threads
700 >     * timed poll retrieves elements across Executor threads
701       */
702      public void testPollInExecutor() {
703          final LinkedTransferQueue q = new LinkedTransferQueue();
704 +        final CountDownLatch threadsStarted = new CountDownLatch(2);
705          ExecutorService executor = Executors.newFixedThreadPool(2);
741        executor.execute(new Runnable() {
742
743            public void run() {
744                threadAssertNull(q.poll());
745                try {
746                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
747                    threadAssertTrue(q.isEmpty());
748                } catch (InterruptedException e) {
749                    threadUnexpectedException();
750                }
751            }
752        });
706  
707 <        executor.execute(new Runnable() {
708 <
709 <            public void run() {
710 <                try {
711 <                    Thread.sleep(SMALL_DELAY_MS);
712 <                    q.put(one);
713 <                } catch (InterruptedException e) {
714 <                    threadUnexpectedException();
715 <                }
716 <            }
717 <        });
707 >        executor.execute(new CheckedRunnable() {
708 >            public void realRun() throws InterruptedException {
709 >                assertNull(q.poll());
710 >                threadsStarted.countDown();
711 >                threadsStarted.await();
712 >                assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS));
713 >                checkEmpty(q);
714 >            }});
715 >
716 >        executor.execute(new CheckedRunnable() {
717 >            public void realRun() throws InterruptedException {
718 >                threadsStarted.countDown();
719 >                threadsStarted.await();
720 >                q.put(one);
721 >            }});
722  
723          joinPool(executor);
724      }
# Line 769 | Line 726 | public class LinkedTransferQueueTest ext
726      /**
727       * A deserialized serialized queue has same elements in same order
728       */
729 <    public void testSerialization() {
729 >    public void testSerialization() throws Exception {
730          LinkedTransferQueue q = populatedQueue(SIZE);
731  
732 <        try {
733 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
734 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
735 <            out.writeObject(q);
736 <            out.close();
737 <
738 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
739 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
740 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
741 <
742 <            assertEquals(q.size(), r.size());
743 <            while (!q.isEmpty()) {
744 <                assertEquals(q.remove(), r.remove());
745 <            }
746 <        } catch (Exception e) {
747 <            unexpectedException();
732 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
733 >        ObjectOutputStream out
734 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
735 >        out.writeObject(q);
736 >        out.close();
737 >
738 >        ByteArrayInputStream bin
739 >            = new ByteArrayInputStream(bout.toByteArray());
740 >        ObjectInputStream in
741 >            = new ObjectInputStream(new BufferedInputStream(bin));
742 >        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
743 >
744 >        assertEquals(q.size(), r.size());
745 >        assertEquals(q.toString(), r.toString());
746 >        assertTrue(Arrays.equals(q.toArray(), r.toArray()));
747 >        while (!q.isEmpty()) {
748 >            assertEquals(q.remove(), r.remove());
749          }
750      }
751  
752      /**
753 <     * drainTo(null) throws NPE
753 >     * drainTo(null) throws NullPointerException
754       */
755      public void testDrainToNull() {
756          LinkedTransferQueue q = populatedQueue(SIZE);
757          try {
758              q.drainTo(null);
759              shouldThrow();
760 <        } catch (NullPointerException success) {
803 <        }
760 >        } catch (NullPointerException success) {}
761      }
762  
763      /**
764 <     * drainTo(this) throws IAE
764 >     * drainTo(this) throws IllegalArgumentException
765       */
766      public void testDrainToSelf() {
767          LinkedTransferQueue q = populatedQueue(SIZE);
768          try {
769              q.drainTo(q);
770              shouldThrow();
771 <        } catch (IllegalArgumentException success) {
815 <        }
771 >        } catch (IllegalArgumentException success) {}
772      }
773  
774      /**
# Line 825 | Line 781 | public class LinkedTransferQueueTest ext
781          assertEquals(q.size(), 0);
782          assertEquals(l.size(), SIZE);
783          for (int i = 0; i < SIZE; ++i) {
784 <            assertEquals(l.get(i), new Integer(i));
784 >            assertEquals(l.get(i), i);
785          }
786          q.add(zero);
787          q.add(one);
# Line 837 | Line 793 | public class LinkedTransferQueueTest ext
793          assertEquals(q.size(), 0);
794          assertEquals(l.size(), 2);
795          for (int i = 0; i < 2; ++i) {
796 <            assertEquals(l.get(i), new Integer(i));
796 >            assertEquals(l.get(i), i);
797          }
798      }
799  
800      /**
801 <     * drainTo empties full queue, unblocking a waiting put.
801 >     * drainTo(c) empties full queue, unblocking a waiting put.
802       */
803 <    public void testDrainToWithActivePut() {
803 >    public void testDrainToWithActivePut() throws InterruptedException {
804          final LinkedTransferQueue q = populatedQueue(SIZE);
805 <        Thread t = new Thread(new Runnable() {
806 <
807 <            public void run() {
808 <                try {
809 <                    q.put(new Integer(SIZE + 1));
810 <                } catch (Exception ie) {
811 <                    threadUnexpectedException();
812 <                }
813 <            }
858 <        });
859 <        try {
860 <            t.start();
861 <            ArrayList l = new ArrayList();
862 <            q.drainTo(l);
863 <            assertTrue(l.size() >= SIZE);
864 <            for (int i = 0; i < SIZE; ++i) {
865 <                assertEquals(l.get(i), new Integer(i));
866 <            }
867 <            t.join();
868 <            assertTrue(q.size() + l.size() >= SIZE);
869 <        } catch (Exception e) {
870 <            unexpectedException();
805 >        Thread t = newStartedThread(new CheckedRunnable() {
806 >            public void realRun() {
807 >                q.put(SIZE + 1);
808 >            }});
809 >        ArrayList l = new ArrayList();
810 >        q.drainTo(l);
811 >        assertTrue(l.size() >= SIZE);
812 >        for (int i = 0; i < SIZE; ++i) {
813 >            assertEquals(l.get(i), i);
814          }
815 +        awaitTermination(t, MEDIUM_DELAY_MS);
816 +        assertTrue(q.size() + l.size() >= SIZE);
817      }
818  
819      /**
820 <     * drainTo(null, n) throws NPE
820 >     * drainTo(null, n) throws NullPointerException
821       */
822      public void testDrainToNullN() {
823          LinkedTransferQueue q = populatedQueue(SIZE);
824          try {
825 <            q.drainTo(null, 0);
825 >            q.drainTo(null, SIZE);
826              shouldThrow();
827 <        } catch (NullPointerException success) {
883 <        }
827 >        } catch (NullPointerException success) {}
828      }
829  
830      /**
831 <     * drainTo(this, n) throws IAE
831 >     * drainTo(this, n) throws IllegalArgumentException
832       */
833      public void testDrainToSelfN() {
834          LinkedTransferQueue q = populatedQueue(SIZE);
835          try {
836 <            q.drainTo(q, 0);
836 >            q.drainTo(q, SIZE);
837              shouldThrow();
838 <        } catch (IllegalArgumentException success) {
895 <        }
838 >        } catch (IllegalArgumentException success) {}
839      }
840  
841      /**
842 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
842 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
843       */
844      public void testDrainToN() {
845          LinkedTransferQueue q = new LinkedTransferQueue();
846          for (int i = 0; i < SIZE + 2; ++i) {
847              for (int j = 0; j < SIZE; j++) {
848 <                assertTrue(q.offer(new Integer(j)));
848 >                assertTrue(q.offer(j));
849              }
850              ArrayList l = new ArrayList();
851              q.drainTo(l, i);
# Line 910 | Line 853 | public class LinkedTransferQueueTest ext
853              assertEquals(l.size(), k);
854              assertEquals(q.size(), SIZE - k);
855              for (int j = 0; j < k; ++j) {
856 <                assertEquals(l.get(j), new Integer(j));
856 >                assertEquals(l.get(j), j);
857              }
858 <            while (q.poll() != null);
858 >            while (q.poll() != null)
859 >                ;
860          }
861      }
862  
863 <    /*
864 <     * poll and take should decrement the waiting consumer count
863 >    /**
864 >     * timed poll() or take() increments the waiting consumer count;
865 >     * offer(e) decrements the waiting consumer count
866       */
867 <    public void testWaitingConsumer() {
868 <        try {
869 <            final LinkedTransferQueue q = new LinkedTransferQueue();
870 <            final ConsumerObserver waiting = new ConsumerObserver();
871 <            new Thread(new Runnable() {
867 >    public void testWaitingConsumer() throws InterruptedException {
868 >        final LinkedTransferQueue q = new LinkedTransferQueue();
869 >        assertEquals(q.getWaitingConsumerCount(), 0);
870 >        assertFalse(q.hasWaitingConsumer());
871 >        final CountDownLatch threadStarted = new CountDownLatch(1);
872  
873 <                public void run() {
874 <                    try {
875 <                        threadAssertTrue(q.hasWaitingConsumer());
876 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
877 <                        threadAssertTrue(q.offer(new Object()));
878 <                    } catch (Exception ex) {
879 <                        threadUnexpectedException();
935 <                    }
873 >        Thread t = newStartedThread(new CheckedRunnable() {
874 >            public void realRun() throws InterruptedException {
875 >                threadStarted.countDown();
876 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
877 >                assertEquals(q.getWaitingConsumerCount(), 0);
878 >                assertFalse(q.hasWaitingConsumer());
879 >            }});
880  
881 <                }
882 <            }).start();
883 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
884 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
885 <        } catch (Exception ex) {
886 <            this.unexpectedException();
887 <        }
881 >        threadStarted.await();
882 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
883 >        assertEquals(q.getWaitingConsumerCount(), 1);
884 >        assertTrue(q.hasWaitingConsumer());
885 >
886 >        assertTrue(q.offer(one));
887 >        assertEquals(q.getWaitingConsumerCount(), 0);
888 >        assertFalse(q.hasWaitingConsumer());
889 >
890 >        awaitTermination(t, MEDIUM_DELAY_MS);
891      }
945    /*
946     * Inserts null into transfer throws NPE
947     */
892  
893 <    public void testTransfer1() {
893 >    /**
894 >     * transfer(null) throws NullPointerException
895 >     */
896 >    public void testTransfer1() throws InterruptedException {
897          try {
898              LinkedTransferQueue q = new LinkedTransferQueue();
899              q.transfer(null);
900              shouldThrow();
901 <        } catch (NullPointerException ex) {
955 <        } catch (Exception ex) {
956 <            this.unexpectedException();
957 <        }
901 >        } catch (NullPointerException success) {}
902      }
903  
904 <    /*
905 <     * transfer attempts to insert into the queue then wait until that
906 <     * object is removed via take or poll.
904 >    /**
905 >     * transfer waits until a poll occurs. The transfered element
906 >     * is returned by this associated poll.
907       */
908 <    public void testTransfer2() {
909 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
910 <        new Thread(new Runnable() {
908 >    public void testTransfer2() throws InterruptedException {
909 >        final LinkedTransferQueue<Integer> q
910 >            = new LinkedTransferQueue<Integer>();
911 >        final CountDownLatch threadStarted = new CountDownLatch(1);
912  
913 <            public void run() {
914 <                try {
915 <                    q.transfer(new Integer(SIZE));
916 <                    threadAssertTrue(q.isEmpty());
917 <                } catch (Exception ex) {
918 <                    threadUnexpectedException();
974 <                }
975 <            }
976 <        }).start();
913 >        Thread t = newStartedThread(new CheckedRunnable() {
914 >            public void realRun() throws InterruptedException {
915 >                threadStarted.countDown();
916 >                q.transfer(five);
917 >                checkEmpty(q);
918 >            }});
919  
920 <        try {
921 <            Thread.sleep(SHORT_DELAY_MS);
922 <            assertEquals(1, q.size());
923 <            q.poll();
924 <            assertTrue(q.isEmpty());
925 <        } catch (Exception ex) {
984 <            this.unexpectedException();
985 <        }
920 >        threadStarted.await();
921 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
922 >        assertEquals(1, q.size());
923 >        assertSame(five, q.poll());
924 >        checkEmpty(q);
925 >        awaitTermination(t, MEDIUM_DELAY_MS);
926      }
927 <    /*
928 <     * transfer will attempt to transfer in fifo order and continue waiting if
929 <     * the element being transfered is not polled or taken
930 <     */
931 <
932 <    public void testTransfer3() {
933 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
934 <        new Thread(new Runnable() {
935 <                public void run() {
936 <                    try {
937 <                        Integer i;
938 <                        q.transfer((i = new Integer(SIZE + 1)));
939 <                        threadAssertTrue(!q.contains(i));
940 <                        threadAssertEquals(1, q.size());
941 <                    } catch (Exception ex) {
942 <                        threadUnexpectedException();
943 <                    }
944 <                }
945 <            }).start();
946 <        Thread interruptedThread =
947 <            new Thread(new Runnable() {
948 <                    public void run() {
949 <                        try {
950 <                            q.transfer(new Integer(SIZE));
951 <                            threadShouldThrow();
952 <                        } catch (InterruptedException ex) {
953 <                        }
954 <                    }
955 <                });
956 <        interruptedThread.start();
957 <        try {
958 <            Thread.sleep(LONG_DELAY_MS);
1019 <            assertEquals(2, q.size());
1020 <            q.poll();
1021 <            Thread.sleep(LONG_DELAY_MS);
1022 <            interruptedThread.interrupt();
1023 <            assertEquals(1, q.size());
1024 <        } catch (Exception ex) {
1025 <            this.unexpectedException();
1026 <        }
927 >
928 >    /**
929 >     * transfer waits until a poll occurs, and then transfers in fifo order
930 >     */
931 >    public void testTransfer3() throws InterruptedException {
932 >        final LinkedTransferQueue<Integer> q
933 >            = new LinkedTransferQueue<Integer>();
934 >
935 >        Thread first = newStartedThread(new CheckedRunnable() {
936 >            public void realRun() throws InterruptedException {
937 >                q.transfer(four);
938 >                assertTrue(!q.contains(four));
939 >                assertEquals(1, q.size());
940 >            }});
941 >
942 >        Thread interruptedThread = newStartedThread(
943 >            new CheckedInterruptedRunnable() {
944 >                public void realRun() throws InterruptedException {
945 >                    while (q.isEmpty())
946 >                        Thread.yield();
947 >                    q.transfer(five);
948 >                }});
949 >
950 >        while (q.size() < 2)
951 >            Thread.yield();
952 >        assertEquals(2, q.size());
953 >        assertSame(four, q.poll());
954 >        first.join();
955 >        assertEquals(1, q.size());
956 >        interruptedThread.interrupt();
957 >        interruptedThread.join();
958 >        checkEmpty(q);
959      }
960  
961      /**
962 <     * transfer will wait as long as a poll or take occurs if one does occur
963 <     * the waiting is finished and the thread that tries to poll/take
1032 <     * wins in retrieving the element
962 >     * transfer waits until a poll occurs, at which point the polling
963 >     * thread returns the element
964       */
965 <    public void testTransfer4() {
965 >    public void testTransfer4() throws InterruptedException {
966          final LinkedTransferQueue q = new LinkedTransferQueue();
1036        new Thread(new Runnable() {
967  
968 <            public void run() {
969 <                try {
970 <                    q.transfer(new Integer(four));
971 <                    threadAssertFalse(q.contains(new Integer(four)));
972 <                    threadAssertEquals(new Integer(three), q.poll());
973 <                } catch (Exception ex) {
974 <                    threadUnexpectedException();
975 <                }
976 <            }
977 <        }).start();
978 <        try {
979 <            Thread.sleep(MEDIUM_DELAY_MS);
980 <            assertTrue(q.offer(three));
981 <            assertEquals(new Integer(four), q.poll());
1052 <        } catch (Exception ex) {
1053 <            this.unexpectedException();
1054 <        }
968 >        Thread t = newStartedThread(new CheckedRunnable() {
969 >            public void realRun() throws InterruptedException {
970 >                q.transfer(four);
971 >                assertFalse(q.contains(four));
972 >                assertSame(three, q.poll());
973 >            }});
974 >
975 >        while (q.isEmpty())
976 >            Thread.yield();
977 >        assertFalse(q.isEmpty());
978 >        assertEquals(1, q.size());
979 >        assertTrue(q.offer(three));
980 >        assertSame(four, q.poll());
981 >        awaitTermination(t, MEDIUM_DELAY_MS);
982      }
983 <    /*
984 <     * Insert null into trTransfer throws NPE
983 >
984 >    /**
985 >     * transfer waits until a take occurs. The transfered element
986 >     * is returned by this associated take.
987       */
988 +    public void testTransfer5() throws InterruptedException {
989 +        final LinkedTransferQueue<Integer> q
990 +            = new LinkedTransferQueue<Integer>();
991  
992 +        Thread t = newStartedThread(new CheckedRunnable() {
993 +            public void realRun() throws InterruptedException {
994 +                q.transfer(four);
995 +                checkEmpty(q);
996 +            }});
997 +
998 +        while (q.isEmpty())
999 +            Thread.yield();
1000 +        assertFalse(q.isEmpty());
1001 +        assertEquals(1, q.size());
1002 +        assertSame(four, q.take());
1003 +        checkEmpty(q);
1004 +        awaitTermination(t, MEDIUM_DELAY_MS);
1005 +    }
1006 +
1007 +    /**
1008 +     * tryTransfer(null) throws NullPointerException
1009 +     */
1010      public void testTryTransfer1() {
1011          try {
1012              final LinkedTransferQueue q = new LinkedTransferQueue();
1013              q.tryTransfer(null);
1014 <            this.shouldThrow();
1015 <        } catch (NullPointerException ex) {
1066 <        } catch (Exception ex) {
1067 <            this.unexpectedException();
1068 <        }
1014 >            shouldThrow();
1015 >        } catch (NullPointerException success) {}
1016      }
1070    /*
1071     * tryTransfer returns false and does not enqueue if there are no consumers
1072     * waiting to poll or take.
1073     */
1017  
1018 <    public void testTryTransfer2() {
1019 <        try {
1020 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1021 <            assertFalse(q.tryTransfer(new Object()));
1022 <            assertEquals(0, q.size());
1023 <        } catch (Exception ex) {
1024 <            this.unexpectedException();
1025 <        }
1018 >    /**
1019 >     * tryTransfer returns false and does not enqueue if there are no
1020 >     * consumers waiting to poll or take.
1021 >     */
1022 >    public void testTryTransfer2() throws InterruptedException {
1023 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1024 >        assertFalse(q.tryTransfer(new Object()));
1025 >        assertFalse(q.hasWaitingConsumer());
1026 >        checkEmpty(q);
1027      }
1028 <    /*
1029 <     * if there is a consumer waiting poll or take tryTransfer returns
1030 <     * true while enqueueing object
1028 >
1029 >    /**
1030 >     * If there is a consumer waiting in timed poll, tryTransfer
1031 >     * returns true while successfully transfering object.
1032       */
1033 +    public void testTryTransfer3() throws InterruptedException {
1034 +        final Object hotPotato = new Object();
1035 +        final LinkedTransferQueue q = new LinkedTransferQueue();
1036  
1037 <    public void testTryTransfer3() {
1038 <        try {
1039 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1040 <            new Thread(new Runnable() {
1037 >        Thread t = newStartedThread(new CheckedRunnable() {
1038 >            public void realRun() {
1039 >                while (! q.hasWaitingConsumer())
1040 >                    Thread.yield();
1041 >                assertTrue(q.hasWaitingConsumer());
1042 >                checkEmpty(q);
1043 >                assertTrue(q.tryTransfer(hotPotato));
1044 >            }});
1045 >
1046 >        assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1047 >        checkEmpty(q);
1048 >        awaitTermination(t, MEDIUM_DELAY_MS);
1049 >    }
1050  
1051 <                public void run() {
1052 <                    try {
1053 <                        threadAssertTrue(q.hasWaitingConsumer());
1054 <                        threadAssertTrue(q.tryTransfer(new Object()));
1055 <                    } catch (Exception ex) {
1056 <                        threadUnexpectedException();
1057 <                    }
1051 >    /**
1052 >     * If there is a consumer waiting in take, tryTransfer returns
1053 >     * true while successfully transfering object.
1054 >     */
1055 >    public void testTryTransfer4() throws InterruptedException {
1056 >        final Object hotPotato = new Object();
1057 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1058  
1059 <                }
1060 <            }).start();
1061 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
1062 <            assertTrue(q.isEmpty());
1063 <        } catch (Exception ex) {
1064 <            this.unexpectedException();
1065 <        }
1059 >        Thread t = newStartedThread(new CheckedRunnable() {
1060 >            public void realRun() {
1061 >                while (! q.hasWaitingConsumer())
1062 >                    Thread.yield();
1063 >                assertTrue(q.hasWaitingConsumer());
1064 >                checkEmpty(q);
1065 >                assertTrue(q.tryTransfer(hotPotato));
1066 >            }});
1067 >
1068 >        assertSame(q.take(), hotPotato);
1069 >        checkEmpty(q);
1070 >        awaitTermination(t, MEDIUM_DELAY_MS);
1071      }
1072  
1073 <    /*
1074 <     * tryTransfer waits the amount given if interrupted, show an
1075 <     * interrupted exception
1073 >    /**
1074 >     * tryTransfer waits the amount given if interrupted, and
1075 >     * throws interrupted exception
1076       */
1077 <    public void testTryTransfer4() {
1077 >    public void testTryTransfer5() throws InterruptedException {
1078          final LinkedTransferQueue q = new LinkedTransferQueue();
1079 <        Thread toInterrupt = new Thread(new Runnable() {
1079 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1080  
1081 <            public void run() {
1081 >        Thread t = newStartedThread(new CheckedRunnable() {
1082 >            public void realRun() throws InterruptedException {
1083 >                long t0 = System.nanoTime();
1084 >                threadStarted.countDown();
1085                  try {
1086 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1087 <                    threadShouldThrow();
1088 <                } catch (InterruptedException ex) {
1089 <                }
1090 <            }
1091 <        });
1092 <        try {
1093 <            toInterrupt.start();
1094 <            Thread.sleep(SMALL_DELAY_MS);
1095 <            toInterrupt.interrupt();
1096 <        } catch (Exception ex) {
1132 <            this.unexpectedException();
1133 <        }
1086 >                    q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1087 >                    shouldThrow();
1088 >                } catch (InterruptedException success) {}
1089 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1090 >            }});
1091 >
1092 >        threadStarted.await();
1093 >        Thread.sleep(SHORT_DELAY_MS);
1094 >        t.interrupt();
1095 >        awaitTermination(t, MEDIUM_DELAY_MS);
1096 >        checkEmpty(q);
1097      }
1098  
1099 <    /*
1100 <     * tryTransfer gives up after the timeout and return false
1099 >    /**
1100 >     * tryTransfer gives up after the timeout and returns false
1101       */
1102 <    public void testTryTransfer5() {
1102 >    public void testTryTransfer6() throws InterruptedException {
1103          final LinkedTransferQueue q = new LinkedTransferQueue();
1141        try {
1142            new Thread(new Runnable() {
1104  
1105 <                public void run() {
1106 <                    try {
1107 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1108 <                    } catch (InterruptedException ex) {
1109 <                        threadUnexpectedException();
1110 <                    }
1111 <                }
1112 <            }).start();
1113 <            Thread.sleep(LONG_DELAY_MS);
1114 <            assertTrue(q.isEmpty());
1115 <        } catch (Exception ex) {
1155 <            this.unexpectedException();
1156 <        }
1105 >        Thread t = newStartedThread(new CheckedRunnable() {
1106 >            public void realRun() throws InterruptedException {
1107 >                long t0 = System.nanoTime();
1108 >                assertFalse(q.tryTransfer(new Object(),
1109 >                                          SHORT_DELAY_MS, MILLISECONDS));
1110 >                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1111 >                checkEmpty(q);
1112 >            }});
1113 >
1114 >        awaitTermination(t, MEDIUM_DELAY_MS);
1115 >        checkEmpty(q);
1116      }
1117  
1118 <    /*
1118 >    /**
1119       * tryTransfer waits for any elements previously in to be removed
1120       * before transfering to a poll or take
1121       */
1122 <    public void testTryTransfer6() {
1122 >    public void testTryTransfer7() throws InterruptedException {
1123          final LinkedTransferQueue q = new LinkedTransferQueue();
1124 <        q.offer(new Integer(four));
1166 <        new Thread(new Runnable() {
1124 >        assertTrue(q.offer(four));
1125  
1126 <            public void run() {
1127 <                try {
1128 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1129 <                    threadAssertTrue(q.isEmpty());
1130 <                } catch (InterruptedException ex) {
1131 <                    threadUnexpectedException();
1132 <                }
1133 <            }
1134 <        }).start();
1135 <        try {
1136 <            Thread.sleep(SHORT_DELAY_MS);
1137 <            assertEquals(2, q.size());
1138 <            assertEquals(new Integer(four), q.poll());
1181 <            assertEquals(new Integer(five), q.poll());
1182 <            assertTrue(q.isEmpty());
1183 <        } catch (Exception ex) {
1184 <            this.unexpectedException();
1185 <        }
1126 >        Thread t = newStartedThread(new CheckedRunnable() {
1127 >            public void realRun() throws InterruptedException {
1128 >                assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS));
1129 >                checkEmpty(q);
1130 >            }});
1131 >
1132 >        while (q.size() != 2)
1133 >            Thread.yield();
1134 >        assertEquals(2, q.size());
1135 >        assertSame(four, q.poll());
1136 >        assertSame(five, q.poll());
1137 >        checkEmpty(q);
1138 >        awaitTermination(t, MEDIUM_DELAY_MS);
1139      }
1140  
1141 <    /*
1142 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1143 <     * enqueueing and the successing poll is null
1141 >    /**
1142 >     * tryTransfer attempts to enqueue into the q and fails returning
1143 >     * false not enqueueing and the successive poll is null
1144       */
1145 <    public void testTryTransfer7() {
1145 >    public void testTryTransfer8() throws InterruptedException {
1146          final LinkedTransferQueue q = new LinkedTransferQueue();
1147 <        q.offer(new Integer(four));
1148 <        new Thread(new Runnable() {
1149 <
1150 <            public void run() {
1151 <                try {
1152 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1153 <                    threadAssertTrue(q.isEmpty());
1154 <                } catch (InterruptedException ex) {
1155 <                    threadUnexpectedException();
1203 <                }
1204 <            }
1205 <        }).start();
1206 <        try {
1207 <            assertEquals(1, q.size());
1208 <            assertEquals(new Integer(four), q.poll());
1209 <            Thread.sleep(MEDIUM_DELAY_MS);
1210 <            assertNull(q.poll());
1211 <        } catch (Exception ex) {
1212 <            this.unexpectedException();
1213 <        }
1147 >        assertTrue(q.offer(four));
1148 >        assertEquals(1, q.size());
1149 >        long t0 = System.nanoTime();
1150 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1151 >        assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
1152 >        assertEquals(1, q.size());
1153 >        assertSame(four, q.poll());
1154 >        assertNull(q.poll());
1155 >        checkEmpty(q);
1156      }
1157  
1158 <    private LinkedTransferQueue populatedQueue(
1159 <            int n) {
1160 <        LinkedTransferQueue q = new LinkedTransferQueue();
1161 <        assertTrue(q.isEmpty());
1162 <        int remainingCapacity = q.remainingCapacity();
1221 <        for (int i = 0; i <
1222 <                n; i++) {
1158 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1159 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1160 >        checkEmpty(q);
1161 >        for (int i = 0; i < n; i++) {
1162 >            assertEquals(i, q.size());
1163              assertTrue(q.offer(i));
1164 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1165          }
1225
1166          assertFalse(q.isEmpty());
1227        assertEquals(remainingCapacity, q.remainingCapacity());
1228        assertEquals(n, q.size());
1167          return q;
1168      }
1231
1232    private static class ConsumerObserver {
1233
1234        private int waitingConsumers;
1235
1236        private ConsumerObserver() {
1237        }
1238
1239        private void setWaitingConsumer(int i) {
1240            this.waitingConsumers = i;
1241        }
1242
1243        private int getWaitingConsumers() {
1244            return waitingConsumers;
1245        }
1246    }
1169   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines