public void Dispose_EndsTheService()
        {
            // Arrange
            var maxDurationInMilliseconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds * 3;
            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();

            var agentControlDefinition = new AgentControlDefinition
            {
                AgentIsEnabled = true,
                Hostaddress = "127.0.0.1",
                Hostname = "www.example.com",
                CheckIntervalInSeconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds,
                SystemInformationSenderPath = "/api/systeminformation"
            };
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            var agentCoordinationService = new AgentCoordinationService(agentControlDefinitionProvider.Object, () => { }, () => { });

            // Act
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            var agentCoordinationServiceTask = new Task(agentCoordinationService.Start);
            agentCoordinationServiceTask.Start();
            Thread.Sleep(500);
            agentCoordinationService.Dispose();
            Task.WaitAll(new[] { agentCoordinationServiceTask }, maxDurationInMilliseconds);

            stopwatch.Stop();

            // Assert
            Assert.Less(stopwatch.ElapsedMilliseconds, maxDurationInMilliseconds);
        }
        public void Constructor_AllParametersAreSet_ObjectIsInstantiated()
        {
            // Arrange
            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();

            // Act
            var agentCoordinationService = new AgentCoordinationService(agentControlDefinitionProvider.Object, () => { }, () => { });

            // Assert
            Assert.IsNotNull(agentCoordinationService);
        }
        public void Start_AgentsAreDisabled_PauseCallbackIsCalled()
        {
            // Arrange
            bool pauseCallbackWasCalled = false;
            var runDurationInMilliseconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds * 1;
            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();

            var agentControlDefinition = new AgentControlDefinition
                {
                    AgentIsEnabled = false,
                    Hostaddress = "127.0.0.1",
                    Hostname = "www.example.com",
                    CheckIntervalInSeconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds,
                    SystemInformationSenderPath = "/api/systeminformation"
                };
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            var agentCoordinationService = new AgentCoordinationService(
                agentControlDefinitionProvider.Object,
                pauseCallback: () =>
                {
                    pauseCallbackWasCalled = true;
                },
                resumeCallback: () => { });

            // Act
            var agentCoordinationServiceTask = new Task(agentCoordinationService.Start);
            agentCoordinationServiceTask.Start();

            Task.WaitAll(new[] { agentCoordinationServiceTask }, runDurationInMilliseconds);

            // Assert
            Assert.IsTrue(pauseCallbackWasCalled);
        }
        public void Start_RunsForThreeIntervals_GetControlDefinitionIsCalledThreeTimes()
        {
            // Arrange
            var runDurationInMilliseconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds * 3;
            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();
            var agentControlDefinition = new AgentControlDefinition
                {
                    AgentIsEnabled = true,
                    Hostaddress = "127.0.0.1",
                    Hostname = "www.example.com",
                    CheckIntervalInSeconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds,
                    SystemInformationSenderPath = "/api/systeminformation"
                };
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            var agentCoordinationService = new AgentCoordinationService(agentControlDefinitionProvider.Object, () => { }, () => { });

            // Act
            var agentCoordinationServiceTask = new Task(agentCoordinationService.Start);
            agentCoordinationServiceTask.Start();

            Task.WaitAll(new[] { agentCoordinationServiceTask }, runDurationInMilliseconds);

            // Assert
            agentControlDefinitionProvider.Verify(a => a.GetControlDefinition(), Times.Between(3, 4, Range.Inclusive));
        }
        public void Start_GetControlDefinitionReturnsNull_PauseCallbackIsCalled()
        {
            // Arrange
            bool pauseCallbackWasCalled = false;
            var runDurationInMilliseconds = AgentCoordinationService.AgentControlDefinitionCheckIntervalInMilliseconds * 1;
            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();

            AgentControlDefinition agentControlDefinition = null;
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            var agentCoordinationService = new AgentCoordinationService(
                agentControlDefinitionProvider.Object,
                pauseCallback: () =>
                    {
                        pauseCallbackWasCalled = true;
                    },
                resumeCallback: () => { });

            // Act
            var agentCoordinationServiceTask = new Task(agentCoordinationService.Start);
            agentCoordinationServiceTask.Start();

            Task.WaitAll(new[] { agentCoordinationServiceTask }, runDurationInMilliseconds);

            // Assert
            Assert.IsTrue(pauseCallbackWasCalled);
        }