Making `OrTimeout` common (#193)

Making `OrTimeout` common
This commit is contained in:
Pawel Kadluczka 2017-02-13 12:38:34 -08:00 committed by GitHub
parent fc93c47789
commit 82cbda4a9a
17 changed files with 46 additions and 47 deletions

View File

@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR.Tests
namespace Microsoft.AspNetCore.SignalR.Tests.Common
{
public static class TaskExtensions
{

View File

@ -5,6 +5,7 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.AspNetCore.Sockets.Client;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
@ -117,9 +118,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
});
await connection.Invoke<Task>("CallEcho", originalMessage);
var completed = await Task.WhenAny(Task.Delay(2000), tcs.Task);
Assert.True(completed == tcs.Task, "Receive timed out!");
Assert.Equal(originalMessage, tcs.Task.Result);
Assert.Equal(originalMessage, await tcs.Task.OrTimeout());
}
}
}

View File

@ -9,6 +9,10 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Client\Microsoft.AspNetCore.SignalR.Client.csproj" />

View File

@ -15,6 +15,7 @@ using Microsoft.Extensions.Logging;
using Xunit;
using ClientConnection = Microsoft.AspNetCore.Sockets.Client.Connection;
using Microsoft.AspNetCore.SignalR.Tests.Common;
namespace Microsoft.AspNetCore.SignalR.Tests
{

View File

@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Moq;
using Xunit;
using Microsoft.AspNetCore.SignalR.Tests.Common;
namespace Microsoft.AspNetCore.SignalR.Tests
{

View File

@ -10,6 +10,10 @@
<RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp1.1' ">win7-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Sockets\Microsoft.AspNetCore.Sockets.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj" />

View File

@ -11,6 +11,7 @@ using Microsoft.Extensions.Logging;
using Moq;
using Moq.Protected;
using Xunit;
using Microsoft.AspNetCore.SignalR.Tests.Common;
namespace Microsoft.AspNetCore.Sockets.Client.Tests
{
@ -37,7 +38,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.Equal(connectionUrl, connection.Url);
}
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
await longPollingTransport.Running.OrTimeout();
}
}
@ -61,7 +62,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await connection.StopAsync();
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
await longPollingTransport.Running.OrTimeout();
}
}
@ -85,7 +86,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.False(longPollingTransport.Running.IsCompleted);
}
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
await longPollingTransport.Running.OrTimeout();
}
}
@ -113,8 +114,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
var data = new byte[] { 1, 1, 2, 3, 5, 8 };
await connection.SendAsync(data, Format.Binary);
Assert.Equal(sendTcs.Task, await Task.WhenAny(Task.Delay(1000), sendTcs.Task));
Assert.Equal(data, sendTcs.Task.Result);
Assert.Equal(data, await sendTcs.Task.OrTimeout());
}
}

View File

@ -12,6 +12,7 @@ using Moq;
using Moq.Protected;
using Xunit;
using Microsoft.AspNetCore.Sockets.Internal;
using Microsoft.AspNetCore.SignalR.Tests.Common;
namespace Microsoft.AspNetCore.Sockets.Client.Tests
{
@ -44,7 +45,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.False(transportActiveTask.IsCompleted);
}
Assert.Equal(transportActiveTask, await Task.WhenAny(Task.Delay(1000), transportActiveTask));
await transportActiveTask.OrTimeout();
}
[Fact]
@ -67,7 +68,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
var channelConnection = new ChannelConnection<Message>(connectionToTransport, transportToConnection);
await longPollingTransport.StartAsync(new Uri("http://fakeuri.org"), channelConnection);
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
await longPollingTransport.Running.OrTimeout();
Assert.True(transportToConnection.In.Completion.IsCompleted);
}
}
@ -92,8 +93,8 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
var channelConnection = new ChannelConnection<Message>(connectionToTransport, transportToConnection);
await longPollingTransport.StartAsync(new Uri("http://fakeuri.org"), channelConnection);
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion);
var exception =
await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion.OrTimeout());
Assert.Contains(" 500 ", exception.Message);
}
}
@ -123,13 +124,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await connectionToTransport.Out.WriteAsync(new Message());
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
await Assert.ThrowsAsync<HttpRequestException>(async () => await longPollingTransport.Running);
await Assert.ThrowsAsync<HttpRequestException>(async () => await longPollingTransport.Running.OrTimeout());
// The channel needs to be drained for the Completion task to be completed
Message message;
while (transportToConnection.In.TryRead(out message))
while (transportToConnection.In.TryRead(out Message message))
{
message.Dispose();
}
@ -161,8 +159,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
connectionToTransport.Out.Complete();
Assert.Equal(longPollingTransport.Running, await Task.WhenAny(Task.Delay(1000), longPollingTransport.Running));
Assert.Equal(connectionToTransport.In.Completion, await Task.WhenAny(Task.Delay(1000), connectionToTransport.In.Completion));
await longPollingTransport.Running.OrTimeout();
await longPollingTransport.Running.OrTimeout();
await connectionToTransport.In.Completion.OrTimeout();
}
}
}

View File

@ -9,6 +9,10 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Sockets.Client\Microsoft.AspNetCore.Sockets.Client.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Sockets.Common\Microsoft.AspNetCore.Sockets.Common.csproj" />

View File

@ -9,6 +9,10 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Common\TaskExtensions.cs" Link="TaskExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Extensions.WebSockets.Internal\Microsoft.Extensions.WebSockets.Internal.csproj" />
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="1.2.0-*" PrivateAssets="All" />

View File

@ -13,31 +13,6 @@ namespace Microsoft.Extensions.WebSockets.Internal.Tests
{
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(1);
public static Task OrTimeout(this Task task) => OrTimeout(task, DefaultTimeout);
public static Task<T> OrTimeout<T>(this Task<T> task) => OrTimeout(task, DefaultTimeout);
public static async Task OrTimeout(this Task task, TimeSpan timeout)
{
var completed = await Task.WhenAny(task, CreateTimeoutTask());
Assert.Same(completed, task);
}
public static async Task<T> OrTimeout<T>(this Task<T> task, TimeSpan timeout)
{
var completed = await Task.WhenAny(task, CreateTimeoutTask());
Assert.Same(task, completed);
return task.Result;
}
public static Task CreateTimeoutTask() => CreateTimeoutTask(DefaultTimeout);
public static Task CreateTimeoutTask(TimeSpan timeout)
{
var tcs = new TaskCompletionSource<object>();
CreateTimeoutToken(timeout).Register(() => tcs.TrySetCanceled());
return tcs.Task;
}
public static CancellationToken CreateTimeoutToken() => CreateTimeoutToken(DefaultTimeout);
public static CancellationToken CreateTimeoutToken(TimeSpan timeout)

View File

@ -1,12 +1,13 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.Extensions.Internal;
using System;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
using Xunit;
namespace Microsoft.Extensions.WebSockets.Internal.Tests

View File

@ -1,6 +1,7 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using System;
using System.Globalization;
using System.IO.Pipelines;

View File

@ -1,6 +1,7 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using System;
using System.IO.Pipelines;
using System.Text;

View File

@ -1,6 +1,7 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using System;
using System.IO.Pipelines;
using System.Text;

View File

@ -1,11 +1,12 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.Extensions.Internal;
using System;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
using Xunit;
namespace Microsoft.Extensions.WebSockets.Internal.Tests

View File

@ -1,6 +1,7 @@
// 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 Microsoft.AspNetCore.SignalR.Tests.Common;
using System.IO.Pipelines;
using System.Linq;
using System.Text;