Skip to content

Commit b99e542

Browse files
committed
fix: typo in extension methods
changes: - renamed the method to `FireAndForgetSafeAsync` - added some unit testing closes #107
1 parent 135e3a7 commit b99e542

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

src/BB84.Notifications/Commands/AsyncActionCommand.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public bool CanExecute(object? parameter)
4646

4747
/// <inheritdoc/>
4848
public void Execute(object? parameter)
49-
=> ExecuteAsync().ToSaveVoid(handler);
49+
=> ExecuteAsync().FireAndForgetSafeAsync(handler);
5050

5151
/// <inheritdoc/>
5252
public void RaiseCanExecuteChanged()
@@ -80,7 +80,7 @@ public bool CanExecute(object? parameter)
8080

8181
/// <inheritdoc/>
8282
public void Execute(object? parameter)
83-
=> ExecuteAsync((T)parameter!).ToSaveVoid(handler);
83+
=> ExecuteAsync((T)parameter!).FireAndForgetSafeAsync(handler);
8484

8585
/// <inheritdoc/>
8686
public async Task ExecuteAsync(T parameter)

src/BB84.Notifications/Extensions/TaskExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ public static class TaskExtensions
99
{
1010
/// <summary>
1111
/// Awaits the provided <paramref name="task"/>, uses the exception <paramref name="handler"/>
12-
/// if an <see cref="Exception"/> occured and returns <see cref="void"/>.
12+
/// if an <see cref="Exception"/> occurs and returns <see cref="void"/>.
1313
/// </summary>
1414
/// <param name="task">The task to await.</param>
1515
/// <param name="handler">Sends the exception to an exception handler.</param>
16-
public static async void ToSaveVoid(this Task task, IExceptionHandler? handler = null)
16+
public static async void FireAndForgetSafeAsync(this Task task, IExceptionHandler? handler = null)
1717
{
1818
try
1919
{
2020
await task;
2121
}
2222
catch (Exception ex)
2323
{
24-
handler?.Handle(ex);
24+
handler?.HandleError(ex);
2525
}
2626
}
2727
}

src/BB84.Notifications/Interfaces/Components/IExceptionHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public interface IExceptionHandler
99
/// If an error occurs, it send the exception to an error handler.
1010
/// </summary>
1111
/// <param name="exception">The exception to handle.</param>
12-
void Handle(Exception exception);
12+
void HandleError(Exception exception);
1313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using BB84.Notifications.Extensions;
2+
using BB84.Notifications.Interfaces.Components;
3+
4+
namespace BB84.NotificationsTests.Extensions;
5+
6+
[TestClass]
7+
[SuppressMessage("Usage", "CA2201", Justification = "UnitTest")]
8+
public sealed class TaskExtensionsTests
9+
{
10+
[TestMethod]
11+
public void FireAndForgetSafeAsyncTest()
12+
{
13+
Task task = Task.Factory.StartNew(() => { });
14+
15+
task.FireAndForgetSafeAsync();
16+
17+
Assert.IsNotNull(task);
18+
}
19+
20+
[TestMethod]
21+
public void FireAndForgetSafeAsyncExceptionHandledTest()
22+
{
23+
VoidHandler handler = new();
24+
Task task = Task.Factory.StartNew(() => throw new Exception(""));
25+
26+
task.FireAndForgetSafeAsync(handler);
27+
}
28+
29+
private sealed class VoidHandler : IExceptionHandler
30+
{
31+
public void HandleError(Exception exception) { }
32+
}
33+
}

0 commit comments

Comments
 (0)