public void ResetReinitializeBarrierAfterExceptionInBarrierAction()
        {
            var           e       = new NullReferenceException();
            CyclicBarrier start   = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3, () => { throw e; });
            ThreadStart   action  = delegate
            {
                start.Await();
                Assert.Catch <BrokenBarrierException>(() => barrier.Await());
            };

            for (int i = 0; i < 2; i++)
            {
                ThreadManager.StartAndAssertRegistered("T" + i + "-", action, action);

                start.Await();
                while (barrier.NumberOfWaitingParties < 2)
                {
                    Thread.Sleep(1);
                }
                Assert.That(Assert.Catch(() => barrier.Await()), Is.SameAs(e));
                ThreadManager.JoinAndVerify();
                Assert.IsTrue(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                barrier.Reset();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
        public void ResetLeakage() //TODO: didn't understand test name and description; copied from Java -K.X.
        {
            CyclicBarrier c    = new CyclicBarrier(2);
            AtomicBoolean done = new AtomicBoolean();
            Thread        t    = ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                while (!done.Value)
                {
                    while (c.IsBroken)
                    {
                        c.Reset();
                    }
                    var e = Assert.Catch(() => c.Await());
                    Assert.That(e,
                                Is.InstanceOf <BrokenBarrierException>()
                                .Or.InstanceOf <ThreadInterruptedException>());
                }
            });


            for (int i = 0; i < 4; i++)
            {
                Thread.Sleep(Delays.Short);
                t.Interrupt();
            }
            done.Value = true;
            t.Interrupt();
            ThreadManager.JoinAndVerify();
        }
        public void ResetReinitializeBarrierAfterTimeout()
        {
            CyclicBarrier start   = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3);

            for (int i = 0; i < 2; i++)
            {
                ThreadManager.StartAndAssertRegistered(
                    "T" + i + "-",
                    delegate
                {
                    start.Await();
                    Assert.Catch <TimeoutException>(() => barrier.Await(Delays.Short));
                },
                    delegate
                {
                    start.Await();
                    Assert.Catch <BrokenBarrierException>(() => barrier.Await());
                });

                start.Await();
                ThreadManager.JoinAndVerify();
                Assert.IsTrue(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                barrier.Reset();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
        public void ResetBreaksBarrierWhenTheadWaiting([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c      = new CyclicBarrier(3);
            ThreadStart   action = () => Assert.Throws <BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c));

            ThreadManager.StartAndAssertRegistered("T", action, action);
            Thread.Sleep(Delays.Short);
            c.Reset();
            ThreadManager.JoinAndVerify();
        }
        public void ResetDoesNotBreakBarrierWhenNoThreadWaiting([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(3);

            ThreadStart action = () => AwaitOrTimedAwait(isTimed, c);

            c.Reset();

            ThreadManager.StartAndAssertRegistered("T", action, action, action);
            ThreadManager.JoinAndVerify();
        }
        public void ResetSucceedsOnNonBrokenBarrier()
        {
            CyclicBarrier start   = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3);
            ThreadStart   action  = () => { start.Await(); barrier.Await(); };

            for (int i = 0; i < 3; i++)
            {
                ThreadManager.StartAndAssertRegistered("T" + i + "-", action, action);

                start.Await();
                barrier.Await();
                ThreadManager.JoinAndVerify();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                if (i == 1)
                {
                    barrier.Reset();
                }
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
 public void ResetBreaksBarrierWhenTheadWaiting([Values(true, false)] bool isTimed)
 {
     CyclicBarrier c = new CyclicBarrier(3);
     ThreadStart action = () => Assert.Throws<BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c));
     ThreadManager.StartAndAssertRegistered("T", action, action);
     Thread.Sleep(Delays.Short);
     c.Reset();
     ThreadManager.JoinAndVerify();
 }
        public void ResetSucceedsOnNonBrokenBarrier()
        {
            CyclicBarrier start = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3);
            ThreadStart action = () => { start.Await(); barrier.Await(); };
            for (int i = 0; i < 3; i++)
            {
                ThreadManager.StartAndAssertRegistered("T" + i + "-", action, action);

                start.Await();
                barrier.Await();
                ThreadManager.JoinAndVerify();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                if (i == 1) barrier.Reset();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
        public void ResetReinitializeBarrierAfterTimeout()
        {
            CyclicBarrier start = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3);
            for (int i = 0; i < 2; i++)
            {
                ThreadManager.StartAndAssertRegistered(
                    "T" + i + "-",
                    delegate
                    {
                        start.Await();
                        Assert.Catch<TimeoutException>(() => barrier.Await(Delays.Short));
                    },
                    delegate
                    {
                        start.Await();
                        Assert.Catch<BrokenBarrierException>(() => barrier.Await());
                    });

                start.Await();
                ThreadManager.JoinAndVerify();
                Assert.IsTrue(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                barrier.Reset();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
        public void ResetReinitializeBarrierAfterExceptionInBarrierAction()
        {
            var e = new NullReferenceException();
            CyclicBarrier start = new CyclicBarrier(3);
            CyclicBarrier barrier = new CyclicBarrier(3, () => { throw e; });
            ThreadStart action = delegate
            {
                start.Await();
                Assert.Catch<BrokenBarrierException>(() => barrier.Await());
            };
            for (int i = 0; i < 2; i++)
            {
                ThreadManager.StartAndAssertRegistered("T" + i + "-", action, action);

                start.Await();
                while (barrier.NumberOfWaitingParties < 2) { Thread.Sleep(1); }
                Assert.That(Assert.Catch(()=>barrier.Await()), Is.SameAs(e));
                ThreadManager.JoinAndVerify();
                Assert.IsTrue(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
                barrier.Reset();
                Assert.IsFalse(barrier.IsBroken);
                Assert.AreEqual(0, barrier.NumberOfWaitingParties);
            }
        }
        public void ResetLeakage()
        {
            CyclicBarrier c = new CyclicBarrier(2);
            AtomicBoolean done = new AtomicBoolean();
            Thread t = ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                {
                    while (!done.Value)
                    {
                        while (c.IsBroken) c.Reset();
                        var e = Assert.Catch(() => c.Await());
                        Assert.That(e,
                                    Is.InstanceOf<BrokenBarrierException>()
                                    .Or.InstanceOf<ThreadInterruptedException>());
                    }
                });

            for (int i = 0; i < 4; i++)
            {
                Thread.Sleep(Delays.Short);
                t.Interrupt();
            }
            done.Value = true;
            t.Interrupt();
            ThreadManager.JoinAndVerify();
        }
        public void ResetDoesNotBreakBarrierWhenNoThreadWaiting([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(3);

            ThreadStart action = () => AwaitOrTimedAwait(isTimed, c);

            c.Reset();

            ThreadManager.StartAndAssertRegistered("T", action, action, action);
            ThreadManager.JoinAndVerify();
        }