Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions PolyShim.Tests/Net50/TaskCompletionSourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net50;

public class TaskCompletionSourceTests
{
[Fact]
public async Task SetCanceled_Test()
{
// Arrange
var cancellationToken = new CancellationToken(true);
var tcs = new TaskCompletionSource<int>();

// Act
tcs.SetCanceled(cancellationToken);
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
await tcs.Task
);

// Assert
tcs.Task.IsCanceled.Should().BeTrue();
ex.CancellationToken.Should().Be(cancellationToken);
ex.CancellationToken.IsCancellationRequested.Should().BeTrue();
}

[Fact]
public void SetCanceled_AlreadyCompleted_Test()
{
// Arrange
var cancellationToken = new CancellationToken(true);
var tcs = new TaskCompletionSource<int>();
tcs.SetResult(42);

// Act & assert
Assert.ThrowsAny<InvalidOperationException>(() => tcs.SetCanceled(cancellationToken));
}
}
18 changes: 17 additions & 1 deletion PolyShim.Tests/NetCore10/TaskCompletionSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@ public async Task TrySetCanceled_Test()
var tcs = new TaskCompletionSource<int>();

// Act
tcs.TrySetCanceled(cancellationToken);
var result = tcs.TrySetCanceled(cancellationToken);
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
await tcs.Task
);

// Assert
result.Should().BeTrue();
tcs.Task.IsCanceled.Should().BeTrue();
ex.CancellationToken.Should().Be(cancellationToken);
ex.CancellationToken.IsCancellationRequested.Should().BeTrue();
}

[Fact]
public void TrySetCanceled_AlreadyCompleted_Test()
{
// Arrange
var cancellationToken = new CancellationToken(true);
var tcs = new TaskCompletionSource<int>();
tcs.SetResult(42);

// Act
var result = tcs.TrySetCanceled(cancellationToken);

// Assert
result.Should().BeFalse();
}
}
29 changes: 29 additions & 0 deletions PolyShim/Net50/TaskCompletionSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if FEATURE_TASK
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Threading;
using System.Threading.Tasks;

internal static partial class PolyfillExtensions
{
extension<T>(TaskCompletionSource<T> source)
{
// https://learn.microsoft.com/dotnet/api/system.threading.tasks.taskcompletionsource-1.setcanceled#system-threading-tasks-taskcompletionsource-1-setcanceled(system-threading-cancellationtoken)
public void SetCanceled(CancellationToken cancellationToken)
{
if (!source.TrySetCanceled(cancellationToken))
throw new InvalidOperationException("The task is already completed, canceled, or failed.");
}
}
}
#endif
#endif
Loading