コード例 #1
0
        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();
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        public void PartiesReturnsTheValueGiveinInConstructor([Values(1, 5)] int parties)
        {
            CyclicBarrier b = new CyclicBarrier(parties);

            Assert.AreEqual(parties, b.Parties);
            Assert.AreEqual(0, b.NumberOfWaitingParties);
        }
コード例 #5
0
        public void AwaitChokesWhenTimeOut()
        {
            CyclicBarrier c = new CyclicBarrier(2);
            ThreadManager.StartAndAssertRegistered(
                "T", () => Assert.Throws<TimeoutException>(() => c.Await(Delays.Short)));

            ThreadManager.JoinAndVerify();
        }
コード例 #6
0
        public void AwaitChokesWhenTimeOut()
        {
            CyclicBarrier c = new CyclicBarrier(2);

            ThreadManager.StartAndAssertRegistered(
                "T", () => Assert.Throws <TimeoutException>(() => c.Await(Delays.Short)));

            ThreadManager.JoinAndVerify();
        }
コード例 #7
0
        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();
        }
コード例 #8
0
        public void TimeoutAwaitBreaksBarrier([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(3);

            ThreadManager.StartAndAssertRegistered(
                "T1", () => Assert.Throws <TimeoutException>(() => c.Await(Delays.Short)));
            ThreadManager.StartAndAssertRegistered(
                "T2", () => Assert.Throws <BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c)));
            ThreadManager.JoinAndVerify();
        }
コード例 #9
0
        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();
        }
コード例 #10
0
 private static void AwaitOrTimedAwait(bool isTimed, CyclicBarrier c)
 {
     if (isTimed)
     {
         c.Await(Delays.Long);
     }
     else
     {
         c.Await();
     }
 }
コード例 #11
0
        public void InterruptedAwaitBreaksBarrier([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(3);

            Thread t1 = ThreadManager.StartAndAssertRegistered(
                "T1", () => Assert.Throws<ThreadInterruptedException>(() => AwaitOrTimedAwait(isTimed, c)));
            ThreadManager.StartAndAssertRegistered(
                "T2", () => Assert.Throws<BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c)));
            Thread.Sleep(Delays.Short);
            t1.Interrupt();
            ThreadManager.JoinAndVerify();
        }
コード例 #12
0
 public void AwaitChokesOnBrokenBarrier([Values(true, false)] bool isTimed)
 {
     CyclicBarrier c = new CyclicBarrier(2);
     ThreadManager.StartAndAssertRegistered(
         "T1",
         delegate
             {
                 Assert.Throws<TimeoutException>(() => c.Await(Delays.Short));
                 Assert.Throws<BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c));
             });
     ThreadManager.JoinAndVerify();
 }
コード例 #13
0
        public void SuppliedRunnableIsRunAtBarries()
        {
            var           runnable = MockRepository.GenerateStub <IRunnable>();
            CyclicBarrier b        = new CyclicBarrier(1, runnable);

            Assert.AreEqual(1, b.Parties);
            Assert.AreEqual(0, b.NumberOfWaitingParties);
            b.Await();
            b.Await();
            Assert.AreEqual(0, b.NumberOfWaitingParties);
            runnable.AssertWasCalled(r => r.Run(), m => m.Repeat.Twice());
        }
コード例 #14
0
        public void SuppliedActionIsRunAtBarries()
        {
            var           action = MockRepository.GenerateStub <Action>();
            CyclicBarrier b      = new CyclicBarrier(1, action);

            Assert.AreEqual(1, b.Parties);
            Assert.AreEqual(0, b.NumberOfWaitingParties);
            b.Await();
            b.Await(Delays.Small);
            Assert.AreEqual(0, b.NumberOfWaitingParties);
            action.AssertWasCalled(a => a(), m => m.Repeat.Twice());
        }
コード例 #15
0
        public void AwaitChokesOnBrokenBarrier([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(2);

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                Assert.Throws <TimeoutException>(() => c.Await(Delays.Short));
                Assert.Throws <BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c));
            });
            ThreadManager.JoinAndVerify();
        }
コード例 #16
0
        public void InterruptedAwaitBreaksBarrier([Values(true, false)] bool isTimed)
        {
            CyclicBarrier c = new CyclicBarrier(3);

            Thread t1 = ThreadManager.StartAndAssertRegistered(
                "T1", () => Assert.Throws <ThreadInterruptedException>(() => AwaitOrTimedAwait(isTimed, c)));

            ThreadManager.StartAndAssertRegistered(
                "T2", () => Assert.Throws <BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c)));
            Thread.Sleep(Delays.Short);
            t1.Interrupt();
            ThreadManager.JoinAndVerify();
        }
コード例 #17
0
        public void TwoPartieBarrierTriggersAfterBothInvokeAwait()
        {
            CyclicBarrier b      = new CyclicBarrier(2);
            ThreadStart   action = () =>
            {
                b.Await();
                b.Await(Delays.Small);
                b.Await();
                b.Await(Delays.Small);
            };

            ThreadManager.StartAndAssertRegistered("T", action, action);
            ThreadManager.JoinAndVerify();
        }
コード例 #18
0
        public void SinglePartyBarrierTriggersAfterSingleAwait([Values(true, false)] bool isTimed)
        {
            CyclicBarrier b = new CyclicBarrier(1);

            Assert.AreEqual(1, b.Parties);
            Assert.AreEqual(0, b.NumberOfWaitingParties);
            if (isTimed)
            {
                b.Await(Delays.Small);
                b.Await(Delays.Small);
            }
            else
            {
                b.Await();
                b.Await();
            }
            Assert.AreEqual(0, b.NumberOfWaitingParties);
        }
コード例 #19
0
        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);
            }
        }
コード例 #20
0
        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);
            }
        }
コード例 #21
0
        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);
            }
        }
コード例 #22
0
        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);
            }
        }
コード例 #23
0
 internal NACalibrationLoop(long endTime, NonAtomicInteger obj, CyclicBarrier b)
 {
     this.endTime = endTime;
     this.obj = obj;
     this.barrier = b;
     obj.set_Renamed(CASLoops.rng.next());
 }
コード例 #24
0
 internal static long runVolatile(int n, long iters)
 {
     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
     CyclicBarrier b = new CyclicBarrier(n + 1, timer);
     VolatileInteger a = new VolatileInteger();
     for (int j = 0; j < n; ++j)
         new SupportClass.ThreadClass(new System.Threading.ThreadStart(new VolatileLoop(iters, a, b).Run)).Start();
     b.Await();
     b.Await();
     if (sum.Value == 0)
         System.Console.Out.Write(" ");
     return timer.Time;
 }
コード例 #25
0
 public void TimeoutAwaitBreaksBarrier([Values(true, false)] bool isTimed)
 {
     CyclicBarrier c = new CyclicBarrier(3);
     ThreadManager.StartAndAssertRegistered(
         "T1", () => Assert.Throws<TimeoutException>(()=>c.Await(Delays.Short)));
     ThreadManager.StartAndAssertRegistered(
         "T2", () => Assert.Throws<BrokenBarrierException>(() => AwaitOrTimedAwait(isTimed, c)));
     ThreadManager.JoinAndVerify();
 }
コード例 #26
0
 public void TwoPartieBarrierTriggersAfterBothInvokeAwait()
 {
     CyclicBarrier b = new CyclicBarrier(2);
     ThreadStart action = () =>
                              {
                                  b.Await();
                                  b.Await(Delays.Small);
                                  b.Await();
                                  b.Await(Delays.Small);
                              };
     ThreadManager.StartAndAssertRegistered("T", action, action);
     ThreadManager.JoinAndVerify();
 }
コード例 #27
0
 private static void AwaitOrTimedAwait(bool isTimed, CyclicBarrier c)
 {
     if (isTimed) c.Await(Delays.Long);
     else c.Await();
 }
コード例 #28
0
        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();
        }
コード例 #29
0
 internal SynchedLoop(long iters, SynchedInteger obj, CyclicBarrier b)
 {
     this.iters = iters;
     this.obj = obj;
     this.barrier = b;
     obj.set_Renamed(CASLoops.rng.next());
 }
コード例 #30
0
 public void PartiesReturnsTheValueGiveinInConstructor([Values(1,5)] int parties)
 {
     CyclicBarrier b = new CyclicBarrier(parties);
     Assert.AreEqual(parties, b.Parties);
     Assert.AreEqual(0, b.NumberOfWaitingParties);
 }
コード例 #31
0
 public void SuppliedRunnableIsRunAtBarries()
 {
     var runnable = MockRepository.GenerateStub<IRunnable>();
     CyclicBarrier b = new CyclicBarrier(1, runnable);
     Assert.AreEqual(1, b.Parties);
     Assert.AreEqual(0, b.NumberOfWaitingParties);
     b.Await();
     b.Await();
     Assert.AreEqual(0, b.NumberOfWaitingParties);
     runnable.AssertWasCalled(r => r.Run(), m => m.Repeat.Twice());
 }
コード例 #32
0
 internal static void runCalibration(int n, long nms)
 {
     long now = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
     long endTime = now + nms;
     CyclicBarrier b = new CyclicBarrier(n + 1);
     totalIters.Value = (0);
     NonAtomicInteger a = new NonAtomicInteger();
     for (int j = 0; j < n; ++j)
         new SupportClass.ThreadClass(new System.Threading.ThreadStart(new NACalibrationLoop(endTime, a, b).Run)).Start();
     b.Await();
     b.Await();
     long ipt = totalIters.Value / n;
     if (ipt > loopIters[n])
         loopIters[n] = ipt;
     if (sum.Value == 0)
         System.Console.Out.Write(" ");
 }
コード例 #33
0
 public void SuppliedActionIsRunAtBarries()
 {
     var action = MockRepository.GenerateStub<Action>();
     CyclicBarrier b = new CyclicBarrier(1, action);
     Assert.AreEqual(1, b.Parties);
     Assert.AreEqual(0, b.NumberOfWaitingParties);
     b.Await();
     b.Await(Delays.Small);
     Assert.AreEqual(0, b.NumberOfWaitingParties);
     action.AssertWasCalled(a => a(), m => m.Repeat.Twice());
 }
コード例 #34
0
 internal AtomicLoop(long iters, AtomicInteger obj, CyclicBarrier b)
 {
     this.iters = iters;
     this.obj = obj;
     this.barrier = b;
     obj.Value = (CASLoops.rng.next());
 }
コード例 #35
0
 public void SinglePartyBarrierTriggersAfterSingleAwait([Values(true, false)] bool isTimed)
 {
     CyclicBarrier b = new CyclicBarrier(1);
     Assert.AreEqual(1, b.Parties);
     Assert.AreEqual(0, b.NumberOfWaitingParties);
     if (isTimed)
     {
         b.Await(Delays.Small);
         b.Await(Delays.Small);
     }
     else
     {
         b.Await();
         b.Await();
     }
     Assert.AreEqual(0, b.NumberOfWaitingParties);
 }
コード例 #36
0
 internal NonAtomicLoop(long iters, NonAtomicInteger obj, CyclicBarrier b)
 {
     this.iters = iters;
     this.obj = obj;
     this.barrier = b;
     obj.set_Renamed(CASLoops.rng.next());
 }
コード例 #37
0
 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();
 }
コード例 #38
0
 internal VolatileLoop(long iters, VolatileInteger obj, CyclicBarrier b)
 {
     this.iters = iters;
     this.obj = obj;
     this.barrier = b;
     obj.set_Renamed(CASLoops.rng.next());
 }
コード例 #39
0
        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();
        }