Reputation: 760
I am looking for guidance on setting up the synchronization context for a complex integration test where the SUT includes async ASP.NET actions.
I have a mock HttpContext and can exercise synchronous action methods. But async actions often fail with HttpContext.Current null. From reading around, I think I need to call SynchronizationContext.SetSynchronizationContext in order to get the HttpContext.Current to be "moved" from one thread to another as the async operations proceed.
I've tried:
Any suggestions?
Upvotes: 2
Views: 307
Reputation: 171216
You should be able to use pretty much any synchronization context that calls all code on the main test thread. That way your artificial HttpContext.Current
will be visible to all code.
The way this works is that there is an internal Queue<Action>
. The initial action is run and can queue additional stuff. The queue is drained until all outstanding work has completed. SynchronizationContext
has a notion of outstanding operations which is essentially an integer counter.
The NitoAsync
project is a popular async extension library. It contains such a synchronization context (I don't have the class name handy; grep for SynchronizationContext).
Upvotes: 1