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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.18 by jsr166, Sat Nov 21 21:00:34 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 >            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() {
290 <                try {
291 <                    LinkedTransferQueue q = populatedQueue(SIZE);
292 <                    for (int i = 0; i < SIZE; ++i) {
293 <                        assertEquals(i, ((Integer) q.take()).intValue());
294 <                    }
295 <                    q.take();
296 <                    threadShouldThrow();
297 <                } 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();
305 <            t.join();
306 <        } catch (InterruptedException ie) {
307 <            unexpectedException();
308 <        }
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) {
329 <                assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue());
330 <            }
331 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
332 <        } catch (InterruptedException e) {
333 <            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));
347 <        } catch (InterruptedException e) {
348 <            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));
366 <                } 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
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() {
372 >        Thread t = new Thread(new CheckedRunnable() {
373 >            void realRun() throws InterruptedException {
374 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
375 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
376                  try {
377 <                    threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
378 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
379 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
380 <                    threadShouldThrow();
381 <                } catch (InterruptedException success) {
382 <                }
383 <            }
384 <        });
385 <        try {
399 <            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 <        }
377 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
378 >                    shouldThrow();
379 >                } catch (InterruptedException success) {}
380 >            }});
381 >
382 >        Thread.sleep(SMALL_DELAY_MS);
383 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
384 >        t.interrupt();
385 >        t.join();
386      }
387  
388      /**
389       * peek returns next element, or null if empty
390       */
391 <    public void testPeek() {
392 <        LinkedTransferQueue q = populatedQueue(SIZE);
391 >    public void testPeek() throws InterruptedException {
392 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
393          for (int i = 0; i < SIZE; ++i) {
394 <            assertEquals(i, ((Integer) q.peek()).intValue());
395 <            q.poll();
394 >            assertEquals(i, (int) q.peek());
395 >            assertEquals(i, (int) q.poll());
396              assertTrue(q.peek() == null ||
397 <                    i != ((Integer) q.peek()).intValue());
397 >                       i != (int) q.peek());
398          }
399          assertNull(q.peek());
400 +        checkEmpty(q);
401      }
402  
403      /**
404 <     * element returns next element, or throws NSEE if empty
404 >     * element returns next element, or throws NoSuchElementException if empty
405       */
406 <    public void testElement() {
407 <        LinkedTransferQueue q = populatedQueue(SIZE);
406 >    public void testElement() throws InterruptedException {
407 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
408          for (int i = 0; i < SIZE; ++i) {
409 <            assertEquals(i, ((Integer) q.element()).intValue());
410 <            q.poll();
409 >            assertEquals(i, (int) q.element());
410 >            assertEquals(i, (int) q.poll());
411          }
412          try {
413              q.element();
414              shouldThrow();
415 <        } catch (NoSuchElementException success) {
416 <        }
415 >        } catch (NoSuchElementException success) {}
416 >        checkEmpty(q);
417      }
418  
419      /**
420 <     * remove removes next element, or throws NSEE if empty
420 >     * remove removes next element, or throws NoSuchElementException if empty
421       */
422 <    public void testRemove() {
423 <        LinkedTransferQueue q = populatedQueue(SIZE);
422 >    public void testRemove() throws InterruptedException {
423 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
424          for (int i = 0; i < SIZE; ++i) {
425 <            assertEquals(i, ((Integer) q.remove()).intValue());
425 >            assertEquals(i, (int) q.remove());
426          }
427          try {
428              q.remove();
429              shouldThrow();
430 <        } catch (NoSuchElementException success) {
431 <        }
430 >        } catch (NoSuchElementException success) {}
431 >        checkEmpty(q);
432      }
433  
434      /**
435       * remove(x) removes x and returns true if present
436       */
437 <    public void testRemoveElement() {
437 >    public void testRemoveElement() throws InterruptedException {
438          LinkedTransferQueue q = populatedQueue(SIZE);
439          for (int i = 1; i < SIZE; i += 2) {
440 <            assertTrue(q.remove(new Integer(i)));
440 >            assertTrue(q.remove(i));
441          }
442          for (int i = 0; i < SIZE; i += 2) {
443 <            assertTrue(q.remove(new Integer(i)));
444 <            assertFalse(q.remove(new Integer(i + 1)));
443 >            assertTrue(q.remove(i));
444 >            assertFalse(q.remove(i + 1));
445          }
446 <        assertTrue(q.isEmpty());
446 >        checkEmpty(q);
447      }
448  
449      /**
450       * An add following remove(x) succeeds
451       */
452 <    public void testRemoveElementAndAdd() {
453 <        try {
454 <            LinkedTransferQueue q = new LinkedTransferQueue();
455 <            assertTrue(q.add(new Integer(1)));
456 <            assertTrue(q.add(new Integer(2)));
457 <            assertTrue(q.remove(new Integer(1)));
458 <            assertTrue(q.remove(new Integer(2)));
459 <            assertTrue(q.add(new Integer(3)));
480 <            assertTrue(q.take() != null);
481 <        } catch (Exception e) {
482 <            unexpectedException();
483 <        }
452 >    public void testRemoveElementAndAdd() throws InterruptedException {
453 >        LinkedTransferQueue q = new LinkedTransferQueue();
454 >        assertTrue(q.add(one));
455 >        assertTrue(q.add(two));
456 >        assertTrue(q.remove(one));
457 >        assertTrue(q.remove(two));
458 >        assertTrue(q.add(three));
459 >        assertTrue(q.take() == three);
460      }
461  
462      /**
463       * contains(x) reports true when elements added but not yet removed
464       */
465      public void testContains() {
466 <        LinkedTransferQueue q = populatedQueue(SIZE);
466 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
467          for (int i = 0; i < SIZE; ++i) {
468 <            assertTrue(q.contains(new Integer(i)));
469 <            q.poll();
470 <            assertFalse(q.contains(new Integer(i)));
468 >            assertTrue(q.contains(i));
469 >            assertEquals(i, (int) q.poll());
470 >            assertFalse(q.contains(i));
471          }
472      }
473  
474      /**
475       * clear removes all elements
476       */
477 <    public void testClear() {
477 >    public void testClear() throws InterruptedException {
478          LinkedTransferQueue q = populatedQueue(SIZE);
503        int remainingCapacity = q.remainingCapacity();
479          q.clear();
480 <        assertTrue(q.isEmpty());
481 <        assertEquals(0, q.size());
507 <        assertEquals(remainingCapacity, q.remainingCapacity());
480 >        checkEmpty(q);
481 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
482          q.add(one);
483          assertFalse(q.isEmpty());
484 +        assertEquals(1, q.size());
485          assertTrue(q.contains(one));
486          q.clear();
487 <        assertTrue(q.isEmpty());
487 >        checkEmpty(q);
488      }
489  
490      /**
491       * containsAll(c) is true when c contains a subset of elements
492       */
493      public void testContainsAll() {
494 <        LinkedTransferQueue q = populatedQueue(SIZE);
495 <        LinkedTransferQueue p = new LinkedTransferQueue();
494 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
495 >        LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>();
496          for (int i = 0; i < SIZE; ++i) {
497              assertTrue(q.containsAll(p));
498              assertFalse(p.containsAll(q));
499 <            p.add(new Integer(i));
499 >            p.add(i);
500          }
501          assertTrue(p.containsAll(q));
502      }
503  
504      /**
505 <     * retainAll(c) retains only those elements of c and reports true if changed
505 >     * retainAll(c) retains only those elements of c and reports true
506 >     * if changed
507       */
508      public void testRetainAll() {
509          LinkedTransferQueue q = populatedQueue(SIZE);
# Line 546 | Line 522 | public class LinkedTransferQueueTest ext
522      }
523  
524      /**
525 <     * removeAll(c) removes only those elements of c and reports true if changed
525 >     * removeAll(c) removes only those elements of c and reports true
526 >     * if changed
527       */
528      public void testRemoveAll() {
529          for (int i = 1; i < SIZE; ++i) {
# Line 555 | Line 532 | public class LinkedTransferQueueTest ext
532              assertTrue(q.removeAll(p));
533              assertEquals(SIZE - i, q.size());
534              for (int j = 0; j < i; ++j) {
535 <                Integer I = (Integer) (p.remove());
559 <                assertFalse(q.contains(I));
535 >                assertFalse(q.contains(p.remove()));
536              }
537          }
538      }
539  
540      /**
541 <     * toArray contains all elements
541 >     * toArray() contains all elements
542       */
543 <    public void testToArray() {
543 >    public void testToArray() throws InterruptedException {
544          LinkedTransferQueue q = populatedQueue(SIZE);
545          Object[] o = q.toArray();
546 <        try {
547 <            for (int i = 0; i < o.length; i++) {
572 <                assertEquals(o[i], q.take());
573 <            }
574 <        } catch (InterruptedException e) {
575 <            unexpectedException();
546 >        for (int i = 0; i < o.length; i++) {
547 >            assertEquals(o[i], q.take());
548          }
549      }
550  
551      /**
552       * toArray(a) contains all elements
553       */
554 <    public void testToArray2() {
555 <        LinkedTransferQueue q = populatedQueue(SIZE);
554 >    public void testToArray2() throws InterruptedException {
555 >        LinkedTransferQueue<Integer> q = populatedQueue(SIZE);
556          Integer[] ints = new Integer[SIZE];
557 <        ints = (Integer[]) q.toArray(ints);
558 <        try {
559 <            for (int i = 0; i < ints.length; i++) {
588 <                assertEquals(ints[i], q.take());
589 <            }
590 <        } catch (InterruptedException e) {
591 <            unexpectedException();
557 >        ints = q.toArray(ints);
558 >        for (int i = 0; i < ints.length; i++) {
559 >            assertEquals(ints[i], q.take());
560          }
561      }
562  
563      /**
564 <     * toArray(null) throws NPE
564 >     * toArray(null) throws NullPointerException
565       */
566      public void testToArray_BadArg() {
567          try {
568              LinkedTransferQueue q = populatedQueue(SIZE);
569              Object o[] = q.toArray(null);
570              shouldThrow();
571 <        } catch (NullPointerException success) {
604 <        }
571 >        } catch (NullPointerException success) {}
572      }
573  
574      /**
575 <     * toArray with incompatible array type throws CCE
575 >     * toArray(incompatible array type) throws CCE
576       */
577      public void testToArray1_BadArg() {
578          try {
579              LinkedTransferQueue q = populatedQueue(SIZE);
580              Object o[] = q.toArray(new String[10]);
581              shouldThrow();
582 <        } catch (ArrayStoreException success) {
616 <        }
582 >        } catch (ArrayStoreException success) {}
583      }
584  
585      /**
586       * iterator iterates through all elements
587       */
588 <    public void testIterator() {
588 >    public void testIterator() throws InterruptedException {
589          LinkedTransferQueue q = populatedQueue(SIZE);
590          Iterator it = q.iterator();
591 <        try {
592 <            while (it.hasNext()) {
593 <                assertEquals(it.next(), q.take());
628 <            }
629 <        } catch (InterruptedException e) {
630 <            unexpectedException();
591 >        int i = 0;
592 >        while (it.hasNext()) {
593 >            assertEquals(it.next(), i++);
594          }
595 +        assertEquals(i, SIZE);
596      }
597  
598      /**
599 <     * iterator.remove removes current element
599 >     * iterator.remove() removes current element
600       */
601      public void testIteratorRemove() {
602          final LinkedTransferQueue q = new LinkedTransferQueue();
# Line 654 | Line 618 | public class LinkedTransferQueueTest ext
618       * iterator ordering is FIFO
619       */
620      public void testIteratorOrdering() {
621 <        final LinkedTransferQueue q = new LinkedTransferQueue();
622 <        int remainingCapacity = q.remainingCapacity();
621 >        final LinkedTransferQueue<Integer> q
622 >            = new LinkedTransferQueue<Integer>();
623 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
624          q.add(one);
625          q.add(two);
626          q.add(three);
627 <        assertEquals(remainingCapacity, q.remainingCapacity());
627 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
628          int k = 0;
629 <        for (Iterator it = q.iterator(); it.hasNext();) {
630 <            int i = ((Integer) (it.next())).intValue();
666 <            assertEquals(++k, i);
629 >        for (Integer n : q) {
630 >            assertEquals(++k, (int) n);
631          }
632          assertEquals(3, k);
633      }
# Line 676 | Line 640 | public class LinkedTransferQueueTest ext
640          q.add(one);
641          q.add(two);
642          q.add(three);
643 <        try {
644 <            for (Iterator it = q.iterator(); it.hasNext();) {
645 <                q.remove();
682 <                it.next();
683 <            }
684 <        } catch (ConcurrentModificationException e) {
685 <            unexpectedException();
643 >        for (Iterator it = q.iterator(); it.hasNext();) {
644 >            q.remove();
645 >            it.next();
646          }
647          assertEquals(0, q.size());
648      }
# Line 706 | Line 666 | public class LinkedTransferQueueTest ext
666          q.add(one);
667          q.add(two);
668          ExecutorService executor = Executors.newFixedThreadPool(2);
709        executor.execute(new Runnable() {
669  
670 <            public void run() {
671 <                try {
672 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
673 <                } catch (Exception e) {
674 <                    threadUnexpectedException();
675 <                }
676 <            }
677 <        });
678 <
679 <        executor.execute(new Runnable() {
680 <
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 <        });
670 >        executor.execute(new CheckedRunnable() {
671 >            void realRun() {
672 >                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS,
673 >                                         MILLISECONDS));
674 >            }});
675 >
676 >        executor.execute(new CheckedRunnable() {
677 >            void realRun() throws InterruptedException {
678 >                Thread.sleep(SMALL_DELAY_MS);
679 >                threadAssertEquals(one, q.take());
680 >            }});
681  
682          joinPool(executor);
683      }
684  
685      /**
686 <     * poll retrieves elements across Executor threads
686 >     * timed poll retrieves elements across Executor threads
687       */
688      public void testPollInExecutor() {
689          final LinkedTransferQueue q = new LinkedTransferQueue();
690          ExecutorService executor = Executors.newFixedThreadPool(2);
741        executor.execute(new Runnable() {
691  
692 <            public void run() {
692 >        executor.execute(new CheckedRunnable() {
693 >            void realRun() throws InterruptedException {
694                  threadAssertNull(q.poll());
695 <                try {
696 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
697 <                    threadAssertTrue(q.isEmpty());
698 <                } catch (InterruptedException e) {
699 <                    threadUnexpectedException();
700 <                }
701 <            }
702 <        });
703 <
704 <        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 <        });
695 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS,
696 >                                                MILLISECONDS));
697 >                threadAssertTrue(q.isEmpty());
698 >            }});
699 >
700 >        executor.execute(new CheckedRunnable() {
701 >            void realRun() throws InterruptedException {
702 >                Thread.sleep(SMALL_DELAY_MS);
703 >                q.put(one);
704 >            }});
705  
706          joinPool(executor);
707      }
# Line 769 | Line 709 | public class LinkedTransferQueueTest ext
709      /**
710       * A deserialized serialized queue has same elements in same order
711       */
712 <    public void testSerialization() {
712 >    public void testSerialization() throws Exception {
713          LinkedTransferQueue q = populatedQueue(SIZE);
714  
715 <        try {
716 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
717 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
718 <            out.writeObject(q);
719 <            out.close();
720 <
721 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
722 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
723 <            LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
724 <
725 <            assertEquals(q.size(), r.size());
726 <            while (!q.isEmpty()) {
727 <                assertEquals(q.remove(), r.remove());
728 <            }
729 <        } catch (Exception e) {
790 <            unexpectedException();
715 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
716 >        ObjectOutputStream out
717 >            = new ObjectOutputStream(new BufferedOutputStream(bout));
718 >        out.writeObject(q);
719 >        out.close();
720 >
721 >        ByteArrayInputStream bin
722 >            = new ByteArrayInputStream(bout.toByteArray());
723 >        ObjectInputStream in
724 >            = new ObjectInputStream(new BufferedInputStream(bin));
725 >        LinkedTransferQueue r = (LinkedTransferQueue) in.readObject();
726 >
727 >        assertEquals(q.size(), r.size());
728 >        while (!q.isEmpty()) {
729 >            assertEquals(q.remove(), r.remove());
730          }
731      }
732  
733      /**
734 <     * drainTo(null) throws NPE
734 >     * drainTo(null) throws NullPointerException
735       */
736      public void testDrainToNull() {
737          LinkedTransferQueue q = populatedQueue(SIZE);
738          try {
739              q.drainTo(null);
740              shouldThrow();
741 <        } catch (NullPointerException success) {
803 <        }
741 >        } catch (NullPointerException success) {}
742      }
743  
744      /**
745 <     * drainTo(this) throws IAE
745 >     * drainTo(this) throws IllegalArgumentException
746       */
747      public void testDrainToSelf() {
748          LinkedTransferQueue q = populatedQueue(SIZE);
749          try {
750              q.drainTo(q);
751              shouldThrow();
752 <        } catch (IllegalArgumentException success) {
815 <        }
752 >        } catch (IllegalArgumentException success) {}
753      }
754  
755      /**
# Line 825 | Line 762 | public class LinkedTransferQueueTest ext
762          assertEquals(q.size(), 0);
763          assertEquals(l.size(), SIZE);
764          for (int i = 0; i < SIZE; ++i) {
765 <            assertEquals(l.get(i), new Integer(i));
765 >            assertEquals(l.get(i), i);
766          }
767          q.add(zero);
768          q.add(one);
# Line 837 | Line 774 | public class LinkedTransferQueueTest ext
774          assertEquals(q.size(), 0);
775          assertEquals(l.size(), 2);
776          for (int i = 0; i < 2; ++i) {
777 <            assertEquals(l.get(i), new Integer(i));
777 >            assertEquals(l.get(i), i);
778          }
779      }
780  
781      /**
782 <     * drainTo empties full queue, unblocking a waiting put.
782 >     * drainTo(c) empties full queue, unblocking a waiting put.
783       */
784 <    public void testDrainToWithActivePut() {
784 >    public void testDrainToWithActivePut() throws InterruptedException {
785          final LinkedTransferQueue q = populatedQueue(SIZE);
786 <        Thread t = new Thread(new Runnable() {
787 <
788 <            public void run() {
789 <                try {
790 <                    q.put(new Integer(SIZE + 1));
791 <                } catch (Exception ie) {
792 <                    threadUnexpectedException();
793 <                }
794 <            }
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();
786 >        Thread t = newStartedThread(new CheckedRunnable() {
787 >            void realRun() {
788 >                q.put(SIZE + 1);
789 >            }});
790 >        ArrayList l = new ArrayList();
791 >        q.drainTo(l);
792 >        assertTrue(l.size() >= SIZE);
793 >        for (int i = 0; i < SIZE; ++i) {
794 >            assertEquals(l.get(i), i);
795          }
796 +        t.join();
797 +        assertTrue(q.size() + l.size() >= SIZE);
798      }
799  
800      /**
801 <     * drainTo(null, n) throws NPE
801 >     * drainTo(null, n) throws NullPointerException
802       */
803      public void testDrainToNullN() {
804          LinkedTransferQueue q = populatedQueue(SIZE);
805          try {
806 <            q.drainTo(null, 0);
806 >            q.drainTo(null, SIZE);
807              shouldThrow();
808 <        } catch (NullPointerException success) {
883 <        }
808 >        } catch (NullPointerException success) {}
809      }
810  
811      /**
812 <     * drainTo(this, n) throws IAE
812 >     * drainTo(this, n) throws IllegalArgumentException
813       */
814      public void testDrainToSelfN() {
815          LinkedTransferQueue q = populatedQueue(SIZE);
816          try {
817 <            q.drainTo(q, 0);
817 >            q.drainTo(q, SIZE);
818              shouldThrow();
819 <        } catch (IllegalArgumentException success) {
895 <        }
819 >        } catch (IllegalArgumentException success) {}
820      }
821  
822      /**
# Line 902 | Line 826 | public class LinkedTransferQueueTest ext
826          LinkedTransferQueue q = new LinkedTransferQueue();
827          for (int i = 0; i < SIZE + 2; ++i) {
828              for (int j = 0; j < SIZE; j++) {
829 <                assertTrue(q.offer(new Integer(j)));
829 >                assertTrue(q.offer(j));
830              }
831              ArrayList l = new ArrayList();
832              q.drainTo(l, i);
# Line 910 | Line 834 | public class LinkedTransferQueueTest ext
834              assertEquals(l.size(), k);
835              assertEquals(q.size(), SIZE - k);
836              for (int j = 0; j < k; ++j) {
837 <                assertEquals(l.get(j), new Integer(j));
837 >                assertEquals(l.get(j), j);
838              }
839 <            while (q.poll() != null);
839 >            while (q.poll() != null)
840 >                ;
841          }
842      }
843  
844 <    /*
845 <     * poll and take should decrement the waiting consumer count
844 >    /**
845 >     * timed poll() or take() increments the waiting consumer count;
846 >     * offer(e) decrements the waiting consumer count
847       */
848 <    public void testWaitingConsumer() {
849 <        try {
850 <            final LinkedTransferQueue q = new LinkedTransferQueue();
851 <            final ConsumerObserver waiting = new ConsumerObserver();
926 <            new Thread(new Runnable() {
848 >    public void testWaitingConsumer() throws InterruptedException {
849 >        final LinkedTransferQueue q = new LinkedTransferQueue();
850 >        assertEquals(q.getWaitingConsumerCount(), 0);
851 >        assertFalse(q.hasWaitingConsumer());
852  
853 <                public void run() {
854 <                    try {
855 <                        threadAssertTrue(q.hasWaitingConsumer());
856 <                        waiting.setWaitingConsumer(q.getWaitingConsumerCount());
857 <                        threadAssertTrue(q.offer(new Object()));
858 <                    } catch (Exception ex) {
859 <                        threadUnexpectedException();
860 <                    }
853 >        Thread t = newStartedThread(new CheckedRunnable() {
854 >            void realRun() throws InterruptedException {
855 >                Thread.sleep(SMALL_DELAY_MS);
856 >                threadAssertTrue(q.hasWaitingConsumer());
857 >                threadAssertEquals(q.getWaitingConsumerCount(), 1);
858 >                threadAssertTrue(q.offer(new Object()));
859 >                threadAssertFalse(q.hasWaitingConsumer());
860 >                threadAssertEquals(q.getWaitingConsumerCount(), 0);
861 >            }});
862  
863 <                }
864 <            }).start();
865 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
866 <            assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers());
941 <        } catch (Exception ex) {
942 <            this.unexpectedException();
943 <        }
863 >        assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null);
864 >        assertEquals(q.getWaitingConsumerCount(), 0);
865 >        assertFalse(q.hasWaitingConsumer());
866 >        t.join();
867      }
945    /*
946     * Inserts null into transfer throws NPE
947     */
868  
869 <    public void testTransfer1() {
869 >    /**
870 >     * transfer(null) throws NullPointerException
871 >     */
872 >    public void testTransfer1() throws InterruptedException {
873          try {
874              LinkedTransferQueue q = new LinkedTransferQueue();
875              q.transfer(null);
876              shouldThrow();
877 <        } catch (NullPointerException ex) {
955 <        } catch (Exception ex) {
956 <            this.unexpectedException();
957 <        }
877 >        } catch (NullPointerException success) {}
878      }
879  
880 <    /*
881 <     * transfer attempts to insert into the queue then wait until that
882 <     * object is removed via take or poll.
880 >    /**
881 >     * transfer waits until a poll occurs. The transfered element
882 >     * is returned by this associated poll.
883       */
884 <    public void testTransfer2() {
885 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
886 <        new Thread(new Runnable() {
884 >    public void testTransfer2() throws InterruptedException {
885 >        final LinkedTransferQueue<Integer> q
886 >            = new LinkedTransferQueue<Integer>();
887  
888 <            public void run() {
889 <                try {
890 <                    q.transfer(new Integer(SIZE));
891 <                    threadAssertTrue(q.isEmpty());
892 <                } catch (Exception ex) {
973 <                    threadUnexpectedException();
974 <                }
975 <            }
976 <        }).start();
888 >        Thread t = newStartedThread(new CheckedRunnable() {
889 >            void realRun() throws InterruptedException {
890 >                q.transfer(SIZE);
891 >                threadAssertTrue(q.isEmpty());
892 >            }});
893  
894 <        try {
895 <            Thread.sleep(SHORT_DELAY_MS);
896 <            assertEquals(1, q.size());
897 <            q.poll();
898 <            assertTrue(q.isEmpty());
899 <        } catch (Exception ex) {
900 <            this.unexpectedException();
901 <        }
902 <    }
903 <    /*
904 <     * transfer will attempt to transfer in fifo order and continue waiting if
905 <     * the element being transfered is not polled or taken
906 <     */
907 <
908 <    public void testTransfer3() {
909 <        final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
910 <        new Thread(new Runnable() {
911 <                public void run() {
912 <                    try {
913 <                        Integer i;
914 <                        q.transfer((i = new Integer(SIZE + 1)));
915 <                        threadAssertTrue(!q.contains(i));
916 <                        threadAssertEquals(1, q.size());
917 <                    } catch (Exception ex) {
918 <                        threadUnexpectedException();
919 <                    }
920 <                }
921 <            }).start();
922 <        Thread interruptedThread =
923 <            new Thread(new Runnable() {
924 <                    public void run() {
925 <                        try {
926 <                            q.transfer(new Integer(SIZE));
927 <                            threadShouldThrow();
928 <                        } catch (InterruptedException ex) {
929 <                        }
930 <                    }
931 <                });
932 <        interruptedThread.start();
933 <        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 <        }
894 >        Thread.sleep(SHORT_DELAY_MS);
895 >        assertEquals(1, q.size());
896 >        assertEquals(SIZE, (int) q.poll());
897 >        assertTrue(q.isEmpty());
898 >        t.join();
899 >    }
900 >
901 >    /**
902 >     * transfer waits until a poll occurs, and then transfers in fifo order
903 >     */
904 >    public void testTransfer3() throws InterruptedException {
905 >        final LinkedTransferQueue<Integer> q
906 >            = new LinkedTransferQueue<Integer>();
907 >
908 >        Thread first = newStartedThread(new CheckedRunnable() {
909 >            void realRun() throws InterruptedException {
910 >                Integer i = SIZE + 1;
911 >                q.transfer(i);
912 >                threadAssertTrue(!q.contains(i));
913 >                threadAssertEquals(1, q.size());
914 >            }});
915 >
916 >        Thread interruptedThread = newStartedThread(
917 >            new CheckedInterruptedRunnable() {
918 >                void realRun() throws InterruptedException {
919 >                    while (q.size() == 0)
920 >                        Thread.yield();
921 >                    q.transfer(SIZE);
922 >                }});
923 >
924 >        while (q.size() < 2)
925 >            Thread.yield();
926 >        assertEquals(2, q.size());
927 >        assertEquals(SIZE + 1, (int) q.poll());
928 >        first.join();
929 >        assertEquals(1, q.size());
930 >        interruptedThread.interrupt();
931 >        interruptedThread.join();
932 >        assertEquals(0, q.size());
933 >        assertTrue(q.isEmpty());
934      }
935  
936      /**
937 <     * transfer will wait as long as a poll or take occurs if one does occur
938 <     * the waiting is finished and the thread that tries to poll/take
1032 <     * wins in retrieving the element
937 >     * transfer waits until a poll occurs, at which point the polling
938 >     * thread returns the element
939       */
940 <    public void testTransfer4() {
940 >    public void testTransfer4() throws InterruptedException {
941          final LinkedTransferQueue q = new LinkedTransferQueue();
1036        new Thread(new Runnable() {
942  
943 <            public void run() {
944 <                try {
945 <                    q.transfer(new Integer(four));
946 <                    threadAssertFalse(q.contains(new Integer(four)));
947 <                    threadAssertEquals(new Integer(three), q.poll());
948 <                } catch (Exception ex) {
949 <                    threadUnexpectedException();
950 <                }
951 <            }
952 <        }).start();
953 <        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 <        }
943 >        Thread t = newStartedThread(new CheckedRunnable() {
944 >            void realRun() throws InterruptedException {
945 >                q.transfer(four);
946 >                threadAssertFalse(q.contains(four));
947 >                threadAssertEquals(three, q.poll());
948 >            }});
949 >
950 >        Thread.sleep(SHORT_DELAY_MS);
951 >        assertTrue(q.offer(three));
952 >        assertEquals(four, q.poll());
953 >        t.join();
954      }
955 <    /*
956 <     * Insert null into trTransfer throws NPE
955 >
956 >    /**
957 >     * transfer waits until a take occurs. The transfered element
958 >     * is returned by this associated take.
959       */
960 +    public void testTransfer5() throws InterruptedException {
961 +        final LinkedTransferQueue<Integer> q
962 +            = new LinkedTransferQueue<Integer>();
963  
964 +        Thread t = newStartedThread(new CheckedRunnable() {
965 +            void realRun() throws InterruptedException {
966 +                q.transfer(SIZE);
967 +                checkEmpty(q);
968 +            }});
969 +
970 +        Thread.sleep(SHORT_DELAY_MS);
971 +        assertEquals(SIZE, (int) q.take());
972 +        checkEmpty(q);
973 +        t.join();
974 +    }
975 +
976 +    /**
977 +     * tryTransfer(null) throws NullPointerException
978 +     */
979      public void testTryTransfer1() {
980          try {
981              final LinkedTransferQueue q = new LinkedTransferQueue();
982              q.tryTransfer(null);
983 <            this.shouldThrow();
984 <        } catch (NullPointerException ex) {
1066 <        } catch (Exception ex) {
1067 <            this.unexpectedException();
1068 <        }
983 >            shouldThrow();
984 >        } catch (NullPointerException success) {}
985      }
1070    /*
1071     * tryTransfer returns false and does not enqueue if there are no consumers
1072     * waiting to poll or take.
1073     */
986  
987 <    public void testTryTransfer2() {
988 <        try {
989 <            final LinkedTransferQueue q = new LinkedTransferQueue();
990 <            assertFalse(q.tryTransfer(new Object()));
991 <            assertEquals(0, q.size());
992 <        } catch (Exception ex) {
993 <            this.unexpectedException();
994 <        }
987 >    /**
988 >     * tryTransfer returns false and does not enqueue if there are no
989 >     * consumers waiting to poll or take.
990 >     */
991 >    public void testTryTransfer2() throws InterruptedException {
992 >        final LinkedTransferQueue q = new LinkedTransferQueue();
993 >        assertFalse(q.tryTransfer(new Object()));
994 >        assertFalse(q.hasWaitingConsumer());
995 >        checkEmpty(q);
996      }
997 <    /*
998 <     * if there is a consumer waiting poll or take tryTransfer returns
999 <     * true while enqueueing object
997 >
998 >    /**
999 >     * If there is a consumer waiting in timed poll, tryTransfer
1000 >     * returns true while successfully transfering object.
1001       */
1002 +    public void testTryTransfer3() throws InterruptedException {
1003 +        final Object hotPotato = new Object();
1004 +        final LinkedTransferQueue q = new LinkedTransferQueue();
1005  
1006 <    public void testTryTransfer3() {
1007 <        try {
1008 <            final LinkedTransferQueue q = new LinkedTransferQueue();
1009 <            new Thread(new Runnable() {
1006 >        Thread t = newStartedThread(new CheckedRunnable() {
1007 >            void realRun() {
1008 >                while (! q.hasWaitingConsumer())
1009 >                    Thread.yield();
1010 >                threadAssertTrue(q.hasWaitingConsumer());
1011 >                threadAssertTrue(q.isEmpty());
1012 >                threadAssertTrue(q.size() == 0);
1013 >                threadAssertTrue(q.tryTransfer(hotPotato));
1014 >            }});
1015 >
1016 >        assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato);
1017 >        checkEmpty(q);
1018 >        t.join();
1019 >    }
1020  
1021 <                public void run() {
1022 <                    try {
1023 <                        threadAssertTrue(q.hasWaitingConsumer());
1024 <                        threadAssertTrue(q.tryTransfer(new Object()));
1025 <                    } catch (Exception ex) {
1026 <                        threadUnexpectedException();
1027 <                    }
1021 >    /**
1022 >     * If there is a consumer waiting in take, tryTransfer returns
1023 >     * true while successfully transfering object.
1024 >     */
1025 >    public void testTryTransfer4() throws InterruptedException {
1026 >        final Object hotPotato = new Object();
1027 >        final LinkedTransferQueue q = new LinkedTransferQueue();
1028  
1029 <                }
1030 <            }).start();
1031 <            assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null);
1032 <            assertTrue(q.isEmpty());
1033 <        } catch (Exception ex) {
1034 <            this.unexpectedException();
1035 <        }
1029 >        Thread t = newStartedThread(new CheckedRunnable() {
1030 >            void realRun() {
1031 >                while (! q.hasWaitingConsumer())
1032 >                    Thread.yield();
1033 >                threadAssertTrue(q.hasWaitingConsumer());
1034 >                threadAssertTrue(q.isEmpty());
1035 >                threadAssertTrue(q.size() == 0);
1036 >                threadAssertTrue(q.tryTransfer(hotPotato));
1037 >            }});
1038 >
1039 >        assertTrue(q.take() == hotPotato);
1040 >        checkEmpty(q);
1041 >        t.join();
1042      }
1043  
1044 <    /*
1045 <     * tryTransfer waits the amount given if interrupted, show an
1046 <     * interrupted exception
1044 >    /**
1045 >     * tryTransfer waits the amount given if interrupted, and
1046 >     * throws interrupted exception
1047       */
1048 <    public void testTryTransfer4() {
1048 >    public void testTryTransfer5() throws InterruptedException {
1049          final LinkedTransferQueue q = new LinkedTransferQueue();
1117        Thread toInterrupt = new Thread(new Runnable() {
1050  
1051 <            public void run() {
1052 <                try {
1053 <                    q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1054 <                    threadShouldThrow();
1055 <                } catch (InterruptedException ex) {
1056 <                }
1057 <            }
1058 <        });
1127 <        try {
1128 <            toInterrupt.start();
1129 <            Thread.sleep(SMALL_DELAY_MS);
1130 <            toInterrupt.interrupt();
1131 <        } catch (Exception ex) {
1132 <            this.unexpectedException();
1133 <        }
1051 >        Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() {
1052 >            void realRun() throws InterruptedException {
1053 >                q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS);
1054 >            }});
1055 >
1056 >        Thread.sleep(SMALL_DELAY_MS);
1057 >        toInterrupt.interrupt();
1058 >        toInterrupt.join();
1059      }
1060  
1061 <    /*
1061 >    /**
1062       * tryTransfer gives up after the timeout and return false
1063       */
1064 <    public void testTryTransfer5() {
1064 >    public void testTryTransfer6() throws InterruptedException {
1065          final LinkedTransferQueue q = new LinkedTransferQueue();
1141        try {
1142            new Thread(new Runnable() {
1066  
1067 <                public void run() {
1068 <                    try {
1069 <                        threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1070 <                    } catch (InterruptedException ex) {
1071 <                        threadUnexpectedException();
1072 <                    }
1073 <                }
1074 <            }).start();
1075 <            Thread.sleep(LONG_DELAY_MS);
1076 <            assertTrue(q.isEmpty());
1154 <        } catch (Exception ex) {
1155 <            this.unexpectedException();
1156 <        }
1067 >        Thread t = newStartedThread(new CheckedRunnable() {
1068 >            void realRun() throws InterruptedException {
1069 >                threadAssertFalse
1070 >                    (q.tryTransfer(new Object(),
1071 >                                   SHORT_DELAY_MS, MILLISECONDS));
1072 >            }});
1073 >
1074 >        Thread.sleep(SMALL_DELAY_MS);
1075 >        checkEmpty(q);
1076 >        t.join();
1077      }
1078  
1079 <    /*
1079 >    /**
1080       * tryTransfer waits for any elements previously in to be removed
1081       * before transfering to a poll or take
1082       */
1083 <    public void testTryTransfer6() {
1083 >    public void testTryTransfer7() throws InterruptedException {
1084          final LinkedTransferQueue q = new LinkedTransferQueue();
1085 <        q.offer(new Integer(four));
1166 <        new Thread(new Runnable() {
1085 >        assertTrue(q.offer(four));
1086  
1087 <            public void run() {
1088 <                try {
1089 <                    threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
1090 <                    threadAssertTrue(q.isEmpty());
1091 <                } catch (InterruptedException ex) {
1092 <                    threadUnexpectedException();
1093 <                }
1094 <            }
1095 <        }).start();
1096 <        try {
1097 <            Thread.sleep(SHORT_DELAY_MS);
1098 <            assertEquals(2, q.size());
1099 <            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 <        }
1087 >        Thread t = newStartedThread(new CheckedRunnable() {
1088 >            void realRun() throws InterruptedException {
1089 >                threadAssertTrue(q.tryTransfer(five,
1090 >                                               MEDIUM_DELAY_MS, MILLISECONDS));
1091 >                threadAssertTrue(q.isEmpty());
1092 >            }});
1093 >
1094 >        Thread.sleep(SHORT_DELAY_MS);
1095 >        assertEquals(2, q.size());
1096 >        assertEquals(four, q.poll());
1097 >        assertEquals(five, q.poll());
1098 >        checkEmpty(q);
1099 >        t.join();
1100      }
1101  
1102 <    /*
1103 <     * tryTransfer attempts to enqueue into the q and fails returning false not
1104 <     * enqueueing and the successing poll is null
1102 >    /**
1103 >     * tryTransfer attempts to enqueue into the q and fails returning
1104 >     * false not enqueueing and the successive poll is null
1105       */
1106 <    public void testTryTransfer7() {
1106 >    public void testTryTransfer8() throws InterruptedException {
1107          final LinkedTransferQueue q = new LinkedTransferQueue();
1108 <        q.offer(new Integer(four));
1109 <        new Thread(new Runnable() {
1110 <
1111 <            public void run() {
1112 <                try {
1113 <                    threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1114 <                    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 <        }
1108 >        assertTrue(q.offer(four));
1109 >        assertEquals(1, q.size());
1110 >        assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS));
1111 >        assertEquals(1, q.size());
1112 >        assertEquals(four, q.poll());
1113 >        assertNull(q.poll());
1114 >        checkEmpty(q);
1115      }
1116  
1117 <    private LinkedTransferQueue populatedQueue(
1118 <            int n) {
1218 <        LinkedTransferQueue q = new LinkedTransferQueue();
1117 >    private LinkedTransferQueue<Integer> populatedQueue(int n) {
1118 >        LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>();
1119          assertTrue(q.isEmpty());
1120 <        int remainingCapacity = q.remainingCapacity();
1121 <        for (int i = 0; i <
1222 <                n; i++) {
1120 >        for (int i = 0; i < n; i++) {
1121 >            assertEquals(i, q.size());
1122              assertTrue(q.offer(i));
1123 +            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
1124          }
1225
1125          assertFalse(q.isEmpty());
1227        assertEquals(remainingCapacity, q.remainingCapacity());
1228        assertEquals(n, q.size());
1126          return q;
1127      }
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    }
1128   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines