1

If I have a method:

    public async Task<string> Get()
    {
        Task<string> a = _db.GetSomething();
        Task<string> b = _db.GetSomethingElse();

        await Task.WhenAll(a, b);

        return a.Result + b.Result;
    }

I don't fully control the code in _db, and would like a way to programatically verify that they are using non-blocking io inside. Yes, they have converted to a Task based api, but that could easily be accomplished with Task.Factory.StartNewand still use blocking IO underneath. That would be undesired in this case.

I do have access to the code, so I can see that they are indeed using Task Factory inside, so decompiling is not really what I am looking for. Ideally I could write a unit test to verify the threading behaviour moving forward.

Is there a way I can somehow check the worker thread count at various points and verify that threads are or are not being blocked in any of these child tasks?

4

1 回答 1

2

您应该能够使用 Microsoft Fakes(仅限最终 IIRC)或 JustMock 来捕获对Task.Factory.StartNew. 就个人而言,我认为这种努力是不值得的。

如果您不关心在代码中执行此操作,则可以(在某种程度上)使用并发可视化工具(或 PerfView)轻松查看它。

于 2013-09-27T21:48:22.400 回答