public void ResponderWorkerScheduler_many_MinThreads_created_but_should_be_idle()
        {
            if (PlatformHelpers.IsLinux())
            {
                Assert.Inconclusive("This test is not designed to run on Linux");
            }

            var minThreads = Environment.ProcessorCount * 4;
            using (var scheduler = new ResponderWorkerScheduler(minThreads, minThreads))
            {
                Assert.AreEqual(minThreads, scheduler.CurrentWorkerThreadCount);

                var timeout = TimeSpan.FromSeconds(5);
                var started = DateTime.UtcNow;
                double oldTime;
                var time = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
                do
                {
                    oldTime = time;
                    Thread.Sleep(1000);
                    time = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;

                    if (DateTime.UtcNow - started > timeout)
                        Assert.Fail("Worker threads should be idle, when no worker is added");

                } while (time - oldTime > 100);

                Assert.Pass();
            }
        }
 public void ResponderWorkerScheduler_number_of_MinThreads_created()
 {
     using (var scheduler = new ResponderWorkerScheduler(1, 2))
     {
         Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
     }
 }
        public void ResponderWorkerScheduler_many_MinThreads_created_but_should_be_idle()
        {
            if (PlatformHelpers.IsLinux())
            {
                Assert.Inconclusive("This test is not designed to run on Linux");
            }

            var minThreads = Environment.ProcessorCount * 4;

            using (var scheduler = new ResponderWorkerScheduler(minThreads, minThreads))
            {
                Assert.AreEqual(minThreads, scheduler.CurrentWorkerThreadCount);

                var    timeout = TimeSpan.FromSeconds(5);
                var    started = DateTime.UtcNow;
                double oldTime;
                var    time = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
                do
                {
                    oldTime = time;
                    Thread.Sleep(1000);
                    time = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;

                    if (DateTime.UtcNow - started > timeout)
                    {
                        Assert.Fail("Worker threads should be idle, when no worker is added");
                    }
                } while (time - oldTime > 100);

                Assert.Pass();
            }
        }
 public void ResponderWorkerScheduler_number_of_MinThreads_created()
 {
     using (var scheduler = new ResponderWorkerScheduler(1, 2))
     {
         Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
     }
 }
        public void ResponderWorkerScheduler_Dispose_kills_all_threads()
        {
            using (var scheduler = new ResponderWorkerScheduler(2, 2))
            {
                scheduler.Dispose();
                
                Thread.Sleep(30);

                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_Dispose_kills_all_threads()
        {
            using (var scheduler = new ResponderWorkerScheduler(2, 2))
            {
                scheduler.Dispose();

                Thread.Sleep(30);

                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_increased_when_busy()
        {
            var worker = new TestWorker(30);

            using (var scheduler = new ResponderWorkerScheduler(0, 1))
            {
                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);

                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_not_decreased_when_back_idle_before_MaxIdleTime()
        {
            var worker = new TestWorker(30);

            using (var scheduler = new ResponderWorkerScheduler(0, 1, TimeSpan.FromMilliseconds(100)))
            {
                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);

                worker.WaitCompleted(Timeout);
                Thread.Sleep(15);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentBusyThreadCount_decreased_when_back_idle()
        {
            var worker = new TestWorker(30);

            using (var scheduler = new ResponderWorkerScheduler(1, 1))
            {
                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);

                worker.WaitCompleted(Timeout);
                Thread.Sleep(15);
                Assert.AreEqual(0, scheduler.CurrentBusyThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_increased_twice()
        {
            using (var scheduler = new ResponderWorkerScheduler(0, 2, TimeSpan.FromMilliseconds(1)))
            {
                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);

                var worker1 = new TestWorker(10);
                scheduler.AddWorker(worker1, new TestMessage(), null);
                worker1.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);

                var worker2 = new TestWorker(10);
                scheduler.AddWorker(worker2, new TestMessage(), null);
                worker2.WaitStarted(Timeout);
                Assert.AreEqual(2, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentBusyThreadCount_increased_then_decreased_then_increased()
        {
            using (var scheduler = new ResponderWorkerScheduler(1, 1))
            {
                var worker1 = new TestWorker(30);
                scheduler.AddWorker(worker1, new TestMessage(), null);

                worker1.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);

                worker1.WaitCompleted(Timeout);
                Thread.Sleep(10);
                Assert.AreEqual(0, scheduler.CurrentBusyThreadCount);

                var worker2 = new TestWorker(30);
                scheduler.AddWorker(worker2, new TestMessage(), null);

                worker2.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_increased_twice()
        {
            using (var scheduler = new ResponderWorkerScheduler(0, 2, TimeSpan.FromMilliseconds(1)))
            {
                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);

                var worker1 = new TestWorker(10);
                scheduler.AddWorker(worker1, new TestMessage(), null);
                worker1.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);

                var worker2 = new TestWorker(10);
                scheduler.AddWorker(worker2, new TestMessage(), null);
                worker2.WaitStarted(Timeout);
                Assert.AreEqual(2, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_not_decreased_when_back_idle_before_MaxIdleTime()
        {
            var worker = new TestWorker(30);
            using (var scheduler = new ResponderWorkerScheduler(0, 1, TimeSpan.FromMilliseconds(100)))
            {
                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);

                worker.WaitCompleted(Timeout);
                Thread.Sleep(15);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentWorkerThreadCount_increased_when_busy()
        {
            var worker = new TestWorker(30);
            using (var scheduler = new ResponderWorkerScheduler(0, 1))
            {
                Assert.AreEqual(0, scheduler.CurrentWorkerThreadCount);

                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentWorkerThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentBusyThreadCount_increased_then_decreased_then_increased()
        {
            using (var scheduler = new ResponderWorkerScheduler(1, 1))
            {
                var worker1 = new TestWorker(30);
                scheduler.AddWorker(worker1, new TestMessage(), null);

                worker1.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);

                worker1.WaitCompleted(Timeout);
                Thread.Sleep(10);
                Assert.AreEqual(0, scheduler.CurrentBusyThreadCount);

                var worker2 = new TestWorker(30);
                scheduler.AddWorker(worker2, new TestMessage(), null);

                worker2.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);
            }
        }
        public void ResponderWorkerScheduler_CurrentBusyThreadCount_decreased_when_back_idle()
        {
            var worker = new TestWorker(30);
            using (var scheduler = new ResponderWorkerScheduler(1, 1))
            {
                scheduler.AddWorker(worker, new TestMessage(), null);

                worker.WaitStarted(Timeout);
                Assert.AreEqual(1, scheduler.CurrentBusyThreadCount);

                worker.WaitCompleted(Timeout);
                Thread.Sleep(15);
                Assert.AreEqual(0, scheduler.CurrentBusyThreadCount);
            }
        }