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.47 by jsr166, Sat Apr 25 04:55:31 2015 UTC vs.
Revision 1.57 by jsr166, Sun May 14 03:48:35 2017 UTC

# Line 18 | Line 18 | import java.util.concurrent.CountDownLat
18   import java.util.concurrent.Executors;
19   import java.util.concurrent.ExecutorService;
20   import java.util.concurrent.SynchronousQueue;
21 + import java.util.concurrent.ThreadLocalRandom;
22  
23   import junit.framework.Test;
24  
# Line 138 | 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 172 | Line 173 | public class SynchronousQueueTest extend
173          catch (InterruptedException e) { threadUnexpectedException(e); }
174  
175          await(pleaseInterrupt);
176 <        assertThreadStaysAlive(t);
176 >        assertThreadBlocks(t, Thread.State.WAITING);
177          t.interrupt();
178          awaitTermination(t);
179          assertEquals(0, q.remainingCapacity());
# Line 181 | Line 182 | public class SynchronousQueueTest extend
182      /**
183       * timed offer times out if elements not taken
184       */
185 <    public void testTimedOffer()      { testTimedOffer(false); }
186 <    public void testTimedOffer_fair() { testTimedOffer(true); }
186 <    public void testTimedOffer(boolean fair) {
185 >    public void testTimedOffer() {
186 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
187          final SynchronousQueue q = new SynchronousQueue(fair);
188          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
189          Thread t = newStartedThread(new CheckedRunnable() {
# Line 196 | Line 196 | public class SynchronousQueueTest extend
196                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
197                      shouldThrow();
198                  } catch (InterruptedException success) {}
199 +                assertFalse(Thread.interrupted());
200              }});
201  
202          await(pleaseInterrupt);
203 <        assertThreadStaysAlive(t);
203 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
204          t.interrupt();
205          awaitTermination(t);
206      }
# Line 228 | Line 229 | public class SynchronousQueueTest extend
229      /**
230       * timed poll with nonzero timeout times out if no active putter
231       */
232 <    public void testTimedPoll()      { testTimedPoll(false); }
233 <    public void testTimedPoll_fair() { testTimedPoll(true); }
233 <    public void testTimedPoll(boolean fair) {
232 >    public void testTimedPoll() {
233 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
234          final SynchronousQueue q = new SynchronousQueue(fair);
235 <        long startTime = System.nanoTime();
235 >        final long startTime = System.nanoTime();
236          try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
237          catch (InterruptedException e) { threadUnexpectedException(e); }
238          assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
# Line 242 | Line 242 | public class SynchronousQueueTest extend
242       * timed poll before a delayed offer times out, returning null;
243       * after offer succeeds; on interruption throws
244       */
245 <    public void testTimedPollWithOffer()      { testTimedPollWithOffer(false); }
246 <    public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); }
247 <    public void testTimedPollWithOffer(boolean fair) {
245 >    public void testTimedPollWithOffer() {
246 >        final boolean fair = ThreadLocalRandom.current().nextBoolean();
247          final SynchronousQueue q = new SynchronousQueue(fair);
248          final CountDownLatch pleaseOffer = new CountDownLatch(1);
249          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
# Line 257 | Line 256 | public class SynchronousQueueTest extend
256                  pleaseOffer.countDown();
257                  startTime = System.nanoTime();
258                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
260                assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
259  
260                  Thread.currentThread().interrupt();
261                  try {
# Line 272 | Line 270 | public class SynchronousQueueTest extend
270                      shouldThrow();
271                  } catch (InterruptedException success) {}
272                  assertFalse(Thread.interrupted());
273 +
274 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
275              }});
276  
277          await(pleaseOffer);
278          long startTime = System.nanoTime();
279          try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); }
280          catch (InterruptedException e) { threadUnexpectedException(e); }
281 <        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
281 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
282  
283          await(pleaseInterrupt);
284 <        assertThreadStaysAlive(t);
284 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
285          t.interrupt();
286          awaitTermination(t);
287      }
# Line 400 | Line 400 | public class SynchronousQueueTest extend
400      public void testToArray2()      { testToArray2(false); }
401      public void testToArray2_fair() { testToArray2(true); }
402      public void testToArray2(boolean fair) {
403 <        final SynchronousQueue<Integer> q
404 <            = new SynchronousQueue<Integer>(fair);
403 >        final SynchronousQueue<Integer> q = new SynchronousQueue<>(fair);
404          Integer[] a;
405  
406          a = new Integer[0];
# Line 469 | Line 468 | public class SynchronousQueueTest extend
468      public void testOfferInExecutor_fair() { testOfferInExecutor(true); }
469      public void testOfferInExecutor(boolean fair) {
470          final SynchronousQueue q = new SynchronousQueue(fair);
472        ExecutorService executor = Executors.newFixedThreadPool(2);
471          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
472 +        final ExecutorService executor = Executors.newFixedThreadPool(2);
473 +        try (PoolCleaner cleaner = cleaner(executor)) {
474  
475 <        executor.execute(new CheckedRunnable() {
476 <            public void realRun() throws InterruptedException {
477 <                assertFalse(q.offer(one));
478 <                threadsStarted.await();
479 <                assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
480 <                assertEquals(0, q.remainingCapacity());
481 <            }});
482 <
483 <        executor.execute(new CheckedRunnable() {
484 <            public void realRun() throws InterruptedException {
485 <                threadsStarted.await();
486 <                assertSame(one, q.take());
487 <            }});
488 <
489 <        joinPool(executor);
475 >            executor.execute(new CheckedRunnable() {
476 >                public void realRun() throws InterruptedException {
477 >                    assertFalse(q.offer(one));
478 >                    threadsStarted.await();
479 >                    assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
480 >                    assertEquals(0, q.remainingCapacity());
481 >                }});
482 >
483 >            executor.execute(new CheckedRunnable() {
484 >                public void realRun() throws InterruptedException {
485 >                    threadsStarted.await();
486 >                    assertSame(one, q.take());
487 >                }});
488 >        }
489      }
490  
491      /**
# Line 497 | Line 496 | public class SynchronousQueueTest extend
496      public void testPollInExecutor(boolean fair) {
497          final SynchronousQueue q = new SynchronousQueue(fair);
498          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
499 <        ExecutorService executor = Executors.newFixedThreadPool(2);
500 <        executor.execute(new CheckedRunnable() {
501 <            public void realRun() throws InterruptedException {
502 <                assertNull(q.poll());
503 <                threadsStarted.await();
504 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
505 <                assertTrue(q.isEmpty());
506 <            }});
507 <
508 <        executor.execute(new CheckedRunnable() {
509 <            public void realRun() throws InterruptedException {
510 <                threadsStarted.await();
511 <                q.put(one);
512 <            }});
513 <
514 <        joinPool(executor);
499 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
500 >        try (PoolCleaner cleaner = cleaner(executor)) {
501 >            executor.execute(new CheckedRunnable() {
502 >                public void realRun() throws InterruptedException {
503 >                    assertNull(q.poll());
504 >                    threadsStarted.await();
505 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
506 >                    assertTrue(q.isEmpty());
507 >                }});
508 >
509 >            executor.execute(new CheckedRunnable() {
510 >                public void realRun() throws InterruptedException {
511 >                    threadsStarted.await();
512 >                    q.put(one);
513 >                }});
514 >        }
515      }
516  
517      /**
# Line 569 | Line 568 | public class SynchronousQueueTest extend
568                  fail("timed out");
569              Thread.yield();
570          }
571 <        assertTrue(l.size() == 1);
571 >        assertEquals(1, l.size());
572          assertSame(one, l.get(0));
573          awaitTermination(t);
574      }
# Line 590 | Line 589 | public class SynchronousQueueTest extend
589              }});
590  
591          ArrayList l = new ArrayList();
592 <        delay(SHORT_DELAY_MS);
593 <        q.drainTo(l, 1);
592 >        int drained;
593 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
594 >        assertEquals(1, drained);
595          assertEquals(1, l.size());
596 <        q.drainTo(l, 1);
596 >        while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
597 >        assertEquals(1, drained);
598          assertEquals(2, l.size());
599          assertTrue(l.contains(one));
600          assertTrue(l.contains(two));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines