-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPreCancelledTokenTests.cs
More file actions
91 lines (77 loc) · 3.07 KB
/
Copy pathPreCancelledTokenTests.cs
File metadata and controls
91 lines (77 loc) · 3.07 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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnumerableAsyncProcessor.Extensions;
using TUnit.Assertions;
using TUnit.Core;
namespace EnumerableAsyncProcessor.UnitTests;
/// <summary>
/// Guards the pre-cancelled token contract (issue #367):
/// building a processor with an already-cancelled token must produce a processor whose
/// per-item tasks are cancelled - matching TPL convention - instead of throwing
/// ArgumentException from an internal constructor parameter the caller never passed.
/// </summary>
public class PreCancelledTokenTests
{
[Test]
public async Task Void_Processor_Built_With_PreCancelled_Token_Yields_Cancelled_Tasks()
{
using var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();
var invoked = 0;
await using var processor = Enumerable.Range(0, 5).ToList()
.ForEachAsync(_ =>
{
Interlocked.Increment(ref invoked);
return Task.CompletedTask;
}, cancellationTokenSource.Token)
.ProcessInParallel(maxConcurrency: 2);
Exception? caught = null;
try
{
await processor.WaitAsync();
}
catch (Exception exception)
{
caught = exception;
}
await Assert.That(caught is OperationCanceledException).IsTrue();
await Assert.That(processor.GetEnumerableTasks().All(t => t.IsCanceled)).IsTrue();
await Assert.That(invoked).IsEqualTo(0);
}
[Test]
public async Task Result_Processor_Built_With_PreCancelled_Token_Yields_Cancelled_Tasks()
{
using var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();
await using var processor = Enumerable.Range(0, 5).ToList()
.SelectAsync(i => Task.FromResult(i), cancellationTokenSource.Token)
.ProcessInParallel(maxConcurrency: 2);
Exception? caught = null;
try
{
_ = await processor.GetResultsAsync();
}
catch (Exception exception)
{
caught = exception;
}
await Assert.That(caught is OperationCanceledException).IsTrue();
await Assert.That(processor.GetEnumerableTasks().All(t => t.IsCanceled)).IsTrue();
}
[Test]
public async Task OneAtATime_And_Batch_Built_With_PreCancelled_Token_Yield_Cancelled_Tasks()
{
using var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();
await using var oneAtATime = new[] { 1, 2, 3 }
.ForEachAsync(_ => Task.CompletedTask, cancellationTokenSource.Token)
.ProcessOneAtATime();
await using var batched = new[] { 1, 2, 3 }
.ForEachAsync(_ => Task.CompletedTask, cancellationTokenSource.Token)
.ProcessInBatches(2);
await Assert.That(oneAtATime.GetEnumerableTasks().All(t => t.IsCanceled)).IsTrue();
await Assert.That(batched.GetEnumerableTasks().All(t => t.IsCanceled)).IsTrue();
}
}