public void Constructor_AllParametersAreSet_ObjectIsInstantiated()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            // Act
            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);

            // Assert
            Assert.IsNotNull(messageQueueFeeder);
        }
        public void Dispose_ServiceIsStopped()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();
            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);
            var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
            messageQueueFeederTask.Start();
            Thread.Sleep(500);

            // Act
            messageQueueFeeder.Dispose();
            Task.WaitAll(messageQueueFeederTask);

            // Assert
            Assert.AreEqual(ServiceStatus.Stopped, messageQueueFeeder.GetStatus());
        }
        public void RunFor10Seconds_SendFailsForAllItems_DispatcherStopsOnlyIfTheQueueIsEmptyAndAllRetryAttempsHaveFailed()
        {
            // Arrange
            int runtimeInMilliseconds = 10 * 1000;
            int itemsReturnedFromSystemInformationProvider = 0;
            int attemptsToSend = 0;

            // prepare system information provider
            var provider = new Mock<ISystemInformationProvider>();
            provider.Setup(p => p.GetSystemInfo()).Returns(() =>
                {
                    itemsReturnedFromSystemInformationProvider++;
                    return new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
                });

            // prepare sender
            var sender = new Mock<ISystemInformationSender>();
            sender.Setup(s => s.Send(It.IsAny<SystemInformation>())).Callback(() => { attemptsToSend++; }).Throws(new SendSystemInformationFailedException("Send failed.", null));

            IMessageQueue<SystemInformation> workQueue = new SystemInformationMessageQueue();
            IMessageQueue<SystemInformation> errorQueue = new SystemInformationMessageQueue();
            IMessageQueueProvider<SystemInformation> messageQueueProvider = new SystemInformationMessageQueueProvider(workQueue, errorQueue);

            IMessageQueueFeeder messageQueueFeeder = new SystemInformationMessageQueueFeeder(provider.Object, workQueue);
            IMessageQueueWorker messageQueueWorker = new SystemInformationMessageQueueWorker(sender.Object, workQueue, errorQueue);

            var agentCoordinationService = new Mock<IAgentCoordinationService>();
            var agentCoordinationServiceFactory = new Mock<IAgentCoordinationServiceFactory>();
            agentCoordinationServiceFactory.Setup(f => f.GetAgentCoordinationService(It.IsAny<Action>(), It.IsAny<Action>())).Returns(
                agentCoordinationService.Object);

            var messageQueueFeederFactory = new Mock<IMessageQueueFeederFactory>();
            messageQueueFeederFactory.Setup(f => f.GetMessageQueueFeeder()).Returns(messageQueueFeeder);

            var messageQueueWorkerFactory = new Mock<IMessageQueueWorkerFactory>();
            messageQueueWorkerFactory.Setup(f => f.GetMessageQueueWorker()).Returns(messageQueueWorker);

            IMessageQueuePersistence<SystemInformation> messageQueuePersistence =
                new JSONSystemInformationMessageQueuePersistence(this.jsonMessageQueuePersistenceConfigurationProvider, this.encodingProvider);

            var systemInformationDispatchingService = new SystemInformationDispatchingService(
                agentCoordinationServiceFactory.Object,
                messageQueueFeederFactory.Object,
                messageQueueWorkerFactory.Object,
                messageQueueProvider,
                messageQueuePersistence);

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

            var dispatcher = new Task(systemInformationDispatchingService.Start);
            dispatcher.Start();

            Thread.Sleep(runtimeInMilliseconds);
            systemInformationDispatchingService.Stop();

            Task.WaitAll(new[] { dispatcher });

            stopwatch.Stop();

            // Assert
            int queueSize = workQueue.GetSize();
            Console.WriteLine(
                "After a runtime of {0} milliseconds the dispatcher has been stopped with {1} items in queue. It took {2} milliseconds until the queue worker stopped sending out all queue items (Attempts To Send: {3}).",
                runtimeInMilliseconds,
                itemsReturnedFromSystemInformationProvider,
                stopwatch.ElapsedMilliseconds,
                attemptsToSend);

            Assert.AreEqual(0, queueSize);
        }
        public void GetStatus_ServiceIsNotStarted_ResultIs_Stopped()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);

            // Act
            var result = messageQueueFeeder.GetStatus();

            // Assert
            Assert.AreEqual(ServiceStatus.Stopped, result);
        }
        public void Stop_ServiceIsStopped_StatusIsNotChanged()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);
            var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
            messageQueueFeederTask.Start();
            messageQueueFeeder.Stop();

            var statusBeforeStop = messageQueueFeeder.GetStatus();

            // Act
            messageQueueFeeder.Stop();
            var statusAfterStop = messageQueueFeeder.GetStatus();

            // Assert
            Assert.AreEqual(statusBeforeStop, statusAfterStop);
        }
        public void Start_SystemInformationProviderReturnsSystemInformation_SystemInformationIsAddedToQueue()
        {
            // Arrange
            int durationInMilliseconds = SystemInformationMessageQueueFeeder.SendIntervalInMilliseconds * 2;

            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            systemInformationProvider.Setup(s => s.GetSystemInfo()).Returns(() => new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow });

            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                // Act
                var feederTask = new Task(messageQueueFeeder.Start);
                feederTask.Start();
                Task.WaitAll(new[] { feederTask }, durationInMilliseconds);
                messageQueueFeeder.Stop();

                // Assert
                workQueue.Verify(s => s.Enqueue(It.IsAny<SystemInformationQueueItem>()), Times.AtLeastOnce());
            }
        }
        public void Stop_EndsARunningService()
        {
            // Arrange
            int durationInMilliseconds = SystemInformationMessageQueueFeeder.SendIntervalInMilliseconds * 3;

            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);
            var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
            messageQueueFeederTask.Start();

            // Act
            Thread.Sleep(durationInMilliseconds);

            messageQueueFeeder.Stop();

            // Assert
            Assert.AreEqual(ServiceStatus.Stopped, messageQueueFeeder.GetStatus());
        }
        public void Start_RunFor3Intervals_SystemInfoIsPulledAtMostThreeTimes()
        {
            // Arrange
            int durationInMilliseconds = SystemInformationMessageQueueFeeder.SendIntervalInMilliseconds * 3;

            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                // Act
                var feederTask = new Task(messageQueueFeeder.Start);
                feederTask.Start();
                Task.WaitAll(new[] { feederTask }, durationInMilliseconds);
                messageQueueFeeder.Stop();

                // Assert
                systemInformationProvider.Verify(s => s.GetSystemInfo(), Times.AtMost(3));
            }
        }
        public void Start_SystemInformationProviderReturnsNull_InfoIsNotQueued()
        {
            // Arrange
            int durationInMilliseconds = SystemInformationMessageQueueFeeder.SendIntervalInMilliseconds * 2;

            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            SystemInformation systemInformation = null;
            systemInformationProvider.Setup(s => s.GetSystemInfo()).Returns(systemInformation);

            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                // Act
                var feederTaks = new Task(messageQueueFeeder.Start);
                feederTaks.Start();
                Task.WaitAll(new[] { feederTaks }, durationInMilliseconds);
                messageQueueFeeder.Stop();

                // Assert
                workQueue.Verify(s => s.Enqueue(It.IsAny<SystemInformationQueueItem>()), Times.Never());
            }
        }
        public void Resume_ServiceIsStopped_StatusIsStillStopped()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
                messageQueueFeederTask.Start();
                messageQueueFeeder.Stop();

                // Act
                messageQueueFeeder.Resume();

                // Assert
                Assert.AreEqual(ServiceStatus.Stopped, messageQueueFeeder.GetStatus());
            }
        }
        public void Resume_ServiceIsRunning_StatusIsNotChanged()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
                messageQueueFeederTask.Start();

                var statusBeforeResume = messageQueueFeeder.GetStatus();

                // Act
                messageQueueFeeder.Resume();
                var statusAfterResume = messageQueueFeeder.GetStatus();

                // Assert
                Assert.AreEqual(statusBeforeResume, statusAfterResume);
            }
        }
        public void Resume_ServiceIsPaused_StatusIsChangedToRunning()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
                messageQueueFeederTask.Start();

                Thread.Sleep(500);
                messageQueueFeeder.Pause();

                // Act
                Thread.Sleep(500);
                messageQueueFeeder.Resume();

                // Assert
                Assert.AreEqual(ServiceStatus.Running, messageQueueFeeder.GetStatus());
            }
        }
        public void Pause_ServiceIsRunning_StatusIsChangedToPaused()
        {
            // Arrange
            int durationInMilliseconds = SystemInformationMessageQueueFeeder.SendIntervalInMilliseconds * 3;

            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            using (var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object))
            {
                var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
                messageQueueFeederTask.Start();
                Thread.Sleep(durationInMilliseconds);

                // Act
                messageQueueFeeder.Pause();
                var statusAfterPause = messageQueueFeeder.GetStatus();

                // Assert
                Assert.AreEqual(ServiceStatus.Paused, statusAfterPause);
            }
        }
        public void GetStatus_ServiceIsStarted_ResultIs_Running()
        {
            // Arrange
            var systemInformationProvider = new Mock<ISystemInformationProvider>();
            var workQueue = new Mock<IMessageQueue<SystemInformation>>();

            var messageQueueFeeder = new SystemInformationMessageQueueFeeder(systemInformationProvider.Object, workQueue.Object);
            var messageQueueFeederTask = new Task(messageQueueFeeder.Start);
            messageQueueFeederTask.Start();

            Thread.Sleep(3000);

            // Act
            var result = messageQueueFeeder.GetStatus();

            // Assert
            Assert.AreEqual(ServiceStatus.Running, result);
        }