I’m writing a unit test suite to test a TCP/IP communication library.
As I’m using BeginAcceptClient and EndAcceptClient, the messages are received in a background thread.
After a message is received, I perform some assertions on it, but if any assertion fails, the VSTestHost.exe crashes.
I googled a bit and found out its the fact the Assert exceptions are being thrown in a background thread.
EDIT: A sample code of what I am doing, just to ilustrate:
public void TestFooMessage() {
Server.OnReceive += (s, e) => {
Assert.IsInstanceOfType(e.Message, typeof(Foo));
};
var message = new Foo();
Client.Send(message);
}
Does anyone know how to make it work as expected: Log the assertion and continues running normally?
You should not write the Asserts in the background thread (say: the background event handler), because the test framework can not handle this. You should only gather values there. You can synchronize the main thread for instance using AutoResetEvents. Write the values into fields, assert the fields in the main thread.
If the messages are never coming in, you need a timeout.
A bit pseudo code (actually not that pseudo):
By the way: you still can write the handler as a lambda expression and even avoid the fields by using local variables. But it could be harder to read if everything is in a single method.