From c7a0333cf1149722cdf4b94652d9ca3291ac699b Mon Sep 17 00:00:00 2001 From: Richard Randak Date: Wed, 21 Oct 2020 10:23:14 +0200 Subject: [PATCH] Increase rate limiter to 4 per second --- FortnoxAPILibrary.Tests/RateLimiterTests.cs | 66 +++++++++++++++++++ .../ReportedIssuesTests.cs | 18 ----- FortnoxAPILibrary.Tests/TestErrorScenarios.cs | 26 -------- FortnoxAPILibrary/BaseClient.cs | 2 +- FortnoxAPILibrary/FortnoxAPILibrary.csproj | 2 +- 5 files changed, 68 insertions(+), 46 deletions(-) create mode 100644 FortnoxAPILibrary.Tests/RateLimiterTests.cs diff --git a/FortnoxAPILibrary.Tests/RateLimiterTests.cs b/FortnoxAPILibrary.Tests/RateLimiterTests.cs new file mode 100644 index 00000000..21095f63 --- /dev/null +++ b/FortnoxAPILibrary.Tests/RateLimiterTests.cs @@ -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")); + } + } +} diff --git a/FortnoxAPILibrary.Tests/ReportedIssuesTests.cs b/FortnoxAPILibrary.Tests/ReportedIssuesTests.cs index af406043..171a995f 100644 --- a/FortnoxAPILibrary.Tests/ReportedIssuesTests.cs +++ b/FortnoxAPILibrary.Tests/ReportedIssuesTests.cs @@ -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 { diff --git a/FortnoxAPILibrary.Tests/TestErrorScenarios.cs b/FortnoxAPILibrary.Tests/TestErrorScenarios.cs index 08137685..d94cb9f3 100644 --- a/FortnoxAPILibrary.Tests/TestErrorScenarios.cs +++ b/FortnoxAPILibrary.Tests/TestErrorScenarios.cs @@ -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")); } } } diff --git a/FortnoxAPILibrary/BaseClient.cs b/FortnoxAPILibrary/BaseClient.cs index 8b6ef6c2..50f696d6 100644 --- a/FortnoxAPILibrary/BaseClient.cs +++ b/FortnoxAPILibrary/BaseClient.cs @@ -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 RateLimiters = new Dictionary(); /// diff --git a/FortnoxAPILibrary/FortnoxAPILibrary.csproj b/FortnoxAPILibrary/FortnoxAPILibrary.csproj index 1d0bfef6..e4d71a11 100644 --- a/FortnoxAPILibrary/FortnoxAPILibrary.csproj +++ b/FortnoxAPILibrary/FortnoxAPILibrary.csproj @@ -4,7 +4,7 @@ netstandard2.0 8 Fortnox.NET.SDK - 3.4.0 + 3.4.1 Richard Randak Softwerk AB 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.