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.31 by jsr166, Thu Oct 28 22:42:05 2010 UTC vs.
Revision 1.86 by dl, Tue Jan 26 13:33:06 2021 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines