1

I have an issue here where during unit tests, the StoreAsync() method returns. When run without debugging, it seems to never return even though it is an async method.

What gives?

   DataWriter writer = new DataWriter(_client.OutputStream);
   writer.WriteBytes(payload);
   await writer.StoreAsync(); // <--- returns only during unit test
   writer.DetachStream();
2
  • One thing's not clear: your problem is the unit test not failing when it should or...? Commented Mar 14, 2012 at 17:03
  • Basically I'm saying.. Why is it not failing in the unit test but failing when run normally. Commented Mar 14, 2012 at 17:04

1 Answer 1

0

If you're using VS 11 Beta, then make sure your unit test is declared as async Task.

If you're using VS 11 Dev Preview, then use the Async Unit Tests project from CodePlex.

I have more information on async unit tests on my blog (part 1, part 2). In summary:

  1. When await needs to (asynchronously) wait, it schedules a continuation and returns to its caller.
  2. MSTest sees the test method return, does not see any exceptions, and marks it as "passed".
  3. Later on, the StoreAsync will complete and the rest of the method will run (on a thread pool thread, not within the context of that unit test).

VS 11 Beta adds first-class support for async Task unit test methods. So the second step above is different: MSTest sees the test method return but knows not to consider it complete until the Task completes.

The Async Unit Tests project takes a different approach: it applies an async context for every unit test method in an [AsyncTestClass]. This async context waits for all asynchronous operations to complete (including async void methods) before completing the unit test.

Sign up to request clarification or add additional context in comments.

2 Comments

hi Stephen, thanks for your reply, the issue here is that when run as a unit test, the StoreAsync operation returns and is able to detach from stream; when run normally, it doesn't. On the receiving end is actually your PacketProtocol handling the TCP message =)
I see. I've only seen deadlocks like this twice: one is due to a bug in ASP.NET MVC 4 Beta and the other is when an async method is attempting to synchronize back to a thread that is doing a blocking wait on the returned task. Neither of these seem to be the case here, but could you try changing await writer.StoreAsync() to await writer.StoreAsync().ConfigureAwait(false)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.