Skip to content

Commit

Permalink
fix: Switched validation order of timeout and task completion
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed Sep 5, 2023
1 parent c171ecb commit 90ebc8d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public static async Task<bool> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public static async Task<bool> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public static async ValueTask<bool> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public static async ValueTask<bool> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 90ebc8d

Please sign in to comment.