From dcf6f94d778e85ec0f2b4a6e0784ae6c41ccb9d0 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 20 Sep 2017 21:28:18 +0200 Subject: [PATCH] Refactoring --- .gitignore | 2 ++ MQTTnet.Core/Internal/TaskExtensions.cs | 24 +++++++++++----------- MQTTnet.Core/MQTTnet.Core.csproj | 2 +- Tests/MQTTnet.Core.Tests/ExtensionTests.cs | 10 ++++----- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 45c63dd..b665d09 100644 --- a/.gitignore +++ b/.gitignore @@ -286,4 +286,6 @@ __pycache__/ *.btm.cs *.odx.cs *.xsd.cs + + Build/nuget.exe diff --git a/MQTTnet.Core/Internal/TaskExtensions.cs b/MQTTnet.Core/Internal/TaskExtensions.cs index 6db8067..45b3510 100644 --- a/MQTTnet.Core/Internal/TaskExtensions.cs +++ b/MQTTnet.Core/Internal/TaskExtensions.cs @@ -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 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()); } return task.Result; diff --git a/MQTTnet.Core/MQTTnet.Core.csproj b/MQTTnet.Core/MQTTnet.Core.csproj index e2c8d6a..797bb85 100644 --- a/MQTTnet.Core/MQTTnet.Core.csproj +++ b/MQTTnet.Core/MQTTnet.Core.csproj @@ -1,7 +1,7 @@  - netstandard1.3 + netstandard1.1 MQTTnet.Core MQTTnet.Core False diff --git a/Tests/MQTTnet.Core.Tests/ExtensionTests.cs b/Tests/MQTTnet.Core.Tests/ExtensionTests.cs index e0d2152..2564e69 100644 --- a/Tests/MQTTnet.Core.Tests/ExtensionTests.cs +++ b/Tests/MQTTnet.Core.Tests/ExtensionTests.cs @@ -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}"); } } }