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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines