public GivenAUrlThatPointsToAWebSiteThatThrowsAnInternalServerError() { _webTextSourceOptions = new WebTextSourceOptions { RetryTimes = new[] { TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0) } }; _testHarness = new TestHarness(_webTextSourceOptions); _testHarness.WebSiteThrowsWebException(HttpStatusCode.InternalServerError); }
public async Task InvokesRetryPolicyOnErrors(Exception webClientException, bool expectRetry) { // ARRANGE var fakeWebTextSourceOptions = new WebTextSourceOptions { RetryTimes = new[] { TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0) } }; var fakeUrl = "http://testing.com"; var fakeToken = new CancellationTokenSource().Token; var mockWebClient = MockRepository.GenerateStrictMock <IWebClient>(); mockWebClient .Expect(x => x.GetHtmlAsync(Arg.Is(fakeUrl), Arg.Is(fakeToken))) .Throw(webClientException) .Repeat.Times( // 1 for initial call and then any retries 1 + (expectRetry ? fakeWebTextSourceOptions.RetryTimes.Length : 0)); var webTextSource = new WebTextSource(mockWebClient, fakeWebTextSourceOptions, fakeUrl); // ACT try { await webTextSource.GetTextAsync(fakeToken); Assert.Fail("Expected an exception to be thrown but was not."); } catch (Exception e) { // ASSERT e.ShouldEqual(webClientException); mockWebClient.VerifyAllExpectations(); } }
public TestHarness(WebTextSourceOptions options = null) { var kernel = new Startup().BuildKernel(); _mockWebClient = MockRepository.GenerateMock <IWebClient>(); kernel.Rebind <IWebClient>().ToConstant(_mockWebClient); _mockEmailClient = MockRepository.GenerateMock <IEmailClient>(); _mockEmailClient .Stub(x => x.SendEmailAsync( to: Arg <string> .Is.Anything, from: Arg <string> .Is.Anything, body: Arg <string> .Is.Anything, token: Arg <CancellationToken> .Is.Anything)) .Return(Task.FromResult(true)); kernel.Rebind <IEmailClient>().ToConstant(_mockEmailClient); _mockLogger = MockRepository.GenerateMock <ILogger>(); _mockLogger .Stub(x => x.Debug(Arg <string> .Is.Anything, Arg <LoggingContext> .Is.Anything)) // capture Debug Messages and write to Console so we can see messages // in test window. .Do(new Action <string, LoggingContext>((msg, ctx) => Console.WriteLine(msg))); kernel.Rebind <ILogger>().ToConstant(_mockLogger); // Disable the Cache Initializer's Thread Sleeper kernel.Rebind <IThreadSleeper>().ToConstant(MockRepository.GenerateMock <IThreadSleeper>()); _wordCountingWorkflow = kernel.Get <WordCountingWorkflow>(); _webTextSource = new WebTextSource( _mockWebClient, options ?? kernel.Get <WebTextSourceOptions>(), _fakeUrl); }
public WebTextSourceFactory(IWebClient webClient, WebTextSourceOptions options) { _webClient = webClient; _options = options; }