Skip to content

Commit

Permalink
Merge pull request #58 from doomchild/dev
Browse files Browse the repository at this point in the history
2.15.0
  • Loading branch information
doomchild authored Nov 1, 2023
2 parents 55ca2ce + 9cf4c19 commit 88e00cd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion project-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
22 changes: 18 additions & 4 deletions src/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,24 @@ public static Task<TNext> BiBind<T, TNext>(
this Task<T> task,
Func<Exception, Task<TNext>> onFaulted,
Func<T, Task<TNext>> 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<TNext>(exception);
}
}
return continuationTask.IsFaulted
? onFaulted(PotentiallyUnwindException(continuationTask.Exception!))
: onFulfilled(await continuationTask);
}).Unwrap().Unwrap();

/// <summary>
/// Disjunctive `leftMap`.
Expand Down
31 changes: 27 additions & 4 deletions tests/unit/TaskChainingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,39 @@ public async void ItShouldCaptureTaskCancellation()
.RespondWith((responseBuilder, _) => responseBuilder.WithStatusCode(HttpStatusCode.OK))
)
.BuildHttpClient();
;

CancellationTokenSource testTokenSource = new();
testTokenSource.Cancel();

Task<string> testTask = Task.FromResult<string>("http://anything.anywhere")
.Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token));
Task<string> testTask = Task.FromResult<string>("https://www.google.com")
.Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token))
.Then(_ => Guid.NewGuid().ToString());

await Task.Delay(50);

await Assert.ThrowsAsync<TaskCanceledException>(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<string> testTask = Task.FromResult<string>("https://www.google.com")
.Then(async url => await testHttpClient.GetStringAsync(url, testTokenSource.Token))
.Then(_ => Guid.NewGuid().ToString());

await Task.Delay(50);

await Assert.ThrowsAsync<TaskCanceledException>(async () => await testTask);
}
}

public class ForBoth
Expand Down

0 comments on commit 88e00cd

Please sign in to comment.