Wait until all Task finish in unit test

后端 未结 3 1381
有刺的猬
有刺的猬 2020-12-30 06:54

I have this class I want to unit test:

public class SomeClass
{
    public void Foo()
    {
        Bar();
    }

    private void Bar()
    {
        Task.F         


        
3条回答
  •  星月不相逢
    2020-12-30 06:56

    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:

    class SynchronousTaskScheduler : TaskScheduler
    {
        protected override void QueueTask(Task task)
        {
            this.TryExecuteTask(task);
        }
    
        protected override bool TryExecuteTaskInline(Task task, bool wasPreviouslyQueued)
        {
            return this.TryExecuteTask(task);
        }
    
        protected override IEnumerable GetScheduledTasks()
        {
            yield break;
        }
    }
    

    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.

    TaskScheduler scheduler = new SynchronousTaskScheduler();
    
    Task.Factory.StartNew(() =>
    {
        // Arrange
        var obj = new SomeClass();
    
        // Act
        obj.Foo();
        obj.Foo();
        obj.Foo();
    }, 
        CancellationToken.None,
        TaskCreationOptions.None,
        scheduler);
    
    // Assert
    /* I need something to wait on all tasks to finish */
    Assert.IsTrue(...);
    

    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.

提交回复
热议问题