I have an odd problem. I have a unit test that keeps getting stuck in Run Mode. When I run the same test in Debug, with no breakpoints, the test passes every time.
Basically, it is a socket connection test. I first disconnect a socket, and then try to reconnect, and I am trying to check if the reconnection was successful.
Somewhere in the connect code, I check if there was a socket exception. When this happens, the user is presented with some choices in a dialog, while the connection code hangs via an AutoResetEvent, waiting for a decision.
It is this AutoResetEvent that hangs the system. It must be provided by code in the unit test. But my question is, how come this passes in Debug mode? Is there something special about debug mode whereby AutoResetEvents are automatically set by Visual Studio?
EDIT
It was indeed a race condition. I added a delay in the code after my disconnect code, and it works now. But it still strikes me as odd that there is a race condition to begin with. Let me elaborate by pasting some of the code.
This is the test code:
MySystem.FindEquipment(new List<string>(1) { "192.1.1.243:28000" });
MySystem.ConstructSystem();
MySystem.IsConstructedFlag.WaitOne();
Assert.AreEqual(1, MySystem.CommunicationController.HardwareIPList.Count);
PFFrame frame1 = MySystem.Frames["0.x.x"];
Assert.IsTrue(frame1.Disconnect());
Thread.Sleep(100);
Assert.IsTrue(frame1.Connect());
The reason this strikes me, is that I am waiting for a return on the diconnect code, before calling the connect code. The last part of the disconnect code looks like this:
lclSocket.Shutdown(SocketShutdown.Both);
lclSocket.Close();
OnSocketDisconnected(new PFSocketConnectionEventArgs(ipEp));
return true;
Is it be because Socket.Shutdown(), and/or Socket.Close() methods run it threads? Thus, even though I am returning a value from my disconnect code, the socket isn't actually truly disconnected?