From 90ebc8d2beec9b4b2f76fbc891b808171910369d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20St=C3=BChmer?= <3283596+samtrion@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:16:30 +0200 Subject: [PATCH] fix: Switched validation order of timeout and task completion --- .../TaskExtensions/WithTimeoutAsync(Task,Timespan).cs | 10 +++++----- .../TaskExtensions/WithTimeoutAsync(Task,int).cs | 10 +++++----- .../WithTimeoutAsync(TaskOfT,Timespan).cs | 8 ++++---- .../TaskExtensions/WithTimeoutAsync(TaskOfT,int).cs | 8 ++++---- .../WithTimeoutAsync(ValueTask,Timespan).cs | 10 +++++----- .../TaskExtensions/WithTimeoutAsync(ValueTask,int).cs | 10 +++++----- .../WithTimeoutAsync(ValueTaskOfT,Timespan).cs | 8 ++++---- .../WithTimeoutAsync(ValueTaskOfT,int).cs | 8 ++++---- 8 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,Timespan).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,Timespan).cs index 350e748..09afa3b 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,Timespan).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,Timespan).cs @@ -32,17 +32,17 @@ public static async Task WithTimeoutAsync( Argument.ThrowIfNull(task); Argument.ThrowIfLessThan(timeout, Timeout.InfiniteTimeSpan); - if (task.IsCompleted) - { - return true; - } - if (timeout <= TimeSpan.Zero) { await task.ConfigureAwait(false); return timeout == Timeout.InfiniteTimeSpan; } + if (task.IsCompleted) + { + return true; + } + var winner = await Task.WhenAny(task, Task.Delay(timeout, cancellationToken)) .ConfigureAwait(false); await winner.ConfigureAwait(false); diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,int).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,int).cs index 660e47b..2f9e40d 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,int).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(Task,int).cs @@ -32,17 +32,17 @@ public static async Task WithTimeoutAsync( Argument.ThrowIfNull(task); Argument.ThrowIfLessThan(timeoutInMilliseconds, Timeout.Infinite); - if (task.IsCompleted) - { - return true; - } - if (timeoutInMilliseconds <= 0) { await task.ConfigureAwait(false); return timeoutInMilliseconds == Timeout.Infinite; } + if (task.IsCompleted) + { + return true; + } + var winner = await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds, cancellationToken)) .ConfigureAwait(false); await winner.ConfigureAwait(false); diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,Timespan).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,Timespan).cs index 974a5c6..7a66912 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,Timespan).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,Timespan).cs @@ -32,14 +32,14 @@ public static partial class TaskExtensions Argument.ThrowIfNull(task); Argument.ThrowIfLessThan(timeout, Timeout.InfiniteTimeSpan); - if (task.IsCompleted) + if (timeout <= TimeSpan.Zero) { - return (true, await task.ConfigureAwait(false)); + return (timeout == Timeout.InfiniteTimeSpan, await task.ConfigureAwait(false)); } - if (timeout <= TimeSpan.Zero) + if (task.IsCompleted) { - return (timeout == Timeout.InfiniteTimeSpan, await task.ConfigureAwait(false)); + return (true, await task.ConfigureAwait(false)); } var winner = await Task.WhenAny(task, Task.Delay(timeout, cancellationToken)) diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,int).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,int).cs index 63ce624..4c04577 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,int).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(TaskOfT,int).cs @@ -31,14 +31,14 @@ public static partial class TaskExtensions Argument.ThrowIfNull(task); Argument.ThrowIfLessThan(timeoutInMilliseconds, Timeout.Infinite); - if (task.IsCompleted) + if (timeoutInMilliseconds <= 0) { - return (true, await task.ConfigureAwait(false)); + return (timeoutInMilliseconds == Timeout.Infinite, await task.ConfigureAwait(false)); } - if (timeoutInMilliseconds <= 0) + if (task.IsCompleted) { - return (timeoutInMilliseconds == Timeout.Infinite, await task.ConfigureAwait(false)); + return (true, await task.ConfigureAwait(false)); } var winner = await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds, cancellationToken)) diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,Timespan).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,Timespan).cs index a9444f9..f5ea2f9 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,Timespan).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,Timespan).cs @@ -25,17 +25,17 @@ public static async ValueTask WithTimeoutAsync( { Argument.ThrowIfLessThan(timeout, Timeout.InfiniteTimeSpan); - if (task.IsCompleted) - { - return true; - } - if (timeout <= TimeSpan.Zero) { await task.ConfigureAwait(false); return timeout == Timeout.InfiniteTimeSpan; } + if (task.IsCompleted) + { + return true; + } + var todoTask = task.AsTask(); var winner = await Task.WhenAny(todoTask, Task.Delay(timeout, cancellationToken)) .ConfigureAwait(false); diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,int).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,int).cs index 7af2d91..f001c35 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,int).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTask,int).cs @@ -24,17 +24,17 @@ public static async ValueTask WithTimeoutAsync( { Argument.ThrowIfLessThan(timeoutInMilliseconds, Timeout.Infinite); - if (task.IsCompleted) - { - return true; - } - if (timeoutInMilliseconds <= 0) { await task.ConfigureAwait(false); return timeoutInMilliseconds == Timeout.Infinite; } + if (task.IsCompleted) + { + return true; + } + var todoTask = task.AsTask(); var winner = await Task.WhenAny( todoTask, diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,Timespan).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,Timespan).cs index 6b13afe..d70a0f6 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,Timespan).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,Timespan).cs @@ -25,14 +25,14 @@ public static partial class TaskExtensions { Argument.ThrowIfLessThan(timeout, Timeout.InfiniteTimeSpan); - if (task.IsCompleted) + if (timeout <= TimeSpan.Zero) { - return (true, await task.ConfigureAwait(false)); + return (timeout == Timeout.InfiniteTimeSpan, await task.ConfigureAwait(false)); } - if (timeout <= TimeSpan.Zero) + if (task.IsCompleted) { - return (timeout == Timeout.InfiniteTimeSpan, await task.ConfigureAwait(false)); + return (true, await task.ConfigureAwait(false)); } var todoTask = task.AsTask(); diff --git a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,int).cs b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,int).cs index 62e55dc..c8d2630 100644 --- a/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,int).cs +++ b/src/NetEvolve.Extensions.Tasks/TaskExtensions/WithTimeoutAsync(ValueTaskOfT,int).cs @@ -24,14 +24,14 @@ public static partial class TaskExtensions { Argument.ThrowIfLessThan(timeoutInMilliseconds, Timeout.Infinite); - if (task.IsCompleted) + if (timeoutInMilliseconds <= 0) { - return (true, await task.ConfigureAwait(false)); + return (timeoutInMilliseconds == Timeout.Infinite, await task.ConfigureAwait(false)); } - if (timeoutInMilliseconds <= 0) + if (task.IsCompleted) { - return (timeoutInMilliseconds == Timeout.Infinite, await task.ConfigureAwait(false)); + return (true, await task.ConfigureAwait(false)); } var todoTask = task.AsTask();