Skip to content

Commit

Permalink
Improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Desmaisons committed May 25, 2016
1 parent bc5985d commit 727ced5
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 6 deletions.
2 changes: 1 addition & 1 deletion RateLimiter/ComposedAwaitableConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<IDisposable> WaitForReadiness(CancellationToken cancellationTo
{
diposables = await Task.WhenAll(_AwaitableConstraint1.WaitForReadiness(cancellationToken), _AwaitableConstraint2.WaitForReadiness(cancellationToken));
}
catch (TaskCanceledException)
catch (Exception)
{
_Semafore.Release();
throw;
Expand Down
2 changes: 1 addition & 1 deletion RateLimiter/CountByIntervalAwaitableConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<IDisposable> WaitForReadiness(CancellationToken cancellationTo
{
await _Time.GetDelay(timetoWait, cancellationToken);
}
catch (TaskCanceledException)
catch (Exception)
{
_Semafore.Release();
throw;
Expand Down
30 changes: 27 additions & 3 deletions RateLimiterTest/ComposedAwaitableConstraintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,34 @@ public async Task WaitForReadiness_Block()
}

[Fact]
public async Task WaitForReadiness_BlockUntillExecuteIsCalled()
public async Task WaitForReadiness_WhenCancelled_DoNotBlock()
{
using (await _Composed.WaitForReadiness(CancellationToken.None)) {
}
var cancellation = new CancellationToken(true);
try
{
await _Composed.WaitForReadiness(cancellation);
}
catch
{
}
var timedOut = await WaitForReadinessHasTimeOut();
timedOut.Should().BeFalse();
}

[Fact]
public void WaitForReadiness_WhenCancelled_ThrowException()
{
var cancellation = new CancellationToken(true);
Func<Task> act = async () => await _Composed.WaitForReadiness(cancellation);
act.ShouldThrow<TaskCanceledException>();
}

[Fact]
public async Task WaitForReadiness_BlockUntillDisposeIsCalled()
{
var disp = await _Composed.WaitForReadiness(CancellationToken.None);
disp.Dispose();

var timedOut = await WaitForReadinessHasTimeOut();
timedOut.Should().BeFalse();
}
Expand Down
65 changes: 64 additions & 1 deletion RateLimiterTest/CountByIntervalAwaitableConstraintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task WaitForReadiness_Block_WhenWaitForReadinessHasBeenCalledButNot
}

[Fact]
public async Task WaitForReadiness_UnBlock_WhenExcecuteIsCalled()
public async Task WaitForReadiness_UnBlock_WhenDisposeIsCalled()
{
var disp = await _CountByIntervalAwaitableConstraint1.WaitForReadiness(CancellationToken.None);
var secondTask = _CountByIntervalAwaitableConstraint1.WaitForReadiness(CancellationToken.None);
Expand All @@ -74,6 +74,69 @@ public async Task WaitForReadiness_UnBlock_WhenExcecuteIsCalled()
timedOut.Should().BeFalse();
}

[Fact]
public async Task WaitForReadiness_WhenCancelled_DoNotBlock()
{
var cancellation = new CancellationToken(true);
try
{
await _CountByIntervalAwaitableConstraint1.WaitForReadiness(cancellation);
}
catch
{
}
var task = _CountByIntervalAwaitableConstraint1.WaitForReadiness(CancellationToken.None);
var timedOut = await TaskHasTimeOut(task, 50);
timedOut.Should().BeFalse();
}

[Fact]
public void WaitForReadiness_WhenCancelled_ThrowException()
{
var cancellation = new CancellationToken(true);
Func<Task> act = async () => await _CountByIntervalAwaitableConstraint1.WaitForReadiness(cancellation);
act.ShouldThrow<TaskCanceledException>();
}

[Fact]
public async Task WaitForReadiness_WhenCancelledAfterSemaforeTaken_DoNotBlock()
{
var cancellation = await SetUpForCancelledAfterSemaforeTaken();
try
{
await _CountByIntervalAwaitableConstraint1.WaitForReadiness(cancellation);
}
catch
{
}
var task = _CountByIntervalAwaitableConstraint1.WaitForReadiness(CancellationToken.None);
var timedOut = await TaskHasTimeOut(task, 50);
timedOut.Should().BeFalse();
}

[Fact]
public async Task WaitForReadiness_WhenCancelledAfterSemaforeTaken_ThrowException()
{
var cancellation = await SetUpForCancelledAfterSemaforeTaken();
Func<Task> act = async () => await _CountByIntervalAwaitableConstraint1.WaitForReadiness(cancellation);
act.ShouldThrow<TaskCanceledException>();
}

private async Task<CancellationToken> SetUpForCancelledAfterSemaforeTaken()
{
var cancellationSource = new CancellationTokenSource();
_MockTime.OnDelay = (t, c) =>
{
cancellationSource.Cancel();
cancellationSource.Token.ThrowIfCancellationRequested();
};

var disp = await _CountByIntervalAwaitableConstraint1.WaitForReadiness(CancellationToken.None);
disp.Dispose();

return cancellationSource.Token;
}

[Fact]
public async Task WaitForReadiness_WhenLimitIsReached_CallDelayToRespectTimeConstraint()
{
Expand Down
4 changes: 4 additions & 0 deletions RateLimiterTest/MockTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public class MockTime : ITime
public DateTime Now { get; private set; }
public int GetNowCount { get; private set; }
public int GetDelayCount { get; private set; }
public Action<TimeSpan, CancellationToken> OnDelay { get; set; }

public MockTime(DateTime now)
{
Now = now;
OnDelay = (t, c) => { };
}

public void AddTime(TimeSpan addedTime)
Expand All @@ -22,6 +25,7 @@ public void AddTime(TimeSpan addedTime)

public Task GetDelay(TimeSpan timespan, CancellationToken cancellationToken)
{
OnDelay(timespan, cancellationToken);
GetDelayCount++;
AddTime(timespan);
return Task.FromResult(0);
Expand Down
89 changes: 89 additions & 0 deletions RateLimiterTest/RateLimiterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ public async Task Perform_CallExcecuteInCaseOfException()
CheckSequence();
}

[Fact]
public void Perform_WhenCancelled_ThrowException()
{
Func<Task> act = async () => await _TimeConstraint.Perform(_FuncTask, new CancellationToken(true));
act.ShouldThrow<TaskCanceledException>();
}

[Fact]
public async Task Perform_WhenCancelled_DoNotCallFunction()
{
try
{
await _TimeConstraint.Perform(_FuncTask, new CancellationToken(true));
}
catch
{
}
await _FuncTask.DidNotReceive().Invoke();
}

[Fact]
public void Perform_WhenAwaitableConstraintIsCancelled_ThrowException()
{
SetUpAwaitableConstraintIsCancelled();
Func<Task> act = async () => await _TimeConstraint.Perform(_FuncTask, new CancellationToken(true));
act.ShouldThrow<TaskCanceledException>();
}

[Fact]
public async Task Perform_WhenAwaitableConstraintIsCancelled_DoNotCallFunction()
{
SetUpAwaitableConstraintIsCancelled();
try
{
await _TimeConstraint.Perform(_FuncTask, new CancellationToken(true));
}
catch
{
}
await _FuncTask.DidNotReceive().Invoke();
}

private void CheckSequence()
{
Received.InOrder(() => {
Expand Down Expand Up @@ -99,6 +141,53 @@ public async Task PerformGeneric_CallExcecuteInCaseOfException()
CheckGenericSequence();
}

[Fact]
public void PerformGeneric_WhenCancelled_ThrowException()
{
Func<Task> act = async () => await _TimeConstraint.Perform(_FuncTaskInt, new CancellationToken(true));
act.ShouldThrow<TaskCanceledException>();
}

[Fact]
public async Task PerformGeneric_WhenCancelled_DoNotCallFunction()
{
try
{
await _TimeConstraint.Perform(_FuncTaskInt, new CancellationToken(true));
}
catch
{
}
await _FuncTaskInt.DidNotReceive().Invoke();
}

[Fact]
public async Task PerformGeneric_WhenAwaitableConstraintIsCancelled_DoNotCallFunction()
{
SetUpAwaitableConstraintIsCancelled();
try
{
await _TimeConstraint.Perform(_FuncTaskInt, new CancellationToken(true));
}
catch
{
}
await _FuncTaskInt.DidNotReceive().Invoke();
}

[Fact]
public void PerformGeneric_WhenAwaitableConstraintIsCancelled_ThrowException()
{
SetUpAwaitableConstraintIsCancelled();
Func<Task> act = async () => await _TimeConstraint.Perform(_FuncTaskInt, new CancellationToken(true));
act.ShouldThrow<TaskCanceledException>();
}

private void SetUpAwaitableConstraintIsCancelled()
{
_IAwaitableConstraint.WaitForReadiness(Arg.Any<CancellationToken>()).Returns(x => { throw new TaskCanceledException(); });
}

private void CheckGenericSequence()
{
Received.InOrder(() => {
Expand Down

0 comments on commit 727ced5

Please sign in to comment.