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 EnqueuCount_IsZeroWhenToObjectIsFirstCreated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.AreEqual(0, systemInformationQueueItem.EnqueuCount);
        }
Пример #4
0
        public void EnqueuCount_IsZeroWhenToObjectIsFirstCreated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.AreEqual(0, systemInformationQueueItem.EnqueuCount);
        }
        public void Constructor_SuppliedItemIsSet_ObjectIsInstantiated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.IsNotNull(systemInformationQueueItem);
        }
Пример #6
0
        public void Constructor_SuppliedItemIsSet_ObjectIsInstantiated()
        {
            // Arrange
            var item = new SystemInformation();

            // Act
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Assert
            Assert.IsNotNull(systemInformationQueueItem);
        }
Пример #7
0
        public void Item_Get_ReturnsTheSameItemThatHasBeenAssignedViaTheConstructor()
        {
            // Arrange
            var assignedItem = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(assignedItem);

            // Act
            var result = systemInformationQueueItem.Item;

            // Assert
            Assert.AreEqual(assignedItem, result);
        }
Пример #8
0
        public void ToString_Contains_Item()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem);

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsTrue(result.Contains(object1.Item.ToString()));
        }
Пример #9
0
        public void Equals_SuppliedObjectIsOfOtherType_ResultIsFalse()
        {
            // Arrange
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow });
            var object2 = new object();

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsFalse(result);
        }
Пример #10
0
        public void ToString_DoesNotContain_UsedMemoryInGB()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem) { EnqueuCount = 15 };

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsFalse(result.Contains(object1.EnqueuCount.ToString()));
        }
        public void Equals_SuppliedObjectIsOfOtherType_ResultIsFalse()
        {
            // Arrange
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow });
            var object2 = new object();

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsFalse(result);
        }
        public void EnqueuCount_CanBeSet_ValueIsTheValueThatHasBeenAssigned()
        {
            // Arrange
            var newEnqueueCount = 5;
            var item = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Act
            systemInformationQueueItem.EnqueuCount = newEnqueueCount;

            // Assert
            Assert.AreEqual(newEnqueueCount, systemInformationQueueItem.EnqueuCount);
        }
Пример #13
0
        public void Equals_TwoIdenticalInitializedObjects_ResultIsTrue()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp });
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp });

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsTrue(result);
        }
Пример #14
0
        public void EnqueuCount_CanBeSet_ValueIsTheValueThatHasBeenAssigned()
        {
            // Arrange
            var newEnqueueCount = 5;
            var item = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(item);

            // Act
            systemInformationQueueItem.EnqueuCount = newEnqueueCount;

            // Assert
            Assert.AreEqual(newEnqueueCount, systemInformationQueueItem.EnqueuCount);
        }
Пример #15
0
        public void GetHashCode_TwoDistinctObjects_HashCodesAreDifferent()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName + "A", Timestamp = timeStamp });
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName + "B", Timestamp = timeStamp });

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreNotEqual(hashCodeObject1, hashCodeObject2);
        }
        public void QueueIsFilled_SendCausesExceptionWhichJustifiesARetry_RetryCountHasBeenExceeded_ItemIsAddedToFailedRequestQueue()
        {
            // Arrange
            int maxRuntime = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 2;

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

            workQueue.Setup(q => q.IsEmpty()).Returns(false);

            bool itemHasBeenAccessed = false;
            var  systemInfo          = new SystemInformation {
                MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow
            };
            var queueItem = new SystemInformationQueueItem(systemInfo)
            {
                EnqueuCount = SystemInformationMessageQueueWorker.MaxRetryCount
            };

            workQueue.Setup(q => q.Dequeue()).Returns(() =>
            {
                if (!itemHasBeenAccessed)
                {
                    itemHasBeenAccessed = true;
                    return(queueItem);
                }

                return(null);
            });

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

            systemInformationSender.Setup(s => s.Send(systemInfo)).Throws(
                new SendSystemInformationFailedException("Some minor exception which justifies a retry", null));

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

                // Assert
                workQueue.Verify(q => q.Enqueue(queueItem), Times.Never());
                errorQueue.Verify(q => q.Enqueue(queueItem), Times.Once());
            }
        }
Пример #17
0
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            int numberOfItems = 10000;
            var items = TestUtilities.GetSystemInformationObjects(numberOfItems);
            var hashCodes = new Dictionary<int, SystemInformationQueueItem>();

            for (var i = 0; i < numberOfItems; i++)
            {
                // Act
                var object1 = new SystemInformationQueueItem(items[i]);

                int generatedHashCode = object1.GetHashCode();

                // Assert
                Assert.IsFalse(hashCodes.ContainsKey(generatedHashCode));
                hashCodes.Add(generatedHashCode, object1);
            }
        }
Пример #18
0
        public void Enqueue_QueueItemCounterIsIncreasedByOne()
        {
            // Arrange
            var item = new SystemInformationQueueItem(new SystemInformation {
                MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow
            });
            int previousCount = item.EnqueuCount;

            var queue = new SystemInformationMessageQueue();

            queue.Enqueue(item);

            // Act
            var dequeuedItem = queue.Dequeue();

            // Assert
            Assert.AreEqual(previousCount + 1, dequeuedItem.EnqueuCount);
        }
Пример #19
0
        public void Equals_TwoIdenticalObjects_WithDifferentEnqueueCountValues_ResultIsTrue()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 1
                };
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 2
                };

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsTrue(result);
        }
