mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Refactoring
This commit is contained in:
@@ -286,4 +286,6 @@ __pycache__/
|
||||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
|
||||
Build/nuget.exe
|
||||
|
||||
@@ -7,28 +7,28 @@ namespace MQTTnet.Core.Internal
|
||||
{
|
||||
public static class TaskExtensions
|
||||
{
|
||||
public static async Task TimeoutAfter( this Task task, TimeSpan timeout )
|
||||
public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
|
||||
{
|
||||
using ( var cancellationTokenSource = new CancellationTokenSource() )
|
||||
using (var cancellationTokenSource = new CancellationTokenSource())
|
||||
{
|
||||
try
|
||||
{
|
||||
var timeoutTask = Task.Delay(timeout, cancellationTokenSource.Token);
|
||||
var finishedTask = await Task.WhenAny(timeoutTask, task).ConfigureAwait(false);
|
||||
|
||||
if ( finishedTask == timeoutTask )
|
||||
if (finishedTask == timeoutTask)
|
||||
{
|
||||
throw new MqttCommunicationTimedOutException();
|
||||
}
|
||||
|
||||
if ( task.IsCanceled )
|
||||
if (task.IsCanceled)
|
||||
{
|
||||
throw new TaskCanceledException();
|
||||
}
|
||||
|
||||
if ( task.IsFaulted )
|
||||
if (task.IsFaulted)
|
||||
{
|
||||
throw new MqttCommunicationException( task.Exception.GetBaseException() );
|
||||
throw new MqttCommunicationException(task.Exception.GetBaseException());
|
||||
}
|
||||
}
|
||||
finally
|
||||
@@ -38,28 +38,28 @@ namespace MQTTnet.Core.Internal
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<TResult> TimeoutAfter<TResult>( this Task<TResult> task, TimeSpan timeout )
|
||||
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
|
||||
{
|
||||
using ( var cancellationTokenSource = new CancellationTokenSource() )
|
||||
using (var cancellationTokenSource = new CancellationTokenSource())
|
||||
{
|
||||
try
|
||||
{
|
||||
var timeoutTask = Task.Delay(timeout, cancellationTokenSource.Token);
|
||||
var finishedTask = await Task.WhenAny(timeoutTask, task).ConfigureAwait(false);
|
||||
|
||||
if ( finishedTask == timeoutTask )
|
||||
if (finishedTask == timeoutTask)
|
||||
{
|
||||
throw new MqttCommunicationTimedOutException();
|
||||
}
|
||||
|
||||
if ( task.IsCanceled )
|
||||
if (task.IsCanceled)
|
||||
{
|
||||
throw new TaskCanceledException();
|
||||
}
|
||||
|
||||
if ( task.IsFaulted )
|
||||
if (task.IsFaulted)
|
||||
{
|
||||
throw new MqttCommunicationException( task.Exception.GetBaseException() );
|
||||
throw new MqttCommunicationException(task.Exception.GetBaseException());
|
||||
}
|
||||
|
||||
return task.Result;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard1.3</TargetFramework>
|
||||
<TargetFramework>netstandard1.1</TargetFramework>
|
||||
<AssemblyName>MQTTnet.Core</AssemblyName>
|
||||
<RootNamespace>MQTTnet.Core</RootNamespace>
|
||||
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
|
||||
|
||||
@@ -75,15 +75,15 @@ namespace MQTTnet.Core.Tests
|
||||
var tasks = Enumerable.Range(0, 100000)
|
||||
.Select(i => Task.Delay(TimeSpan.FromMilliseconds(1)).TimeoutAfter(TimeSpan.FromMinutes(1)));
|
||||
|
||||
await Task.WhenAll( tasks );
|
||||
AssertIsLess( 3_000_000, GC.GetTotalMemory( true ) );
|
||||
await Task.WhenAll(tasks);
|
||||
AssertIsLess(3_000_000, GC.GetTotalMemory(true));
|
||||
}
|
||||
|
||||
private void AssertIsLess( long bound, long actual )
|
||||
private static void AssertIsLess(long bound, long actual)
|
||||
{
|
||||
if ( bound < actual )
|
||||
if (bound < actual)
|
||||
{
|
||||
Assert.Fail( $"value must be less than {bound:N0} but is {actual:N0}" );
|
||||
Assert.Fail($"value must be less than {bound:N0} but is {actual:N0}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user