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.23 by jsr166, Sun Aug 11 22:29:26 2019 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 131 | Line 134 | public abstract class BlockingQueueTest
134      public void testToArray_NullArray() {
135          final Collection q = emptyCollection();
136          try {
137 <            q.toArray(null);
137 >            q.toArray((Object[])null);
138              shouldThrow();
139          } catch (NullPointerException success) {}
140      }
# 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 >        final ArrayList sink = new ArrayList();
193 >        for (int n : ns) {
194 >            assertEquals(0, q.drainTo(sink, n));
195 >            assertTrue(sink.isEmpty());
196 >        }
197 >        if (q.remainingCapacity() > 0) {
198 >            // Not SynchronousQueue, that is
199              Object one = makeElement(1);
200              q.add(one);
201 <            ArrayList c = new ArrayList();
202 <            assertEquals(0, q.drainTo(c, 0));
201 >            for (int n : ns)
202 >                assertEquals(0, q.drainTo(sink, n));
203              assertEquals(1, q.size());
204              assertSame(one, q.poll());
205 <            assertTrue(c.isEmpty());
205 >            assertTrue(sink.isEmpty());
206          }
207      }
208  
# Line 227 | Line 226 | public abstract class BlockingQueueTest
226  
227                  Thread.currentThread().interrupt();
228                  try {
229 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
229 >                    q.poll(randomTimeout(), randomTimeUnit());
230                      shouldThrow();
231                  } catch (InterruptedException success) {}
232                  assertFalse(Thread.interrupted());
# Line 238 | Line 237 | public abstract class BlockingQueueTest
237                      shouldThrow();
238                  } catch (InterruptedException success) {}
239                  assertFalse(Thread.interrupted());
240 +
241 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
242              }});
243  
244          barrier.await();
# Line 246 | Line 247 | public abstract class BlockingQueueTest
247          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
248  
249          barrier.await();
250 <        assertThreadStaysAlive(t);
250 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
251          t.interrupt();
252          awaitTermination(t);
253      }
# Line 268 | Line 269 | public abstract class BlockingQueueTest
269              }});
270  
271          await(threadStarted);
272 <        assertThreadStaysAlive(t);
272 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
273          t.interrupt();
274          awaitTermination(t);
275      }
# Line 301 | Line 302 | public abstract class BlockingQueueTest
302          Thread t = newStartedThread(new CheckedRunnable() {
303              public void realRun() {
304                  threadStarted.countDown();
305 +                long startTime = System.nanoTime();
306 +
307                  try {
308 <                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
308 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
309                      shouldThrow();
310                  } catch (InterruptedException success) {}
311                  assertFalse(Thread.interrupted());
312 +
313 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
314              }});
315  
316          await(threadStarted);
317 <        assertThreadStaysAlive(t);
317 >        if (randomBoolean())  assertThreadBlocks(t, Thread.State.TIMED_WAITING);
318          t.interrupt();
319          awaitTermination(t);
320      }
# Line 322 | Line 327 | public abstract class BlockingQueueTest
327          final BlockingQueue q = emptyCollection();
328          Thread t = newStartedThread(new CheckedRunnable() {
329              public void realRun() {
330 +                long startTime = System.nanoTime();
331 +
332                  Thread.currentThread().interrupt();
333                  try {
334 <                    q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
334 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
335                      shouldThrow();
336                  } catch (InterruptedException success) {}
337                  assertFalse(Thread.interrupted());
338 +
339 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
340              }});
341  
342          awaitTermination(t);
343      }
344  
345 +    /**
346 +     * remove(x) removes x and returns true if present
347 +     * TODO: move to superclass CollectionTest.java
348 +     */
349 +    public void testRemoveElement() {
350 +        final BlockingQueue q = emptyCollection();
351 +        final int size = Math.min(q.remainingCapacity(), SIZE);
352 +        final Object[] elts = new Object[size];
353 +        assertFalse(q.contains(makeElement(99)));
354 +        assertFalse(q.remove(makeElement(99)));
355 +        checkEmpty(q);
356 +        for (int i = 0; i < size; i++)
357 +            q.add(elts[i] = makeElement(i));
358 +        for (int i = 1; i < size; i += 2) {
359 +            for (int pass = 0; pass < 2; pass++) {
360 +                assertEquals((pass == 0), q.contains(elts[i]));
361 +                assertEquals((pass == 0), q.remove(elts[i]));
362 +                assertFalse(q.contains(elts[i]));
363 +                assertTrue(q.contains(elts[i - 1]));
364 +                if (i < size - 1)
365 +                    assertTrue(q.contains(elts[i + 1]));
366 +            }
367 +        }
368 +        if (size > 0)
369 +            assertTrue(q.contains(elts[0]));
370 +        for (int i = size - 2; i >= 0; i -= 2) {
371 +            assertTrue(q.contains(elts[i]));
372 +            assertFalse(q.contains(elts[i + 1]));
373 +            assertTrue(q.remove(elts[i]));
374 +            assertFalse(q.contains(elts[i]));
375 +            assertFalse(q.remove(elts[i + 1]));
376 +            assertFalse(q.contains(elts[i + 1]));
377 +        }
378 +        checkEmpty(q);
379 +    }
380 +
381      /** For debugging. */
382      public void XXXXtestFails() {
383          fail(emptyCollection().getClass().toString());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines