Path: redb-route/tests/redb.Route.Tests.Sql/Batch/Fakes/CountingAsyncSource.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/Batch/Fakes/CountingAsyncSource.cs
----
namespace redb.Route.Tests.Sql.Batch.Fakes;
///
/// An asynchronous batch source that counts what the connector read from it, can fail after a given number of items, and
/// records whether its enumerator was disposed.
///
internal sealed class CountingAsyncSource : IAsyncEnumerable
{
private readonly IReadOnlyList _items;
private readonly int? _failAfter;
public CountingAsyncSource(IReadOnlyList items, int? failAfter = null)
{
_items = items;
_failAfter = failAfter;
}
/// Items handed out so far.
public int ItemsRead { get; private set; }
/// The enumerator was disposed.
public bool Disposed { get; private set; }
public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) =>
new Enumerator(this, cancellationToken);
private sealed class Enumerator(CountingAsyncSource source, CancellationToken ct) : IAsyncEnumerator
{
private int _index = -1;
public T Current => source._items[_index];
public ValueTask MoveNextAsync()
{
ct.ThrowIfCancellationRequested();
if (source._failAfter is { } failAfter && _index + 1 == failAfter)
throw new FakeSourceException($"fake: the source failed after {failAfter} items");
if (_index + 1 >= source._items.Count)
return ValueTask.FromResult(false);
_index++;
source.ItemsRead++;
return ValueTask.FromResult(true);
}
public ValueTask DisposeAsync()
{
source.Disposed = true;
return ValueTask.CompletedTask;
}
}
}
/// A failure of the batch source itself, not of an item's statement.
internal sealed class FakeSourceException(string message) : Exception(message);