public void WillPutUrlsInTheQueue()
            {
                var        testable = new TestableReducingQueue();
                IQueueItem result;
                var        item = new QueueItem <CssResource>();

                testable.ClassUnderTest.Enqueue(item);

                result = testable.ClassUnderTest.BaseQueue.Dequeue();
                Assert.Equal(item, result);
                testable.Dispose();
            }
            public void WillNotReduceQueuedCSSUntilRepositoryHasLoadedSavedItems()
            {
                var testable = new TestableReducingQueue();

                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url"
                });
                testable.ClassUnderTest.ReductionRepository.HasLoadedSavedEntries = false;

                testable.ClassUnderTest.ProcessQueuedItem();

                testable.MockedReducer.Verify(x => x.Process(It.IsAny <Guid>(), "url"), Times.Never());
                testable.Dispose();
            }
            public void WillNotReduceItemIfAlreadyReducedOnAntherServer()
            {
                var testable = new TestableReducingQueue();

                testable.Mock <IStore>().Setup(x => x.GetUrlByKey(Hasher.Hash("url"), It.IsAny <Type>())).Returns("newUrl");
                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url"
                });

                testable.ClassUnderTest.ProcessQueuedItem();

                testable.MockedReducer.Verify(x => x.Process(It.IsAny <Guid>(), "url"), Times.Never());
                testable.Dispose();
            }
            public void WillNotReduceItemIfAlreadyReduced()
            {
                var testable = new TestableReducingQueue();

                testable.ClassUnderTest.ReductionRepository.AddReduction(Hasher.Hash("url"), "url");
                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url"
                });

                testable.ClassUnderTest.ProcessQueuedItem();

                testable.MockedReducer.Verify(x => x.Process(It.IsAny <Guid>(), "url"), Times.Never());
                testable.Dispose();
            }
            public void WillReSetCurrentProcessedItemToNullAfterProcessing()
            {
                RRContainer.Current = null;
                var testable = new TestableReducingQueue();
                var item     = new QueueItem <CssResource> {
                    Urls = "url"
                };

                testable.ClassUnderTest.Enqueue(item);

                testable.ClassUnderTest.ProcessQueuedItem();

                Assert.Null(testable.ClassUnderTest.ItemBeingProcessed);
                testable.Dispose();
            }
            public void WillSetCurrentProcessedItem()
            {
                RRContainer.Current = null;
                var testable = new TestableReducingQueue();
                var item     = new QueueItem <CssResource> {
                    Urls = "url"
                };

                testable.ClassUnderTest.Enqueue(item);

                testable.ClassUnderTest.ProcessQueuedItem();

                Assert.Equal(item, testable.ClassUnderTest.LastProcessedItem);
                testable.Dispose();
            }
            public void WillReduceQueuedJavaScript()
            {
                RRContainer.Current = null;
                var testable = new TestableReducingQueue();

                testable.MockedReducer.Setup(x => x.SupportedResourceType).Returns(typeof(JavaScriptResource));
                testable.ClassUnderTest.Enqueue(new QueueItem <JavaScriptResource> {
                    Urls = "url"
                });

                testable.ClassUnderTest.ProcessQueuedItem();

                testable.MockedReducer.Verify(x => x.Process(It.IsAny <Guid>(), "url"), Times.Once());
                testable.Dispose();
            }
            public void WillReturnTheCountOfTheBaseQueue()
            {
                var testable = new TestableReducingQueue();

                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url"
                });
                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url2"
                });

                var result = testable.ClassUnderTest.Count;

                Assert.Equal(2, result);
                testable.Dispose();
            }
            public void WillNotPutUrlsInTheQueueThatAreBeingProcessed()
            {
                var testable = new TestableReducingQueue();
                var item     = new QueueItem <CssResource>()
                {
                    Urls = "urls"
                };

                testable.ClassUnderTest.ForceSetItemBeingProcessed = new QueueItem <CssResource>()
                {
                    Urls = "urls"
                };

                testable.ClassUnderTest.Enqueue(item);

                Assert.Equal(0, testable.ClassUnderTest.BaseQueue.Count);
                testable.Dispose();
            }
            public void WillCaptureErrorsIfAnErrorActionIsRegistered()
            {
                var       testable   = new TestableReducingQueue();
                Exception error      = null;
                var       innerError = new ApplicationException();

                Registry.CaptureErrorAction = (x => error = x);
                testable.MockedReducer.Setup(x => x.SupportedResourceType).Returns(typeof(CssResource));
                testable.MockedReducer.Setup(x => x.Process(It.IsAny <Guid>(), "url")).Throws(innerError);
                testable.ClassUnderTest.Enqueue(new QueueItem <CssResource> {
                    Urls = "url"
                });

                testable.ClassUnderTest.ProcessQueuedItem();

                Assert.Equal(innerError, error.InnerException);
                Assert.Contains("url", error.Message);
                testable.Dispose();
            }
            public void WillNotPutUrlsInTheQueueThatAreAlreadyQueued()
            {
                var testable = new TestableReducingQueue();
                var item     = new QueueItem <CssResource>()
                {
                    Urls = "urls"
                };

                testable.ClassUnderTest.Enqueue(item);
                var item2 = new QueueItem <CssResource>()
                {
                    Urls = "urls"
                };

                testable.ClassUnderTest.Enqueue(item2);

                Assert.Equal(1, testable.ClassUnderTest.BaseQueue.Count);
                testable.Dispose();
            }
            public void WillStopProcessingAfterExceedingFailureThreshold()
            {
                var testable = new TestableReducingQueue();
                var badUrl   = new QueueItem <CssResource> {
                    Urls = "url"
                };
                var badKey = Hasher.Hash(badUrl.Urls);

                testable.MockedReducer.Setup(x => x.SupportedResourceType).Returns(typeof(CssResource));
                testable.MockedReducer.Setup(x => x.Process(badKey, badUrl.Urls)).Throws(new Exception());

                for (int i = 0; i < ReducingQueue.FailureThreshold + 1; i++)
                {
                    testable.ClassUnderTest.Enqueue(badUrl);
                    testable.ClassUnderTest.ProcessQueuedItem();
                }

                testable.MockedReducer.Verify(x => x.Process(badKey, badUrl.Urls), Times.Exactly(ReducingQueue.FailureThreshold));
                testable.Dispose();
            }
            public void WillProcessFailedUrlAfterFailuresAreCleared()
            {
                RRContainer.Current = null;
                var testable = new TestableReducingQueue();
                var badUrl   = new QueueItem <CssResource> {
                    Urls = "url"
                };
                var badKey = Hasher.Hash(badUrl.Urls);

                testable.MockedReducer.Setup(x => x.SupportedResourceType).Returns(typeof(CssResource));
                testable.MockedReducer.Setup(x => x.Process(badKey, badUrl.Urls)).Throws(new Exception());
                for (int i = 0; i < ReducingQueue.FailureThreshold; i++)
                {
                    testable.ClassUnderTest.Enqueue(badUrl);
                    testable.ClassUnderTest.ProcessQueuedItem();
                }

                testable.ClassUnderTest.ClearFailures();
                testable.ClassUnderTest.Enqueue(badUrl);
                testable.ClassUnderTest.ProcessQueuedItem();

                testable.MockedReducer.Verify(x => x.Process(badKey, badUrl.Urls), Times.Exactly(ReducingQueue.FailureThreshold + 1));
                testable.Dispose();
            }