From 80fb19e6886424870e0005e603539066ae444f43 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Tue, 12 May 2020 19:57:29 +0200 Subject: [PATCH] simplified test using setup class --- .../Http/SimpleRetryHandlerTest.cs | 113 ++++++++---------- 1 file changed, 53 insertions(+), 60 deletions(-) diff --git a/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs b/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs index 404fa257..42adb016 100644 --- a/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs +++ b/SpotifyAPI.Web.Tests/Http/SimpleRetryHandlerTest.cs @@ -14,157 +14,150 @@ namespace SpotifyAPI.Web [Test] public async Task HandleRetry_TooManyRequestsWithNoSuccess() { - var sleep = new Mock>(); - - var request = new Mock(); - var initialResponse = new Mock(); - initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); - initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary { + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); + setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary { { "Retry-After", "50" } }); var retryCalled = 0; - Task retry(IRequest request) + setup.Retry = (IRequest request) => { retryCalled++; - return Task.FromResult(initialResponse.Object); - } + return Task.FromResult(setup.Response.Object); + }; - var handler = new SimpleRetryHandler(sleep.Object) + var handler = new SimpleRetryHandler(setup.Sleep.Object) { TooManyRequestsConsumesARetry = true, RetryTimes = 2 }; - var response = await handler.HandleRetry(request.Object, initialResponse.Object, retry); + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); Assert.AreEqual(2, retryCalled); - Assert.AreEqual(initialResponse.Object, response); - sleep.Verify(s => s(50000), Times.Exactly(2)); - + Assert.AreEqual(setup.Response.Object, response); + setup.Sleep.Verify(s => s(50000), Times.Exactly(2)); } [Test] public async Task HandleRetry_TooManyRetriesWithSuccess() { - var sleep = new Mock>(); - - var request = new Mock(); - var initialResponse = new Mock(); - initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); - initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary { + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); + setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary { { "Retry-After", "50" } }); + var successResponse = new Mock(); successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK); var retryCalled = 0; - Task retry(IRequest request) + setup.Retry = (request) => { retryCalled++; return Task.FromResult(successResponse.Object); - } + }; - var handler = new SimpleRetryHandler(sleep.Object) + var handler = new SimpleRetryHandler(setup.Sleep.Object) { TooManyRequestsConsumesARetry = true, RetryTimes = 10 }; - var response = await handler.HandleRetry(request.Object, initialResponse.Object, retry); + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); Assert.AreEqual(1, retryCalled); Assert.AreEqual(successResponse.Object, response); - sleep.Verify(s => s(50000), Times.Once); + setup.Sleep.Verify(s => s(50000), Times.Once); } [Test] public async Task HandleRetry_TooManyRetriesWithSuccessNoConsume() { - var sleep = new Mock>(); - - var request = new Mock(); - var initialResponse = new Mock(); - initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); - initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary { + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests); + setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary { { "Retry-After", "50" } }); var successResponse = new Mock(); successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK); var retryCalled = 0; - Task retry(IRequest request) + setup.Retry = (IRequest request) => { retryCalled++; return Task.FromResult(successResponse.Object); - } + }; - var handler = new SimpleRetryHandler(sleep.Object) + var handler = new SimpleRetryHandler(setup.Sleep.Object) { TooManyRequestsConsumesARetry = false, RetryTimes = 0 }; - var response = await handler.HandleRetry(request.Object, initialResponse.Object, retry); + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); Assert.AreEqual(1, retryCalled); Assert.AreEqual(successResponse.Object, response); - sleep.Verify(s => s(50000), Times.Once); + setup.Sleep.Verify(s => s(50000), Times.Once); } [Test] public async Task HandleRetry_ServerErrors() { - var sleep = new Mock>(); - - var request = new Mock(); - var initialResponse = new Mock(); - initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.BadGateway); + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.BadGateway); var retryCalled = 0; - Task retry(IRequest request) + setup.Retry = (request) => { retryCalled++; - return Task.FromResult(initialResponse.Object); - } + return Task.FromResult(setup.Response.Object); + }; - var handler = new SimpleRetryHandler(sleep.Object) + var handler = new SimpleRetryHandler(setup.Sleep.Object) { TooManyRequestsConsumesARetry = true, RetryTimes = 10, RetryAfter = 50 }; - var response = await handler.HandleRetry(request.Object, initialResponse.Object, retry); + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); Assert.AreEqual(10, retryCalled); - Assert.AreEqual(initialResponse.Object, response); - sleep.Verify(s => s(50), Times.Exactly(10)); + Assert.AreEqual(setup.Response.Object, response); + setup.Sleep.Verify(s => s(50), Times.Exactly(10)); } [Test] public async Task HandleRetry_DirectSuccess() { - var sleep = new Mock>(); - - var request = new Mock(); - var initialResponse = new Mock(); - initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK); + var setup = new Setup(); + setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK); var retryCalled = 0; - Task retry(IRequest request) + setup.Retry = (request) => { retryCalled++; - return Task.FromResult(initialResponse.Object); - } + return Task.FromResult(setup.Response.Object); + }; - var handler = new SimpleRetryHandler(sleep.Object) + var handler = new SimpleRetryHandler(setup.Sleep.Object) { TooManyRequestsConsumesARetry = true, RetryTimes = 10, RetryAfter = 50 }; - var response = await handler.HandleRetry(request.Object, initialResponse.Object, retry); + var response = await handler.HandleRetry(setup.Request.Object, setup.Response.Object, setup.Retry); Assert.AreEqual(0, retryCalled); - Assert.AreEqual(initialResponse.Object, response); - sleep.Verify(s => s(50), Times.Exactly(0)); + Assert.AreEqual(setup.Response.Object, response); + setup.Sleep.Verify(s => s(50), Times.Exactly(0)); + } + + private class Setup + { + public Mock> Sleep { get; set; } = new Mock>(); + public Mock Response { get; set; } = new Mock(); + public Mock Request { get; set; } = new Mock(); + public Func> Retry { get; set; } } } }