Skip to content

Commit

Permalink
style: fix issues in RateLimiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Sep 23, 2022
1 parent 08fedce commit e6b22af
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 158 deletions.
11 changes: 5 additions & 6 deletions RateLimiter/RateLimiter.Tests/FakeStopwatchProviderAndBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ internal class FakeStopwatchProviderAndBlocker : IStopwatchProvider<long>, IAsyn
{
private long instant = 0;

public bool IsHighResolution => throw new NotImplementedException();

internal IList<long> Events { get; } = new List<long>();

public long GetTimestamp()
{
return instant;
return this.instant;
}

public TimeSpan ParseDuration(long from, long to)
Expand All @@ -34,13 +36,11 @@ public long GetNextTimestamp(long from, TimeSpan interval)

public Task WaitAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
instant += timeout.Ticks;
Events.Add(timeout.Ticks);
this.instant += timeout.Ticks;
this.Events.Add(timeout.Ticks);
return Task.FromResult<object>(null);
}

public bool IsHighResolution => throw new NotImplementedException();

public IStopwatch Create()
{
throw new NotImplementedException();
Expand All @@ -52,4 +52,3 @@ public IStopwatch StartNew()
}
}
}

63 changes: 31 additions & 32 deletions RateLimiter/RateLimiter.Tests/RateLimiterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,71 @@ namespace RateLimiter.Tests
{
public class RateLimiterTest
{
private readonly FakeStopwatchProviderAndBlocker stopwatchProviderAndBlocker =
new FakeStopwatchProviderAndBlocker();

internal IRateLimiter Create(double permitsPerSecond)
{
return Create(permitsPerSecond, 1.0);
}

internal IRateLimiter Create(double permitsPerSecond, double maxBurstSeconds)
{
return new SmoothBurstyRateLimiter(stopwatchProviderAndBlocker, maxBurstSeconds, stopwatchProviderAndBlocker)
{
PermitsPerSecond = permitsPerSecond
};
}

internal IRateLimiter Create(double permitsPerSecond, TimeSpan warmupPeriod)
{
return new SmoothWarmingUpRateLimiter(stopwatchProviderAndBlocker, warmupPeriod, 3, stopwatchProviderAndBlocker)
{
PermitsPerSecond = permitsPerSecond
};
}
private readonly FakeStopwatchProviderAndBlocker stopwatchProviderAndBlocker = new ();

[Fact]
public void TestSimple()
{
var limiter = Create(5, 1);
var limiter = this.Create(5, 1);
limiter.Acquire(); // R0.00, since it's the first request
limiter.Acquire(); // R0.20
limiter.Acquire(); // R0.20
Assert.Equal(new[]
Assert.Equal(
new[]
{
0L, TimeSpan.FromSeconds(0.2).Ticks, TimeSpan.FromSeconds(0.2).Ticks
}, stopwatchProviderAndBlocker.Events);
0L, TimeSpan.FromSeconds(0.2).Ticks, TimeSpan.FromSeconds(0.2).Ticks,
}, this.stopwatchProviderAndBlocker.Events);
}

[Fact]
public void TestImmediateTryAcquire()
{
var limiter = Create(1);
var limiter = this.Create(1);
Assert.True(limiter.TryAcquire().Succeed, "Unable to acquire initial permit");
Assert.False(limiter.TryAcquire().Succeed, "Capable of acquiring secondary permit");
}

[Fact]
public void TestDoubleMinValueCanAcquireExactlyOnce()
{
var r = Create(double.Epsilon);
var r = this.Create(double.Epsilon);
Assert.True(r.TryAcquire().Succeed, "Unable to acquire initial permit");
Assert.False(r.TryAcquire().Succeed, "Capable of acquiring an additional permit");
stopwatchProviderAndBlocker.WaitAsync(TimeSpan.MaxValue.Subtract(TimeSpan.FromTicks(1)), CancellationToken.None).GetAwaiter().GetResult();
this.stopwatchProviderAndBlocker.WaitAsync(TimeSpan.MaxValue.Subtract(TimeSpan.FromTicks(1)), CancellationToken.None).GetAwaiter().GetResult();
Assert.False(r.TryAcquire().Succeed, "Capable of acquiring an additional permit after sleeping");
}

[Fact]
public void TestSimpleRateUpdate()
{
var limiter = Create(5.0, TimeSpan.FromSeconds(5));
var limiter = this.Create(5.0, TimeSpan.FromSeconds(5));
Assert.Equal(5.0, limiter.PermitsPerSecond);
limiter.PermitsPerSecond = 10.0;
Assert.Equal(10.0, limiter.PermitsPerSecond);

Assert.Throws<ArgumentOutOfRangeException>(() => limiter.PermitsPerSecond = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => limiter.PermitsPerSecond = -10);
}

internal IRateLimiter Create(double permitsPerSecond)
{
return this.Create(permitsPerSecond, 1.0);
}

internal IRateLimiter Create(double permitsPerSecond, double maxBurstSeconds)
{
return new SmoothBurstyRateLimiter(this.stopwatchProviderAndBlocker, maxBurstSeconds, this.stopwatchProviderAndBlocker)
{
PermitsPerSecond = permitsPerSecond,
};
}

internal IRateLimiter Create(double permitsPerSecond, TimeSpan warmupPeriod)
{
return new SmoothWarmingUpRateLimiter(this.stopwatchProviderAndBlocker, warmupPeriod, 3, this.stopwatchProviderAndBlocker)
{
PermitsPerSecond = permitsPerSecond,
};
}
}
}

1 change: 0 additions & 1 deletion RateLimiter/RateLimiter/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("RateLimiter.Tests")]

1 change: 0 additions & 1 deletion RateLimiter/RateLimiter/IAsyncBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ public Task WaitAsync(TimeSpan timeout, CancellationToken cancellationToken)
}
}
}

1 change: 0 additions & 1 deletion RateLimiter/RateLimiter/IRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ public interface IRateLimiter
#endif
}
}

5 changes: 2 additions & 3 deletions RateLimiter/RateLimiter/RateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static IRateLimiter CreateBursty(
{
return new SmoothBurstyRateLimiter(stopwatchProvider, maxBurstSeconds)
{
PermitsPerSecond = permitsPerSecond
PermitsPerSecond = permitsPerSecond,
};
}

Expand All @@ -29,7 +29,7 @@ public static IRateLimiter CreateWarmingUp(
{
return new SmoothWarmingUpRateLimiter(stopwatchProvider, warmupPeriod, coldFactor)
{
PermitsPerSecond = permitsPerSecond
PermitsPerSecond = permitsPerSecond,
};
}

Expand All @@ -49,4 +49,3 @@ public static IRateLimiter Create(
}
}
}

2 changes: 1 addition & 1 deletion RateLimiter/RateLimiter/RateLimiter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Clocks.Net\Clocks.Abstraction\Clocks.Abstraction.csproj" />
<ProjectReference Include="$(EnlistmentRoot)\Clocks.Net\Clocks.Abstraction\Clocks.Abstraction.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit e6b22af

Please sign in to comment.