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.42 by jsr166, Sun Nov 23 22:27:06 2014 UTC vs.
Revision 1.67 by jsr166, Mon Dec 16 22:55:54 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 171 | Line 179 | public class SynchronousQueueTest extend
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(0, q.remainingCapacity());
# Line 180 | Line 188 | public class SynchronousQueueTest extend
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(), LONGER_DELAY_MS, MILLISECONDS);
212                      shouldThrow();
213                  } catch (InterruptedException success) {}
214 +                assertFalse(Thread.interrupted());
215              }});
216  
217          await(pleaseInterrupt);
218 <        assertThreadStaysAlive(t);
218 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
219          t.interrupt();
220          awaitTermination(t);
221      }
# Line 227 | Line 244 | public class SynchronousQueueTest extend
244      /**
245       * timed poll with nonzero timeout times out if no active putter
246       */
247 <    public void testTimedPoll()      { testTimedPoll(false); }
248 <    public void testTimedPoll_fair() { testTimedPoll(true); }
232 <    public void testTimedPoll(boolean fair) {
247 >    public void testTimedPoll() {
248 >        final boolean fair = randomBoolean();
249          final SynchronousQueue q = new SynchronousQueue(fair);
250 <        long startTime = System.nanoTime();
250 >        final long startTime = System.nanoTime();
251          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
252          catch (InterruptedException e) { threadUnexpectedException(e); }
253          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 241 | Line 257 | public class SynchronousQueueTest extend
257       * timed poll before a delayed offer times out, returning null;
258       * after offer succeeds; on interruption throws
259       */
260 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
261 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
246 <    public void testTimedPollWithOffer(boolean fair) {
260 >    public void testTimedPollWithOffer() {
261 >        final boolean fair = randomBoolean();
262          final SynchronousQueue q = new SynchronousQueue(fair);
263          final CountDownLatch pleaseOffer = new CountDownLatch(1);
264          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 256 | Line 271 | public class SynchronousQueueTest extend
271                  pleaseOffer.countDown();
272                  startTime = System.nanoTime();
273                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
259                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
274  
275                  Thread.currentThread().interrupt();
276                  try {
277 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
277 >                    q.poll(randomTimeout(), randomTimeUnit());
278                      shouldThrow();
279                  } catch (InterruptedException success) {}
280                  assertFalse(Thread.interrupted());
# Line 271 | Line 285 | public class SynchronousQueueTest extend
285                      shouldThrow();
286                  } catch (InterruptedException success) {}
287                  assertFalse(Thread.interrupted());
288 +
289 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
290              }});
291  
292          await(pleaseOffer);
293          long startTime = System.nanoTime();
294          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
295          catch (InterruptedException e) { threadUnexpectedException(e); }
296 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
296 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
297  
298          await(pleaseInterrupt);
299 <        assertThreadStaysAlive(t);
299 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
300          t.interrupt();
301          awaitTermination(t);
302      }
# Line 399 | Line 415 | public class SynchronousQueueTest extend
415      public void testToArray2()      { testToArray2(false); }
416      public void testToArray2_fair() { testToArray2(true); }
417      public void testToArray2(boolean fair) {
418 <        final SynchronousQueue<Integer> q
403 <            = new SynchronousQueue<Integer>(fair);
418 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
419          Integer[] a;
420  
421          a = new Integer[0];
# Line 422 | Line 437 | public class SynchronousQueueTest extend
437      public void testToArray_null(boolean fair) {
438          final SynchronousQueue q = new SynchronousQueue(fair);
439          try {
440 <            Object o[] = q.toArray(null);
440 >            Object[] unused = q.toArray((Object[])null);
441              shouldThrow();
442          } catch (NullPointerException success) {}
443      }
# Line 433 | Line 448 | public class SynchronousQueueTest extend
448      public void testIterator()      { testIterator(false); }
449      public void testIterator_fair() { testIterator(true); }
450      public void testIterator(boolean fair) {
451 <        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) {}
451 >        assertIteratorExhausted(new SynchronousQueue(fair).iterator());
452      }
453  
454      /**
455 <     * iterator remove throws ISE
455 >     * iterator remove throws IllegalStateException
456       */
457      public void testIteratorRemove()      { testIteratorRemove(false); }
458      public void testIteratorRemove_fair() { testIteratorRemove(true); }
# Line 474 | Line 483 | public class SynchronousQueueTest extend
483      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
484      public void testOfferInExecutor(boolean fair) {
485          final SynchronousQueue q = new SynchronousQueue(fair);
477        ExecutorService executor = Executors.newFixedThreadPool(2);
486          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
487 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
488 +        try (PoolCleaner cleaner = cleaner(executor)) {
489  
490 <        executor.execute(new CheckedRunnable() {
491 <            public void realRun() throws InterruptedException {
492 <                assertFalse(q.offer(one));
493 <                threadsStarted.await();
494 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
495 <                assertEquals(0, q.remainingCapacity());
496 <            }});
497 <
498 <        executor.execute(new CheckedRunnable() {
499 <            public void realRun() throws InterruptedException {
500 <                threadsStarted.await();
501 <                assertSame(one, q.take());
502 <            }});
503 <
494 <        joinPool(executor);
490 >            executor.execute(new CheckedRunnable() {
491 >                public void realRun() throws InterruptedException {
492 >                    assertFalse(q.offer(one));
493 >                    threadsStarted.await();
494 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
495 >                    assertEquals(0, q.remainingCapacity());
496 >                }});
497 >
498 >            executor.execute(new CheckedRunnable() {
499 >                public void realRun() throws InterruptedException {
500 >                    threadsStarted.await();
501 >                    assertSame(one, q.take());
502 >                }});
503 >        }
504      }
505  
506      /**
# Line 502 | Line 511 | public class SynchronousQueueTest extend
511      public void testPollInExecutor(boolean fair) {
512          final SynchronousQueue q = new SynchronousQueue(fair);
513          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
514 <        ExecutorService executor = Executors.newFixedThreadPool(2);
515 <        executor.execute(new CheckedRunnable() {
516 <            public void realRun() throws InterruptedException {
517 <                assertNull(q.poll());
518 <                threadsStarted.await();
519 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
520 <                assertTrue(q.isEmpty());
521 <            }});
522 <
523 <        executor.execute(new CheckedRunnable() {
524 <            public void realRun() throws InterruptedException {
525 <                threadsStarted.await();
526 <                q.put(one);
527 <            }});
528 <
529 <        joinPool(executor);
514 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
515 >        try (PoolCleaner cleaner = cleaner(executor)) {
516 >            executor.execute(new CheckedRunnable() {
517 >                public void realRun() throws InterruptedException {
518 >                    assertNull(q.poll());
519 >                    threadsStarted.await();
520 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
521 >                    assertTrue(q.isEmpty());
522 >                }});
523 >
524 >            executor.execute(new CheckedRunnable() {
525 >                public void realRun() throws InterruptedException {
526 >                    threadsStarted.await();
527 >                    q.put(one);
528 >                }});
529 >        }
530      }
531  
532      /**
533 <     * a deserialized serialized queue is usable
533 >     * a deserialized/reserialized queue is usable
534       */
535      public void testSerialization() {
536          final SynchronousQueue x = new SynchronousQueue();
# Line 574 | Line 583 | public class SynchronousQueueTest extend
583                  fail("timed out");
584              Thread.yield();
585          }
586 <        assertTrue(l.size() == 1);
586 >        assertEquals(1, l.size());
587          assertSame(one, l.get(0));
588          awaitTermination(t);
589      }
# Line 595 | Line 604 | public class SynchronousQueueTest extend
604              }});
605  
606          ArrayList l = new ArrayList();
607 <        delay(SHORT_DELAY_MS);
608 <        q.drainTo(l, 1);
607 >        int drained;
608 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
609 >        assertEquals(1, drained);
610          assertEquals(1, l.size());
611 <        q.drainTo(l, 1);
611 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
612 >        assertEquals(1, drained);
613          assertEquals(2, l.size());
614          assertTrue(l.contains(one));
615          assertTrue(l.contains(two));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines