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
4 changes: 2 additions & 2 deletions src/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private static Exception HandleCancellation<T>(this Task<T> task)

return new Exception("Expected canceled task");
}
catch (OperationCanceledException exception)
catch (Exception exception)
{
return exception;
return PotentiallyUnwindException(exception);
}
}

Expand Down
60 changes: 50 additions & 10 deletions src/TaskExtensionsTap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public static Task<T> Tap<T>(this Task<T> task, Action<T> onFulfilled, Action<Ex
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

onFaulted(exception);

return Task.FromException<T>(exception);
}

return continuationTask.IsFaulted
Expand Down Expand Up @@ -64,7 +68,11 @@ public static Task<T> Tap<T>(this Task<T> task, Action<T> onFulfilled, Func<Exce
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

onFaulted(exception);

return Task.FromException<T>(exception);
}

return continuationTask.IsFaulted
Expand All @@ -88,7 +96,11 @@ public static Task<T> Tap<T, R>(this Task<T> task, Func<T, Task<R>> onFulfilled,
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

onFaulted(exception);

return Task.FromException<T>(exception);
}

return continuationTask.IsFaulted
Expand All @@ -111,7 +123,11 @@ public static Task<T> Tap<T>(this Task<T> task, Func<T, Task> onFulfilled, Actio
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

onFaulted(exception);

return Task.FromException<T>(exception);
}

return continuationTask.IsFaulted
Expand All @@ -131,17 +147,21 @@ public static Task<T> Tap<T>(this Task<T> task, Func<T, Task> onFulfilled, Actio
/// <param name="onFaulted">The function to execute if the task is faulted.</param>
/// <returns>The task.</returns>
public static Task<T> Tap<T, R, S>(this Task<T> task, Func<T, Task<R>> onFulfilled, Func<Exception, Task<S>> onFaulted)
=> task.ContinueWith(continuationTask =>
=> task.ContinueWith(async continuationTask =>
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

await onFaulted(exception);

return Task.FromException<T>(exception);
}

return continuationTask.IsFaulted
? continuationTask.IfFaulted(onFaulted)
: continuationTask.IfFulfilled(onFulfilled);
}).Unwrap();
}).Unwrap().Unwrap();

/// <summary>
/// Executes a function and discards the result on a <see name="Task{T}"/> whether it is in a fulfilled or faulted state.
Expand All @@ -154,17 +174,37 @@ public static Task<T> Tap<T, R, S>(this Task<T> task, Func<T, Task<R>> onFulfill
/// <param name="onFaulted">The function to execute if the task is faulted.</param>
/// <returns>The task.</returns>
public static Task<T> Tap<T>(this Task<T> task, Func<T, Task> onFulfilled, Func<Exception, Task> onFaulted)
=> task.ContinueWith(continuationTask =>
=> task.ContinueWith(async continuationTask =>
{
if (continuationTask.IsCanceled)
{
return continuationTask;
Exception exception = HandleCancellation(continuationTask);

await onFaulted(exception);

return Task.FromException<T>(exception);
}

if (continuationTask is { IsFaulted: false, Result: Task { IsCanceled: true } resultTask })
{
try
{
await resultTask;
}
catch (Exception exception)
{
var resultException = PotentiallyUnwindException(exception);

await onFaulted(resultException);

return continuationTask;
}
}

return continuationTask.IsFaulted
? continuationTask.IfFaulted(onFaulted)
: continuationTask.IfFulfilled(onFulfilled);
}).Unwrap();
}).Unwrap().Unwrap();

/// <summary>
/// Executes a function and discards the result on a <see name="Task{T}"/> whether it is in a fulfilled or faulted state.
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/IfFaulted/WithAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using RLC.TaskChaining;
using Xunit;
Expand Down Expand Up @@ -55,9 +56,12 @@ public async Task ItShouldPerformASideEffectForACancellation()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Action<Exception> onFaulted = _ => { actualValue = 5; };

cts.Cancel();

try
{
await Task.FromResult(0)
Expand All @@ -76,9 +80,12 @@ public async Task ItShouldPerformASideEffectForACancellationWithoutAwaiting()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Action<Exception> onFaulted = _ => { actualValue = 5; };

cts.Cancel();

_ = Task.FromResult(0)
.Then(func)
.IfFaulted(onFaulted);
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/IfFaulted/WithFullTaskFunc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using RLC.TaskChaining;
using Xunit;
Expand Down Expand Up @@ -90,20 +91,23 @@ public async Task ItShouldPerformASideEffectForACancellation()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<Exception, Task<int>> onFaulted = _ =>
{
actualValue = 5;
return Task.FromResult(5);
};

cts.Cancel();

try
{
await Task.FromResult(0)
.Then(func)
.IfFaulted(onFaulted);
}
catch (OperationCanceledException)
catch (TaskCanceledException)
{
}

Expand All @@ -115,13 +119,16 @@ public async Task ItShouldPerformASideEffectForACancellationWithoutAwaiting()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<Exception, Task<int>> onFaulted = _ =>
{
actualValue = 5;
return Task.FromResult(5);
};

cts.Cancel();

_ = Task.FromResult(0)
.Then(func)
.IfFaulted(onFaulted);
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/IfFaulted/WithRawTaskFunc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using RLC.TaskChaining;
using Xunit;
Expand Down Expand Up @@ -91,20 +92,23 @@ public async Task ItShouldPerformASideEffectForACancellation()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<Exception, Task> onFaulted = _ =>
{
actualValue = 5;
return Task.CompletedTask;
};

cts.Cancel();

try
{
await Task.FromResult(0)
.Then(func)
.IfFaulted(onFaulted);
}
catch (OperationCanceledException)
catch (TaskCanceledException)
{
}

Expand All @@ -116,13 +120,16 @@ public async Task ItShouldPerformASideEffectForACancellationWithoutAwaiting()
{
int actualValue = 0;
int expectedValue = 5;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<Exception, Task> onFaulted = _ =>
{
actualValue = 5;
return Task.CompletedTask;
};

cts.Cancel();

_ = Task.FromResult(0)
.Then(func)
.IfFaulted(onFaulted);
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/IfFulfilled/WithAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using RLC.TaskChaining;
using Xunit;
Expand Down Expand Up @@ -70,15 +71,18 @@ public async Task ItShouldNotPerformASideEffectForACancellation()
{
int actualValue = 0;
int expectedValue = 0;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);

cts.Cancel();

try
{
await Task.FromResult<int>(0)
.Then(func)
.IfFulfilled(_ => { actualValue = 5; });
}
catch (OperationCanceledException)
catch (TaskCanceledException)
{
}

Expand All @@ -90,7 +94,10 @@ public async Task ItShouldNotPerformASideEffectForACancellationWithoutAwaiting()
{
int actualValue = 0;
int expectedValue = 0;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);

cts.Cancel();

_ = Task.FromResult<int>(0)
.Then(func)
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/IfFulfilled/WithFullTaskFunc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using RLC.TaskChaining;
using Xunit;
Expand Down Expand Up @@ -90,20 +91,23 @@ public async Task ItShouldNotPerformASideEffectForACancellation()
{
int actualValue = 0;
int expectedValue = 0;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<string, Task<int>> onFulfilled = _ =>
{
actualValue = 5;
return Task.FromResult(5);
};

cts.Cancel();

try
{
await Task.FromResult(0)
.Then(func)
.IfFulfilled(onFulfilled);
}
catch (OperationCanceledException)
catch (TaskCanceledException)
{
}

Expand All @@ -115,13 +119,16 @@ public async Task ItShouldNotPerformASideEffectForACancellationWithoutAwaiting()
{
int actualValue = 0;
int expectedValue = 0;
Func<int, string> func = _ => throw new TaskCanceledException();
CancellationTokenSource cts = new();
Func<int, Task<string>> func = _ => Task.Run(() => string.Empty, cts.Token);
Func<string, Task<int>> onFulfilled = _ =>
{
actualValue = 5;
return Task.FromResult(5);
};

cts.Cancel();

_ = Task.FromResult(0)
.Then(func)
.IfFulfilled(onFulfilled);
Expand Down
Loading