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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines