Example #1
0
        public void Test_SingleEntranceTimer_OnlyOneRunningInstance()
        {
            int                 parallism = 3;
            DataBag             db        = new DataBag();
            Action              action    = db.RunUntilCancelled;
            SingleEntranceTimer timer     = new SingleEntranceTimer(action, TimeSpan.FromMilliseconds(2));

            Assert.IsFalse(timer.Started);
            List <Task> tasks = Enumerable.Range(0, parallism).Select(i => timer.ForceFire()).ToList();

            SpinWait spinWait = new SpinWait();

            while (timer.NumFired < parallism || db.NumRun == 0)
            {
                spinWait.SpinOnce();
            }
            db.Cancelled = true;

            Task.WhenAll(tasks).Wait();

            Assert.AreEqual(1, db.NumRun);
            Assert.AreEqual(1, timer.NumExecuted);
            Assert.AreEqual(parallism, timer.NumFired);
            timer.Dispose();
        }
Example #2
0
        public void Test_SingleEntranceTimer_MaxRunTimeLimitation()
        {
            int                 parallism = 10;
            int                 runLimit  = 3;
            DataBag             db        = new DataBag();
            Action              action    = db.RunIfNotCancelled;
            SingleEntranceTimer timer     = new SingleEntranceTimer(action, TimeSpan.FromMilliseconds(2), maxExecutionLimit: runLimit);

            Assert.IsFalse(timer.Started);
            for (int i = 0; i < parallism; i++)
            {
                timer.ForceFire().Wait();
            }

            db.Cancelled = true;

            Assert.AreEqual(runLimit, db.NumRun);
            Assert.AreEqual(runLimit, timer.NumExecuted);
            Assert.AreEqual(parallism, timer.NumFired);
            timer.Dispose();
        }