Skip to content

Commit

Permalink
test: fixing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Nov 10, 2024
1 parent bc3b1df commit 8c5aba3
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,20 @@
namespace Codibre.EnumerableExtensions.Branching;
public class AsyncBranchingBuilder<T>(IAsyncEnumerable<T> source) : BaseBranchingBuilder<T>
{
protected override IAsyncEnumerable<T> Source { get; } = source;
internal override async ValueTask<(LinkedNode<T>?, Task)> Iterate()
{
var enumerator = source.GetAsyncEnumerator();
var node = await enumerator.MoveNextAsync() ? new LinkedNode<T>(enumerator.Current) : null;
return (node, node is null ? Task.CompletedTask : Task.Run(async () =>
{
try
{
while (await enumerator.MoveNextAsync()) node = node.Next = new LinkedNode<T>(enumerator.Current);
}
finally
{
node.End = true;
}
}));
}
}
19 changes: 2 additions & 17 deletions src/Codibre.EnumerableExtensions.Branching/BaseBranchingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,15 @@ internal BaseBranchingBuilder<T> Add(Func<IAsyncEnumerable<T>, Task> branch)
return this;
}

protected abstract IAsyncEnumerable<T> Source { get; }

public async Task Run()
{
var enumerator = Source.GetAsyncEnumerator();
ILinkedNode<T>? node = await enumerator.MoveNextAsync() ? new LinkedNode<T>(enumerator.Current) : null;
var iterate = Iterate(node, enumerator);
var (node, iterate) = await Iterate();
await Task.WhenAll(
_branches
.Select((x, index) => x(node.GetBranchedIterable()))
.Append(iterate)
);
}

private static async Task Iterate(ILinkedNode<T>? node, IAsyncEnumerator<T> enumerator)
{
if (node is null) return;
try
{
while (await enumerator.MoveNextAsync()) node = node.Next = new LinkedNode<T>(enumerator.Current);
}
finally
{
node.End = true;
}
}
internal abstract ValueTask<(LinkedNode<T>?, Task)> Iterate();
}
17 changes: 16 additions & 1 deletion src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@ namespace Codibre.EnumerableExtensions.Branching;

public class BranchingBuilder<T>(IEnumerable<T> source) : BaseBranchingBuilder<T>
{
protected override IAsyncEnumerable<T> Source { get; } = source.ToAsyncEnumerable();
internal override ValueTask<(LinkedNode<T>?, Task)> Iterate()
{
var enumerator = source.GetEnumerator();
var node = enumerator.MoveNext() ? new LinkedNode<T>(enumerator.Current) : null;
return ValueTask.FromResult((node, node is null ? Task.CompletedTask : Task.Run(async () =>

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 15 in src/Codibre.EnumerableExtensions.Branching/BranchingBuilder.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
try
{
while (enumerator.MoveNext()) node = node.Next = new LinkedNode<T>(enumerator.Current);
}
finally
{
node.End = true;
}
})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Codibre.EnumerableExtensions.Branching.Internal;

internal record BranchedEnumerable<T>(ILinkedNode<T> node) : IAsyncEnumerable<T>
internal record BranchedEnumerable<T>(LinkedNode<T> node) : IAsyncEnumerable<T>
{
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
=> new BranchedEnumerator<T>(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ namespace Codibre.EnumerableExtensions.Branching.Internal;

internal record BranchedEnumerator<T> : IAsyncEnumerator<T>
{
private ILinkedNode<T> _node;
public BranchedEnumerator(ILinkedNode<T> node) => _node = new LinkedNode<T>(default)
private LinkedNode<T> _node;
private readonly int _altenateOn;
private int _count;
public BranchedEnumerator(LinkedNode<T> node, int altenateOn = 100)
{
Next = node,
};
_node = new LinkedNode<T>(default)

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.

Check warning on line 12 in src/Codibre.EnumerableExtensions.Branching/Internal/BranchedEnumerator.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'value' in 'LinkedNode<T>.LinkedNode(T value)'.
{
Next = node,
};
_altenateOn = altenateOn;
_count = altenateOn;
}

public T Current => _node.Value;
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
Expand All @@ -18,6 +25,12 @@ public async ValueTask<bool> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Codibre.EnumerableExtensions.Branching.Internal;

internal static class BranchingHelper
{
internal static IAsyncEnumerable<T> GetBranchedIterable<T>(this ILinkedNode<T>? node)
=> node is null ? new List<T>().ToAsyncEnumerable() : new BranchedEnumerable<T>(node);
internal static IAsyncEnumerable<T> GetBranchedIterable<T>(this LinkedNode<T>? node)
=> node is null ? Array.Empty<T>().ToAsyncEnumerable() : new BranchedEnumerable<T>(node);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Codibre.EnumerableExtensions.Branching.Internal;

internal struct LinkedNode<T>(T value) : ILinkedNode<T>
internal record LinkedNode<T>(T value)
{
public T Value { get; } = value;
public ILinkedNode<T>? Next { get; set; } = null;
public LinkedNode<T>? Next { get; set; } = null;
public bool End { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static IEnumerable<int> AddOps(this IEnumerable<int> source, int value)

public class Benchmarks
{
[Params(100, 1000, 10000)]
[Params(100)]
public int _size = 100;
private IEnumerable<int> GetBaseEnumerable()
=> Enumerable.Range(0, _size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> steps = [];
var enumerable = Op(list);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> steps = [];
var enumerable = Op(list);
Expand All @@ -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);
}
Expand Down

0 comments on commit 8c5aba3

Please sign in to comment.