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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines