-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAsyncEnumerableProcessorTests.cs
More file actions
232 lines (197 loc) · 6.81 KB
/
Copy pathAsyncEnumerableProcessorTests.cs
File metadata and controls
232 lines (197 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#if NET6_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnumerableAsyncProcessor.Extensions;
using System.Runtime.CompilerServices;
using TUnit.Assertions;
using TUnit.Core;
namespace EnumerableAsyncProcessor.UnitTests;
public class AsyncEnumerableProcessorTests
{
private static async IAsyncEnumerable<int> GenerateAsyncEnumerable(int count, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
for (int i = 1; i <= count; i++)
{
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();
yield return i;
}
}
[Test]
public async Task ForEachAsync_ProcessOneAtATime_ProcessesAllItems()
{
var processedItems = new List<int>();
var asyncEnumerable = GenerateAsyncEnumerable(10);
await asyncEnumerable
.ForEachAsync(async item =>
{
await Task.Delay(10);
lock (processedItems)
{
processedItems.Add(item);
}
})
.ProcessOneAtATime()
.ExecuteAsync();
await Assert.That(processedItems.Count).IsEqualTo(10);
await Assert.That(processedItems).IsEquivalentTo(Enumerable.Range(1, 10));
}
[Test]
public async Task ForEachAsync_ProcessInParallel_ProcessesAllItems()
{
var processedItems = new List<int>();
var asyncEnumerable = GenerateAsyncEnumerable(20);
await asyncEnumerable
.ForEachAsync(async item =>
{
await Task.Delay(10);
lock (processedItems)
{
processedItems.Add(item);
}
})
.ProcessInParallel(5)
.ExecuteAsync();
await Assert.That(processedItems.Count).IsEqualTo(20);
await Assert.That(processedItems.OrderBy(x => x)).IsEquivalentTo(Enumerable.Range(1, 20));
}
[Test]
public async Task SelectAsync_ProcessOneAtATime_ReturnsTransformedItems()
{
var asyncEnumerable = GenerateAsyncEnumerable(5);
var results = await asyncEnumerable
.SelectAsync(async item =>
{
await Task.Delay(10);
return item * 2;
})
.ProcessOneAtATime()
.ExecuteAsync()
.ToListAsync();
await Assert.That(results.Count).IsEqualTo(5);
await Assert.That(results).IsEquivalentTo(new[] { 2, 4, 6, 8, 10 });
}
[Test]
public async Task SelectAsync_ProcessInParallel_ReturnsAllTransformedItems()
{
var asyncEnumerable = GenerateAsyncEnumerable(10);
var results = await asyncEnumerable
.SelectAsync(async item =>
{
await Task.Delay(10);
return item * 2;
})
.ProcessInParallel(3)
.ExecuteAsync()
.ToListAsync();
await Assert.That(results.Count).IsEqualTo(10);
await Assert.That(results.OrderBy(x => x)).IsEquivalentTo(Enumerable.Range(1, 10).Select(x => x * 2));
}
[Test]
public async Task ForEachAsync_ProcessInParallel_WithHighConcurrency_HandlesCorrectly()
{
var processedCount = 0;
var asyncEnumerable = GenerateAsyncEnumerable(100);
await asyncEnumerable
.ForEachAsync(async item =>
{
await Task.Delay(5);
Interlocked.Increment(ref processedCount);
})
.ProcessInParallel(50)
.ExecuteAsync();
await Assert.That(processedCount).IsEqualTo(100);
}
[Test]
public async Task SelectAsync_ProcessInParallel_WithHighConcurrency_HandlesCorrectly()
{
var asyncEnumerable = GenerateAsyncEnumerable(50);
var results = await asyncEnumerable
.SelectAsync(async item =>
{
await Task.Delay(5);
return item * 3;
})
.ProcessInParallel(25)
.ExecuteAsync()
.ToListAsync();
await Assert.That(results.Count).IsEqualTo(50);
await Assert.That(results.OrderBy(x => x)).IsEquivalentTo(Enumerable.Range(1, 50).Select(x => x * 3));
}
[Test]
public async Task ForEachAsync_WithCancellation_StopsProcessing()
{
var cts = new CancellationTokenSource();
var processedCount = 0;
var asyncEnumerable = GenerateAsyncEnumerable(100, cts.Token);
var task = asyncEnumerable
.ForEachAsync(async item =>
{
// Check cancellation before processing
if (cts.Token.IsCancellationRequested)
return;
if (item == 10)
{
cts.Cancel();
}
await Task.Delay(10);
// Check cancellation after delay
if (cts.Token.IsCancellationRequested)
return;
Interlocked.Increment(ref processedCount);
}, cts.Token)
.ProcessInParallel(5)
.ExecuteAsync();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
await Assert.That(processedCount).IsLessThan(100);
}
[Test]
public async Task SelectAsync_WithEmptyAsyncEnumerable_ReturnsEmptyResults()
{
var asyncEnumerable = GenerateAsyncEnumerable(0);
var results = await asyncEnumerable
.SelectAsync(async item =>
{
await Task.Delay(10);
return item * 2;
})
.ProcessInParallel(5)
.ExecuteAsync()
.ToListAsync();
await Assert.That(results).IsEmpty();
}
[Test]
public async Task ForEachAsync_WithException_PropagatesException()
{
var asyncEnumerable = GenerateAsyncEnumerable(10);
var task = asyncEnumerable
.ForEachAsync(async item =>
{
await Task.Delay(10);
if (item == 5)
{
throw new InvalidOperationException("Test exception");
}
})
.ProcessInParallel(3)
.ExecuteAsync();
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await task);
await Assert.That(exception!.Message).IsEqualTo("Test exception");
}
}
internal static class AsyncEnumerableExtensionsForTests
{
public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
{
var list = new List<T>();
await foreach (var item in source)
{
list.Add(item);
}
return list;
}
}
#endif