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.17 by jsr166, Sat Nov 21 19:45:16 2009 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 void main(String[] args) {
# Line 30 | Line 32 | public class LinkedTransferQueueTest ext
32          return new TestSuite(LinkedTransferQueueTest.class);
33      }
34  
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 being true
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());
# Line 39 | Line 65 | public class LinkedTransferQueueTest ext
65      }
66  
67      /**
68 <     * Initializing constructor with null collection throws NPE
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) {
49 <        }
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) {
61 <        }
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
91       * Initializing constructor with a collection containing some null elements
92 <     * throws NPE
92 >     * throws NullPointerException
93       */
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) {
77 <        }
102 >        } catch (NullPointerException success) {}
103      }
104  
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);
102 <        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) {
124 <        }
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) {
136 <        }
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) {
148 <        }
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) {
160 <        }
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 169 | Line 196 | public class LinkedTransferQueueTest ext
196              Integer[] ints = new Integer[SIZE];
197              q.addAll(Arrays.asList(ints));
198              shouldThrow();
199 <        } catch (NullPointerException success) {
173 <        }
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) {
190 <        }
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) {
207 <                assertEquals(ints[i], q.poll());
208 <            }
209 <        } 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) {
222 <        } catch (Exception ie) {
223 <            unexpectedException();
224 <        }
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);
236 <                assertTrue(q.contains(I));
237 <            }
238 <        } catch (Exception ie) {
239 <            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) {
250 <                assertEquals(i, ((Integer) q.take()).intValue());
251 <            }
252 <        } catch (InterruptedException e) {
253 <            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) {
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 <        }
273 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
274 >            void realRun() throws InterruptedException {
275 >                q.take();
276 >            }});
277 >        Thread.sleep(SHORT_DELAY_MS);
278 >        t.interrupt();
279 >        t.join();
280      }
281  
282      /**
283       * Take removes existing elements until empty, then blocks interruptibly
284       */
285 <    public void testBlockingTake() {
286 <        Thread t = new Thread(new Runnable() {
287 <
288 <            public void run() {
289 <                try {
290 <                    LinkedTransferQueue q = populatedQueue(SIZE);
291 <                    for (int i = 0; i < SIZE; ++i) {
292 <                        assertEquals(i, ((Integer) q.take()).intValue());
293 <                    }
294 <                    q.take();
295 <                    threadShouldThrow();
296 <                } catch (InterruptedException success) {
285 >    public void testBlockingTake() throws InterruptedException {
286 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
287 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
288 >            void realRun() throws InterruptedException {
289 >                for (int i = 0; i < SIZE; ++i) {
290 >                    threadAssertEquals(i, (int) q.take());
291                  }
292 <            }
293 <        });
294 <        t.start();
295 <        try {
296 <            Thread.sleep(SHORT_DELAY_MS);
297 <            t.interrupt();
304 <            t.join();
305 <        } catch (InterruptedException ie) {
306 <            unexpectedException();
307 <        }
292 >                q.take();
293 >            }});
294 >        Thread.sleep(SMALL_DELAY_MS);
295 >        t.interrupt();
296 >        t.join();
297 >        checkEmpty(q);
298      }
299  
300      /**
301       * poll succeeds unless empty
302       */
303 <    public void testPoll() {
304 <        LinkedTransferQueue q = populatedQueue(SIZE);
303 >    public void testPoll() throws InterruptedException {
304 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306 <            assertEquals(i, ((Integer) q.poll()).intValue());
306 >            assertEquals(i, (int) q.poll());
307          }
308          assertNull(q.poll());
309 +        checkEmpty(q);
310      }
311  
312      /**
313       * timed pool with zero timeout succeeds when non-empty, else times out
314       */
315 <    public void testTimedPoll0() {
316 <        try {
317 <            LinkedTransferQueue q = populatedQueue(SIZE);
318 <            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();
315 >    public void testTimedPoll0() throws InterruptedException {
316 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
317 >        for (int i = 0; i < SIZE; ++i) {
318 >            assertEquals(i, (int) q.poll(0, MILLISECONDS));
319          }
320 +        assertNull(q.poll(0, MILLISECONDS));
321 +        checkEmpty(q);
322      }
323  
324      /**
325       * timed pool with nonzero timeout succeeds when non-empty, else times out
326       */
327 <    public void testTimedPoll() {
328 <        try {
329 <            LinkedTransferQueue q = populatedQueue(SIZE);
330 <            for (int i = 0; i < SIZE; ++i) {
331 <                assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
332 <            }
333 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
346 <        } catch (InterruptedException e) {
347 <            unexpectedException();
327 >    public void testTimedPoll() throws InterruptedException {
328 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
329 >        for (int i = 0; i < SIZE; ++i) {
330 >            long t0 = System.nanoTime();
331 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
332 >            long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
333 >            assertTrue(millisElapsed < SMALL_DELAY_MS);
334          }
335 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
336 +        checkEmpty(q);
337      }
338  
339      /**
340       * Interrupted timed poll throws InterruptedException instead of
341       * returning timeout status
342       */
343 <    public void testInterruptedTimedPoll() {
344 <        Thread t = new Thread(new Runnable() {
345 <
346 <            public void run() {
347 <                try {
348 <                    LinkedTransferQueue q = populatedQueue(SIZE);
349 <                    for (int i = 0; i < SIZE; ++i) {
350 <                        threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
351 <                    }
352 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
365 <                } catch (InterruptedException success) {
343 >    public void testInterruptedTimedPoll() throws InterruptedException {
344 >        final LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
345 >        Thread t = newStartedThread(new CheckedRunnable() {
346 >            void realRun() throws InterruptedException {
347 >                for (int i = 0; i < SIZE; ++i) {
348 >                    long t0 = System.nanoTime();
349 >                    threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS,
350 >                                                       MILLISECONDS));
351 >                    long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024);
352 >                    assertTrue(millisElapsed < SMALL_DELAY_MS);
353                  }
354 <            }
355 <        });
356 <        t.start();
357 <        try {
358 <            Thread.sleep(SHORT_DELAY_MS);
359 <            t.interrupt();
360 <            t.join();
361 <        } catch (InterruptedException ie) {
362 <            unexpectedException();
363 <        }
354 >                try {
355 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
356 >                    shouldThrow();
357 >                } catch (InterruptedException success) {}
358 >            }});
359 >
360 >        Thread.sleep(SMALL_DELAY_MS);
361 >        t.interrupt();
362 >        t.join();
363 >        checkEmpty(q);
364      }
365  
366      /**
367       * timed poll before a delayed offer fails; after offer succeeds;
368       * on interruption throws
369       */
370 <    public void testTimedPollWithOffer() {
370 >    public void testTimedPollWithOffer() throws InterruptedException {
371          final LinkedTransferQueue q = new LinkedTransferQueue();
372 <        Thread t = new Thread(new Runnable() {
373 <
374 <            public void run() {
375 <                try {
376 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
377 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
378 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
379 <                    threadShouldThrow();
380 <                } catch (InterruptedException success) {
381 <                }
395 <            }
396 <        });
397 <        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 <        }
372 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
373 >            void realRun() throws InterruptedException {
374 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
376 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
377 >            }});
378 >        Thread.sleep(SMALL_DELAY_MS);
379 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
380 >        t.interrupt();
381 >        t.join();
382      }
383  
384      /**
385       * peek returns next element, or null if empty
386       */
387 <    public void testPeek() {
388 <        LinkedTransferQueue q = populatedQueue(SIZE);
387 >    public void testPeek() throws InterruptedException {
388 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
389          for (int i = 0; i < SIZE; ++i) {
390 <            assertEquals(i, ((Integer) q.peek()).intValue());
391 <            q.poll();
390 >            assertEquals(i, (int) q.peek());
391 >            assertEquals(i, (int) q.poll());
392              assertTrue(q.peek() == null ||
393 <                    i != ((Integer) q.peek()).intValue());
393 >                       i != (int) q.peek());
394          }
395          assertNull(q.peek());
396 +        checkEmpty(q);
397      }
398  
399      /**
400 <     * element returns next element, or throws NSEE if empty
400 >     * element returns next element, or throws NoSuchElementException if empty
401       */
402 <    public void testElement() {
403 <        LinkedTransferQueue q = populatedQueue(SIZE);
402 >    public void testElement() throws InterruptedException {
403 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
404          for (int i = 0; i < SIZE; ++i) {
405 <            assertEquals(i, ((Integer) q.element()).intValue());
406 <            q.poll();
405 >            assertEquals(i, (int) q.element());
406 >            assertEquals(i, (int) q.poll());
407          }
408          try {
409              q.element();
410              shouldThrow();
411 <        } catch (NoSuchElementException success) {
412 <        }
411 >        } catch (NoSuchElementException success) {}
412 >        checkEmpty(q);
413      }
414  
415      /**
416 <     * remove removes next element, or throws NSEE if empty
416 >     * remove removes next element, or throws NoSuchElementException if empty
417       */
418 <    public void testRemove() {
419 <        LinkedTransferQueue q = populatedQueue(SIZE);
418 >    public void testRemove() throws InterruptedException {
419 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
420          for (int i = 0; i < SIZE; ++i) {
421 <            assertEquals(i, ((Integer) q.remove()).intValue());
421 >            assertEquals(i, (int) q.remove());
422          }
423          try {
424              q.remove();
425              shouldThrow();
426 <        } catch (NoSuchElementException success) {
427 <        }
426 >        } catch (NoSuchElementException success) {}
427 >        checkEmpty(q);
428      }
429  
430      /**
431       * remove(x) removes x and returns true if present
432       */
433 <    public void testRemoveElement() {
433 >    public void testRemoveElement() throws InterruptedException {
434          LinkedTransferQueue q = populatedQueue(SIZE);
435          for (int i = 1; i < SIZE; i += 2) {
436 <            assertTrue(q.remove(new Integer(i)));
436 >            assertTrue(q.remove(i));
437          }
438          for (int i = 0; i < SIZE; i += 2) {
439 <            assertTrue(q.remove(new Integer(i)));
440 <            assertFalse(q.remove(new Integer(i + 1)));
439 >            assertTrue(q.remove(i));
440 >            assertFalse(q.remove(i + 1));
441          }
442 <        assertTrue(q.isEmpty());
442 >        checkEmpty(q);
443      }
444  
445      /**
446       * An add following remove(x) succeeds
447       */
448 <    public void testRemoveElementAndAdd() {
449 <        try {
450 <            LinkedTransferQueue q = new LinkedTransferQueue();
451 <            assertTrue(q.add(new Integer(1)));
452 <            assertTrue(q.add(new Integer(2)));
453 <            assertTrue(q.remove(new Integer(1)));
454 <            assertTrue(q.remove(new Integer(2)));
455 <            assertTrue(q.add(new Integer(3)));
479 <            assertTrue(q.take() != null);
480 <        } catch (Exception e) {
481 <            unexpectedException();
482 <        }
448 >    public void testRemoveElementAndAdd() throws InterruptedException {
449 >        LinkedTransferQueue q = new LinkedTransferQueue();
450 >        assertTrue(q.add(one));
451 >        assertTrue(q.add(two));
452 >        assertTrue(q.remove(one));
453 >        assertTrue(q.remove(two));
454 >        assertTrue(q.add(three));
455 >        assertTrue(q.take() == three);
456      }
457  
458      /**
459       * contains(x) reports true when elements added but not yet removed
460       */
461      public void testContains() {
462 <        LinkedTransferQueue q = populatedQueue(SIZE);
462 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
463          for (int i = 0; i < SIZE; ++i) {
464 <            assertTrue(q.contains(new Integer(i)));
465 <            q.poll();
466 <            assertFalse(q.contains(new Integer(i)));
464 >            assertTrue(q.contains(i));
465 >            assertEquals(i, (int) q.poll());
466 >            assertFalse(q.contains(i));
467          }
468      }
469  
470      /**
471       * clear removes all elements
472       */
473 <    public void testClear() {
473 >    public void testClear() throws InterruptedException {
474          LinkedTransferQueue q = populatedQueue(SIZE);
502        int remainingCapacity = q.remainingCapacity();
475          q.clear();
476 <        assertTrue(q.isEmpty());
477 <        assertEquals(0, q.size());
506 <        assertEquals(remainingCapacity, q.remainingCapacity());
476 >        checkEmpty(q);
477 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
478          q.add(one);
479          assertFalse(q.isEmpty());
480 +        assertEquals(1, q.size());
481          assertTrue(q.contains(one));
482          q.clear();
483 <        assertTrue(q.isEmpty());
483 >        checkEmpty(q);
484      }
485  
486      /**
487       * containsAll(c) is true when c contains a subset of elements
488       */
489      public void testContainsAll() {
490 <        LinkedTransferQueue q = populatedQueue(SIZE);
491 <        LinkedTransferQueue p = new LinkedTransferQueue();
490 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
491 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
492          for (int i = 0; i < SIZE; ++i) {
493              assertTrue(q.containsAll(p));
494              assertFalse(p.containsAll(q));
495 <            p.add(new Integer(i));
495 >            p.add(i);
496          }
497          assertTrue(p.containsAll(q));
498      }
499  
500      /**
501 <     * retainAll(c) retains only those elements of c and reports true if changed
501 >     * retainAll(c) retains only those elements of c and reports true
502 >     * if changed
503       */
504      public void testRetainAll() {
505          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 545 | Line 518 | public class LinkedTransferQueueTest ext
518      }
519  
520      /**
521 <     * removeAll(c) removes only those elements of c and reports true if changed
521 >     * removeAll(c) removes only those elements of c and reports true
522 >     * if changed
523       */
524      public void testRemoveAll() {
525          for (int i = 1; i < SIZE; ++i) {
# Line 554 | Line 528 | public class LinkedTransferQueueTest ext
528              assertTrue(q.removeAll(p));
529              assertEquals(SIZE - i, q.size());
530              for (int j = 0; j < i; ++j) {
531 <                Integer I = (Integer) (p.remove());
558 <                assertFalse(q.contains(I));
531 >                assertFalse(q.contains(p.remove()));
532              }
533          }
534      }
535  
536      /**
537 <     * toArray contains all elements
537 >     * toArray() contains all elements
538       */
539 <    public void testToArray() {
539 >    public void testToArray() throws InterruptedException {
540          LinkedTransferQueue q = populatedQueue(SIZE);
541          Object[] o = q.toArray();
542 <        try {
543 <            for (int i = 0; i < o.length; i++) {
571 <                assertEquals(o[i], q.take());
572 <            }
573 <        } catch (InterruptedException e) {
574 <            unexpectedException();
542 >        for (int i = 0; i < o.length; i++) {
543 >            assertEquals(o[i], q.take());
544          }
545      }
546  
547      /**
548       * toArray(a) contains all elements
549       */
550 <    public void testToArray2() {
551 <        LinkedTransferQueue q = populatedQueue(SIZE);
550 >    public void testToArray2() throws InterruptedException {
551 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
552          Integer[] ints = new Integer[SIZE];
553 <        ints = (Integer[]) q.toArray(ints);
554 <        try {
555 <            for (int i = 0; i < ints.length; i++) {
587 <                assertEquals(ints[i], q.take());
588 <            }
589 <        } catch (InterruptedException e) {
590 <            unexpectedException();
553 >        ints = q.toArray(ints);
554 >        for (int i = 0; i < ints.length; i++) {
555 >            assertEquals(ints[i], q.take());
556          }
557      }
558  
559      /**
560 <     * toArray(null) throws NPE
560 >     * toArray(null) throws NullPointerException
561       */
562      public void testToArray_BadArg() {
563          try {
564              LinkedTransferQueue q = populatedQueue(SIZE);
565              Object o[] = q.toArray(null);
566              shouldThrow();
567 <        } catch (NullPointerException success) {
603 <        }
567 >        } catch (NullPointerException success) {}
568      }
569  
570      /**
571 <     * toArray with incompatible array type throws CCE
571 >     * toArray(incompatible array type) throws CCE
572       */
573      public void testToArray1_BadArg() {
574          try {
575              LinkedTransferQueue q = populatedQueue(SIZE);
576              Object o[] = q.toArray(new String[10]);
577              shouldThrow();
578 <        } catch (ArrayStoreException success) {
615 <        }
578 >        } catch (ArrayStoreException success) {}
579      }
580  
581      /**
582       * iterator iterates through all elements
583       */
584 <    public void testIterator() {
584 >    public void testIterator() throws InterruptedException {
585          LinkedTransferQueue q = populatedQueue(SIZE);
586          Iterator it = q.iterator();
587 <        try {
588 <            while (it.hasNext()) {
589 <                assertEquals(it.next(), q.take());
627 <            }
628 <        } catch (InterruptedException e) {
629 <            unexpectedException();
587 >        int i = 0;
588 >        while (it.hasNext()) {
589 >            assertEquals(it.next(), i++);
590          }
591 +        assertEquals(i, SIZE);
592      }
593  
594      /**
595 <     * iterator.remove removes current element
595 >     * iterator.remove() removes current element
596       */
597      public void testIteratorRemove() {
598          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 653 | Line 614 | public class LinkedTransferQueueTest ext
614       * iterator ordering is FIFO
615       */
616      public void testIteratorOrdering() {
617 <        final LinkedTransferQueue q = new LinkedTransferQueue();
618 <        int remainingCapacity = q.remainingCapacity();
617 >        final LinkedTransferQueue<Integer> q
618 >            = new LinkedTransferQueue<Integer>();
619 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
620          q.add(one);
621          q.add(two);
622          q.add(three);
623 <        assertEquals(remainingCapacity, q.remainingCapacity());
623 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
624          int k = 0;
625 <        for (Iterator it = q.iterator(); it.hasNext();) {
626 <            int i = ((Integer) (it.next())).intValue();
665 <            assertEquals(++k, i);
625 >        for (Integer n : q) {
626 >            assertEquals(++k, (int) n);
627          }
628          assertEquals(3, k);
629      }
# Line 675 | Line 636 | public class LinkedTransferQueueTest ext
636          q.add(one);
637          q.add(two);
638          q.add(three);
639 <        try {
640 <            for (Iterator it = q.iterator(); it.hasNext();) {
641 <                q.remove();
681 <                it.next();
682 <            }
683 <        } catch (ConcurrentModificationException e) {
684 <            unexpectedException();
639 >        for (Iterator it = q.iterator(); it.hasNext();) {
640 >            q.remove();
641 >            it.next();
642          }
643          assertEquals(0, q.size());
644      }
# Line 705 | Line 662 | public class LinkedTransferQueueTest ext
662          q.add(one);
663          q.add(two);
664          ExecutorService executor = Executors.newFixedThreadPool(2);
708        executor.execute(new Runnable() {
665  
666 <            public void run() {
667 <                try {
668 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
669 <                } catch (Exception e) {
670 <                    threadUnexpectedException();
671 <                }
672 <            }
673 <        });
674 <
675 <        executor.execute(new Runnable() {
676 <
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 <        });
666 >        executor.execute(new CheckedRunnable() {
667 >            void realRun() {
668 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
669 >                                         MILLISECONDS));
670 >            }});
671 >
672 >        executor.execute(new CheckedRunnable() {
673 >            void realRun() throws InterruptedException {
674 >                Thread.sleep(SMALL_DELAY_MS);
675 >                threadAssertEquals(one, q.take());
676 >            }});
677  
678          joinPool(executor);
679      }
680  
681      /**
682 <     * poll retrieves elements across Executor threads
682 >     * timed poll retrieves elements across Executor threads
683       */
684      public void testPollInExecutor() {
685          final LinkedTransferQueue q = new LinkedTransferQueue();
686          ExecutorService executor = Executors.newFixedThreadPool(2);
740        executor.execute(new Runnable() {
687  
688 <            public void run() {
688 >        executor.execute(new CheckedRunnable() {
689 >            void realRun() throws InterruptedException {
690                  threadAssertNull(q.poll());
691 <                try {
692 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
693 <                    threadAssertTrue(q.isEmpty());
694 <                } catch (InterruptedException e) {
695 <                    threadUnexpectedException();
696 <                }
697 <            }
698 <        });
699 <
700 <        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 <        });
691 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
692 >                                                MILLISECONDS));
693 >                threadAssertTrue(q.isEmpty());
694 >            }});
695 >
696 >        executor.execute(new CheckedRunnable() {
697 >            void realRun() throws InterruptedException {
698 >                Thread.sleep(SMALL_DELAY_MS);
699 >                q.put(one);
700 >            }});
701  
702          joinPool(executor);
703      }
# Line 768 | Line 705 | public class LinkedTransferQueueTest ext
705      /**
706       * A deserialized serialized queue has same elements in same order
707       */
708 <    public void testSerialization() {
708 >    public void testSerialization() throws Exception {
709          LinkedTransferQueue q = populatedQueue(SIZE);
710  
711 <        try {
712 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
713 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
714 <            out.writeObject(q);
715 <            out.close();
716 <
717 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
718 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
719 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
720 <
721 <            assertEquals(q.size(), r.size());
722 <            while (!q.isEmpty()) {
723 <                assertEquals(q.remove(), r.remove());
724 <            }
725 <        } catch (Exception e) {
789 <            unexpectedException();
711 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
712 >        ObjectOutputStream out
713 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
714 >        out.writeObject(q);
715 >        out.close();
716 >
717 >        ByteArrayInputStream bin
718 >            = new ByteArrayInputStream(bout.toByteArray());
719 >        ObjectInputStream in
720 >            = new ObjectInputStream(new BufferedInputStream(bin));
721 >        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
722 >
723 >        assertEquals(q.size(), r.size());
724 >        while (!q.isEmpty()) {
725 >            assertEquals(q.remove(), r.remove());
726          }
727      }
728  
729      /**
730 <     * drainTo(null) throws NPE
730 >     * drainTo(null) throws NullPointerException
731       */
732      public void testDrainToNull() {
733          LinkedTransferQueue q = populatedQueue(SIZE);
734          try {
735              q.drainTo(null);
736              shouldThrow();
737 <        } catch (NullPointerException success) {
802 <        }
737 >        } catch (NullPointerException success) {}
738      }
739  
740      /**
741 <     * drainTo(this) throws IAE
741 >     * drainTo(this) throws IllegalArgumentException
742       */
743      public void testDrainToSelf() {
744          LinkedTransferQueue q = populatedQueue(SIZE);
745          try {
746              q.drainTo(q);
747              shouldThrow();
748 <        } catch (IllegalArgumentException success) {
814 <        }
748 >        } catch (IllegalArgumentException success) {}
749      }
750  
751      /**
# Line 824 | Line 758 | public class LinkedTransferQueueTest ext
758          assertEquals(q.size(), 0);
759          assertEquals(l.size(), SIZE);
760          for (int i = 0; i < SIZE; ++i) {
761 <            assertEquals(l.get(i), new Integer(i));
761 >            assertEquals(l.get(i), i);
762          }
763          q.add(zero);
764          q.add(one);
# Line 836 | Line 770 | public class LinkedTransferQueueTest ext
770          assertEquals(q.size(), 0);
771          assertEquals(l.size(), 2);
772          for (int i = 0; i < 2; ++i) {
773 <            assertEquals(l.get(i), new Integer(i));
773 >            assertEquals(l.get(i), i);
774          }
775      }
776  
777      /**
778 <     * drainTo empties full queue, unblocking a waiting put.
778 >     * drainTo(c) empties full queue, unblocking a waiting put.
779       */
780 <    public void testDrainToWithActivePut() {
780 >    public void testDrainToWithActivePut() throws InterruptedException {
781          final LinkedTransferQueue q = populatedQueue(SIZE);
782 <        Thread t = new Thread(new Runnable() {
783 <
784 <            public void run() {
785 <                try {
786 <                    q.put(new Integer(SIZE + 1));
787 <                } catch (Exception ie) {
788 <                    threadUnexpectedException();
789 <                }
790 <            }
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();
782 >        Thread t = newStartedThread(new CheckedRunnable() {
783 >            void realRun() {
784 >                q.put(SIZE + 1);
785 >            }});
786 >        ArrayList l = new ArrayList();
787 >        q.drainTo(l);
788 >        assertTrue(l.size() >= SIZE);
789 >        for (int i = 0; i < SIZE; ++i) {
790 >            assertEquals(l.get(i), i);
791          }
792 +        t.join();
793 +        assertTrue(q.size() + l.size() >= SIZE);
794      }
795  
796      /**
797 <     * drainTo(null, n) throws NPE
797 >     * drainTo(null, n) throws NullPointerException
798       */
799      public void testDrainToNullN() {
800          LinkedTransferQueue q = populatedQueue(SIZE);
801          try {
802 <            q.drainTo(null, 0);
802 >            q.drainTo(null, SIZE);
803              shouldThrow();
804 <        } catch (NullPointerException success) {
882 <        }
804 >        } catch (NullPointerException success) {}
805      }
806  
807      /**
808 <     * drainTo(this, n) throws IAE
808 >     * drainTo(this, n) throws IllegalArgumentException
809       */
810      public void testDrainToSelfN() {
811          LinkedTransferQueue q = populatedQueue(SIZE);
812          try {
813 <            q.drainTo(q, 0);
813 >            q.drainTo(q, SIZE);
814              shouldThrow();
815 <        } catch (IllegalArgumentException success) {
894 <        }
815 >        } catch (IllegalArgumentException success) {}
816      }
817  
818      /**
# Line 901 | Line 822 | public class LinkedTransferQueueTest ext
822          LinkedTransferQueue q = new LinkedTransferQueue();
823          for (int i = 0; i < SIZE + 2; ++i) {
824              for (int j = 0; j < SIZE; j++) {
825 <                assertTrue(q.offer(new Integer(j)));
825 >                assertTrue(q.offer(j));
826              }
827              ArrayList l = new ArrayList();
828              q.drainTo(l, i);
# Line 909 | Line 830 | public class LinkedTransferQueueTest ext
830              assertEquals(l.size(), k);
831              assertEquals(q.size(), SIZE - k);
832              for (int j = 0; j < k; ++j) {
833 <                assertEquals(l.get(j), new Integer(j));
833 >                assertEquals(l.get(j), j);
834              }
835 <            while (q.poll() != null);
835 >            while (q.poll() != null)
836 >                ;
837          }
838      }
839  
840      /**
841 <     * poll and take should decrement the waiting consumer count
841 >     * timed poll() or take() increments the waiting consumer count;
842 >     * offer(e) decrements the waiting consumer count
843       */
844 <    public void testWaitingConsumer() {
845 <        try {
846 <            final LinkedTransferQueue q = new LinkedTransferQueue();
847 <            final ConsumerObserver waiting = new ConsumerObserver();
925 <            new Thread(new Runnable() {
844 >    public void testWaitingConsumer() throws InterruptedException {
845 >        final LinkedTransferQueue q = new LinkedTransferQueue();
846 >        assertEquals(q.getWaitingConsumerCount(), 0);
847 >        assertFalse(q.hasWaitingConsumer());
848  
849 <                public void run() {
850 <                    try {
851 <                        threadAssertTrue(q.hasWaitingConsumer());
852 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
853 <                        threadAssertTrue(q.offer(new Object()));
854 <                    } catch (Exception ex) {
855 <                        threadUnexpectedException();
856 <                    }
849 >        Thread t = newStartedThread(new CheckedRunnable() {
850 >            void realRun() throws InterruptedException {
851 >                Thread.sleep(SMALL_DELAY_MS);
852 >                threadAssertTrue(q.hasWaitingConsumer());
853 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
854 >                threadAssertTrue(q.offer(new Object()));
855 >                threadAssertFalse(q.hasWaitingConsumer());
856 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
857 >            }});
858  
859 <                }
860 <            }).start();
861 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
862 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
940 <        } catch (Exception ex) {
941 <            this.unexpectedException();
942 <        }
859 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
860 >        assertEquals(q.getWaitingConsumerCount(), 0);
861 >        assertFalse(q.hasWaitingConsumer());
862 >        t.join();
863      }
864  
865      /**
866 <     * Inserts null into transfer throws NPE
866 >     * transfer(null) throws NullPointerException
867       */
868 <    public void testTransfer1() {
868 >    public void testTransfer1() throws InterruptedException {
869          try {
870              LinkedTransferQueue q = new LinkedTransferQueue();
871              q.transfer(null);
872              shouldThrow();
873 <        } catch (NullPointerException ex) {
954 <        } catch (Exception ex) {
955 <            this.unexpectedException();
956 <        }
873 >        } catch (NullPointerException success) {}
874      }
875  
876      /**
877 <     * transfer attempts to insert into the queue then wait until that
878 <     * object is removed via take or poll.
877 >     * transfer waits until a poll occurs. The transfered element
878 >     * is returned by this associated poll.
879       */
880 <    public void testTransfer2() {
881 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
882 <        new Thread(new Runnable() {
880 >    public void testTransfer2() throws InterruptedException {
881 >        final LinkedTransferQueue<Integer> q
882 >            = new LinkedTransferQueue<Integer>();
883  
884 <            public void run() {
885 <                try {
886 <                    q.transfer(new Integer(SIZE));
887 <                    threadAssertTrue(q.isEmpty());
888 <                } catch (Exception ex) {
972 <                    threadUnexpectedException();
973 <                }
974 <            }
975 <        }).start();
884 >        Thread t = newStartedThread(new CheckedRunnable() {
885 >            void realRun() throws InterruptedException {
886 >                q.transfer(SIZE);
887 >                threadAssertTrue(q.isEmpty());
888 >            }});
889  
890 <        try {
891 <            Thread.sleep(SHORT_DELAY_MS);
892 <            assertEquals(1, q.size());
893 <            q.poll();
894 <            assertTrue(q.isEmpty());
982 <        } catch (Exception ex) {
983 <            this.unexpectedException();
984 <        }
890 >        Thread.sleep(SHORT_DELAY_MS);
891 >        assertEquals(1, q.size());
892 >        assertEquals(SIZE, (int) q.poll());
893 >        assertTrue(q.isEmpty());
894 >        t.join();
895      }
896  
897      /**
898 <     * transfer will attempt to transfer in fifo order and continue
899 <     * waiting if the element being transfered is not polled or taken
900 <     */
901 <    public void testTransfer3() {
902 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
903 <        new Thread(new Runnable() {
904 <                public void run() {
905 <                    try {
906 <                        Integer i;
907 <                        q.transfer((i = new Integer(SIZE + 1)));
908 <                        threadAssertTrue(!q.contains(i));
909 <                        threadAssertEquals(1, q.size());
910 <                    } catch (Exception ex) {
911 <                        threadUnexpectedException();
912 <                    }
913 <                }
914 <            }).start();
915 <        Thread interruptedThread =
916 <            new Thread(new Runnable() {
917 <                    public void run() {
918 <                        try {
919 <                            q.transfer(new Integer(SIZE));
920 <                            threadShouldThrow();
921 <                        } catch (InterruptedException ex) {
922 <                        }
923 <                    }
924 <                });
925 <        interruptedThread.start();
926 <        try {
927 <            Thread.sleep(LONG_DELAY_MS);
928 <            assertEquals(2, q.size());
929 <            q.poll();
1020 <            Thread.sleep(LONG_DELAY_MS);
1021 <            interruptedThread.interrupt();
1022 <            assertEquals(1, q.size());
1023 <        } catch (Exception ex) {
1024 <            this.unexpectedException();
1025 <        }
898 >     * transfer waits until a poll occurs, and then transfers in fifo order
899 >     */
900 >    public void testTransfer3() throws InterruptedException {
901 >        final LinkedTransferQueue<Integer> q
902 >            = new LinkedTransferQueue<Integer>();
903 >
904 >        Thread first = newStartedThread(new CheckedRunnable() {
905 >            void realRun() throws InterruptedException {
906 >                Integer i = SIZE + 1;
907 >                q.transfer(i);
908 >                threadAssertTrue(!q.contains(i));
909 >                threadAssertEquals(1, q.size());
910 >            }});
911 >
912 >        Thread interruptedThread = newStartedThread(
913 >            new CheckedInterruptedRunnable() {
914 >                void realRun() throws InterruptedException {
915 >                    while (q.size() == 0)
916 >                        Thread.yield();
917 >                    q.transfer(SIZE);
918 >                }});
919 >
920 >        while (q.size() < 2)
921 >            Thread.yield();
922 >        assertEquals(2, q.size());
923 >        assertEquals(SIZE + 1, (int) q.poll());
924 >        first.join();
925 >        assertEquals(1, q.size());
926 >        interruptedThread.interrupt();
927 >        interruptedThread.join();
928 >        assertEquals(0, q.size());
929 >        assertTrue(q.isEmpty());
930      }
931  
932      /**
933 <     * transfer will wait as long as a poll or take occurs if one does occur
934 <     * the waiting is finished and the thread that tries to poll/take
1031 <     * wins in retrieving the element
933 >     * transfer waits until a poll occurs, at which point the polling
934 >     * thread returns the element
935       */
936 <    public void testTransfer4() {
936 >    public void testTransfer4() throws InterruptedException {
937          final LinkedTransferQueue q = new LinkedTransferQueue();
1035        new Thread(new Runnable() {
938  
939 <            public void run() {
940 <                try {
941 <                    q.transfer(new Integer(four));
942 <                    threadAssertFalse(q.contains(new Integer(four)));
943 <                    threadAssertEquals(new Integer(three), q.poll());
944 <                } catch (Exception ex) {
945 <                    threadUnexpectedException();
946 <                }
947 <            }
948 <        }).start();
949 <        try {
950 <            Thread.sleep(MEDIUM_DELAY_MS);
951 <            assertTrue(q.offer(three));
952 <            assertEquals(new Integer(four), q.poll());
953 <        } catch (Exception ex) {
954 <            this.unexpectedException();
955 <        }
939 >        Thread t = newStartedThread(new CheckedRunnable() {
940 >            void realRun() throws InterruptedException {
941 >                q.transfer(four);
942 >                threadAssertFalse(q.contains(four));
943 >                threadAssertEquals(three, q.poll());
944 >            }});
945 >
946 >        Thread.sleep(SHORT_DELAY_MS);
947 >        assertTrue(q.offer(three));
948 >        assertEquals(four, q.poll());
949 >        t.join();
950 >    }
951 >
952 >    /**
953 >     * transfer waits until a take occurs. The transfered element
954 >     * is returned by this associated take.
955 >     */
956 >    public void testTransfer5() throws InterruptedException {
957 >        final LinkedTransferQueue<Integer> q
958 >            = new LinkedTransferQueue<Integer>();
959 >
960 >        Thread t = newStartedThread(new CheckedRunnable() {
961 >            void realRun() throws InterruptedException {
962 >                q.transfer(SIZE);
963 >                checkEmpty(q);
964 >            }});
965 >
966 >        Thread.sleep(SHORT_DELAY_MS);
967 >        assertEquals(SIZE, (int) q.take());
968 >        checkEmpty(q);
969 >        t.join();
970      }
971  
972      /**
973 <     * Insert null into tryTransfer throws NPE
973 >     * tryTransfer(null) throws NullPointerException
974       */
975      public void testTryTransfer1() {
976          try {
977              final LinkedTransferQueue q = new LinkedTransferQueue();
978              q.tryTransfer(null);
979 <            this.shouldThrow();
980 <        } catch (NullPointerException ex) {
1065 <        } catch (Exception ex) {
1066 <            this.unexpectedException();
1067 <        }
979 >            shouldThrow();
980 >        } catch (NullPointerException success) {}
981      }
982  
983      /**
984 <     * tryTransfer returns false and does not enqueue if there are no consumers
985 <     * waiting to poll or take.
984 >     * tryTransfer returns false and does not enqueue if there are no
985 >     * consumers waiting to poll or take.
986       */
987 <    public void testTryTransfer2() {
988 <        try {
989 <            final LinkedTransferQueue q = new LinkedTransferQueue();
990 <            assertFalse(q.tryTransfer(new Object()));
991 <            assertEquals(0, q.size());
1079 <        } catch (Exception ex) {
1080 <            this.unexpectedException();
1081 <        }
987 >    public void testTryTransfer2() throws InterruptedException {
988 >        final LinkedTransferQueue q = new LinkedTransferQueue();
989 >        assertFalse(q.tryTransfer(new Object()));
990 >        assertFalse(q.hasWaitingConsumer());
991 >        checkEmpty(q);
992      }
993  
994      /**
995 <     * if there is a consumer waiting poll or take tryTransfer returns
996 <     * true while enqueueing object
995 >     * If there is a consumer waiting in timed poll, tryTransfer
996 >     * returns true while successfully transfering object.
997       */
998 <    public void testTryTransfer3() {
999 <        try {
1000 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1091 <            new Thread(new Runnable() {
998 >    public void testTryTransfer3() throws InterruptedException {
999 >        final Object hotPotato = new Object();
1000 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1001  
1002 <                public void run() {
1003 <                    try {
1004 <                        threadAssertTrue(q.hasWaitingConsumer());
1005 <                        threadAssertTrue(q.tryTransfer(new Object()));
1006 <                    } catch (Exception ex) {
1007 <                        threadUnexpectedException();
1008 <                    }
1002 >        Thread t = newStartedThread(new CheckedRunnable() {
1003 >            void realRun() {
1004 >                while (! q.hasWaitingConsumer())
1005 >                    Thread.yield();
1006 >                threadAssertTrue(q.hasWaitingConsumer());
1007 >                threadAssertTrue(q.isEmpty());
1008 >                threadAssertTrue(q.size() == 0);
1009 >                threadAssertTrue(q.tryTransfer(hotPotato));
1010 >            }});
1011 >
1012 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1013 >        checkEmpty(q);
1014 >        t.join();
1015 >    }
1016  
1017 <                }
1018 <            }).start();
1019 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
1020 <            assertTrue(q.isEmpty());
1021 <        } catch (Exception ex) {
1022 <            this.unexpectedException();
1023 <        }
1017 >    /**
1018 >     * If there is a consumer waiting in take, tryTransfer returns
1019 >     * true while successfully transfering object.
1020 >     */
1021 >    public void testTryTransfer4() throws InterruptedException {
1022 >        final Object hotPotato = new Object();
1023 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1024 >
1025 >        Thread t = newStartedThread(new CheckedRunnable() {
1026 >            void realRun() {
1027 >                while (! q.hasWaitingConsumer())
1028 >                    Thread.yield();
1029 >                threadAssertTrue(q.hasWaitingConsumer());
1030 >                threadAssertTrue(q.isEmpty());
1031 >                threadAssertTrue(q.size() == 0);
1032 >                threadAssertTrue(q.tryTransfer(hotPotato));
1033 >            }});
1034 >
1035 >        assertTrue(q.take() == hotPotato);
1036 >        checkEmpty(q);
1037 >        t.join();
1038      }
1039  
1040      /**
1041 <     * tryTransfer waits the amount given if interrupted, show an
1042 <     * interrupted exception
1041 >     * tryTransfer waits the amount given if interrupted, and
1042 >     * throws interrupted exception
1043       */
1044 <    public void testTryTransfer4() {
1044 >    public void testTryTransfer5() throws InterruptedException {
1045          final LinkedTransferQueue q = new LinkedTransferQueue();
1116        Thread toInterrupt = new Thread(new Runnable() {
1046  
1047 <            public void run() {
1048 <                try {
1049 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1050 <                    threadShouldThrow();
1051 <                } catch (InterruptedException ex) {
1052 <                }
1053 <            }
1054 <        });
1126 <        try {
1127 <            toInterrupt.start();
1128 <            Thread.sleep(SMALL_DELAY_MS);
1129 <            toInterrupt.interrupt();
1130 <        } catch (Exception ex) {
1131 <            this.unexpectedException();
1132 <        }
1047 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1048 >            void realRun() throws InterruptedException {
1049 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1050 >            }});
1051 >
1052 >        Thread.sleep(SMALL_DELAY_MS);
1053 >        toInterrupt.interrupt();
1054 >        toInterrupt.join();
1055      }
1056  
1057      /**
1058       * tryTransfer gives up after the timeout and return false
1059       */
1060 <    public void testTryTransfer5() {
1060 >    public void testTryTransfer6() throws InterruptedException {
1061          final LinkedTransferQueue q = new LinkedTransferQueue();
1140        try {
1141            new Thread(new Runnable() {
1062  
1063 <                public void run() {
1064 <                    try {
1065 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1066 <                    } catch (InterruptedException ex) {
1067 <                        threadUnexpectedException();
1068 <                    }
1069 <                }
1070 <            }).start();
1071 <            Thread.sleep(LONG_DELAY_MS);
1072 <            assertTrue(q.isEmpty());
1153 <        } catch (Exception ex) {
1154 <            this.unexpectedException();
1155 <        }
1063 >        Thread t = newStartedThread(new CheckedRunnable() {
1064 >            void realRun() throws InterruptedException {
1065 >                threadAssertFalse
1066 >                    (q.tryTransfer(new Object(),
1067 >                                   SHORT_DELAY_MS, MILLISECONDS));
1068 >            }});
1069 >
1070 >        Thread.sleep(SMALL_DELAY_MS);
1071 >        checkEmpty(q);
1072 >        t.join();
1073      }
1074  
1075      /**
1076       * tryTransfer waits for any elements previously in to be removed
1077       * before transfering to a poll or take
1078       */
1079 <    public void testTryTransfer6() {
1079 >    public void testTryTransfer7() throws InterruptedException {
1080          final LinkedTransferQueue q = new LinkedTransferQueue();
1081 <        q.offer(new Integer(four));
1165 <        new Thread(new Runnable() {
1081 >        assertTrue(q.offer(four));
1082  
1083 <            public void run() {
1084 <                try {
1085 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1086 <                    threadAssertTrue(q.isEmpty());
1087 <                } catch (InterruptedException ex) {
1088 <                    threadUnexpectedException();
1089 <                }
1090 <            }
1091 <        }).start();
1092 <        try {
1093 <            Thread.sleep(SHORT_DELAY_MS);
1094 <            assertEquals(2, q.size());
1095 <            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 <        }
1083 >        Thread t = newStartedThread(new CheckedRunnable() {
1084 >            void realRun() throws InterruptedException {
1085 >                threadAssertTrue(q.tryTransfer(five,
1086 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1087 >                threadAssertTrue(q.isEmpty());
1088 >            }});
1089 >
1090 >        Thread.sleep(SHORT_DELAY_MS);
1091 >        assertEquals(2, q.size());
1092 >        assertEquals(four, q.poll());
1093 >        assertEquals(five, q.poll());
1094 >        checkEmpty(q);
1095 >        t.join();
1096      }
1097  
1098      /**
1099 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1100 <     * enqueueing and the successive poll is null
1099 >     * tryTransfer attempts to enqueue into the q and fails returning
1100 >     * false not enqueueing and the successive poll is null
1101       */
1102 <    public void testTryTransfer7() {
1102 >    public void testTryTransfer8() throws InterruptedException {
1103          final LinkedTransferQueue q = new LinkedTransferQueue();
1104 <        q.offer(new Integer(four));
1105 <        new Thread(new Runnable() {
1106 <
1107 <            public void run() {
1108 <                try {
1109 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1110 <                    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 <        }
1104 >        assertTrue(q.offer(four));
1105 >        assertEquals(1, q.size());
1106 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1107 >        assertEquals(1, q.size());
1108 >        assertEquals(four, q.poll());
1109 >        assertNull(q.poll());
1110 >        checkEmpty(q);
1111      }
1112  
1113 <    private LinkedTransferQueue populatedQueue(
1114 <            int n) {
1217 <        LinkedTransferQueue q = new LinkedTransferQueue();
1113 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1114 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1115          assertTrue(q.isEmpty());
1116 <        int remainingCapacity = q.remainingCapacity();
1117 <        for (int i = 0; i <
1221 <                n; i++) {
1116 >        for (int i = 0; i < n; i++) {
1117 >            assertEquals(i, q.size());
1118              assertTrue(q.offer(i));
1119 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1120          }
1224
1121          assertFalse(q.isEmpty());
1226        assertEquals(remainingCapacity, q.remainingCapacity());
1227        assertEquals(n, q.size());
1122          return q;
1123      }
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    }
1124   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines