|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Runtime.CompilerServices; |
3 | 4 | using System.Threading; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 | using EnumerableAsyncProcessor.Builders; |
@@ -83,19 +84,57 @@ public async Task ExternalCancellation_Interrupts_AsyncEnumerable_Selector(Cance |
83 | 84 | using var cancellationTokenSource = new CancellationTokenSource(); |
84 | 85 | var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
85 | 86 | var interrupted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 87 | + var allowCleanupToFinish = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
86 | 88 |
|
87 | 89 | var processor = GetItemsAsync() |
88 | 90 | .ForEachAsync( |
89 | | - (_, processorToken) => WaitUntilCanceledAsync(processorToken, started, interrupted), |
| 91 | + (_, processorToken) => WaitUntilCanceledAfterCleanupAsync( |
| 92 | + processorToken, |
| 93 | + started, |
| 94 | + interrupted, |
| 95 | + allowCleanupToFinish), |
90 | 96 | cancellationTokenSource.Token) |
91 | 97 | .ProcessInParallel(); |
92 | 98 |
|
93 | 99 | var executeTask = processor.ExecuteAsync(); |
94 | 100 | await started.Task.WaitAsync(cancellationToken); |
95 | 101 | await cancellationTokenSource.CancelAsync(); |
96 | 102 |
|
97 | | - await interrupted.Task.WaitAsync(cancellationToken); |
98 | | - await Assert.ThrowsAsync<OperationCanceledException>(() => executeTask); |
| 103 | + await AssertExecutionWaitsForCleanupAsync( |
| 104 | + executeTask, |
| 105 | + interrupted, |
| 106 | + allowCleanupToFinish, |
| 107 | + cancellationToken); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public async Task ExternalCancellation_Waits_For_AsyncEnumerable_Result_Selector_Cleanup(CancellationToken cancellationToken) |
| 112 | + { |
| 113 | + using var cancellationTokenSource = new CancellationTokenSource(); |
| 114 | + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 115 | + var interrupted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 116 | + var allowCleanupToFinish = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 117 | + |
| 118 | + var processor = GetItemsAsync() |
| 119 | + .SelectAsync( |
| 120 | + (item, processorToken) => WaitUntilCanceledAfterCleanupAsync( |
| 121 | + item, |
| 122 | + processorToken, |
| 123 | + started, |
| 124 | + interrupted, |
| 125 | + allowCleanupToFinish), |
| 126 | + cancellationTokenSource.Token) |
| 127 | + .ProcessInParallel(); |
| 128 | + |
| 129 | + var executeTask = processor.ExecuteAsync().ToListAsync(); |
| 130 | + await started.Task.WaitAsync(cancellationToken); |
| 131 | + await cancellationTokenSource.CancelAsync(); |
| 132 | + |
| 133 | + await AssertExecutionWaitsForCleanupAsync( |
| 134 | + executeTask, |
| 135 | + interrupted, |
| 136 | + allowCleanupToFinish, |
| 137 | + cancellationToken); |
99 | 138 | } |
100 | 139 |
|
101 | 140 | private static async Task WaitUntilCanceledAsync( |
@@ -125,9 +164,66 @@ private static async Task<T> WaitUntilCanceledAsync<T>( |
125 | 164 | return result; |
126 | 165 | } |
127 | 166 |
|
128 | | - private static async IAsyncEnumerable<int> GetItemsAsync() |
| 167 | + private static async Task WaitUntilCanceledAfterCleanupAsync( |
| 168 | + CancellationToken cancellationToken, |
| 169 | + TaskCompletionSource started, |
| 170 | + TaskCompletionSource interrupted, |
| 171 | + TaskCompletionSource allowCleanupToFinish) |
| 172 | + { |
| 173 | + started.TrySetResult(); |
| 174 | + try |
| 175 | + { |
| 176 | + await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken); |
| 177 | + } |
| 178 | + catch (OperationCanceledException) |
| 179 | + { |
| 180 | + interrupted.TrySetResult(); |
| 181 | + await allowCleanupToFinish.Task; |
| 182 | + throw; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private static async Task<T> WaitUntilCanceledAfterCleanupAsync<T>( |
| 187 | + T result, |
| 188 | + CancellationToken cancellationToken, |
| 189 | + TaskCompletionSource started, |
| 190 | + TaskCompletionSource interrupted, |
| 191 | + TaskCompletionSource allowCleanupToFinish) |
| 192 | + { |
| 193 | + await WaitUntilCanceledAfterCleanupAsync( |
| 194 | + cancellationToken, |
| 195 | + started, |
| 196 | + interrupted, |
| 197 | + allowCleanupToFinish); |
| 198 | + |
| 199 | + return result; |
| 200 | + } |
| 201 | + |
| 202 | + private static async Task AssertExecutionWaitsForCleanupAsync( |
| 203 | + Task executeTask, |
| 204 | + TaskCompletionSource interrupted, |
| 205 | + TaskCompletionSource allowCleanupToFinish, |
| 206 | + CancellationToken cancellationToken) |
| 207 | + { |
| 208 | + await interrupted.Task.WaitAsync(cancellationToken); |
| 209 | + await Task.Delay(100, cancellationToken); |
| 210 | + |
| 211 | + try |
| 212 | + { |
| 213 | + await Assert.That(executeTask.IsCompleted).IsFalse(); |
| 214 | + } |
| 215 | + finally |
| 216 | + { |
| 217 | + allowCleanupToFinish.TrySetResult(); |
| 218 | + } |
| 219 | + |
| 220 | + await Assert.ThrowsAsync<OperationCanceledException>(() => executeTask); |
| 221 | + } |
| 222 | + |
| 223 | + private static async IAsyncEnumerable<int> GetItemsAsync( |
| 224 | + [EnumeratorCancellation] CancellationToken cancellationToken = default) |
129 | 225 | { |
130 | 226 | yield return 1; |
131 | | - await Task.CompletedTask; |
| 227 | + await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken); |
132 | 228 | } |
133 | 229 | } |
0 commit comments