From 8c5aba3969fb5775a992ebe6e9ae4b5132a6a0a4 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Sat, 9 Nov 2024 18:56:49 -0300 Subject: [PATCH] test: fixing test --- .../AsyncBranchingBuilder.cs | 17 ++++++++++++++- .../BaseBranchingBuilder.cs | 19 ++--------------- .../BranchingBuilder.cs | 17 ++++++++++++++- .../Internal/BranchedEnumerable.cs | 2 +- .../Internal/BranchedEnumerator.cs | 21 +++++++++++++++---- .../Internal/BranchingHelper.cs | 4 ++-- .../Internal/ILinkedNode.cs | 8 ------- .../Internal/LinkedNode.cs | 4 ++-- .../Benchmarks.cs | 2 +- .../AsyncEnumerableExtensionsTest.cs | 3 +-- .../EnumerableExtensionsTest.cs | 3 +-- 11 files changed, 59 insertions(+), 41 deletions(-) delete mode 100644 src/Codibre.EnumerableExtensions.Branching/Internal/ILinkedNode.cs diff --git a/src/Codibre.EnumerableExtensions.Branching/AsyncBranchingBuilder.cs b/src/Codibre.EnumerableExtensions.Branching/AsyncBranchingBuilder.cs index fc7b558..3ff8965 100644 --- a/src/Codibre.EnumerableExtensions.Branching/AsyncBranchingBuilder.cs +++ b/src/Codibre.EnumerableExtensions.Branching/AsyncBranchingBuilder.cs @@ -7,5 +7,20 @@ namespace Codibre.EnumerableExtensions.Branching; public class AsyncBranchingBuilder(IAsyncEnumerable source) : BaseBranchingBuilder { - protected override IAsyncEnumerable Source { get; } = source; + internal override async ValueTask<(LinkedNode?, Task)> Iterate() + { + var enumerator = source.GetAsyncEnumerator(); + var node = await enumerator.MoveNextAsync() ? new LinkedNode(enumerator.Current) : null; + return (node, node is null ? Task.CompletedTask : Task.Run(async () => + { + try + { + while (await enumerator.MoveNextAsync()) node = node.Next = new LinkedNode(enumerator.Current); + } + finally + { + node.End = true; + } + })); + } } \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/BaseBranchingBuilder.cs b/src/Codibre.EnumerableExtensions.Branching/BaseBranchingBuilder.cs index 5994a88..0a97530 100644 --- a/src/Codibre.EnumerableExtensions.Branching/BaseBranchingBuilder.cs +++ b/src/Codibre.EnumerableExtensions.Branching/BaseBranchingBuilder.cs @@ -16,13 +16,9 @@ internal BaseBranchingBuilder Add(Func, Task> branch) return this; } - protected abstract IAsyncEnumerable Source { get; } - public async Task Run() { - var enumerator = Source.GetAsyncEnumerator(); - ILinkedNode? node = await enumerator.MoveNextAsync() ? new LinkedNode(enumerator.Current) : null; - var iterate = Iterate(node, enumerator); + var (node, iterate) = await Iterate(); await Task.WhenAll( _branches .Select((x, index) => x(node.GetBranchedIterable())) @@ -30,16 +26,5 @@ await Task.WhenAll( ); } - private static async Task Iterate(ILinkedNode? node, IAsyncEnumerator enumerator) - { - if (node is null) return; - try - { - while (await enumerator.MoveNextAsync()) node = node.Next = new LinkedNode(enumerator.Current); - } - finally - { - node.End = true; - } - } + internal abstract ValueTask<(LinkedNode?, Task)> Iterate(); } \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs b/src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs index 26321c7..11f2d59 100644 --- a/src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs +++ b/src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs @@ -8,5 +8,20 @@ namespace Codibre.EnumerableExtensions.Branching; public class BranchingBuilder(IEnumerable source) : BaseBranchingBuilder { - protected override IAsyncEnumerable Source { get; } = source.ToAsyncEnumerable(); + internal override ValueTask<(LinkedNode?, Task)> Iterate() + { + var enumerator = source.GetEnumerator(); + var node = enumerator.MoveNext() ? new LinkedNode(enumerator.Current) : null; + return ValueTask.FromResult((node, node is null ? Task.CompletedTask : Task.Run(async () => + { + try + { + while (enumerator.MoveNext()) node = node.Next = new LinkedNode(enumerator.Current); + } + finally + { + node.End = true; + } + }))); + } } \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerable.cs b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerable.cs index cc8480b..6308983 100644 --- a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerable.cs +++ b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerable.cs @@ -2,7 +2,7 @@ namespace Codibre.EnumerableExtensions.Branching.Internal; -internal record BranchedEnumerable(ILinkedNode node) : IAsyncEnumerable +internal record BranchedEnumerable(LinkedNode node) : IAsyncEnumerable { public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) => new BranchedEnumerator(node); diff --git a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs index 6239dc1..7877a27 100644 --- a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs +++ b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs @@ -4,11 +4,18 @@ namespace Codibre.EnumerableExtensions.Branching.Internal; internal record BranchedEnumerator : IAsyncEnumerator { - private ILinkedNode _node; - public BranchedEnumerator(ILinkedNode node) => _node = new LinkedNode(default) + private LinkedNode _node; + private readonly int _altenateOn; + private int _count; + public BranchedEnumerator(LinkedNode node, int altenateOn = 100) { - Next = node, - }; + _node = new LinkedNode(default) + { + Next = node, + }; + _altenateOn = altenateOn; + _count = altenateOn; + } public T Current => _node.Value; public ValueTask DisposeAsync() => ValueTask.CompletedTask; @@ -18,6 +25,12 @@ public async ValueTask MoveNextAsync() while (!_node.End && _node.Next is null) await Task.Yield(); if (_node.Next is null) return false; _node = _node.Next; + _count--; + if (_count <= 0) + { + _count = _altenateOn; + await Task.Yield(); + } return true; } } \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchingHelper.cs b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchingHelper.cs index f4d379b..e6bff55 100644 --- a/src/Codibre.EnumerableExtensions.Branching/Internal/BranchingHelper.cs +++ b/src/Codibre.EnumerableExtensions.Branching/Internal/BranchingHelper.cs @@ -8,6 +8,6 @@ namespace Codibre.EnumerableExtensions.Branching.Internal; internal static class BranchingHelper { - internal static IAsyncEnumerable GetBranchedIterable(this ILinkedNode? node) - => node is null ? new List().ToAsyncEnumerable() : new BranchedEnumerable(node); + internal static IAsyncEnumerable GetBranchedIterable(this LinkedNode? node) + => node is null ? Array.Empty().ToAsyncEnumerable() : new BranchedEnumerable(node); } \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/Internal/ILinkedNode.cs b/src/Codibre.EnumerableExtensions.Branching/Internal/ILinkedNode.cs deleted file mode 100644 index 35009c6..0000000 --- a/src/Codibre.EnumerableExtensions.Branching/Internal/ILinkedNode.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Codibre.EnumerableExtensions.Branching.Internal; - -internal interface ILinkedNode -{ - public T Value { get; } - public ILinkedNode? Next { get; set; } - public bool End { get; set; } -} \ No newline at end of file diff --git a/src/Codibre.EnumerableExtensions.Branching/Internal/LinkedNode.cs b/src/Codibre.EnumerableExtensions.Branching/Internal/LinkedNode.cs index 2bc0715..35b2b8c 100644 --- a/src/Codibre.EnumerableExtensions.Branching/Internal/LinkedNode.cs +++ b/src/Codibre.EnumerableExtensions.Branching/Internal/LinkedNode.cs @@ -1,8 +1,8 @@ namespace Codibre.EnumerableExtensions.Branching.Internal; -internal struct LinkedNode(T value) : ILinkedNode +internal record LinkedNode(T value) { public T Value { get; } = value; - public ILinkedNode? Next { get; set; } = null; + public LinkedNode? Next { get; set; } = null; public bool End { get; set; } = false; } \ No newline at end of file diff --git a/test/Codibre.EnumerableExtensions.Branching.Benchmark/Benchmarks.cs b/test/Codibre.EnumerableExtensions.Branching.Benchmark/Benchmarks.cs index 5135907..2070413 100644 --- a/test/Codibre.EnumerableExtensions.Branching.Benchmark/Benchmarks.cs +++ b/test/Codibre.EnumerableExtensions.Branching.Benchmark/Benchmarks.cs @@ -13,7 +13,7 @@ public static IEnumerable AddOps(this IEnumerable source, int value) public class Benchmarks { - [Params(100, 1000, 10000)] + [Params(100)] public int _size = 100; private IEnumerable GetBaseEnumerable() => Enumerable.Range(0, _size) diff --git a/test/Codibre.EnumerableExtensions.Branching.Test/AsyncEnumerableExtensionsTest.cs b/test/Codibre.EnumerableExtensions.Branching.Test/AsyncEnumerableExtensionsTest.cs index 24c7e26..29dfe71 100644 --- a/test/Codibre.EnumerableExtensions.Branching.Test/AsyncEnumerableExtensionsTest.cs +++ b/test/Codibre.EnumerableExtensions.Branching.Test/AsyncEnumerableExtensionsTest.cs @@ -74,7 +74,7 @@ await enumerable.Branch() public async Task Should_Intercalate_The_Steps_Between_Every_Branch() { // Arrange - var total = 100; + var total = 10000; var list = Enumerable.Range(0, total).ToAsyncEnumerable(); List steps = []; var enumerable = Op(list); @@ -99,7 +99,6 @@ await enumerable.Branch() .Run(); // Assert - helper.WriteLine(string.Join(",", steps.GetRange(0, 8))); var refValue = steps[0]; steps.TakeWhile((x) => x == refValue).Count().Should().BeLessThan(total); } diff --git a/test/Codibre.EnumerableExtensions.Branching.Test/EnumerableExtensionsTest.cs b/test/Codibre.EnumerableExtensions.Branching.Test/EnumerableExtensionsTest.cs index 925834f..8658e34 100644 --- a/test/Codibre.EnumerableExtensions.Branching.Test/EnumerableExtensionsTest.cs +++ b/test/Codibre.EnumerableExtensions.Branching.Test/EnumerableExtensionsTest.cs @@ -74,7 +74,7 @@ await enumerable.Branch() public async Task Should_Intercalate_The_Steps_Between_Every_Branch() { // Arrange - var total = 100; + var total = 10000; var list = Enumerable.Range(0, total); List steps = []; var enumerable = Op(list); @@ -99,7 +99,6 @@ await enumerable.Branch() .Run(); // Assert - helper.WriteLine(string.Join(",", steps.GetRange(0, 8))); var refValue = steps[0]; steps.TakeWhile((x) => x == refValue).Count().Should().BeLessThan(total); }