OrTimeout Extension

This commit is contained in:
Dylan Dmitri Gray 2019-05-17 10:40:19 -07:00
parent 04ce6ef4c8
commit 9363eff2a8
2 changed files with 14 additions and 5 deletions

View File

@ -26,7 +26,9 @@ namespace RequestThrottlingSample
{ {
app.Run(async context => app.Run(async context =>
{ {
Console.WriteLine("HEWWWO?");
await context.Response.WriteAsync("Hello world!"); await context.Response.WriteAsync("Hello world!");
Console.WriteLine("GOODBWWYWE!");
}); });
} }

View File

@ -10,6 +10,13 @@ using Microsoft.AspNetCore.Testing;
namespace Microsoft.AspNetCore.RequestThrottling.Tests namespace Microsoft.AspNetCore.RequestThrottling.Tests
{ {
public static class TaskExtensions
{
public static Task OrTimeout(this Task task, int seconds = 30)
{
return task.TimeoutAfter(TimeSpan.FromSeconds(seconds));
}
}
public class SemaphoreWrapperTests public class SemaphoreWrapperTests
{ {
[Fact] [Fact]
@ -18,7 +25,7 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
using var s = new SemaphoreWrapper(1); using var s = new SemaphoreWrapper(1);
Assert.Equal(1, s.Count); Assert.Equal(1, s.Count);
await s.EnterQueue(); await s.EnterQueue().OrTimeout();
Assert.Equal(0, s.Count); Assert.Equal(0, s.Count);
s.LeaveQueue(); s.LeaveQueue();
@ -44,13 +51,13 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
public async Task WaitsIfNoSpaceAvailible() public async Task WaitsIfNoSpaceAvailible()
{ {
using var s = new SemaphoreWrapper(1); using var s = new SemaphoreWrapper(1);
await s.EnterQueue(); await s.EnterQueue().OrTimeout();
var waitingTask = s.EnterQueue(); var waitingTask = s.EnterQueue();
Assert.False(waitingTask.IsCompleted); Assert.False(waitingTask.IsCompleted);
s.LeaveQueue(); s.LeaveQueue();
await waitingTask.TimeoutAfter(TimeSpan.FromSeconds(30)); await waitingTask.OrTimeout();
} }
[Fact] [Fact]
@ -59,8 +66,8 @@ namespace Microsoft.AspNetCore.RequestThrottling.Tests
using var s1 = new SemaphoreWrapper(1); using var s1 = new SemaphoreWrapper(1);
using var s2 = new SemaphoreWrapper(1); using var s2 = new SemaphoreWrapper(1);
await s1.EnterQueue(); await s1.EnterQueue().OrTimeout();
await s2.EnterQueue().TimeoutAfter(TimeSpan.FromSeconds(30)); await s2.EnterQueue().OrTimeout();
} }
} }
} }