mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 22:56:25 +00:00
simplified test using setup class
This commit is contained in:
parent
589f50b25f
commit
80fb19e688
@ -14,157 +14,150 @@ namespace SpotifyAPI.Web
|
|||||||
[Test]
|
[Test]
|
||||||
public async Task HandleRetry_TooManyRequestsWithNoSuccess()
|
public async Task HandleRetry_TooManyRequestsWithNoSuccess()
|
||||||
{
|
{
|
||||||
var sleep = new Mock<Func<int, Task>>();
|
var setup = new Setup();
|
||||||
|
setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
||||||
var request = new Mock<IRequest>();
|
setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
||||||
var initialResponse = new Mock<IResponse>();
|
|
||||||
initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
|
||||||
initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
|
||||||
{ "Retry-After", "50" }
|
{ "Retry-After", "50" }
|
||||||
});
|
});
|
||||||
|
|
||||||
var retryCalled = 0;
|
var retryCalled = 0;
|
||||||
Task<IResponse> retry(IRequest request)
|
setup.Retry = (IRequest request) =>
|
||||||
{
|
{
|
||||||
retryCalled++;
|
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,
|
TooManyRequestsConsumesARetry = true,
|
||||||
RetryTimes = 2
|
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(2, retryCalled);
|
||||||
Assert.AreEqual(initialResponse.Object, response);
|
Assert.AreEqual(setup.Response.Object, response);
|
||||||
sleep.Verify(s => s(50000), Times.Exactly(2));
|
setup.Sleep.Verify(s => s(50000), Times.Exactly(2));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task HandleRetry_TooManyRetriesWithSuccess()
|
public async Task HandleRetry_TooManyRetriesWithSuccess()
|
||||||
{
|
{
|
||||||
var sleep = new Mock<Func<int, Task>>();
|
var setup = new Setup();
|
||||||
|
setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
||||||
var request = new Mock<IRequest>();
|
setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
||||||
var initialResponse = new Mock<IResponse>();
|
|
||||||
initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
|
||||||
initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
|
||||||
{ "Retry-After", "50" }
|
{ "Retry-After", "50" }
|
||||||
});
|
});
|
||||||
|
|
||||||
var successResponse = new Mock<IResponse>();
|
var successResponse = new Mock<IResponse>();
|
||||||
successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
||||||
|
|
||||||
var retryCalled = 0;
|
var retryCalled = 0;
|
||||||
Task<IResponse> retry(IRequest request)
|
setup.Retry = (request) =>
|
||||||
{
|
{
|
||||||
retryCalled++;
|
retryCalled++;
|
||||||
return Task.FromResult(successResponse.Object);
|
return Task.FromResult(successResponse.Object);
|
||||||
}
|
};
|
||||||
|
|
||||||
var handler = new SimpleRetryHandler(sleep.Object)
|
var handler = new SimpleRetryHandler(setup.Sleep.Object)
|
||||||
{
|
{
|
||||||
TooManyRequestsConsumesARetry = true,
|
TooManyRequestsConsumesARetry = true,
|
||||||
RetryTimes = 10
|
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(1, retryCalled);
|
||||||
Assert.AreEqual(successResponse.Object, response);
|
Assert.AreEqual(successResponse.Object, response);
|
||||||
sleep.Verify(s => s(50000), Times.Once);
|
setup.Sleep.Verify(s => s(50000), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task HandleRetry_TooManyRetriesWithSuccessNoConsume()
|
public async Task HandleRetry_TooManyRetriesWithSuccessNoConsume()
|
||||||
{
|
{
|
||||||
var sleep = new Mock<Func<int, Task>>();
|
var setup = new Setup();
|
||||||
|
setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
||||||
var request = new Mock<IRequest>();
|
setup.Response.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
||||||
var initialResponse = new Mock<IResponse>();
|
|
||||||
initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.TooManyRequests);
|
|
||||||
initialResponse.SetupGet(r => r.Headers).Returns(new Dictionary<string, string> {
|
|
||||||
{ "Retry-After", "50" }
|
{ "Retry-After", "50" }
|
||||||
});
|
});
|
||||||
var successResponse = new Mock<IResponse>();
|
var successResponse = new Mock<IResponse>();
|
||||||
successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
successResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
||||||
|
|
||||||
var retryCalled = 0;
|
var retryCalled = 0;
|
||||||
Task<IResponse> retry(IRequest request)
|
setup.Retry = (IRequest request) =>
|
||||||
{
|
{
|
||||||
retryCalled++;
|
retryCalled++;
|
||||||
return Task.FromResult(successResponse.Object);
|
return Task.FromResult(successResponse.Object);
|
||||||
}
|
};
|
||||||
|
|
||||||
var handler = new SimpleRetryHandler(sleep.Object)
|
var handler = new SimpleRetryHandler(setup.Sleep.Object)
|
||||||
{
|
{
|
||||||
TooManyRequestsConsumesARetry = false,
|
TooManyRequestsConsumesARetry = false,
|
||||||
RetryTimes = 0
|
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(1, retryCalled);
|
||||||
Assert.AreEqual(successResponse.Object, response);
|
Assert.AreEqual(successResponse.Object, response);
|
||||||
sleep.Verify(s => s(50000), Times.Once);
|
setup.Sleep.Verify(s => s(50000), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task HandleRetry_ServerErrors()
|
public async Task HandleRetry_ServerErrors()
|
||||||
{
|
{
|
||||||
var sleep = new Mock<Func<int, Task>>();
|
var setup = new Setup();
|
||||||
|
setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.BadGateway);
|
||||||
var request = new Mock<IRequest>();
|
|
||||||
var initialResponse = new Mock<IResponse>();
|
|
||||||
initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.BadGateway);
|
|
||||||
|
|
||||||
var retryCalled = 0;
|
var retryCalled = 0;
|
||||||
Task<IResponse> retry(IRequest request)
|
setup.Retry = (request) =>
|
||||||
{
|
{
|
||||||
retryCalled++;
|
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,
|
TooManyRequestsConsumesARetry = true,
|
||||||
RetryTimes = 10,
|
RetryTimes = 10,
|
||||||
RetryAfter = 50
|
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(10, retryCalled);
|
||||||
Assert.AreEqual(initialResponse.Object, response);
|
Assert.AreEqual(setup.Response.Object, response);
|
||||||
sleep.Verify(s => s(50), Times.Exactly(10));
|
setup.Sleep.Verify(s => s(50), Times.Exactly(10));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task HandleRetry_DirectSuccess()
|
public async Task HandleRetry_DirectSuccess()
|
||||||
{
|
{
|
||||||
var sleep = new Mock<Func<int, Task>>();
|
var setup = new Setup();
|
||||||
|
setup.Response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
||||||
var request = new Mock<IRequest>();
|
|
||||||
var initialResponse = new Mock<IResponse>();
|
|
||||||
initialResponse.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
|
|
||||||
|
|
||||||
var retryCalled = 0;
|
var retryCalled = 0;
|
||||||
Task<IResponse> retry(IRequest request)
|
setup.Retry = (request) =>
|
||||||
{
|
{
|
||||||
retryCalled++;
|
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,
|
TooManyRequestsConsumesARetry = true,
|
||||||
RetryTimes = 10,
|
RetryTimes = 10,
|
||||||
RetryAfter = 50
|
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(0, retryCalled);
|
||||||
Assert.AreEqual(initialResponse.Object, response);
|
Assert.AreEqual(setup.Response.Object, response);
|
||||||
sleep.Verify(s => s(50), Times.Exactly(0));
|
setup.Sleep.Verify(s => s(50), Times.Exactly(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Setup
|
||||||
|
{
|
||||||
|
public Mock<Func<int, Task>> Sleep { get; set; } = new Mock<Func<int, Task>>();
|
||||||
|
public Mock<IResponse> Response { get; set; } = new Mock<IResponse>();
|
||||||
|
public Mock<IRequest> Request { get; set; } = new Mock<IRequest>();
|
||||||
|
public Func<IRequest, Task<IResponse>> Retry { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user