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.28 by jsr166, Thu Sep 15 01:18:01 2016 UTC vs.
Revision 1.33 by jsr166, Mon Sep 9 00:03:07 2019 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
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 java.util.concurrent.atomic.AtomicInteger;
# Line 39 | 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 49 | 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 292 | Line 296 | public class CyclicBarrierTest extends J
296      }
297  
298      /**
295     * All threads block while a barrier is broken.
296     */
297    public void testReset_Leakage() throws InterruptedException {
298        final CyclicBarrier c = new CyclicBarrier(2);
299        final AtomicBoolean done = new AtomicBoolean();
300        Thread t = newStartedThread(new CheckedRunnable() {
301            public void realRun() {
302                while (!done.get()) {
303                    try {
304                        while (c.isBroken())
305                            c.reset();
306
307                        c.await();
308                        shouldThrow();
309                    }
310                    catch (BrokenBarrierException ok) {}
311                    catch (InterruptedException ok) {}
312                }}});
313
314        for (int i = 0; i < 4; i++) {
315            delay(timeoutMillis());
316            t.interrupt();
317        }
318        done.set(true);
319        t.interrupt();
320        awaitTermination(t);
321    }
322
323    /**
299       * Reset of a non-broken barrier does not break barrier
300       */
301      public void testResetWithoutBreakage() throws Exception {
# Line 459 | Line 434 | public class CyclicBarrierTest extends J
434              assertEquals(0, barrier.getNumberWaiting());
435          }
436      }
437 +
438 +    /**
439 +     * There can be more threads calling await() than parties, as long as each
440 +     * task only calls await once and the task count is a multiple of parties.
441 +     */
442 +    public void testMoreTasksThanParties() throws Exception {
443 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
444 +        final int parties = rnd.nextInt(1, 5);
445 +        final int nTasks = rnd.nextInt(1, 5) * parties;
446 +        final AtomicInteger tripCount = new AtomicInteger(0);
447 +        final AtomicInteger awaitCount = new AtomicInteger(0);
448 +        final CyclicBarrier barrier =
449 +            new CyclicBarrier(parties, () -> tripCount.getAndIncrement());
450 +        final ExecutorService e = Executors.newFixedThreadPool(nTasks);
451 +        final Runnable awaiter = () -> {
452 +            try {
453 +                if (randomBoolean())
454 +                    barrier.await();
455 +                else
456 +                    barrier.await(LONG_DELAY_MS, MILLISECONDS);
457 +                awaitCount.getAndIncrement();
458 +            } catch (Throwable fail) { threadUnexpectedException(fail); }};
459 +        try (PoolCleaner cleaner = cleaner(e)) {
460 +            for (int i = nTasks; i--> 0; )
461 +                e.execute(awaiter);
462 +        }
463 +        assertEquals(nTasks / parties, tripCount.get());
464 +        assertEquals(nTasks, awaitCount.get());
465 +        assertEquals(0, barrier.getNumberWaiting());
466 +    }
467   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines