public void ProcessThreadList_GivenInvalidConfig_ExpectThreadListStopMonitoring()
        {
            var mockConfig = new Mock <IJobConfig>();

            mockConfig.Setup(o => o.Concurrency).Returns(3);
            var mockProcessFactory = new Mock <ITestProcessFactory>();

            var mockProcessThread1 = CreateMockProcessThread();
            var mockProcessThread2 = CreateMockProcessThread();
            var mockProcessThread3 = CreateMockProcessThread();

            var processThread1 = mockProcessThread1.Object;
            var processThread2 = mockProcessThread2.Object;
            var processThread3 = mockProcessThread3.Object;

            mockProcessFactory.SetupSequence(o => o.New())
            .Returns(mockProcessThread1.Object)
            .Returns(mockProcessThread2.Object)
            .Returns(mockProcessThread3.Object);

            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);

            list.UpdateConfig(null);
            list.Monitor();

            Assert.IsTrue(list.NeedUpdate);
            Assert.IsFalse(processThread1.IsAlive);
            Assert.IsFalse(processThread2.IsAlive);
            Assert.IsFalse(processThread3.IsAlive);
            mockProcessThread1.Verify(o => o.Start(), Times.Never);
            mockProcessThread2.Verify(o => o.Start(), Times.Never);
            mockProcessThread3.Verify(o => o.Start(), Times.Never);
        }
        public void ProcessThreadList_GivenConfig_ExpectNeedsUpdate()
        {
            var mockConfig     = new Mock <IJobConfig>();
            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, null);

            Assert.AreEqual(expectedConfig, list.Config);
            Assert.IsTrue(list.NeedUpdate);

            var newConfig = new Mock <IJobConfig>().Object;

            list.UpdateConfig(newConfig);
            Assert.AreEqual(newConfig, list.Config);
        }
        public void ProcessThreadList_GivenConfigHighConcurrency_ExpectLogicalProcessorCount()
        {
            var mockConfig         = new Mock <IJobConfig>();
            var mockProcessFactory = new Mock <ITestProcessFactory>();

            mockProcessFactory.Setup(o => o.New()).Returns(new Mock <IProcessThread>().Object);
            mockConfig.Setup(o => o.Concurrency).Returns(102);
            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);

            Assert.AreEqual(expectedConfig, list.Config);
            Assert.IsTrue(list.NeedUpdate);

            list.Monitor();
            mockProcessFactory.Verify(o => o.New(), Times.Exactly(Environment.ProcessorCount));
        }
Esempio n. 4
0
        public void ProcessThreadList_GivenKillCalled_ExpectAllWorkersKilled()
        {
            var mockConfig = new Mock <IJobConfig>();

            mockConfig.Setup(o => o.Concurrency).Returns(3);
            var mockProcessFactory = new Mock <ITestProcessFactory>();

            var mockProcessThread1 = CreateMockProcessThread();
            var mockProcessThread2 = CreateMockProcessThread();
            var mockProcessThread3 = CreateMockProcessThread();

            var processThread1 = mockProcessThread1.Object;
            var processThread2 = mockProcessThread2.Object;
            var processThread3 = mockProcessThread3.Object;

            mockProcessFactory.SetupSequence(o => o.New())
            .Returns(mockProcessThread1.Object)
            .Returns(mockProcessThread2.Object)
            .Returns(mockProcessThread3.Object);

            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);

            list.Monitor();

            Assert.IsFalse(list.NeedUpdate);
            Assert.IsTrue(processThread1.IsAlive);
            Assert.IsTrue(processThread2.IsAlive);
            Assert.IsTrue(processThread3.IsAlive);
            mockProcessThread1.Verify(o => o.Start(), Times.Once);
            mockProcessThread2.Verify(o => o.Start(), Times.Once);
            mockProcessThread3.Verify(o => o.Start(), Times.Once);

            list.Kill();

            mockProcessThread1.Verify(o => o.Kill(), Times.Once);
            mockProcessThread2.Verify(o => o.Kill(), Times.Once);
            mockProcessThread3.Verify(o => o.Kill(), Times.Once);

            // At this point a ProcessThread will restart it's process
            // do not expect Start() to be called again here, this logic is in ProcessThread.
        }
        public void ProcessThreadList_GivenDefaultConfig_ExpectNoWorker()
        {
            var mockConfig         = new Mock <IJobConfig>();
            var mockProcessFactory = new Mock <ITestProcessFactory>();

            var mockProcessThread = CreateMockProcessThread();

            var processThread = mockProcessThread.Object;

            mockProcessFactory.Setup(o => o.New()).Returns(mockProcessThread.Object);

            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);

            list.Monitor();

            Assert.IsTrue(list.NeedUpdate);
            Assert.IsFalse(processThread.IsAlive);
            mockProcessThread.Verify(o => o.Start(), Times.Never);
        }
        public void ProcessThreadList_GivenConfigConcurrent3_ExpectThreeWorkers()
        {
            var mockConfig = new Mock <IJobConfig>();

            mockConfig.Setup(o => o.Concurrency).Returns(3);
            var mockProcessFactory = new Mock <ITestProcessFactory>();

            var mockProcessThread1 = CreateMockProcessThread();
            var mockProcessThread2 = CreateMockProcessThread();
            var mockProcessThread3 = CreateMockProcessThread();

            var processThread1 = mockProcessThread1.Object;
            var processThread2 = mockProcessThread2.Object;
            var processThread3 = mockProcessThread3.Object;

            mockProcessFactory.SetupSequence(o => o.New())
            .Returns(mockProcessThread1.Object)
            .Returns(mockProcessThread2.Object)
            .Returns(mockProcessThread3.Object);

            var expectedConfig = mockConfig.Object;
            var list           = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);

            list.Monitor();

            Assert.IsFalse(list.NeedUpdate);
            Assert.IsTrue(processThread1.IsAlive);
            Assert.IsTrue(processThread2.IsAlive);
            Assert.IsTrue(processThread3.IsAlive);
            mockProcessThread1.Verify(o => o.Start(), Times.Once);
            mockProcessThread2.Verify(o => o.Start(), Times.Once);
            mockProcessThread3.Verify(o => o.Start(), Times.Once);

            foreach (var process in list)
            {
                Assert.IsTrue(process.IsAlive);
            }
        }
        public void ProcessThreadList_GivenWorkerDies_ExpectOnProcessDiedCalled()
        {
            var mockConfig         = new Mock <IJobConfig>();
            var mockProcessFactory = new Mock <ITestProcessFactory>();
            var process            = new ProcessThreadForTesting(mockConfig.Object);

            mockProcessFactory.Setup(o => o.New()).Returns(process);
            mockConfig.Setup(o => o.Concurrency).Returns(102);
            var expectedConfig            = mockConfig.Object;
            var list                      = new ProcessThreadListForTesting(expectedConfig, mockProcessFactory.Object);
            var lists_OnProcessDiedCalled = false;

            list.OnProcessDied += (config) => lists_OnProcessDiedCalled = true;

            Assert.AreEqual(expectedConfig, list.Config);
            Assert.IsTrue(list.NeedUpdate);

            list.Monitor();
            mockProcessFactory.Verify(o => o.New(), Times.Exactly(Environment.ProcessorCount));

            process.ForceProcessDiedEvent();

            Assert.IsTrue(lists_OnProcessDiedCalled);
        }