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

Comparing jsr166/src/test/tck/BlockingQueueTest.java (file contents):
Revision 1.10 by jsr166, Tue Jun 14 03:22:38 2011 UTC vs.
Revision 1.20 by jsr166, Sun May 14 14:52:50 2017 UTC

# Line 7 | Line 7
7   * Pat Fisher, Mike Judd.
8   */
9  
10 < import junit.framework.*;
10 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 >
12   import java.util.ArrayList;
13   import java.util.Arrays;
14   import java.util.Collection;
15   import java.util.Queue;
16   import java.util.concurrent.BlockingQueue;
17   import java.util.concurrent.CountDownLatch;
18 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   /**
23   * Contains "contract" tests applicable to all BlockingQueue implementations.
# Line 103 | Line 106 | public abstract class BlockingQueueTest
106      }
107  
108      /**
109 <     * put(null) throws NullPointerException
109 >     * addAll(null) throws NullPointerException
110       */
111      public void testAddAllNull() throws InterruptedException {
112          final Collection q = emptyCollection();
# Line 181 | Line 184 | public abstract class BlockingQueueTest
184      }
185  
186      /**
187 <     * drainTo(c, -n) returns 0
185 <     */
186 <    public void testDrainToNegativeMaxElements() {
187 <        final BlockingQueue q = emptyCollection();
188 <        assertEquals(0, q.drainTo(new ArrayList(), -42));
189 <    }
190 <
191 <    /**
192 <     * drainTo(c, 0) returns 0 and does nothing
187 >     * drainTo(c, n) returns 0 and does nothing when n <= 0
188       */
189 <    public void testDrainToZeroMaxElements() {
189 >    public void testDrainToNonPositiveMaxElements() {
190          final BlockingQueue q = emptyCollection();
191 <        if (q.remainingCapacity() == 0) {
192 <            // SynchronousQueue, for example
193 <            assertEquals(0, q.drainTo(new ArrayList(), 0));
194 <        } else {
191 >        final int[] ns = { 0, -1, -42, Integer.MIN_VALUE };
192 >        for (int n : ns)
193 >            assertEquals(0, q.drainTo(new ArrayList(), n));
194 >        if (q.remainingCapacity() > 0) {
195 >            // Not SynchronousQueue, that is
196              Object one = makeElement(1);
197              q.add(one);
198              ArrayList c = new ArrayList();
199 <            assertEquals(0, q.drainTo(c, 0));
199 >            for (int n : ns)
200 >                assertEquals(0, q.drainTo(new ArrayList(), n));
201              assertEquals(1, q.size());
202              assertSame(one, q.poll());
203              assertTrue(c.isEmpty());
# Line 238 | Line 235 | public abstract class BlockingQueueTest
235                      shouldThrow();
236                  } catch (InterruptedException success) {}
237                  assertFalse(Thread.interrupted());
238 +
239 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
240              }});
241  
242          barrier.await();
# Line 246 | Line 245 | public abstract class BlockingQueueTest
245          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
246  
247          barrier.await();
248 <        assertThreadStaysAlive(t);
248 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
249          t.interrupt();
250          awaitTermination(t);
251      }
# Line 268 | Line 267 | public abstract class BlockingQueueTest
267              }});
268  
269          await(threadStarted);
270 <        assertThreadStaysAlive(t);
270 >        assertThreadBlocks(t, Thread.State.WAITING);
271          t.interrupt();
272          awaitTermination(t);
273      }
# Line 309 | Line 308 | public abstract class BlockingQueueTest
308              }});
309  
310          await(threadStarted);
311 <        assertThreadStaysAlive(t);
311 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
312          t.interrupt();
313          awaitTermination(t);
314      }
# Line 333 | Line 332 | public abstract class BlockingQueueTest
332          awaitTermination(t);
333      }
334  
335 +    /**
336 +     * remove(x) removes x and returns true if present
337 +     * TODO: move to superclass CollectionTest.java
338 +     */
339 +    public void testRemoveElement() {
340 +        final BlockingQueue q = emptyCollection();
341 +        final int size = Math.min(q.remainingCapacity(), SIZE);
342 +        final Object[] elts = new Object[size];
343 +        assertFalse(q.contains(makeElement(99)));
344 +        assertFalse(q.remove(makeElement(99)));
345 +        checkEmpty(q);
346 +        for (int i = 0; i < size; i++)
347 +            q.add(elts[i] = makeElement(i));
348 +        for (int i = 1; i < size; i += 2) {
349 +            for (int pass = 0; pass < 2; pass++) {
350 +                assertEquals((pass == 0), q.contains(elts[i]));
351 +                assertEquals((pass == 0), q.remove(elts[i]));
352 +                assertFalse(q.contains(elts[i]));
353 +                assertTrue(q.contains(elts[i - 1]));
354 +                if (i < size - 1)
355 +                    assertTrue(q.contains(elts[i + 1]));
356 +            }
357 +        }
358 +        if (size > 0)
359 +            assertTrue(q.contains(elts[0]));
360 +        for (int i = size - 2; i >= 0; i -= 2) {
361 +            assertTrue(q.contains(elts[i]));
362 +            assertFalse(q.contains(elts[i + 1]));
363 +            assertTrue(q.remove(elts[i]));
364 +            assertFalse(q.contains(elts[i]));
365 +            assertFalse(q.remove(elts[i + 1]));
366 +            assertFalse(q.contains(elts[i + 1]));
367 +        }
368 +        checkEmpty(q);
369 +    }
370 +
371      /** For debugging. */
372      public void XXXXtestFails() {
373          fail(emptyCollection().getClass().toString());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines