We plan to use async / await in our MVVM viewer models, but we are facing a tough problem with the testing module of this code. When using NUnit and manual messaging, we lose the current SynchronizationContext .
This is best shown with the following small sample playback code:
[Test] public void TestMethod() { Func<Task> asyncMethod = async () => { var context = SynchronizationContext.Current; await TaskEx.Yield(); Assert.AreEqual(context, SynchronizationContext.Current); };
In fact, most of this code is stolen from Stephen Toub's AsyncPump implementation on his blog .
Interestingly, everything needed to complete this test pass is thrown into ExecutionContext.SuppressFlow(); before calling the async method. This may be enough to fix our problem, but I do not know enough about ExecutionContext, and I need a deeper understanding of what is happening.
Why does the code generated by the wait statement swallow the current SynchronizationContext?
Is there another obvious way to use a single thread to unit test async / await code?
PS: We use .Net4 and Microsoft.CompilerServices.AsyncTargetingPack.Net4
PPS: This also happens in a simple project using stable Microsoft.Bcl.Async instead of ATP
source share