Skip to content

Commit

Permalink
Increase rate limiter to 4 per second
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrandak committed Oct 21, 2020
1 parent 60efe53 commit c7a0333
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 46 deletions.
66 changes: 66 additions & 0 deletions FortnoxAPILibrary.Tests/RateLimiterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Diagnostics;
using FortnoxAPILibrary.Connectors;
using FortnoxAPILibrary.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FortnoxAPILibrary.Tests
{
[TestClass]
public class RateLimiterTests
{
[TestInitialize]
public void Init()
{
//Set global credentials for SDK
//--- Open 'TestCredentials.resx' to edit the values ---\\
ConnectionCredentials.AccessToken = TestCredentials.Access_Token;
ConnectionCredentials.ClientSecret = TestCredentials.Client_Secret;
}

[TestMethod]
public void Test_RateLimiter_NoError()
{
var connector = new CustomerConnector();

var watch = new Stopwatch();
watch.Start();
for (var i = 0; i < 200; i++)
{
connector.City = TestUtils.RandomString(); //Needs to be random to make unique GET request
connector.Find();
MyAssert.HasNoError(connector);
}

watch.Stop();
Console.WriteLine(@"Total time: " + watch.ElapsedMilliseconds);
}

[TestMethod]
public void Test_NoRateLimiter_TooManyRequest_Error()
{
ConnectionSettings.UseRateLimiter = false;
var connector = new CustomerConnector();

ErrorInformation error = null;
for (var i = 0; i < 200; i++)
{
connector.City = TestUtils.RandomString();
connector.Find();
if (connector.HasError)
{
error = connector.Error;
break;
}
}

//Restore settings
ConnectionSettings.UseRateLimiter = true;

//Assert
//Assert.IsTrue(failed > 0);
Assert.IsNotNull(error);
Assert.IsTrue(error.Message.Contains("Too Many Requests"));
}
}
}
18 changes: 0 additions & 18 deletions FortnoxAPILibrary.Tests/ReportedIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,6 @@ public void Test_issue51_fixed() //Origins from https://github.com/FortnoxAB/csh
MyAssert.HasNoError(connector);
}

[TestMethod]
public void Test_TooManyRequests_fixed()
{
var connector = new VoucherConnector();
connector.Limit = 2;

var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 40; i++)
{
connector.Find();
MyAssert.HasNoError(connector);
}

watch.Stop();
Console.WriteLine(@"Total time: "+watch.ElapsedMilliseconds);
}

[TestMethod]
public void Test_issue57_fixed() // Origins from https://github.com/FortnoxAB/csharp-api-sdk/issues/57
{
Expand Down
26 changes: 0 additions & 26 deletions FortnoxAPILibrary.Tests/TestErrorScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,5 @@ public void Test_FailedUpdate_NoEntity()
Assert.IsTrue(connector.HasError);
Assert.IsNull(createdCustomer);
}

[TestMethod]
public void Test_NoRateLimiter_TooManyRequest_Error()
{
ConnectionSettings.UseRateLimiter = false;
var connector = new CustomerConnector();

ErrorInformation error = null;
for (var i = 0; i < 200; i++)
{
connector.City = TestUtils.RandomString();
connector.Find();
if (connector.HasError)
{
error = connector.Error;
break;
}
}

//Restore settings
ConnectionSettings.UseRateLimiter = true;

//Assert
//Assert.IsTrue(failed > 0);
Assert.IsNotNull(error);
Assert.IsTrue(error.Message.Contains("Too Many Requests")); }
}
}
2 changes: 1 addition & 1 deletion FortnoxAPILibrary/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BaseClient
private const string AccessTokenHeader = "Access-Token";
private const string ClientSecretHeader = "Client-Secret";

private const int LimitPerSecond = 3;
private const int LimitPerSecond = 4;
private static readonly Dictionary<string, TimeLimiter> RateLimiters = new Dictionary<string, TimeLimiter>();

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion FortnoxAPILibrary/FortnoxAPILibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<PackageId>Fortnox.NET.SDK</PackageId>
<Version>3.4.0</Version>
<Version>3.4.1</Version>
<Authors>Richard Randak</Authors>
<Company>Softwerk AB</Company>
<Description>Official .NET SDK for Fortnox API. This package is on behalf of Fortnox AB developed and maintained by Softwerk AB. For more information please visit our repository on Github.</Description>
Expand Down

0 comments on commit c7a0333

Please sign in to comment.