Skip to content

Commit

Permalink
Fixed issue with the Rate limit manager where tests would wait forever
Browse files Browse the repository at this point in the history
  • Loading branch information
JRegnier-Magnet committed Oct 10, 2018
1 parent 199b4d0 commit a02518c
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 106 deletions.
20 changes: 10 additions & 10 deletions MtgApiManager.Lib.Test/Service/CardServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async Task AllAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardListDto() { Cards = cards });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);
service = service.Where(x => x.Name, "name1");

var result = await service.AllAsync();
Expand Down Expand Up @@ -275,7 +275,7 @@ public void AllTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardListDto() { Cards = cards });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);
service = service.Where(x => x.Name, "name1");

var result = service.All();
Expand Down Expand Up @@ -375,7 +375,7 @@ public async Task FindAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardDto() { Card = cardDto });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.FindAsync(1);
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -518,7 +518,7 @@ public void FindTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardDto() { Card = cardDto });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.Find(1);
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -628,7 +628,7 @@ public async Task GetCardSubTypesAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardSubTypeDto() { SubTypes = cardSubTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.GetCardSubTypesAsync();
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -692,7 +692,7 @@ public void GetCardSubTypesTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardSubTypeDto() { SubTypes = cardSubTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.GetCardSubTypes();
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -757,7 +757,7 @@ public async Task GetCardSuperTypesAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardSuperTypeDto() { SuperTypes = cardSuperTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.GetCardSuperTypesAsync();
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -821,7 +821,7 @@ public void GetCardSuperTypesTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardSuperTypeDto() { SuperTypes = cardSuperTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.GetCardSuperTypes();
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -886,7 +886,7 @@ public async Task GetCardTypesAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardTypeDto() { Types = cardTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.GetCardTypesAsync();
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -950,7 +950,7 @@ public void GetCardTypesTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardTypeDto() { Types = cardTypes });

var service = new CardService(moqAdapter.Object);
var service = new CardService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.GetCardTypes();
Assert.False(result.IsSuccess);
Expand Down
4 changes: 2 additions & 2 deletions MtgApiManager.Lib.Test/Service/ServiceBaseObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ServiceBaseObjectService : ServiceBase<CardService, Card>
/// Initializes a new instance of the <see cref="ServiceBaseObjectService"/> class. Defaults to version 1.0 of the API.
/// </summary>
public ServiceBaseObjectService()
: base(new MtgApiServiceAdapter(), ApiVersion.V1_0, ApiEndPoint.Cards)
: base(new MtgApiServiceAdapter(), ApiVersion.V1_0, ApiEndPoint.Cards, false)
{
}

Expand All @@ -30,7 +30,7 @@ public ServiceBaseObjectService()
/// </summary>
/// <param name="adapter">The adapter to use.</param>
public ServiceBaseObjectService(IMtgApiServiceAdapter adapter)
: base(adapter, ApiVersion.V1_0, ApiEndPoint.Cards)
: base(adapter, ApiVersion.V1_0, ApiEndPoint.Cards, false)
{
}

Expand Down
12 changes: 6 additions & 6 deletions MtgApiManager.Lib.Test/Service/SetServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task AllAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootSetListDto() { Sets = sets });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);
service = service.Where(x => x.Name, "name1");

var result = await service.AllAsync();
Expand Down Expand Up @@ -211,7 +211,7 @@ public void AllTest()
.Throws<Exception>()
.ReturnsAsync(new RootSetListDto() { Sets = sets });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);
service = service.Where(x => x.Name, "name1");

var result = service.All();
Expand Down Expand Up @@ -296,7 +296,7 @@ public async Task FindAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootSetDto() { Set = setDto });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.FindAsync("code1");
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -379,7 +379,7 @@ public void FindTest()
.Throws<Exception>()
.ReturnsAsync(new RootSetDto() { Set = setDto });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.Find("code1");
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -523,7 +523,7 @@ public async Task GenerateBoosterAsyncTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardListDto() { Cards = cards });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = await service.GenerateBoosterAsync("ktk");
Assert.False(result.IsSuccess);
Expand Down Expand Up @@ -666,7 +666,7 @@ public void GenerateBoosterTest()
.Throws<Exception>()
.ReturnsAsync(new RootCardListDto() { Cards = cards });

var service = new SetService(moqAdapter.Object);
var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

var result = service.GenerateBooster("ktk");
Assert.False(result.IsSuccess);
Expand Down
86 changes: 58 additions & 28 deletions MtgApiManager.Lib/Core/MtgApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

[assembly: InternalsVisibleTo("MtgApiManager.Lib.Test")]
Expand All @@ -13,6 +14,8 @@
/// </summary>
internal static class MtgApiController
{
private static readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);

/// <summary>
/// The rate limit which controls the calls to the API.
/// </summary>
Expand Down Expand Up @@ -79,14 +82,32 @@ public static PagingInfo CreatePagingInfo()

