Remove timeout from receive tcs. (#1471)

* Remove timeout from receive tcs.
- This test echos a large message and it fails sometimes before the entire thing is delivered. Just drop the timeout.
- Avoid creating timers for already completed tasks
This commit is contained in:
David Fowler 2018-02-19 20:15:47 -08:00 committed by GitHub
parent a2ac9c573e
commit bfa2df1fc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 65 deletions

View File

@ -1,64 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR.Tests.Common
{
public static class TaskExtensions
{
private const int DefaultTimeout = 5000;
public static Task OrTimeout(this Task task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null)
{
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)
{
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;
}
public static Task<T> OrTimeout<T>(this Task<T> task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null)
{
return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber);
}
public static async Task<T> OrTimeout<T>(this Task<T> task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null)
{
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;
}
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";
}
}
}
}

View File

@ -17,6 +17,12 @@ namespace System.Threading.Tasks
public static async 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)
@ -35,6 +41,11 @@ namespace System.Threading.Tasks
public static async Task<T> OrTimeout<T>(this Task<T> 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)

View File

@ -290,7 +290,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
logger.LogInformation("Sent message", bytes.Length);
logger.LogInformation("Receiving message");
var receivedData = await receiveTcs.Task.OrTimeout();
// No timeout here because it can take a while to receive all the bytes
var receivedData = await receiveTcs.Task;
Assert.Equal(message, Encoding.UTF8.GetString(receivedData));
logger.LogInformation("Completed receive");
}