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?

captncraig
  • 22,118
  • 17
  • 108
  • 151
  • Unit testing someone else's code? Why!? – cdhowie Sep 27 '13 at 21:36
  • Really it is a set of internal libraries that are intercepting my code and doing weird things. I'm trying to have a programatic way to show that they are altering the bahavior in bad ways. – captncraig Sep 27 '13 at 21:40
  • Really it is a test that my code has consistent threading behavior. If libraries I depend on change their behavior, I want to know about it. – captncraig Sep 27 '13 at 21:45
  • Normally this is detected with timeouts – Jeroen van Langen Sep 27 '13 at 21:48
  • I've suggested a duplicate which seems to address your question. TL;DR: apply load and break the debugger. Look at the stacks. – usr Sep 27 '13 at 22:23

1 Answers1

2

You should be able to use Microsoft Fakes (Ultimate only IIRC) or JustMock to capture calls to Task.Factory.StartNew. Personally, I don't think the effort is worth it.

If you don't care about doing it in code, you can (somewhat) easily see it using the Concurrency Visualizer (or PerfView).

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810