public async static Task HandleRateLimit()
{
int delayInMilliseconds = _apiRateLimit.GetDelay(MtgApiController.RatelimitLimit);
await _semaphoreSlim.WaitAsync().ConfigureAwait(false);

try
{
int delayInMilliseconds = _apiRateLimit.GetDelay(RatelimitLimit);

if (delayInMilliseconds > 0)
if (delayInMilliseconds > 0)
{
await Task.Delay(delayInMilliseconds).ConfigureAwait(false);
}

_apiRateLimit.AddApiCall();
}
finally
{
await Task.Delay(delayInMilliseconds);
_semaphoreSlim.Release();
}
}

_apiRateLimit.AddApiCall();
public static void ResetRateLimit()
{
_semaphoreSlim.Wait();

_apiRateLimit = new RateLimit();

_semaphoreSlim.Release();
}

/// <summary>
Expand All @@ -100,34 +121,43 @@ public static void ParseHeaders(HttpResponseHeaders headers)
throw new ArgumentNullException("headers");
}

if (headers.TryGetValues("Link", out IEnumerable<string> resultHeaders))
{
Link = resultHeaders.FirstOrDefault();
}
_semaphoreSlim.Wait();

if (headers.TryGetValues("Page-Size", out resultHeaders))
try
{
PageSize = int.Parse(resultHeaders.FirstOrDefault());
if (headers.TryGetValues("Link", out IEnumerable<string> resultHeaders))
{
Link = resultHeaders.FirstOrDefault();
}

if (headers.TryGetValues("Page-Size", out resultHeaders))
{
PageSize = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Count", out resultHeaders))
{
Count = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Total-Count", out resultHeaders))
{
TotalCount = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Ratelimit-Limit", out resultHeaders))
{
RatelimitLimit = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Ratelimit-Remaining", out resultHeaders))
{
RatelimitRemaining = int.Parse(resultHeaders.FirstOrDefault());
}
}

if (headers.TryGetValues("Count", out resultHeaders))
{
Count = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Total-Count", out resultHeaders))
{
TotalCount = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Ratelimit-Limit", out resultHeaders))
{
RatelimitLimit = int.Parse(resultHeaders.FirstOrDefault());
}

if (headers.TryGetValues("Ratelimit-Remaining", out resultHeaders))
finally
{
RatelimitRemaining = int.Parse(resultHeaders.FirstOrDefault());
_semaphoreSlim.Release();
}
}
}
2 changes: 1 addition & 1 deletion MtgApiManager.Lib/Core/RateLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class RateLimit
/// <summary>
/// The calls that have been made to the web service.
/// </summary>
private List<DateTime> _webServiceCalls;
private readonly List<DateTime> _webServiceCalls;

/// <summary>
/// Initializes a new instance of the <see cref="RateLimit"/> class.
Expand Down
18 changes: 9 additions & 9 deletions MtgApiManager.Lib/Service/CardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CardService
/// <summary>
/// The list of queries to apply.
/// </summary>
private NameValueCollection _whereQueries = null;
private readonly NameValueCollection _whereQueries;

/// <summary>
/// Initializes a new instance of the <see cref="CardService"/> class. Defaults to version 1.0 of the API.
Expand All @@ -48,8 +48,9 @@ public CardService()
/// </summary>
/// <param name="serviceAdapter">The service adapter used to interact with the MTG API.</param>
/// <param name="version">The version of the API</param>
public CardService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version)
: base(serviceAdapter, version, ApiEndPoint.Cards)
/// <param name="rateLimitOn">Turn the rate limit on or off.</param>
public CardService(IMtgApiServiceAdapter serviceAdapter, ApiVersion version, bool rateLimitOn = true)
: base(serviceAdapter, version, ApiEndPoint.Cards, rateLimitOn)
{
_whereQueries = new NameValueCollection();
}
Expand All @@ -63,7 +64,7 @@ public static List<Card> MapCardsList(RootCardListDto cardListDto)
{
if (cardListDto == null)
{
throw new ArgumentNullException("cardListDto");
throw new ArgumentNullException(nameof(cardListDto));
}

if (cardListDto.Cards == null)
Expand Down Expand Up @@ -319,12 +320,12 @@ public CardService Where<U>(Expression<Func<CardQueryParameter, U>> property, U
{
if (property == null)
{
throw new ArgumentNullException("property");
throw new ArgumentNullException(nameof(property));
}

if (value == null)
if (EqualityComparer<U>.Default.Equals(value, default(U)))
{
throw new ArgumentNullException("value");
throw new ArgumentNullException(nameof(value));
}

MemberExpression expression = property.Body as MemberExpression;
Expand All @@ -333,8 +334,7 @@ public CardService Where<U>(Expression<Func<CardQueryParameter, U>> property, U
Type valueType = value.GetType();
if (valueType.IsArray)
{
string val = string.Join("|", (IEnumerable<object>)value);
_whereQueries[queryName] = val;
_whereQueries[queryName] = string.Join("|", (IEnumerable<object>)value);
}
else
{
Expand Down
Loading

0 comments on commit a02518c

Please sign in to comment.