public void Constructor_AllParametersAreSet_ObjectIsInstantiated()
        {
            // Arrange
            var agentCoordinationServiceFactory = new Mock<IAgentCoordinationServiceFactory>();
            var messageQueueFeederFactory = new Mock<IMessageQueueFeederFactory>();
            var messageQueueWorkerFactory = new Mock<IMessageQueueWorkerFactory>();
            var messageQueueProvider = new Mock<IMessageQueueProvider<SystemInformation>>();
            var messageQueuePersistence = new Mock<IMessageQueuePersistence<SystemInformation>>();

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

            // Assert
            Assert.IsNotNull(systemInformationDispatchingService);
        }
        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 Stop_MessageQueueWorker_StopIsCalled()
        {
            // Arrange
            // Arrange
            var agentCoordinationService = new Mock<IAgentCoordinationService>();
            var messageQueueFeeder = new Mock<IMessageQueueFeeder>();
            var messageQueueWorker = new Mock<IMessageQueueWorker>();

            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.Object);

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

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

            var messageQueueProvider = new Mock<IMessageQueueProvider<SystemInformation>>();
            messageQueueProvider.Setup(m => m.WorkQueue).Returns(workQueue.Object);
            messageQueueProvider.Setup(m => m.ErrorQueue).Returns(errorQueue.Object);

            var messageQueuePersistence = new Mock<IMessageQueuePersistence<SystemInformation>>();

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

            // Act
            systemInformationDispatchingService.Stop();

            // Assert
            messageQueueWorker.Verify(f => f.Stop(), Times.Once());
        }