-
Notifications
You must be signed in to change notification settings - Fork 1
Add ObserveException extension methods for Task and Task<T> #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dae23e1
Add TaskExtensions with ObserveException for Task and Task<T>
Copilot a7b6dbb
Use explicit TaskScheduler.Default in ObserveException continuations
Copilot f36241e
Change ObserveException to return Task<AggregateException?>
Copilot 7cb958f
Potential fix for pull request finding
Tyrrrz 349efa6
Potential fix for pull request finding
Tyrrrz af94b87
Potential fix for pull request finding
Tyrrrz e0566aa
Potential fix for pull request finding
Tyrrrz 1da1c7a
refactor ObserveException to single Task overload
Copilot 75919ba
Update TaskExtensionsTests.cs
Tyrrrz 72b75b7
Update TaskExtensionsTests.cs
Tyrrrz 37220ad
fix test build warning in ObserveException test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using PowerKit.Extensions; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests.Extensions; | ||
|
|
||
| public class TaskExtensionsTests | ||
| { | ||
| [Fact] | ||
| public async Task ObserveException_Task_DoesNotThrowUnobservedException_Test() | ||
| { | ||
| // Arrange | ||
| var task = Task.Run(() => throw new InvalidOperationException("test error")); | ||
| task.ObserveException(); | ||
|
|
||
| // Act & assert: waiting for the task to complete should not raise an unobserved exception | ||
| await Task.Delay(100); | ||
|
|
||
| // Force GC to collect the task and trigger finalizer-based unobserved exception detection | ||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
|
|
||
| // The exception was observed, so no UnobservedTaskException should have been raised | ||
| task.IsFaulted.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ObserveException_TaskOfT_DoesNotThrowUnobservedException_Test() | ||
| { | ||
| // Arrange | ||
| var task = Task.Run(new Func<int>(() => throw new InvalidOperationException("test error"))); | ||
| task.ObserveException(); | ||
|
|
||
| // Act & assert | ||
| await Task.Delay(100); | ||
|
|
||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
|
|
||
| task.IsFaulted.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ObserveException_Task_SuccessfulTask_IsUnaffected_Test() | ||
| { | ||
| // Arrange | ||
| var task = Task.CompletedTask; | ||
| task.ObserveException(); | ||
|
|
||
| // Act & assert | ||
| await Task.Delay(50); | ||
|
|
||
| task.IsCompletedSuccessfully.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ObserveException_TaskOfT_SuccessfulTask_IsUnaffected_Test() | ||
| { | ||
| // Arrange | ||
| var task = Task.FromResult(42); | ||
| task.ObserveException(); | ||
|
|
||
| // Act & assert | ||
| await Task.Delay(50); | ||
|
|
||
| task.IsCompletedSuccessfully.Should().BeTrue(); | ||
| (await task).Should().Be(42); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #if !NETFRAMEWORK || NET45_OR_GREATER | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace PowerKit.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Extensions for <see cref="Task" />. | ||
| /// </summary> | ||
| public static class TaskExtensions | ||
| { | ||
| extension(Task task) | ||
| { | ||
| /// <summary> | ||
| /// Registers a continuation that observes and suppresses the task's exception, | ||
| /// preventing it from surfacing as an unobserved task exception. | ||
| /// Intended for use on detached (fire-and-forget) tasks. | ||
| /// </summary> | ||
| public void ObserveException() => | ||
| task.ContinueWith( | ||
| t => _ = t.Exception, | ||
| default, | ||
| TaskContinuationOptions.OnlyOnFaulted, | ||
| TaskScheduler.Default | ||
|
Copilot marked this conversation as resolved.
|
||
| ); | ||
| } | ||
|
|
||
| extension<T>(Task<T> task) | ||
| { | ||
| /// <summary> | ||
| /// Registers a continuation that observes and suppresses the task's exception, | ||
| /// preventing it from surfacing as an unobserved task exception. | ||
| /// Intended for use on detached (fire-and-forget) tasks. | ||
| /// </summary> | ||
| public void ObserveException() => | ||
| task.ContinueWith( | ||
| t => _ = t.Exception, | ||
| default, | ||
| TaskContinuationOptions.OnlyOnFaulted, | ||
| TaskScheduler.Default | ||
| ); | ||
| } | ||
| } | ||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.