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.2 by jsr166, Fri Jul 31 23:37:31 2009 UTC vs.
Revision 1.21 by jsr166, Thu Nov 26 15:42:15 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines