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.65 by jsr166, Tue Aug 13 23:06:39 2019 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 >
22 > import junit.framework.Test;
23  
24   public class SynchronousQueueTest extends JSR166TestCase {
25  
# Line 35 | Line 36 | public class SynchronousQueueTest extend
36      }
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41  
42      public static Test suite() {
# Line 95 | Line 96 | public class SynchronousQueueTest extend
96      }
97  
98      /**
99 <     * addAll throws ISE if no active taker
99 >     * addAll throws IllegalStateException if no active taker
100       */
101      public void testAddAll_ISE()      { testAddAll_ISE(false); }
102      public void testAddAll_ISE_fair() { testAddAll_ISE(true); }
# Line 137 | Line 138 | public class SynchronousQueueTest extend
138              }});
139  
140          await(pleaseInterrupt);
141 <        assertThreadStaysAlive(t);
141 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
142          t.interrupt();
143          awaitTermination(t);
144          assertEquals(0, q.remainingCapacity());
# Line 157 | Line 158 | public class SynchronousQueueTest extend
158                  pleaseTake.countDown();
159                  q.put(one);
160  
161 +                Thread.currentThread().interrupt();
162 +                try {
163 +                    q.put(99);
164 +                    shouldThrow();
165 +                } catch (InterruptedException success) {}
166 +                assertFalse(Thread.interrupted());
167 +
168                  pleaseInterrupt.countDown();
169                  try {
170                      q.put(99);
# Line 166 | Line 174 | public class SynchronousQueueTest extend
174              }});
175  
176          await(pleaseTake);
177 <        assertEquals(q.remainingCapacity(), 0);
177 >        assertEquals(0, q.remainingCapacity());
178          try { assertSame(one, q.take()); }
179          catch (InterruptedException e) { threadUnexpectedException(e); }
180  
181          await(pleaseInterrupt);
182 <        assertThreadStaysAlive(t);
182 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
183          t.interrupt();
184          awaitTermination(t);
185 <        assertEquals(q.remainingCapacity(), 0);
185 >        assertEquals(0, q.remainingCapacity());
186      }
187  
188      /**
189       * timed offer times out if elements not taken
190       */
191 <    public void testTimedOffer()      { testTimedOffer(false); }
192 <    public void testTimedOffer_fair() { testTimedOffer(true); }
185 <    public void testTimedOffer(boolean fair) {
191 >    public void testTimedOffer() {
192 >        final boolean fair = randomBoolean();
193          final SynchronousQueue q = new SynchronousQueue(fair);
194          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
195          Thread t = newStartedThread(new CheckedRunnable() {
196              public void realRun() throws InterruptedException {
197                  long startTime = System.nanoTime();
198 +
199                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
200                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
201 +
202 +                Thread.currentThread().interrupt();
203 +                try {
204 +                    q.offer(new Object(), randomTimeout(), randomTimeUnit());
205 +                    shouldThrow();
206 +                } catch (InterruptedException success) {}
207 +                assertFalse(Thread.interrupted());
208 +
209                  pleaseInterrupt.countDown();
210                  try {
211 <                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
211 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
212                      shouldThrow();
213                  } catch (InterruptedException success) {}
214 +                assertFalse(Thread.interrupted());
215 +
216 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
217              }});
218  
219          await(pleaseInterrupt);
220 <        assertThreadStaysAlive(t);
220 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
221          t.interrupt();
222          awaitTermination(t);
223      }
# Line 227 | Line 246 | public class SynchronousQueueTest extend
246      /**
247       * timed poll with nonzero timeout times out if no active putter
248       */
249 <    public void testTimedPoll()      { testTimedPoll(false); }
250 <    public void testTimedPoll_fair() { testTimedPoll(true); }
232 <    public void testTimedPoll(boolean fair) {
249 >    public void testTimedPoll() {
250 >        final boolean fair = randomBoolean();
251          final SynchronousQueue q = new SynchronousQueue(fair);
252 <        long startTime = System.nanoTime();
252 >        final long startTime = System.nanoTime();
253          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
254          catch (InterruptedException e) { threadUnexpectedException(e); }
255          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 241 | Line 259 | public class SynchronousQueueTest extend
259       * timed poll before a delayed offer times out, returning null;
260       * after offer succeeds; on interruption throws
261       */
262 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
263 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
246 <    public void testTimedPollWithOffer(boolean fair) {
262 >    public void testTimedPollWithOffer() {
263 >        final boolean fair = randomBoolean();
264          final SynchronousQueue q = new SynchronousQueue(fair);
265          final CountDownLatch pleaseOffer = new CountDownLatch(1);
266          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 256 | Line 273 | public class SynchronousQueueTest extend
273                  pleaseOffer.countDown();
274                  startTime = System.nanoTime();
275                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
259                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
276  
277                  Thread.currentThread().interrupt();
278                  try {
279 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
279 >                    q.poll(randomTimeout(), randomTimeUnit());
280                      shouldThrow();
281                  } catch (InterruptedException success) {}
282                  assertFalse(Thread.interrupted());
# Line 271 | Line 287 | public class SynchronousQueueTest extend
287                      shouldThrow();
288                  } catch (InterruptedException success) {}
289                  assertFalse(Thread.interrupted());
290 +
291 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
292              }});
293  
294          await(pleaseOffer);
295          long startTime = System.nanoTime();
296          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
297          catch (InterruptedException e) { threadUnexpectedException(e); }
298 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
298 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
299  
300          await(pleaseInterrupt);
301 <        assertThreadStaysAlive(t);
301 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
302          t.interrupt();
303          awaitTermination(t);
304      }
# Line 322 | Line 340 | public class SynchronousQueueTest extend
340      }
341  
342      /**
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    /**
343       * contains returns false
344       */
345      public void testContains()      { testContains(false); }
# Line 400 | Line 407 | public class SynchronousQueueTest extend
407      public void testToArray(boolean fair) {
408          final SynchronousQueue q = new SynchronousQueue(fair);
409          Object[] o = q.toArray();
410 <        assertEquals(o.length, 0);
410 >        assertEquals(0, o.length);
411      }
412  
413      /**
414 <     * toArray(a) is nulled at position 0
414 >     * toArray(Integer array) returns its argument with the first
415 >     * element (if present) nulled out
416       */
417      public void testToArray2()      { testToArray2(false); }
418      public void testToArray2_fair() { testToArray2(true); }
419      public void testToArray2(boolean fair) {
420 <        final SynchronousQueue q = new SynchronousQueue(fair);
421 <        Integer[] ints = new Integer[1];
422 <        assertNull(ints[0]);
420 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
421 >        Integer[] a;
422 >
423 >        a = new Integer[0];
424 >        assertSame(a, q.toArray(a));
425 >
426 >        a = new Integer[3];
427 >        Arrays.fill(a, 42);
428 >        assertSame(a, q.toArray(a));
429 >        assertNull(a[0]);
430 >        for (int i = 1; i < a.length; i++)
431 >            assertEquals(42, (int) a[i]);
432      }
433  
434      /**
# Line 422 | Line 439 | public class SynchronousQueueTest extend
439      public void testToArray_null(boolean fair) {
440          final SynchronousQueue q = new SynchronousQueue(fair);
441          try {
442 <            Object o[] = q.toArray(null);
442 >            Object[] o = q.toArray((Object[])null);
443              shouldThrow();
444          } catch (NullPointerException success) {}
445      }
# Line 433 | Line 450 | public class SynchronousQueueTest extend
450      public void testIterator()      { testIterator(false); }
451      public void testIterator_fair() { testIterator(true); }
452      public void testIterator(boolean fair) {
453 <        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) {}
453 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
454      }
455  
456      /**
457 <     * iterator remove throws ISE
457 >     * iterator remove throws IllegalStateException
458       */
459      public void testIteratorRemove()      { testIteratorRemove(false); }
460      public void testIteratorRemove_fair() { testIteratorRemove(true); }
# Line 474 | Line 485 | public class SynchronousQueueTest extend
485      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
486      public void testOfferInExecutor(boolean fair) {
487          final SynchronousQueue q = new SynchronousQueue(fair);
477        ExecutorService executor = Executors.newFixedThreadPool(2);
488          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
489 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
490 +        try (PoolCleaner cleaner = cleaner(executor)) {
491  
492 <        executor.execute(new CheckedRunnable() {
493 <            public void realRun() throws InterruptedException {
494 <                assertFalse(q.offer(one));
495 <                threadsStarted.await();
496 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
497 <                assertEquals(0, q.remainingCapacity());
498 <            }});
499 <
500 <        executor.execute(new CheckedRunnable() {
501 <            public void realRun() throws InterruptedException {
502 <                threadsStarted.await();
503 <                assertSame(one, q.take());
504 <            }});
505 <
494 <        joinPool(executor);
492 >            executor.execute(new CheckedRunnable() {
493 >                public void realRun() throws InterruptedException {
494 >                    assertFalse(q.offer(one));
495 >                    threadsStarted.await();
496 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
497 >                    assertEquals(0, q.remainingCapacity());
498 >                }});
499 >
500 >            executor.execute(new CheckedRunnable() {
501 >                public void realRun() throws InterruptedException {
502 >                    threadsStarted.await();
503 >                    assertSame(one, q.take());
504 >                }});
505 >        }
506      }
507  
508      /**
# Line 502 | Line 513 | public class SynchronousQueueTest extend
513      public void testPollInExecutor(boolean fair) {
514          final SynchronousQueue q = new SynchronousQueue(fair);
515          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
516 <        ExecutorService executor = Executors.newFixedThreadPool(2);
517 <        executor.execute(new CheckedRunnable() {
518 <            public void realRun() throws InterruptedException {
519 <                assertNull(q.poll());
520 <                threadsStarted.await();
521 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
522 <                assertTrue(q.isEmpty());
523 <            }});
524 <
525 <        executor.execute(new CheckedRunnable() {
526 <            public void realRun() throws InterruptedException {
527 <                threadsStarted.await();
528 <                q.put(one);
529 <            }});
530 <
531 <        joinPool(executor);
516 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
517 >        try (PoolCleaner cleaner = cleaner(executor)) {
518 >            executor.execute(new CheckedRunnable() {
519 >                public void realRun() throws InterruptedException {
520 >                    assertNull(q.poll());
521 >                    threadsStarted.await();
522 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
523 >                    assertTrue(q.isEmpty());
524 >                }});
525 >
526 >            executor.execute(new CheckedRunnable() {
527 >                public void realRun() throws InterruptedException {
528 >                    threadsStarted.await();
529 >                    q.put(one);
530 >                }});
531 >        }
532      }
533  
534      /**
535 <     * a deserialized serialized queue is usable
535 >     * a deserialized/reserialized queue is usable
536       */
537 <    public void testSerialization()      { testSerialization(false); }
538 <    public void testSerialization_fair() { testSerialization(true); }
539 <    public void testSerialization(boolean fair) {
540 <        final SynchronousQueue x = new SynchronousQueue(fair);
541 <        final SynchronousQueue y = serialClone(x);
542 <        assertTrue(x != y);
543 <        assertTrue(x.isEmpty());
544 <        assertTrue(y.isEmpty());
537 >    public void testSerialization() {
538 >        final SynchronousQueue x = new SynchronousQueue();
539 >        final SynchronousQueue y = new SynchronousQueue(false);
540 >        final SynchronousQueue z = new SynchronousQueue(true);
541 >        assertSerialEquals(x, y);
542 >        assertNotSerialEquals(x, z);
543 >        SynchronousQueue[] qs = { x, y, z };
544 >        for (SynchronousQueue q : qs) {
545 >            SynchronousQueue clone = serialClone(q);
546 >            assertNotSame(q, clone);
547 >            assertSerialEquals(q, clone);
548 >            assertTrue(clone.isEmpty());
549 >            assertEquals(0, clone.size());
550 >            assertEquals(0, clone.remainingCapacity());
551 >            assertFalse(clone.offer(zero));
552 >        }
553      }
554  
555      /**
# Line 542 | Line 561 | public class SynchronousQueueTest extend
561          final SynchronousQueue q = new SynchronousQueue(fair);
562          ArrayList l = new ArrayList();
563          q.drainTo(l);
564 <        assertEquals(q.size(), 0);
565 <        assertEquals(l.size(), 0);
564 >        assertEquals(0, q.size());
565 >        assertEquals(0, l.size());
566      }
567  
568      /**
# Line 566 | Line 585 | public class SynchronousQueueTest extend
585                  fail("timed out");
586              Thread.yield();
587          }
588 <        assertTrue(l.size() == 1);
588 >        assertEquals(1, l.size());
589          assertSame(one, l.get(0));
590          awaitTermination(t);
591      }
# Line 587 | Line 606 | public class SynchronousQueueTest extend
606              }});
607  
608          ArrayList l = new ArrayList();
609 <        delay(SHORT_DELAY_MS);
610 <        q.drainTo(l, 1);
609 >        int drained;
610 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
611 >        assertEquals(1, drained);
612          assertEquals(1, l.size());
613 <        q.drainTo(l, 1);
613 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
614 >        assertEquals(1, drained);
615          assertEquals(2, l.size());
616          assertTrue(l.contains(one));
617          assertTrue(l.contains(two));
# Line 598 | Line 619 | public class SynchronousQueueTest extend
619          awaitTermination(t2);
620      }
621  
622 +    /**
623 +     * remove(null), contains(null) always return false
624 +     */
625 +    public void testNeverContainsNull() {
626 +        Collection<?> q = new SynchronousQueue();
627 +        assertFalse(q.contains(null));
628 +        assertFalse(q.remove(null));
629 +    }
630 +
631   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines