public void WorkerIsStarted_QueueIsEmpty_WorkerKeepsPollingForItemsInQueue() { // Arrange int maxRuntime = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 5; var workQueue = new Mock<IMessageQueue<SystemInformation>>(); workQueue.Setup(q => q.IsEmpty()).Returns(true); SystemInformationQueueItem queueItem = null; workQueue.Setup(q => q.Dequeue()).Returns(queueItem); var errorQueue = new Mock<IMessageQueue<SystemInformation>>(); var systemInformationSender = new Mock<ISystemInformationSender>(); using (var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object)) { // Act var worker = new Task(messageQueueWorker.Start); worker.Start(); Task.WaitAll(new[] { worker }, maxRuntime); messageQueueWorker.Stop(); // Assert workQueue.Verify(q => q.Dequeue(), Times.Between(4, 5, Range.Inclusive)); } }
public void WorkerIsStarted_QueueIsEmpty_SendIsNotCalled() { // Arrange int maxRuntime = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 2; var workQueue = new Mock<IMessageQueue<SystemInformation>>(); workQueue.Setup(q => q.IsEmpty()).Returns(true); SystemInformationQueueItem queueItem = null; workQueue.Setup(q => q.Dequeue()).Returns(queueItem); var errorQueue = new Mock<IMessageQueue<SystemInformation>>(); var systemInformationSender = new Mock<ISystemInformationSender>(); var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object); // Act var worker = new Task(messageQueueWorker.Start); worker.Start(); Task.WaitAll(new[] { worker }, maxRuntime); messageQueueWorker.Stop(); // Assert systemInformationSender.Verify(s => s.Send(It.IsAny<SystemInformation>()), Times.Never()); }
public void Stop_ServiceIsNotStartet_StatusIsNotChanged() { // Arrange var systemInformationSender = new Mock<ISystemInformationSender>(); var workQueue = new Mock<IMessageQueue<SystemInformation>>(); workQueue.Setup(w => w.Dequeue()).Returns(new SystemInformationQueueItem(new SystemInformation())); var errorQueue = new Mock<IMessageQueue<SystemInformation>>(); using (var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object)) { var statusBefore = messageQueueWorker.GetStatus(); // Act messageQueueWorker.Stop(); var statusAfter = messageQueueWorker.GetStatus(); // Assert Assert.AreEqual(statusBefore, statusAfter); } }
public void Stop_StatusIsChangedToStopped() { // Arrange var systemInformationSender = new Mock<ISystemInformationSender>(); var workQueue = new Mock<IMessageQueue<SystemInformation>>(); workQueue.Setup(w => w.Dequeue()).Returns(new SystemInformationQueueItem(new SystemInformation())); var errorQueue = new Mock<IMessageQueue<SystemInformation>>(); using (var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object)) { var workerTaks = new Task(messageQueueWorker.Start); workerTaks.Start(); Thread.Sleep(500); // Act messageQueueWorker.Stop(); Thread.Sleep(500); // Assert Assert.AreEqual(ServiceStatus.Stopped, messageQueueWorker.GetStatus()); } }
public void Stop_EndsTheWorkerLoop() { // Arrange int timeToWaitBeforeStop = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 5; var workQueue = new Mock<IMessageQueue<SystemInformation>>(); workQueue.Setup(q => q.IsEmpty()).Returns(true); SystemInformationQueueItem queueItem = null; workQueue.Setup(q => q.Dequeue()).Returns(queueItem); var errorQueue = new Mock<IMessageQueue<SystemInformation>>(); var systemInformationSender = new Mock<ISystemInformationSender>(); using (var messageQueueWorker = new SystemInformationMessageQueueWorker(systemInformationSender.Object, workQueue.Object, errorQueue.Object)) { // Act var stopwatch = new Stopwatch(); stopwatch.Start(); var worker = new Task(messageQueueWorker.Start); worker.Start(); Thread.Sleep(timeToWaitBeforeStop); messageQueueWorker.Stop(); Task.WaitAll(new[] { worker }, timeToWaitBeforeStop); stopwatch.Stop(); // Assert Assert.LessOrEqual(stopwatch.ElapsedMilliseconds - timeToWaitBeforeStop, SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds + 100); } }