ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedTransferQueueTest.java
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines