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

Comparing jsr166/src/test/tck/SynchronousQueueTest.java (file contents):
Revision 1.38 by jsr166, Tue May 31 16:16:24 2011 UTC vs.
Revision 1.58 by jsr166, Sun May 14 04:02:06 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.Arrays;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Iterator;
15   import java.util.NoSuchElementException;
15 import java.util.Queue;
16   import java.util.concurrent.BlockingQueue;
17   import java.util.concurrent.CountDownLatch;
18   import java.util.concurrent.Executors;
19   import java.util.concurrent.ExecutorService;
20   import java.util.concurrent.SynchronousQueue;
21 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 > import java.util.concurrent.ThreadLocalRandom;
22 >
23 > import junit.framework.Test;
24  
25   public class SynchronousQueueTest extends JSR166TestCase {
26  
# Line 35 | Line 37 | public class SynchronousQueueTest extend
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 137 | Line 139 | public class SynchronousQueueTest extend
139              }});
140  
141          await(pleaseInterrupt);
142 <        assertThreadStaysAlive(t);
142 >        assertThreadBlocks(t, Thread.State.WAITING);
143          t.interrupt();
144          awaitTermination(t);
145          assertEquals(0, q.remainingCapacity());
# Line 157 | Line 159 | public class SynchronousQueueTest extend
159                  pleaseTake.countDown();
160                  q.put(one);
161  
162 +                Thread.currentThread().interrupt();
163 +                try {
164 +                    q.put(99);
165 +                    shouldThrow();
166 +                } catch (InterruptedException success) {}
167 +                assertFalse(Thread.interrupted());
168 +
169                  pleaseInterrupt.countDown();
170                  try {
171                      q.put(99);
# Line 166 | Line 175 | public class SynchronousQueueTest extend
175              }});
176  
177          await(pleaseTake);
178 <        assertEquals(q.remainingCapacity(), 0);
178 >        assertEquals(0, q.remainingCapacity());
179          try { assertSame(one, q.take()); }
180          catch (InterruptedException e) { threadUnexpectedException(e); }
181  
182          await(pleaseInterrupt);
183 <        assertThreadStaysAlive(t);
183 >        assertThreadBlocks(t, Thread.State.WAITING);
184          t.interrupt();
185          awaitTermination(t);
186 <        assertEquals(q.remainingCapacity(), 0);
186 >        assertEquals(0, q.remainingCapacity());
187      }
188  
189      /**
190       * timed offer times out if elements not taken
191       */
192 <    public void testTimedOffer()      { testTimedOffer(false); }
193 <    public void testTimedOffer_fair() { testTimedOffer(true); }
185 <    public void testTimedOffer(boolean fair) {
192 >    public void testTimedOffer() {
193 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
194          final SynchronousQueue q = new SynchronousQueue(fair);
195          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
196          Thread t = newStartedThread(new CheckedRunnable() {
# Line 195 | Line 203 | public class SynchronousQueueTest extend
203                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
204                      shouldThrow();
205                  } catch (InterruptedException success) {}
206 +                assertFalse(Thread.interrupted());
207              }});
208  
209          await(pleaseInterrupt);
210 <        assertThreadStaysAlive(t);
210 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
211          t.interrupt();
212          awaitTermination(t);
213      }
# Line 227 | Line 236 | public class SynchronousQueueTest extend
236      /**
237       * timed poll with nonzero timeout times out if no active putter
238       */
239 <    public void testTimedPoll()      { testTimedPoll(false); }
240 <    public void testTimedPoll_fair() { testTimedPoll(true); }
232 <    public void testTimedPoll(boolean fair) {
239 >    public void testTimedPoll() {
240 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
241          final SynchronousQueue q = new SynchronousQueue(fair);
242 <        long startTime = System.nanoTime();
242 >        final long startTime = System.nanoTime();
243          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
244          catch (InterruptedException e) { threadUnexpectedException(e); }
245          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 241 | Line 249 | public class SynchronousQueueTest extend
249       * timed poll before a delayed offer times out, returning null;
250       * after offer succeeds; on interruption throws
251       */
252 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
253 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
246 <    public void testTimedPollWithOffer(boolean fair) {
252 >    public void testTimedPollWithOffer() {
253 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
254          final SynchronousQueue q = new SynchronousQueue(fair);
255          final CountDownLatch pleaseOffer = new CountDownLatch(1);
256          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 256 | Line 263 | public class SynchronousQueueTest extend
263                  pleaseOffer.countDown();
264                  startTime = System.nanoTime();
265                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
259                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
266  
267                  Thread.currentThread().interrupt();
268                  try {
# Line 271 | Line 277 | public class SynchronousQueueTest extend
277                      shouldThrow();
278                  } catch (InterruptedException success) {}
279                  assertFalse(Thread.interrupted());
280 +
281 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282              }});
283  
284          await(pleaseOffer);
285          long startTime = System.nanoTime();
286          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
287          catch (InterruptedException e) { threadUnexpectedException(e); }
288 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
288 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
289  
290          await(pleaseInterrupt);
291 <        assertThreadStaysAlive(t);
291 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
292          t.interrupt();
293          awaitTermination(t);
294      }
# Line 322 | Line 330 | public class SynchronousQueueTest extend
330      }
331  
332      /**
325     * remove(x) returns false
326     */
327    public void testRemoveElement()      { testRemoveElement(false); }
328    public void testRemoveElement_fair() { testRemoveElement(true); }
329    public void testRemoveElement(boolean fair) {
330        final SynchronousQueue q = new SynchronousQueue(fair);
331        assertFalse(q.remove(zero));
332        assertTrue(q.isEmpty());
333    }
334
335    /**
333       * contains returns false
334       */
335      public void testContains()      { testContains(false); }
# Line 400 | Line 397 | public class SynchronousQueueTest extend
397      public void testToArray(boolean fair) {
398          final SynchronousQueue q = new SynchronousQueue(fair);
399          Object[] o = q.toArray();
400 <        assertEquals(o.length, 0);
400 >        assertEquals(0, o.length);
401      }
402  
403      /**
404 <     * toArray(a) is nulled at position 0
404 >     * toArray(Integer array) returns its argument with the first
405 >     * element (if present) nulled out
406       */
407      public void testToArray2()      { testToArray2(false); }
408      public void testToArray2_fair() { testToArray2(true); }
409      public void testToArray2(boolean fair) {
410 <        final SynchronousQueue q = new SynchronousQueue(fair);
411 <        Integer[] ints = new Integer[1];
412 <        assertNull(ints[0]);
410 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
411 >        Integer[] a;
412 >
413 >        a = new Integer[0];
414 >        assertSame(a, q.toArray(a));
415 >
416 >        a = new Integer[3];
417 >        Arrays.fill(a, 42);
418 >        assertSame(a, q.toArray(a));
419 >        assertNull(a[0]);
420 >        for (int i = 1; i < a.length; i++)
421 >            assertEquals(42, (int) a[i]);
422      }
423  
424      /**
# Line 422 | Line 429 | public class SynchronousQueueTest extend
429      public void testToArray_null(boolean fair) {
430          final SynchronousQueue q = new SynchronousQueue(fair);
431          try {
432 <            Object o[] = q.toArray(null);
432 >            Object[] o = q.toArray(null);
433              shouldThrow();
434          } catch (NullPointerException success) {}
435      }
# Line 433 | Line 440 | public class SynchronousQueueTest extend
440      public void testIterator()      { testIterator(false); }
441      public void testIterator_fair() { testIterator(true); }
442      public void testIterator(boolean fair) {
443 <        final SynchronousQueue q = new SynchronousQueue(fair);
437 <        Iterator it = q.iterator();
438 <        assertFalse(it.hasNext());
439 <        try {
440 <            Object x = it.next();
441 <            shouldThrow();
442 <        } catch (NoSuchElementException success) {}
443 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
444      }
445  
446      /**
# Line 474 | Line 475 | public class SynchronousQueueTest extend
475      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
476      public void testOfferInExecutor(boolean fair) {
477          final SynchronousQueue q = new SynchronousQueue(fair);
477        ExecutorService executor = Executors.newFixedThreadPool(2);
478          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
479 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
480 +        try (PoolCleaner cleaner = cleaner(executor)) {
481  
482 <        executor.execute(new CheckedRunnable() {
483 <            public void realRun() throws InterruptedException {
484 <                assertFalse(q.offer(one));
485 <                threadsStarted.await();
486 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
487 <                assertEquals(0, q.remainingCapacity());
488 <            }});
489 <
490 <        executor.execute(new CheckedRunnable() {
491 <            public void realRun() throws InterruptedException {
492 <                threadsStarted.await();
493 <                assertSame(one, q.take());
494 <            }});
495 <
494 <        joinPool(executor);
482 >            executor.execute(new CheckedRunnable() {
483 >                public void realRun() throws InterruptedException {
484 >                    assertFalse(q.offer(one));
485 >                    threadsStarted.await();
486 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
487 >                    assertEquals(0, q.remainingCapacity());
488 >                }});
489 >
490 >            executor.execute(new CheckedRunnable() {
491 >                public void realRun() throws InterruptedException {
492 >                    threadsStarted.await();
493 >                    assertSame(one, q.take());
494 >                }});
495 >        }
496      }
497  
498      /**
# Line 502 | Line 503 | public class SynchronousQueueTest extend
503      public void testPollInExecutor(boolean fair) {
504          final SynchronousQueue q = new SynchronousQueue(fair);
505          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
506 <        ExecutorService executor = Executors.newFixedThreadPool(2);
507 <        executor.execute(new CheckedRunnable() {
508 <            public void realRun() throws InterruptedException {
509 <                assertNull(q.poll());
510 <                threadsStarted.await();
511 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
512 <                assertTrue(q.isEmpty());
513 <            }});
514 <
515 <        executor.execute(new CheckedRunnable() {
516 <            public void realRun() throws InterruptedException {
517 <                threadsStarted.await();
518 <                q.put(one);
519 <            }});
520 <
521 <        joinPool(executor);
506 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
507 >        try (PoolCleaner cleaner = cleaner(executor)) {
508 >            executor.execute(new CheckedRunnable() {
509 >                public void realRun() throws InterruptedException {
510 >                    assertNull(q.poll());
511 >                    threadsStarted.await();
512 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
513 >                    assertTrue(q.isEmpty());
514 >                }});
515 >
516 >            executor.execute(new CheckedRunnable() {
517 >                public void realRun() throws InterruptedException {
518 >                    threadsStarted.await();
519 >                    q.put(one);
520 >                }});
521 >        }
522      }
523  
524      /**
525       * a deserialized serialized queue is usable
526       */
527 <    public void testSerialization()      { testSerialization(false); }
528 <    public void testSerialization_fair() { testSerialization(true); }
529 <    public void testSerialization(boolean fair) {
530 <        final SynchronousQueue x = new SynchronousQueue(fair);
531 <        final SynchronousQueue y = serialClone(x);
532 <        assertTrue(x != y);
533 <        assertTrue(x.isEmpty());
534 <        assertTrue(y.isEmpty());
527 >    public void testSerialization() {
528 >        final SynchronousQueue x = new SynchronousQueue();
529 >        final SynchronousQueue y = new SynchronousQueue(false);
530 >        final SynchronousQueue z = new SynchronousQueue(true);
531 >        assertSerialEquals(x, y);
532 >        assertNotSerialEquals(x, z);
533 >        SynchronousQueue[] qs = { x, y, z };
534 >        for (SynchronousQueue q : qs) {
535 >            SynchronousQueue clone = serialClone(q);
536 >            assertNotSame(q, clone);
537 >            assertSerialEquals(q, clone);
538 >            assertTrue(clone.isEmpty());
539 >            assertEquals(0, clone.size());
540 >            assertEquals(0, clone.remainingCapacity());
541 >            assertFalse(clone.offer(zero));
542 >        }
543      }
544  
545      /**
# Line 542 | Line 551 | public class SynchronousQueueTest extend
551          final SynchronousQueue q = new SynchronousQueue(fair);
552          ArrayList l = new ArrayList();
553          q.drainTo(l);
554 <        assertEquals(q.size(), 0);
555 <        assertEquals(l.size(), 0);
554 >        assertEquals(0, q.size());
555 >        assertEquals(0, l.size());
556      }
557  
558      /**
# Line 566 | Line 575 | public class SynchronousQueueTest extend
575                  fail("timed out");
576              Thread.yield();
577          }
578 <        assertTrue(l.size() == 1);
578 >        assertEquals(1, l.size());
579          assertSame(one, l.get(0));
580          awaitTermination(t);
581      }
# Line 587 | Line 596 | public class SynchronousQueueTest extend
596              }});
597  
598          ArrayList l = new ArrayList();
599 <        delay(SHORT_DELAY_MS);
600 <        q.drainTo(l, 1);
599 >        int drained;
600 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
601 >        assertEquals(1, drained);
602          assertEquals(1, l.size());
603 <        q.drainTo(l, 1);
603 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
604 >        assertEquals(1, drained);
605          assertEquals(2, l.size());
606          assertTrue(l.contains(one));
607          assertTrue(l.contains(two));
# Line 598 | Line 609 | public class SynchronousQueueTest extend
609          awaitTermination(t2);
610      }
611  
612 +    /**
613 +     * remove(null), contains(null) always return false
614 +     */
615 +    public void testNeverContainsNull() {
616 +        Collection<?> q = new SynchronousQueue();
617 +        assertFalse(q.contains(null));
618 +        assertFalse(q.remove(null));
619 +    }
620 +
621   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines