Friday, 25 November 2011 15:03
|Jon Skeet: Coding Blog|
In the last post I showed a method to implement "majority voting" for tasks, allowing a result to become available as soon as possible. At the end, I mentioned that I was reasonably confident that it worked because of the unit tests... but I didn't show the tests themselves. I felt they deserved their own post, as there's a bigger point here: it's possible to unit test async code. At least sometimes. Testing code involving asynchrony is generally a pain. Introducing the exact order of events that you want is awkward, as is managing the threading within tests.
With a few benefits with async methods: We know that the async method itself will only execute in a single thread at a time We can control the thread in which the async method will execute, if it doesn't configure its awaits explicitly Assuming the async method returns Task or Task
, we can check whether or not it's finished Between Task and TaskCompletionSource , we have a way of injecting tasks that we understand Now in our sample method we have the...(Read whole news on source site)