public void IdleTrigger_WhenFired_ShouldCheckIdleTimeoutEveryTime()
        {
            var idleTrigger = new StubIdleTrigger();
            int checkCount  = 0;

            using (var scheduler = new STATaskScheduler(1, idleTrigger))
            {
                idleTrigger.FireCheckIdle();
                idleTrigger.FireCheckIdle();
                checkCount = idleTrigger.IdleTimeoutCheckCount;
            }

            Assert.AreEqual(2, checkCount);
        }
        public void Scheduler_WhenThreadNotIdleForTimeout_ShouldNotThreadFromPool()
        {
            var idleTrigger          = new StubIdleTrigger(10000);
            int numThreadsBeforeIdle = -1;
            int numThreadsAfterIdle  = -1;
            var evt = new ManualResetEventSlim();

            using (var scheduler = new STATaskScheduler(1, idleTrigger))
            {
                Task t = new Task(() =>
                {
                    evt.Wait();
                });

                t.Start(scheduler);

                numThreadsBeforeIdle = scheduler.GetThreadCount();

                idleTrigger.FireCheckIdle();

                numThreadsAfterIdle = scheduler.GetThreadCount();

                evt.Set();
            }

            Assert.AreEqual(1, numThreadsBeforeIdle);
            Assert.AreEqual(1, numThreadsAfterIdle);
        }
        public void Scheduler_WhenThreadRunsTask_ShouldUpdateLastUsed()
        {
            const int IDLE_TIMEOUT        = 10;
            var       idleTrigger         = new StubIdleTrigger(IDLE_TIMEOUT);
            int       numThreadsAfterIdle = -1;
            var       evt = new ManualResetEventSlim();

            using (var scheduler = new STATaskScheduler(1, idleTrigger))
            {
                Task t1 = new Task(() =>
                {
                });

                t1.Start(scheduler);

                Thread.Sleep(2 * IDLE_TIMEOUT);

                Task t2 = new Task(() =>
                {
                });

                t2.Start(scheduler);

                t2.Wait();

                idleTrigger.FireCheckIdle();

                numThreadsAfterIdle = scheduler.GetThreadCount();

                evt.Set();
            }

            Assert.AreEqual(1, numThreadsAfterIdle);
        }
        public void Scheduler_WhenThreadIdleForTimeout_ShouldRemoveThreadFromPool()
        {
            var idleTrigger          = new StubIdleTrigger(0);
            int numThreadsBeforeIdle = -1;
            int numThreadsAfterIdle  = -1;

            using (var scheduler = new STATaskScheduler(1, idleTrigger))
            {
                Task t = new Task(() =>
                {
                });

                t.Start(scheduler);

                t.Wait();

                numThreadsBeforeIdle = scheduler.GetThreadCount();

                idleTrigger.FireCheckIdle();

                numThreadsAfterIdle = scheduler.GetThreadCount();
            }

            Assert.AreEqual(1, numThreadsBeforeIdle);
            Assert.AreEqual(0, numThreadsAfterIdle);
        }
        public void Ctor_WhenPassedAnIdleTrigger_ShouldSubscribeToCheckIdleEvent()
        {
            var  idleTrigger = new StubIdleTrigger();
            bool subscribed  = false;

            using (var scheduler = new STATaskScheduler(1, idleTrigger))
            {
                subscribed = idleTrigger.FireCheckIdle();
            }

            Assert.IsTrue(subscribed);
        }