|
| 1 | +# InvokeAsync Unit Test Instructions for Copilot |
| 2 | + |
| 3 | +## Method Signatures to Test |
| 4 | + |
| 5 | +```csharp |
| 6 | +// Async callback returning ValueTask |
| 7 | +public async Task InvokeAsync(Func<CancellationToken, ValueTask> callback, CancellationToken cancellationToken = default) |
| 8 | + |
| 9 | +// Async callback returning ValueTask<T> |
| 10 | +public async Task<T> InvokeAsync<T>(Func<CancellationToken, ValueTask<T>> callback, CancellationToken cancellationToken = default) |
| 11 | + |
| 12 | +// Sync callback returning T |
| 13 | +public async Task<T> InvokeAsync<T>(Func<T> callback, CancellationToken cancellationToken = default) |
| 14 | + |
| 15 | +// Sync callback returning void |
| 16 | +public async Task InvokeAsync(Action callback, CancellationToken cancellationToken = default) |
| 17 | +``` |
| 18 | + |
| 19 | +## Required Test Coverage |
| 20 | + |
| 21 | +Please create comprehensive unit tests for each overload that verify: |
| 22 | + |
| 23 | +### Core Functionality |
| 24 | +- **UI Thread Delegation**: Verify the callback executes on the UI thread (different from calling thread) |
| 25 | +- **Cancellation Support**: Test cancellation works even when callback doesn't support it (sync overloads) |
| 26 | +- **Async Cancellation**: Test cancellation works when callback supports it (async overloads with CancellationToken) |
| 27 | +- **Exception Propagation**: Verify exceptions from callbacks are properly propagated to caller |
| 28 | + |
| 29 | +### Edge Cases |
| 30 | +- **Handle Not Created**: Verify `InvalidOperationException` when control handle isn't created |
| 31 | +- **Pre-cancelled Token**: Verify early return when token is already cancelled |
| 32 | +- **Multiple Concurrent Calls**: Test thread safety with overlapping invocations |
| 33 | +- **Reentry Scenarios**: Test calling InvokeAsync from within a callback |
| 34 | + |
| 35 | +### Cancellation Scenarios |
| 36 | +- **External Cancellation**: Cancel token while callback is queued/running |
| 37 | +- **Callback Cancellation**: For async overloads, test cancellation within the callback itself |
| 38 | +- **Registration Cleanup**: Verify cancellation registrations are properly disposed |
| 39 | + |
| 40 | +### Return Value Testing |
| 41 | +- **Generic Overloads**: Test proper return value handling for `Task<T>` variants |
| 42 | +- **Void Overload**: Test completion signaling for `Action` overload |
| 43 | + |
| 44 | +### Performance/Resource Testing |
| 45 | +- **Memory Leaks**: Verify no leaked registrations or task completion sources |
| 46 | +- **Async Context**: Verify ConfigureAwait behavior and sync context handling |
| 47 | + |
| 48 | +## Test Structure Guidance |
| 49 | + |
| 50 | +- Use a test control with proper handle creation for UI thread tests |
| 51 | +- Use `Thread.CurrentThread.ManagedThreadId` to verify thread marshalling |
| 52 | +- Use `CancellationTokenSource` with timeouts for cancellation tests |
| 53 | +- Include both immediate and delayed cancellation scenarios |
| 54 | +- Test with both short-running and long-running callbacks |
| 55 | +- Use appropriate async test patterns with proper awaiting |
| 56 | + |
| 57 | +Create tests that are robust, deterministic, and cover both happy path and error conditions. |
0 commit comments