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

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.24 by jsr166, Tue May 31 16:16:23 2011 UTC vs.
Revision 1.32 by jsr166, Tue Aug 13 00:54:51 2019 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.concurrent.BrokenBarrierException;
12   import java.util.concurrent.CountDownLatch;
13   import java.util.concurrent.CyclicBarrier;
14 + import java.util.concurrent.ExecutorService;
15 + import java.util.concurrent.Executors;
16 + import java.util.concurrent.ThreadLocalRandom;
17   import java.util.concurrent.TimeoutException;
18   import java.util.concurrent.atomic.AtomicBoolean;
19 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 > import java.util.concurrent.atomic.AtomicInteger;
20 >
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class CyclicBarrierTest extends JSR166TestCase {
25      public static void main(String[] args) {
26 <        junit.textui.TestRunner.run(suite());
26 >        main(suite(), args);
27      }
28      public static Test suite() {
29          return new TestSuite(CyclicBarrierTest.class);
30      }
31  
26    private volatile int countAction;
27    private class MyAction implements Runnable {
28        public void run() { ++countAction; }
29    }
30
32      /**
33       * Spin-waits till the number of waiters == numberOfWaiters.
34       */
# Line 41 | Line 42 | public class CyclicBarrierTest extends J
42      }
43  
44      /**
45 <     * Creating with negative parties throws IAE
45 >     * Creating with negative parties throws IllegalArgumentException
46       */
47      public void testConstructor1() {
48          try {
# Line 51 | Line 52 | public class CyclicBarrierTest extends J
52      }
53  
54      /**
55 <     * Creating with negative parties and no action throws IAE
55 >     * Creating with negative parties and no action throws
56 >     * IllegalArgumentException
57       */
58      public void testConstructor2() {
59          try {
# Line 85 | Line 87 | public class CyclicBarrierTest extends J
87       * The supplied barrier action is run at barrier
88       */
89      public void testBarrierAction() throws Exception {
90 <        countAction = 0;
91 <        CyclicBarrier b = new CyclicBarrier(1, new MyAction());
90 >        final AtomicInteger count = new AtomicInteger(0);
91 >        final Runnable incCount = new Runnable() { public void run() {
92 >            count.getAndIncrement(); }};
93 >        CyclicBarrier b = new CyclicBarrier(1, incCount);
94          assertEquals(1, b.getParties());
95          assertEquals(0, b.getNumberWaiting());
96          b.await();
97          b.await();
98          assertEquals(0, b.getNumberWaiting());
99 <        assertEquals(countAction, 2);
99 >        assertEquals(2, count.get());
100      }
101  
102      /**
# Line 307 | Line 311 | public class CyclicBarrierTest extends J
311                          c.await();
312                          shouldThrow();
313                      }
314 <                    catch (BrokenBarrierException ok) {}
311 <                    catch (InterruptedException ok) {}
314 >                    catch (BrokenBarrierException | InterruptedException ok) {}
315                  }}});
316  
317          for (int i = 0; i < 4; i++) {
# Line 459 | Line 462 | public class CyclicBarrierTest extends J
462              assertEquals(0, barrier.getNumberWaiting());
463          }
464      }
465 +
466 +    /**
467 +     * There can be more threads calling await() than parties, as long as each
468 +     * task only calls await once and the task count is a multiple of parties.
469 +     */
470 +    public void testMoreTasksThanParties() throws Exception {
471 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
472 +        final int parties = rnd.nextInt(1, 5);
473 +        final int nTasks = rnd.nextInt(1, 5) * parties;
474 +        final AtomicInteger tripCount = new AtomicInteger(0);
475 +        final AtomicInteger awaitCount = new AtomicInteger(0);
476 +        final CyclicBarrier barrier =
477 +            new CyclicBarrier(parties, () -> tripCount.getAndIncrement());
478 +        final ExecutorService e = Executors.newFixedThreadPool(nTasks);
479 +        final Runnable awaiter = () -> {
480 +            try {
481 +                if (randomBoolean())
482 +                    barrier.await();
483 +                else
484 +                    barrier.await(LONG_DELAY_MS, MILLISECONDS);
485 +                awaitCount.getAndIncrement();
486 +            } catch (Throwable fail) { threadUnexpectedException(fail); }};
487 +        try (PoolCleaner cleaner = cleaner(e)) {
488 +            for (int i = nTasks; i--> 0; )
489 +                e.execute(awaiter);
490 +        }
491 +        assertEquals(nTasks / parties, tripCount.get());
492 +        assertEquals(nTasks, awaitCount.get());
493 +        assertEquals(0, barrier.getNumberWaiting());
494 +    }
495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines