Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed Apr 17, 2023
1 parent 8013708 commit 1db4ada
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<RepositoryUrl>https://github.com/managedcode/Orleans.RateLimiting</RepositoryUrl>
<PackageProjectUrl>https://github.com/managedcode/Orleans.RateLimiting</PackageProjectUrl>
<Product>Managed Code - Orleans RateLimiting</Product>
<Version>0.0.2</Version>
<PackageVersion>0.0.2</PackageVersion>
<Version>0.0.3</Version>
<PackageVersion>0.0.3</PackageVersion>

</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class FixedWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Fixe
/// </summary>
/// <param name="keyType">What key to use to identify RateLimiting, can be overridden by setting key property</param>
/// <param name="key">Custom string as key for RateLimiting</param>
/// <param name="window">
/// Specifies the time window that takes in the requests.
/// <param name="windowInSeconds">
/// Specifies the time window that takes in the requests in seconds.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="FixedWindowRateLimiter"/>.
/// </param>
/// <param name="permitLimit">
Expand All @@ -33,7 +33,7 @@ public class FixedWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Fixe
/// </param>
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan window = default,
int windowInSeconds = default,
int permitLimit = default,
int queueLimit = 0,
bool autoReplenishment = true,
Expand All @@ -49,7 +49,7 @@ public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
}

int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;
TimeSpan? windowNullable = windowInSeconds > 0 ? TimeSpan.FromSeconds(windowInSeconds) : null;

if (permitLimitNullable.HasValue || windowNullable.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class SlidingWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Sl
/// </summary>
/// <param name="keyType">What key to use to identify RateLimiting, can be overridden by setting key property</param>
/// <param name="key">Custom string as key for RateLimiting</param>
/// <param name="window">
/// Specifies the time window that takes in the requests.
/// <param name="windowinSeconds">
/// Specifies the time window that takes in the requests in seconds.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="SlidingWindowRateLimiter"/>.
/// </param>
/// <param name="permitLimit">
Expand All @@ -37,7 +37,7 @@ public class SlidingWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Sl
/// </param>
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan window = default,
int windowinSeconds = default,
int permitLimit = default,
int queueLimit = 0,
int segmentsPerWindow = default,
Expand All @@ -54,8 +54,8 @@ public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, stri
}

int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
int? segmentsPerWindowNullable = segmentsPerWindow >= 0 ? segmentsPerWindow : null;
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;
int? segmentsPerWindowNullable = segmentsPerWindow > 0 ? segmentsPerWindow : null;
TimeSpan? windowNullable = windowinSeconds > 0 ? TimeSpan.FromSeconds(windowinSeconds) : null;


if (permitLimitNullable.HasValue || windowNullable.HasValue || segmentsPerWindowNullable.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class TokenBucketRateLimiterAttribute : Attribute, ILimiterAttribute<Toke
/// </summary>
/// <param name="keyType">What key to use to identify RateLimiting, can be overridden by setting key property</param>
/// <param name="key">Custom string as key for RateLimiting</param>
/// <param name="replenishmentPeriod">
/// Specifies the minimum period between replenishments.
/// <param name="replenishmentPeriodInSeconds">
/// Specifies the minimum period between replenishments in seconds.
/// Must be set to a value greater than <see cref="TimeSpan.Zero" /> by the time these options are passed to the constructor of <see cref="TokenBucketRateLimiter"/>.
/// </param>
/// <param name="tokensPerPeriod">
Expand All @@ -37,7 +37,7 @@ public class TokenBucketRateLimiterAttribute : Attribute, ILimiterAttribute<Toke
/// </param>
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan replenishmentPeriod = default,
int replenishmentPeriodInSeconds = default,
int tokensPerPeriod = default,
int queueLimit = 0,
int tokenLimit = default,
Expand All @@ -54,8 +54,8 @@ public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
}

int? tokensPerPeriodNullable = tokensPerPeriod > 0 ? tokensPerPeriod : null;
int? tokenLimitNullable = tokenLimit >= 0 ? tokenLimit : null;
TimeSpan? replenishmentPeriodNullable = replenishmentPeriod > TimeSpan.Zero ? replenishmentPeriod : null;
int? tokenLimitNullable = tokenLimit > 0 ? tokenLimit : null;
TimeSpan? replenishmentPeriodNullable = replenishmentPeriodInSeconds > 0 ? TimeSpan.FromSeconds(replenishmentPeriodInSeconds) : null;

