Skip to content

Commit d1186db

Browse files
Fix bug in async Batch operator (#652)
1 parent 768f18c commit d1186db

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Source/SuperLinq.Async/Batch.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SuperLinq.Async;
1+
namespace SuperLinq.Async;
22

33
public static partial class AsyncSuperEnumerable
44
{
@@ -42,6 +42,7 @@ static async IAsyncEnumerable<IList<TSource>> Core(
4242
if (n == size)
4343
{
4444
yield return array;
45+
array = null;
4546
n = 0;
4647
}
4748
}

Tests/SuperLinq.Async.Test/BatchTest.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Test.Async;
1+
namespace Test.Async;
22

33
public sealed class BatchTest
44
{
@@ -17,6 +17,20 @@ public void BatchValidatesSize()
1717
.Batch(0));
1818
}
1919

20+
[Fact]
21+
public async Task BatchDoesNotReturnSameArrayInstance()
22+
{
23+
await using var seq = Enumerable.Range(1, 4).AsTestingSequence();
24+
await using var e = seq.Batch(2).GetAsyncEnumerator();
25+
26+
_ = await e.MoveNextAsync();
27+
var batch1 = e.Current;
28+
_ = await e.MoveNextAsync();
29+
var batch2 = e.Current;
30+
31+
Assert.NotEqual(batch1, batch2);
32+
}
33+
2034
[Fact]
2135
public async Task BatchWithEmptySource()
2236
{

Tests/SuperLinq.Test/BatchTest.cs

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ public static IEnumerable<object[]> GetFourElementSequences() =>
4444
.GetListSequences()
4545
.Select(x => new object[] { x });
4646

47+
[Theory]
48+
[MemberData(nameof(GetFourElementSequences))]
49+
public void BatchDoesNotReturnSameArrayInstance(IDisposableEnumerable<int> seq)
50+
{
51+
using (seq)
52+
{
53+
using var e = seq.Batch(2).GetEnumerator();
54+
55+
_ = e.MoveNext();
56+
var batch1 = e.Current;
57+
_ = e.MoveNext();
58+
var batch2 = e.Current;
59+
60+
Assert.NotEqual(batch1, batch2);
61+
}
62+
}
63+
4764
[Theory]
4865
[MemberData(nameof(GetFourElementSequences))]
4966
public void BatchModifiedBeforeMoveNextDoesNotAffectNextBatch(IDisposableEnumerable<int> seq)

0 commit comments

Comments
 (0)