From c461f276267e9939b16798f6920e702a900d2144 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 31 Oct 2023 19:05:49 -0500 Subject: [PATCH 1/2] Updated BiBind to transition canceled tasks to faulted --- src/TaskExtensions.cs | 22 ++++++++++++++++++---- tests/unit/TaskChainingTests.cs | 31 +++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/TaskExtensions.cs b/src/TaskExtensions.cs index d19d106..6b6ce46 100644 --- a/src/TaskExtensions.cs +++ b/src/TaskExtensions.cs @@ -99,10 +99,24 @@ public static Task BiBind( this Task task, Func> onFaulted, Func> onFulfilled - ) => task.ContinueWith(async continuationTask => continuationTask.IsFaulted - ? onFaulted(PotentiallyUnwindException(continuationTask.Exception!)) - : onFulfilled(await continuationTask) - ).Unwrap().Unwrap(); + ) => task.ContinueWith(async continuationTask => + { + if (continuationTask.IsCanceled) + { + try + { + await continuationTask; + } + catch (OperationCanceledException exception) + { + return Task.FromException(exception); + } + } + + return continuationTask.IsFaulted + ? onFaulted(PotentiallyUnwindException(continuationTask.Exception!)) + : onFulfilled(await continuationTask); + }).Unwrap().Unwrap(); /// /// Disjunctive `leftMap`. diff --git a/tests/unit/TaskChainingTests.cs b/tests/unit/TaskChainingTests.cs index 0b14d31..af38323 100644 --- a/tests/unit/TaskChainingTests.cs +++ b/tests/unit/TaskChainingTests.cs @@ -819,16 +819,39 @@ public async void ItShouldCaptureTaskCancellation() .RespondWith((responseBuilder, _) => responseBuilder.WithStatusCode(HttpStatusCode.OK)) ) .BuildHttpClient(); - ; + CancellationTokenSource testTokenSource = new(); testTokenSource.Cancel(); - Task testTask = Task.FromResult("http://anything.anywhere") - .Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token)); + Task testTask = Task.FromResult("https://www.google.com") + .Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token)) + .Then(_ => Guid.NewGuid().ToString()); + + await Task.Delay(50); - await Assert.ThrowsAsync(async () => await testTask); + Assert.True(testTask.IsFaulted); } } + + public async void ItShouldCaptureTaskCancellationException() + { + HttpClient testHttpClient = new MockHttpBuilder() + .WithHandler(messageCaseBuilder => messageCaseBuilder.AcceptAll() + .RespondWith((responseBuilder, _) => responseBuilder.WithStatusCode(HttpStatusCode.OK)) + ) + .BuildHttpClient(); + + CancellationTokenSource testTokenSource = new(); + testTokenSource.Cancel(); + + Task testTask = Task.FromResult("https://www.google.com") + .Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token)) + .Then(_ => Guid.NewGuid().ToString()); + + await Task.Delay(50); + + await Assert.ThrowsAsync(async () => await testTask); + } } public class ForBoth From 9cf4c193ea576497972295c73c51927477e214bc Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 1 Nov 2023 10:19:17 -0500 Subject: [PATCH 2/2] Updated package version to 2.15.0 --- project-metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project-metadata.json b/project-metadata.json index de929d6..8d99ca5 100644 --- a/project-metadata.json +++ b/project-metadata.json @@ -2,7 +2,7 @@ "name": "task-chaining", "description": "Extension methods to System.Threading.Task to allow Promise-like chaining", "title": "TaskChaining", - "version": "2.14.0", + "version": "2.15.0", "ciEnvironment": { "variables": [ {