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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines