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 GetSystemInfo_ResultIsNotNull()
        {
            // Arrange
            ITimeProvider timeProvider = new UTCTimeProvider();
            IMachineNameProvider machineNameProvider = new EnvironmentMachineNameProvider();

            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();
            var agentControlDefinition = new AgentControlDefinition { AgentIsEnabled = true, CheckIntervalInSeconds = 60 };
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            IProcessorStatusProvider processorStatusProvider = new ProcessorStatusProvider();
            IMemoryUnitConverter memoryUnitConverter = new MemoryUnitConverter();
            ISystemMemoryStatusProvider systemMemoryStatusProvider = new SystemMemoryStatusProvider(memoryUnitConverter);
            ILogicalDiscInstanceNameProvider logicalDiscInstanceNameProvider = new LogicalDiscInstanceNameProvider();
            ISystemStorageStatusProvider systemStorageStatusProvider = new SystemStorageStatusProvider(logicalDiscInstanceNameProvider);
            ISystemPerformanceDataProvider systemPerformanceDataProvider = new SystemPerformanceDataProvider(
                agentControlDefinitionProvider.Object, processorStatusProvider, systemMemoryStatusProvider, systemStorageStatusProvider);

            var httpStatusCodeFetcher = new Mock<IHttpStatusCodeFetcher>();

            IHttpStatusCodeCheckResultProvider httpStatusCodeCheckResultProvider = new HttpStatusCodeCheckResultProvider(
                agentControlDefinitionProvider.Object, httpStatusCodeFetcher.Object);

            var systemInformationProvider = new SystemInformationProvider(
                timeProvider, machineNameProvider, systemPerformanceDataProvider, httpStatusCodeCheckResultProvider);

            // Act
            var result = systemInformationProvider.GetSystemInfo();

            // Assert
            Assert.IsNotNull(result);
        }
        public void Dispose_TimerIsNotExecutedAfterDisposeHasBeenCalled()
        {
            // Arrange
            int newCheckIntervalInSeconds = 1;
            int waitTimeInMilliseconds = (newCheckIntervalInSeconds * 1000) * 5;
            var agentControlDefinitionWithReducedInterval = new AgentControlDefinition
                {
                    AgentIsEnabled = true,
                    Hostaddress = "127.0.0.1",
                    Hostname = "www.example.com",
                    CheckIntervalInSeconds = newCheckIntervalInSeconds,
                    SystemInformationSenderPath = "/api/systeminformation"
                };

            var agentControlDefinitionAccessor = new Mock<IAgentControlDefinitionAccessor>();
            agentControlDefinitionAccessor.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinitionWithReducedInterval);

            var agentControlDefinitionProvider = new AgentControlDefinitionProvider(agentControlDefinitionAccessor.Object);

            // Act
            agentControlDefinitionProvider.Dispose();
            Thread.Sleep(waitTimeInMilliseconds);

            // Assert
            agentControlDefinitionAccessor.Verify(a => a.GetControlDefinition(), Times.AtMostOnce());
        }
        private void UpdateControlDefinition()
        {
            this.controlDefinition = this.agentControlDefinitionAccessor.GetControlDefinition();

            // update the check interval
            if (this.controlDefinition != null && this.controlDefinition.CheckIntervalInSeconds > 0)
            {
                var timerStartTime = new TimeSpan(0, 0, this.controlDefinition.CheckIntervalInSeconds);
                var timerInterval = new TimeSpan(0, 0, 0, this.controlDefinition.CheckIntervalInSeconds);
                this.timer.Change(timerStartTime, timerInterval);
            }
        }
        public void GetConfiguration_AgentControlDefinitionProviderReturnsAgentConfiguration_ResultIsNotNull()
        {
            // Arrange
            var agentControlDefinition = new AgentControlDefinition
                {
                    AgentIsEnabled = true,
                    Hostaddress = "127.0.0.1",
                    Hostname = "www.example.com",
                    CheckIntervalInSeconds = 30,
                    SystemInformationSenderPath = "/api/systeminformation"
                };

            var agentControlDefinitionProvider = new Mock<IAgentControlDefinitionProvider>();
            agentControlDefinitionProvider.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);

            var systemInformationSenderConfigurationProvider = new RESTBasedSystemInformationSenderConfigurationProvider(agentControlDefinitionProvider.Object);

            // Act
            var result = systemInformationSenderConfigurationProvider.GetConfiguration();

            // Assert
            Assert.IsNotNull(result);
        }
        public void GetControlDefinition_ReturnsTheSameAgentControlDefinitionThatIsReturnedByTheAgentControlDefinitionAccessor()
        {
            // Arrange
            var agentControlDefinition = new AgentControlDefinition
            {
                AgentIsEnabled = true,
                Hostaddress = "127.0.0.1",
                Hostname = "www.example.com",
                CheckIntervalInSeconds = 10,
                SystemInformationSenderPath = "/api/systeminformation"
            };
            var agentControlDefinitionAccessor = new Mock<IAgentControlDefinitionAccessor>();
            agentControlDefinitionAccessor.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinition);
            var agentControlDefinitionProvider = new AgentControlDefinitionProvider(agentControlDefinitionAccessor.Object);
            Thread.Sleep(2000);

            // Act
            var result = agentControlDefinitionProvider.GetControlDefinition();

            // Assert
            Assert.AreEqual(agentControlDefinition, result);
        }
        public void Timer_IntervalIsChangedToOnceSecond_GetControlDefinitionIsCalledEverySecond()
        {
            // Arrange
            int newCheckIntervalInSeconds = 1;
            int waitTimeInSeconds = AgentControlDefinitionProvider.DefaultCheckIntervalInSeconds;
            int expectedNumberOfTimesGetControlDefinitionIsCalled = (int)((double)waitTimeInSeconds / newCheckIntervalInSeconds);

            int waitTimeInMilliseconds = waitTimeInSeconds * 1000;

            var agentControlDefinitionWithReducedInterval = new AgentControlDefinition
            {
                AgentIsEnabled = true,
                Hostaddress = "127.0.0.1",
                Hostname = "www.example.com",
                CheckIntervalInSeconds = newCheckIntervalInSeconds,
                SystemInformationSenderPath = "/api/systeminformation"
            };

            var agentControlDefinitionAccessor = new Mock<IAgentControlDefinitionAccessor>();
            agentControlDefinitionAccessor.Setup(a => a.GetControlDefinition()).Returns(agentControlDefinitionWithReducedInterval);

            // Act
            var agentControlDefinitionProvider = new AgentControlDefinitionProvider(agentControlDefinitionAccessor.Object);
            Thread.Sleep(waitTimeInMilliseconds);

            // Assert
            agentControlDefinitionAccessor.Verify(a => a.GetControlDefinition(), Times.AtLeast(expectedNumberOfTimesGetControlDefinitionIsCalled));
        }
        public void GetControlDefinition_ResponseData_IsReturned()
        {
            // Arrange
            var serviceConfiguration = new AgentControlDefinitionServiceConfiguration { Hostaddress = "127.0.0.0:8181", Hostname = "localhost", ResourcePath = "/api/AgentControlDefinition" };

            var machineNameProvider = new Mock<IMachineNameProvider>();

            var responseData = new AgentControlDefinition
                {
                    AgentIsEnabled = true,
                    Hostaddress = "127.0.0.1",
                    Hostname = "www.example.com",
                    CheckIntervalInSeconds = 1,
                    SystemInformationSenderPath = Guid.NewGuid().ToString()
                };

            var response = new Mock<IRestResponse<AgentControlDefinition>>();
            response.Setup(r => r.Data).Returns(responseData);

            var restClient = new Mock<IRestClient>();
            restClient.Setup(c => c.Execute<AgentControlDefinition>(It.IsAny<IRestRequest>())).Returns(response.Object);
            var request = new Mock<IRestRequest>();

            var configurationServiceUrlProvider = new Mock<IAgentControlDefinitionServiceUrlProvider>();
            configurationServiceUrlProvider.Setup(c => c.GetServiceConfiguration()).Returns(serviceConfiguration);

            var restClientFactory = new Mock<IRESTClientFactory>();
            restClientFactory.Setup(r => r.GetRESTClient(It.IsAny<string>())).Returns(restClient.Object);

            var requestFactory = new Mock<IRESTRequestFactory>();
            requestFactory.Setup(f => f.CreateGetRequest(It.IsAny<string>(), It.IsAny<string>())).Returns(request.Object);

            var agentControlDefinitionAccessor = new AgentControlDefinitionAccessor(
                configurationServiceUrlProvider.Object, machineNameProvider.Object, restClientFactory.Object, requestFactory.Object);

            // Act
            var result = agentControlDefinitionAccessor.GetControlDefinition();

            // Assert
            Assert.AreEqual(responseData, result);
        }
        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));
        }