Пример #20
0
        public void Enqueue_EveryTimeAnItemIsEnqueued_TheItemCounterIsIncreasedByOne()
        {
            // Arrange
            int timesItemIsQueued = 10;
            var item = new SystemInformationQueueItem(new SystemInformation {
                MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow
            });

            var queue = new SystemInformationMessageQueue();

            // Act
            for (int i = 1; i <= timesItemIsQueued; i++)
            {
                queue.Enqueue(item);
                queue.Dequeue();
            }

            // Assert
            Assert.AreEqual(timesItemIsQueued, item.EnqueuCount);
        }
Пример #21
0
        public void GetHashCode_TwoIdenticalObjects_WithDifferentEnqueueCountValues_HashCodesAreEqual()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 1
                };

            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 3
                };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        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);
            }
        }
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            int numberOfItems = 10000;
            var items = TestUtilities.GetSystemInformationObjects(numberOfItems);
            var hashCodes = new Dictionary<int, SystemInformationQueueItem>();

            for (var i = 0; i < numberOfItems; i++)
            {
                // Act
                var object1 = new SystemInformationQueueItem(items[i]);

                int generatedHashCode = object1.GetHashCode();

                // Assert
                Assert.IsFalse(hashCodes.ContainsKey(generatedHashCode));
                hashCodes.Add(generatedHashCode, object1);
            }
        }
        public void GetHashCode_TwoDistinctObjects_HashCodesAreDifferent()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName + "A", Timestamp = timeStamp });
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName + "B", Timestamp = timeStamp });

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreNotEqual(hashCodeObject1, hashCodeObject2);
        }
        public void GetHashCode_TwoIdenticalObjects_WithDifferentEnqueueCountValues_HashCodesAreEqual()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 1
                };

            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 3
                };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        public void Item_Get_ReturnsTheSameItemThatHasBeenAssignedViaTheConstructor()
        {
            // Arrange
            var assignedItem = new SystemInformation();
            var systemInformationQueueItem = new SystemInformationQueueItem(assignedItem);

            // Act
            var result = systemInformationQueueItem.Item;

            // Assert
            Assert.AreEqual(assignedItem, result);
        }
        public void ToString_Contains_Item()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem);

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsTrue(result.Contains(object1.Item.ToString()));
        }
        public void ToString_DoesNotContain_UsedMemoryInGB()
        {
            // Arrange
            var systemInformationItem = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var object1 = new SystemInformationQueueItem(systemInformationItem) { EnqueuCount = 15 };

            // Act
            string result = object1.ToString();

            // Assert
            Assert.IsFalse(result.Contains(object1.EnqueuCount.ToString()));
        }
        public void Equals_TwoIdenticalInitializedObjects_ResultIsTrue()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp });
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp });

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsTrue(result);
        }
        public void Enqueue_EveryTimeAnItemIsEnqueued_TheItemCounterIsIncreasedByOne()
        {
            // Arrange
            int timesItemIsQueued = 10;
            var item = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow });

            var queue = new SystemInformationMessageQueue();

            // Act
            for (int i = 1; i <= timesItemIsQueued; i++)
            {
                queue.Enqueue(item);
                queue.Dequeue();
            }

            // Assert
            Assert.AreEqual(timesItemIsQueued, item.EnqueuCount);
        }
        public void Equals_TwoIdenticalObjects_WithDifferentEnqueueCountValues_ResultIsTrue()
        {
            // Arrange
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 1
                };
            var object2 = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = timeStamp })
                {
                    EnqueuCount = 2
                };

            // Act
            bool result = object1.Equals(object2);

            // Assert
            Assert.IsTrue(result);
        }
        public void Enqueue_QueueItemCounterIsIncreasedByOne()
        {
            // Arrange
            var item = new SystemInformationQueueItem(new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow });
            int previousCount = item.EnqueuCount;

            var queue = new SystemInformationMessageQueue();
            queue.Enqueue(item);

            // Act
            var dequeuedItem = queue.Dequeue();

            // Assert
            Assert.AreEqual(previousCount + 1, dequeuedItem.EnqueuCount);
        }
        public void QueueIsFilled_SendCausesExceptionWhichJustifiesARetry_RetryCountHasNotBeenExceeded_ItemIsAddedToQueue()
        {
            // Arrange
            int maxRuntime = SystemInformationMessageQueueWorker.WorkIntervalInMilliseconds * 2;

            var workQueue = new Mock<IMessageQueue<SystemInformation>>();
            workQueue.Setup(q => q.IsEmpty()).Returns(false);

            bool itemHasBeenAccessed = false;
            var systemInfo = new SystemInformation { MachineName = Environment.MachineName, Timestamp = DateTime.UtcNow };
            var queueItem = new SystemInformationQueueItem(systemInfo);
            workQueue.Setup(q => q.Dequeue()).Returns(() =>
                {
                    if (!itemHasBeenAccessed)
                    {
                        itemHasBeenAccessed = true;
                        return queueItem;
                    }

                    return null;
                });

            var errorQueue = new Mock<IMessageQueue<SystemInformation>>();
            var systemInformationSender = new Mock<ISystemInformationSender>();
            systemInformationSender.Setup(s => s.Send(systemInfo)).Throws(
                new SendSystemInformationFailedException("Some minor exception which justifies a retry", null));

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

                // Assert
                workQueue.Verify(q => q.Enqueue(It.IsAny<SystemInformationQueueItem>()), Times.Once());
            }
        }