Skip to content

Commit b2e660f

Browse files
thomhurstclaude
andcommitted
fix: Fix cancellation handling in channel processor test
The test ProcessWithChannel_WithCancellation_ShouldCancelGracefully was failing because cancellation could occur before any items were written to the channel. Changes: - Added TaskCompletionSource to signal when processing starts - Wait for processing to begin before triggering cancellation - Reduced cancellation delay from 200ms to 50ms after processing starts This ensures at least some items are processed before cancellation, making the test behavior more predictable and reliable. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bfe0bb2 commit b2e660f

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

EnumerableAsyncProcessor.UnitTests/ChannelBasedAsyncProcessorTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,20 @@ public async Task ProcessWithChannel_WithCancellation_ShouldCancelGracefully()
153153
var processedItems = new List<int>();
154154
var lockObj = new object();
155155
var items = Enumerable.Range(1, itemCount);
156+
var processingStarted = new TaskCompletionSource<bool>();
156157

157158
using var cts = new CancellationTokenSource();
158159
var options = ChannelProcessorOptions.CreateUnbounded(consumerCount: 2);
159160

160161
// Act
161162
var processor = items.ForEachWithChannelAsync(async item =>
162163
{
164+
// Signal that processing has started
165+
if (item == 1)
166+
{
167+
processingStarted.TrySetResult(true);
168+
}
169+
163170
// Don't pass the token to Task.Delay to allow some items to complete
164171
await Task.Delay(10);
165172

@@ -173,10 +180,11 @@ public async Task ProcessWithChannel_WithCancellation_ShouldCancelGracefully()
173180
}
174181
}, options, cts.Token);
175182

176-
// Cancel after a short delay
183+
// Wait for processing to start, then cancel after a short delay
177184
_ = Task.Run(async () =>
178185
{
179-
await Task.Delay(200);
186+
await processingStarted.Task;
187+
await Task.Delay(50); // Give time for some items to be processed
180188
cts.Cancel();
181189
});
182190

0 commit comments

Comments
 (0)