-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWorkerPoolBehaviourTests.cs
More file actions
152 lines (125 loc) · 5.3 KB
/
Copy pathWorkerPoolBehaviourTests.cs
File metadata and controls
152 lines (125 loc) · 5.3 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
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnumerableAsyncProcessor.Extensions;
using EnumerableAsyncProcessor.RunnableProcessors;
namespace EnumerableAsyncProcessor.UnitTests;
/// <summary>
/// Guards the worker-pool execution model used by the bounded parallel and timed
/// rate-limited processors: the concurrency limit must hold, every item must be
/// processed exactly once, oversized limits must not break anything, and cancellation
/// must promptly cancel the unprocessed remainder.
/// </summary>
public class WorkerPoolBehaviourTests
{
[Test]
public async Task Positional_And_Named_Concurrency_Limits_Use_The_Same_Processor()
{
await using var positional = new[] { 1 }
.ForEachAsync(_ => Task.CompletedTask)
.ProcessInParallel(1);
await using var named = new[] { 1 }
.ForEachAsync(_ => Task.CompletedTask)
.ProcessInParallel(maxConcurrency: 1);
await Assert.That(positional.GetType()).IsEqualTo(typeof(ParallelAsyncProcessor<int>));
await Assert.That(named.GetType()).IsEqualTo(typeof(ParallelAsyncProcessor<int>));
}
[Test, Repeat(3)]
public async Task MaxConcurrency_Path_Obeys_The_Limit_And_Processes_Everything(CancellationToken cancellationToken)
{
const int itemCount = 100;
const int limit = 8;
var currentlyRunning = 0;
var maxObserved = 0;
await using var processor = Enumerable.Range(0, itemCount).ToList()
.ForEachAsync(async _ =>
{
var running = Interlocked.Increment(ref currentlyRunning);
int snapshot;
while (running > (snapshot = Volatile.Read(ref maxObserved)))
{
Interlocked.CompareExchange(ref maxObserved, running, snapshot);
}
await Task.Delay(10, cancellationToken);
Interlocked.Decrement(ref currentlyRunning);
}, cancellationToken)
.ProcessInParallel(maxConcurrency: limit);
await processor.WaitAsync();
await Assert.That(maxObserved).IsLessThanOrEqualTo(limit);
await Assert.That(processor.GetEnumerableTasks().Count(x => x.IsCompletedSuccessfully)).IsEqualTo(itemCount);
}
[Test]
public async Task Every_Item_Is_Processed_Exactly_Once_With_Bounded_Concurrency()
{
const int itemCount = 500;
var processedItems = new ConcurrentBag<int>();
await using var processor = Enumerable.Range(0, itemCount).ToList()
.ForEachAsync(async i =>
{
processedItems.Add(i);
await Task.Yield();
})
.ProcessInParallel(16);
await processor.WaitAsync();
await Assert.That(processedItems.Count).IsEqualTo(itemCount);
await Assert.That(processedItems.Distinct().Count()).IsEqualTo(itemCount);
}
[Test]
public async Task Parallelism_Limit_Larger_Than_Item_Count_Completes_Normally()
{
await using var bounded = Enumerable.Range(0, 5).ToList()
.ForEachAsync(_ => Task.CompletedTask)
.ProcessInParallel(100);
await bounded.WaitAsync();
await using var throttled = Enumerable.Range(0, 5).ToList()
.SelectAsync(i => Task.FromResult(i))
.ProcessInParallel(maxConcurrency: 100);
var results = await throttled.GetResultsAsync();
await Assert.That(bounded.GetEnumerableTasks().Count(x => x.IsCompletedSuccessfully)).IsEqualTo(5);
await Assert.That(results.Length).IsEqualTo(5);
}
[Test]
public async Task Timed_RateLimited_Processor_Cancels_Unprocessed_Items_Promptly()
{
using var cancellationTokenSource = new CancellationTokenSource();
var firstItemStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var processor = Enumerable.Range(0, 20).ToList()
.ForEachAsync(_ =>
{
firstItemStarted.TrySetResult();
return Task.CompletedTask;
}, cancellationTokenSource.Token)
.ProcessInParallel(1, TimeSpan.FromMilliseconds(200));
await firstItemStarted.Task;
cancellationTokenSource.Cancel();
Exception? caught = null;
try
{
await processor.WaitAsync();
}
catch (OperationCanceledException exception)
{
caught = exception;
}
await Assert.That(caught).IsNotNull();
await Assert.That(processor.GetEnumerableTasks().Any(x => x.IsCanceled)).IsTrue();
await Assert.That(processor.GetEnumerableTasks().Count(x => !x.IsCompleted)).IsEqualTo(0);
await processor.DisposeAsync();
}
[Test]
public async Task Result_Order_Is_Preserved_Regardless_Of_Completion_Order()
{
await using var processor = Enumerable.Range(0, 50).ToList()
.SelectAsync(async i =>
{
// Later items complete sooner
await Task.Delay(50 - i);
return i;
})
.ProcessInParallel(maxConcurrency: 50);
var results = await processor.GetResultsAsync();
await Assert.That(results.SequenceEqual(Enumerable.Range(0, 50))).IsTrue();
}
}