Relative Content

Tag Archive for c#async-awaitmstest

How to use MSTest to test a C# async method where it doesn’t complete until I feed it more data?

I’m writing unit tests where the SUT is a queue; the language is C#, and I’m using the MSTest framework. One method in the queue, async Task<int[]> DequeueAsync(int count), waits until the queue contains enough items before returning the requested number of them in an array. I want to verify that if I call DequeueAsync() requesting n+k items but the queue presently only contains n items, the method will wait until I enqueue at least k more items before returning the result. So, for example, I might call DequeAsync(count: 10) when there are only four items in the queue, and then enqueue another six items so that there are now ten items in the queue, allowing DequeueAsync() to return. But if I use await, won’t the task block, preventing me from feeding the additional six items to the queue? How can I start the task and then do the other stuff the task relies upon to complete?