if (tokensPerPeriodNullable.HasValue || tokenLimitNullable.HasValue || replenishmentPeriodNullable.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

public interface ITestFixedWindowRateLimiterGrain : IGrainWithStringKey
{
Task<string> Do();
Task<string> Go();
Task<string> Take();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

public interface ITestSlidingWindowRateLimiterGrain : IGrainWithStringKey
{
Task<string> Do();
Task<string> Go();
Task<string> Take();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

public interface ITestTokenBucketRateLimiterGrain : IGrainWithStringKey
{
Task<string> Do();
Task<string> Go();
Task<string> Take();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains
{
public class TestConcurrencyLimiterGrain : Grain, ITestConcurrencyLimiterGrain
{
[ConcurrencyLimiter] //GrainId, default options PermitLimit = 10; QueueLimit = 15;
[ConcurrencyLimiter] //GrainId as key, default options
public async Task<string> Do()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}

[ConcurrencyLimiter(KeyType.Key, "go")] //Key, default options PermitLimit = 10; QueueLimit = 15;
[ConcurrencyLimiter(KeyType.Key, "go")] //String as Key, default options
public async Task<string> Go()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}

[ConcurrencyLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1)]
[ConcurrencyLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1)] //GrainType as Key, custom options, some of them are default (check Attribute)
public async Task<string> Take()
{
await Task.Delay(TimeSpan.FromSeconds(5));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ManagedCode.Orleans.RateLimiting.Core.Attributes;
using ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains;

public class TestFixedWindowRateLimiterGrain : Grain, ITestFixedWindowRateLimiterGrain
{
[FixedWindowRateLimiter] //GrainId as key, default options
public async Task<string> Do()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}

[FixedWindowRateLimiter(KeyType.Key, "go")] //String as Key, default options
public async Task<string> Go()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}

[FixedWindowRateLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1)] //GrainType as Key, custom options, some of them are default (check Attribute)
public async Task<string> Take()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Take";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ManagedCode.Orleans.RateLimiting.Core.Attributes;
using ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains;

public class TestSlidingWindowRateLimiterGrain : Grain, ITestSlidingWindowRateLimiterGrain
{
[SlidingWindowRateLimiter] //GrainId as key, default options
public async Task<string> Do()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}

[SlidingWindowRateLimiter(KeyType.Key, "go")] //String as Key, default options
public async Task<string> Go()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}

[SlidingWindowRateLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1, segmentsPerWindow:2)] //GrainType as Key, custom options, some of them are default (check Attribute)
public async Task<string> Take()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Take";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ManagedCode.Orleans.RateLimiting.Core.Attributes;
using ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces;

namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains;

public class TestTokenBucketRateLimiterGrain : Grain, ITestTokenBucketRateLimiterGrain
{
[TokenBucketRateLimiter] //GrainId as key, default options
public async Task<string> Do()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}

[TokenBucketRateLimiter(KeyType.Key, "go")] //String as Key, default options
public async Task<string> Go()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}

[TokenBucketRateLimiter(KeyType.GrainType, tokenLimit:2, queueLimit:1)] //GrainType as Key, custom options, some of them are default (check Attribute)
public async Task<string> Take()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Take";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
namespace ManagedCode.Orleans.RateLimiting.Tests;

[Collection(nameof(TestClusterApplication))]
public class GrainsRateLimiterTests
public class ConcurrencyLimiterGrainTests
{
private readonly ITestOutputHelper _outputHelper;
private readonly TestClusterApplication _testApp;

public GrainsRateLimiterTests(TestClusterApplication testApp, ITestOutputHelper outputHelper)
public ConcurrencyLimiterGrainTests(TestClusterApplication testApp, ITestOutputHelper outputHelper)
{
_testApp = testApp;
_outputHelper = outputHelper;
}

[Fact]
public async Task GetConcurrencyLimiterGrainIdTests()
public async Task GrainIdTests()
{
int count = 100;
int success = 0;
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task GetConcurrencyLimiterGrainIdTests()
}

[Fact]
public async Task GetConcurrencyLimiterKeyTests()
public async Task KeyTests()
{
int count = 100;
int success = 0;
Expand All @@ -77,4 +77,31 @@ public async Task GetConcurrencyLimiterKeyTests()
(success + errors).Should().Be(count);
success.Should().BeLessThan(errors);
}

[Fact]
public async Task TypeTests()
{
int count = 100;
int success = 0;
int errors = 0;

var tasks = Enumerable.Range(0, count).Select(s => Task.Run(async () =>
{
try
{
await _testApp.Cluster.Client.GetGrain<ITestConcurrencyLimiterGrain>(s.ToString()).Take();
Interlocked.Increment(ref success);
}
catch
{
Interlocked.Increment(ref errors);
}
}));

await Task.WhenAll(tasks);

(success + errors).Should().Be(count);
success.Should().BeLessThan(errors);
}
}
Loading

0 comments on commit 1db4ada

Please sign in to comment.