Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/TaskSafetyTests.cs
T
PrimeBuild-pc acfd8dca7e Switch project license to AGPLv3 and test visibility
Replaces the project's license from GNU GPL v3 to the GNU Affero General Public License v3: updated LICENSE (full AGPL text) and updated license headers across source files. Adds InternalsVisibleTo("ThreadPilot.Core.Tests") (plus using System.Runtime.CompilerServices) in AssemblyInfo to allow test access to internal types. Also introduces Services/TaskSafety.cs and two new test files under Tests/ThreadPilot.Core.Tests to cover task safety. These changes apply the AGPLv3 terms and enable internal API access for unit tests.
2026-04-12 16:57:52 +02:00

48 lines
1.6 KiB
C#

/*
* ThreadPilot - async safety unit tests.
*/
namespace ThreadPilot.Core.Tests
{
using ThreadPilot.Services;
/// <summary>
/// Unit tests for <see cref="TaskSafety"/>.
/// </summary>
public sealed class TaskSafetyTests
{
/// <summary>
/// Ensures faulted tasks are observed and routed to the provided callback.
/// </summary>
[Fact]
public async Task FireAndForget_InvokesErrorCallback_ForFaultedTask()
{
var completion = new TaskCompletionSource<Exception>(TaskCreationOptions.RunContinuationsAsynchronously);
var expected = new InvalidOperationException("boom");
TaskSafety.FireAndForget(Task.FromException(expected), ex => completion.TrySetResult(ex));
var finishedTask = await Task.WhenAny(completion.Task, Task.Delay(TimeSpan.FromSeconds(2)));
Assert.Same(completion.Task, finishedTask);
var observed = await completion.Task;
Assert.IsType<InvalidOperationException>(observed);
Assert.Equal("boom", observed.Message);
}
/// <summary>
/// Ensures cancellation does not trigger the error callback.
/// </summary>
[Fact]
public async Task FireAndForget_DoesNotInvokeErrorCallback_ForCanceledTask()
{
var callbackInvoked = false;
using var cancellation = new CancellationTokenSource();
cancellation.Cancel();
TaskSafety.FireAndForget(Task.FromCanceled(cancellation.Token), _ => callbackInvoked = true);
await Task.Delay(150);
Assert.False(callbackInvoked);
}
}
}