diff --git a/src/SignalR/src/Microsoft.AspNetCore.SignalR.Specification.Tests/Microsoft.AspNetCore.SignalR.Specification.Tests.csproj b/src/SignalR/src/Microsoft.AspNetCore.SignalR.Specification.Tests/Microsoft.AspNetCore.SignalR.Specification.Tests.csproj index e7835cdbf5..cd33432739 100644 --- a/src/SignalR/src/Microsoft.AspNetCore.SignalR.Specification.Tests/Microsoft.AspNetCore.SignalR.Specification.Tests.csproj +++ b/src/SignalR/src/Microsoft.AspNetCore.SignalR.Specification.Tests/Microsoft.AspNetCore.SignalR.Specification.Tests.csproj @@ -1,4 +1,4 @@ - + Tests for users to verify their own implementations of SignalR types @@ -19,6 +19,7 @@ + diff --git a/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj b/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj index 211e6824e7..a8063bdbc2 100644 --- a/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj +++ b/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/Microsoft.AspNetCore.SignalR.Tests.Utils.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -22,6 +22,7 @@ + diff --git a/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TaskExtensions.cs b/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TaskExtensions.cs index e64be4fbc7..3707af046d 100644 --- a/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TaskExtensions.cs +++ b/src/SignalR/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TaskExtensions.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO.Pipelines; using System.Runtime.CompilerServices; +using Microsoft.AspNetCore.Testing; namespace System.Threading.Tasks { @@ -21,23 +22,9 @@ namespace System.Threading.Tasks return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); } - public static async Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + public static Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) { - if (task.IsCompleted) - { - await task; - return; - } - - var cts = new CancellationTokenSource(); - var completed = await Task.WhenAny(task, Task.Delay(Debugger.IsAttached ? Timeout.InfiniteTimeSpan : timeout, cts.Token)); - if (completed != task) - { - throw new TimeoutException(GetMessage(memberName, filePath, lineNumber)); - } - cts.Cancel(); - - await task; + return task.TimeoutAfter(timeout, filePath, lineNumber ?? 0); } public static Task OrTimeout(this ValueTask task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) => @@ -51,22 +38,9 @@ namespace System.Threading.Tasks return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); } - public static async Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + public static Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) { - if (task.IsCompleted) - { - return await task; - } - - var cts = new CancellationTokenSource(); - var completed = await Task.WhenAny(task, Task.Delay(Debugger.IsAttached ? Timeout.InfiniteTimeSpan : timeout, cts.Token)); - if (completed != task) - { - throw new TimeoutException(GetMessage(memberName, filePath, lineNumber)); - } - cts.Cancel(); - - return await task; + return task.TimeoutAfter(timeout, filePath, lineNumber ?? 0); } public static async Task OrThrowIfOtherFails(this Task task, Task otherTask) @@ -92,17 +66,5 @@ namespace System.Threading.Tasks // If we get here, 'task' is finished and succeeded. return task.GetAwaiter().GetResult(); } - - private static string GetMessage(string memberName, string filePath, int? lineNumber) - { - if (!string.IsNullOrEmpty(memberName)) - { - return $"Operation in {memberName} timed out at {filePath}:{lineNumber}"; - } - else - { - return "Operation timed out"; - } - } } }