I have this class I want to unit test:
public class SomeClass
{
public void Foo()
{
Bar();
}
private void Bar()
{
Task.Factory.StartNew(() =>
{
// Do something that takes some time (e.g. an HTTP request)
});
}
}
And this is how my unit test looks like:
[TestMethod]
public void TestFoo()
{
// Arrange
var obj = new SomeClass();
// Act
obj.Foo();
obj.Foo();
obj.Foo();
// Assert
/* I need something to wait on all tasks to finish */
Assert.IsTrue(...);
}
So, I need to make the unit test thread wait until all tasks started in the Bar
method have finished their job before starting my assertions.
IMPORTANT: I cannot change SomeClass
How can I do that?
One way to solve this problem is to define your own task scheduler in such a way that would allow you to keep track of the completion of your nested tasks. For example, you could define a scheduler that executes tasks synchronously, as below:
Subsequently, create an instance of this synchronous task scheduler, and use it to execute a root task which, in turn, spawns all of your "hidden" tasks. Since nested tasks inherit the current task scheduler from their parent, all your inner tasks will also get run on our synchronous scheduler, implying that our outermost
StartNew
call will only return when all tasks complete.A downside to this approach is that you will lose all concurrency from your tasks; however, you could fix this by enhancing the custom scheduler to one which is concurrent but still allows you to track executing tasks.
Not sure if you are allowed to make this change but I got it to work doing this:
If it's a
void async
method then you can't do it. The design is broken. They're only for fire and forget.