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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines