A small service prepares a short summary for an incoming batch of codes. It must be able to stop quickly on cancellation and consistently count processed, skipped, and failed items.
Requirements
- The
BuildAsync method accepts IEnumerable<string?> items, CancellationToken cancellationToken, and returns Task<BatchPreview>.
- Each item is first normalized with
Trim().
- Empty strings after normalization are counted as skips.
- The same code within a single call is counted only once; repeated occurrences are counted as skips.
- An asynchronous validation
ValidateAsync(code, cancellationToken) is called for each unique non-empty code.
- If validation returns
false, the item is counted as skipped.
- If validation throws an exception other than cancellation, it must be added to
Failed, and processing of the remaining items continues.
- Cancellation must interrupt waiting for validation without unnecessary delay.
- The summary must be built from a fixed snapshot of the input, not depend on later changes to the original collection.
Reproduction
- Pass a list with duplicate values, whitespace, and one value that should fail validation to
BuildAsync.
- During execution, modify the original list and verify that the result does not change.
- Trigger token cancellation while waiting for validation and check that the method finishes as canceled, not successful.
- Verify that a failure in one validation does not stop processing of subsequent unique values.
Expected behavior
Processed reflects the number of unique non-empty values that were passed to validation.
Skipped includes empty values, duplicates, and validation-rejected items.
Failed contains only items for which validation ended with an exception not related to cancellation.
- On cancellation, the method finishes with
TaskCanceledException or OperationCanceledException.
- The result does not depend on later changes to the input collection.