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.3 by jsr166, Sat Aug 1 21:46:28 2009 UTC vs.
Revision 1.46 by jsr166, Fri May 27 20:07:24 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines