Skip to content

Commit

Permalink
Merge pull request #84 from doomchild/feature/issue-83
Browse files Browse the repository at this point in the history
Issue-83: FIx Partition to returned resolved values, not Tasks
  • Loading branch information
doomchild authored Jul 26, 2024
2 parents 5d9099a + 76739d2 commit c5cb775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/TaskExtras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ private static Task<T> DoRetry<T>(
/// <typeparam name="T">The underlying type of the tasks in <paramref name="tasks"/>.</typeparam>
/// <param name="tasks">The collection of tasks to partition.</param>
/// <returns>A tuple of the partitioned tasks.</returns>
public static Task<(IReadOnlyList<Task<T>> Faulted, IReadOnlyList<Task<T>> Fulfilled)> Partition<T>(
public static Task<(IReadOnlyList<Exception> Faulted, IReadOnlyList<T> Fulfilled)> Partition<T>(
IEnumerable<Task<T>> tasks
)
{
return tasks.Aggregate<Task<T>, Task<(IReadOnlyList<Task<T>> Faulted, IReadOnlyList<Task<T>> Fulfilled)>>(
Task.FromResult(((IReadOnlyList<Task<T>>)new List<Task<T>>(), (IReadOnlyList<Task<T>>)new List<Task<T>>())),
ZipTasksWith<T, (IReadOnlyList<Task<T>> Faulted, IReadOnlyList<Task<T>> Fulfilled), (IReadOnlyList<Task<T>> Faulted, IReadOnlyList<Task<T>> Fulfilled), Exception>
return tasks.Aggregate<Task<T>, Task<(IReadOnlyList<Exception> Faulted, IReadOnlyList<T> Fulfilled)>>(
Task.FromResult(((IReadOnlyList<Exception>)new List<Exception>(), (IReadOnlyList<T>)new List<T>())),
ZipTasksWith<T, (IReadOnlyList<Exception> Faulted, IReadOnlyList<T> Fulfilled), (IReadOnlyList<Exception> Faulted, IReadOnlyList<T> Fulfilled), Exception>
(
(values, v) => (values.Faulted, values.Fulfilled.Append(Task.FromResult(v)).ToList()),
(values, v) => (values.Faulted.Append<Task<T>>(Task.FromException<T>(v)).ToList(), values.Fulfilled)
(values, v) => (values.Faulted, values.Fulfilled.Append(v).ToList()),
(values, v) => (values.Faulted.Append<Exception>(v).ToList(), values.Fulfilled)
)
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/TaskChainingTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Jds.TestingUtils.MockHttp" Version="0.2.0" />
<PackageReference Include="FluentAssertions" Version="6.12" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 25 additions & 7 deletions tests/unit/TaskExtrasPartitionTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using FluentAssertions;
using RLC.TaskChaining;
using Xunit;

Expand All @@ -13,20 +13,38 @@ public class PartitionTests
[Fact]
public async Task ItShouldPartition()
{
int expectedFulfills = 4;
int expectedFaults = 1;
Func<Task<string>> throwFunc = () => throw new NullReferenceException();
List<Task<string>> tasks = new()
{
Task.FromResult("abc"),
Task.FromResult("def"),
Task.FromException<string>(new InvalidOperationException()),
Task.FromResult("ghi"),
TaskExtras.Defer(() => Task.FromResult("jkl"), TimeSpan.FromSeconds(1))
TaskExtras.Defer(() => Task.FromResult("jkl"), TimeSpan.FromSeconds(1)),
TaskExtras.Defer(throwFunc, TimeSpan.FromSeconds(1))
};
List<string> expectedFulfillments = new()
{
"abc",
"def",
"ghi",
"jkl"
};
List<Exception> expectedFaults = new()
{
new InvalidOperationException(),
new NullReferenceException()
};

(IEnumerable<Task<string>> Faulted, IEnumerable<Task<string>> Fulfilled) partition = await TaskExtras.Partition(tasks);
(IEnumerable<Exception> Faulted, IEnumerable<string> Fulfilled) partition = await TaskExtras.Partition(tasks);

Assert.Equal(expectedFaults, partition.Faulted.Count());
Assert.Equal(expectedFulfills, partition.Fulfilled.Count());
partition.Faulted.Should().BeEquivalentTo(
expectedFaults,
options => options.Excluding(ex => ex.TargetSite)
.Excluding(ex => ex.Source)
.Excluding(ex => ex.StackTrace)
.Excluding(ex => ex.HResult)
);
partition.Fulfilled.Should().BeEquivalentTo(expectedFulfillments);
}
}

0 comments on commit c5cb775

Please sign in to comment.