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

Comparing jsr166/src/test/tck/LinkedTransferQueueTest.java (file contents):
Revision 1.1 by dl, Fri Jul 31 23:02:49 2009 UTC vs.
Revision 1.54 by jsr166, Wed Dec 31 16:44:02 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines