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.39 by jsr166, Fri Nov 5 00:17:22 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines