Skip to content

Commit

Permalink
Add test for non-cancellable await
Browse files Browse the repository at this point in the history
  • Loading branch information
bitc0der committed Nov 8, 2024
1 parent b8daaae commit 76fc861
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
27 changes: 25 additions & 2 deletions src/AsyncTools.Tests/WaitHandleExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,31 @@ public void Should_CompleteTask_When_HandlerIsSet()

// Assert
Assert.DoesNotThrowAsync(() => task);
Assert.That(task.IsCompleted, Is.True);
Assert.That(task.IsCanceled, Is.False);
Assert.Multiple(() =>
{
Assert.That(task.IsCompleted, Is.True);
Assert.That(task.IsCanceled, Is.False);
});
}

[Test]
public void Should_CompleteTask_When_HandlerIsSetWithoutToken()
{
// Arrange
using var handler = new ManualResetEvent(initialState: false);

var task = handler.WaitAsync();

// Act
handler.Set();

// Assert
Assert.DoesNotThrowAsync(() => task);
Assert.Multiple(() =>
{
Assert.That(task.IsCompleted, Is.True);
Assert.That(task.IsCanceled, Is.False);
});
}

[Test]
Expand Down
13 changes: 8 additions & 5 deletions src/AsyncTools/WaitHandleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public static Task WaitAsync(
executeOnlyOnce: true
);

tcs.Task.ContinueWith(result =>
{
rwh.Unregister(null);
return ctr.Unregister();
});
tcs.Task.ContinueWith(
continuationFunction: result =>
{
rwh.Unregister(null);
return !ctr.Token.CanBeCanceled || ctr.Unregister();
},
continuationOptions: TaskContinuationOptions.ExecuteSynchronously
);

return tcs.Task;
}
Expand Down

0 comments on commit 76fc861

Please sign in to comment.