I am playing around with a socket server and try to work according to the Test Driven Development pattern.
There's a working test for the socket creation method, but my test hangs due to the fact that I have a ManualResetEvent.WaitOne() so that my socket waits for a connection before creating another one.
Here's the code block to test:
ManualResetEvent allDone = new ManualResetEvent(false);
public void StartListening()
{
allDone.Reset();
//Inform on the console that the socket is ready
Console.WriteLine("Waiting for a connection...");
/* Waits for a connection and accepts it. AcceptCallback is called and the actual
* socket passed as AsyncResult
*/
listener.BeginAcceptTcpClient(
new AsyncCallback(AcceptCallback),
listener);
allDone.WaitOne();
}
And here's the test method which hangs at "networkChannel.StartListening();
[TestMethod]
public void NetworkChannel_IsAcceptingConnectionsAsynchronously()
{
ITcpClient client = MockRepository.GenerateMock<ITcpClient>();
ITcpListener listener = MockRepository.GenerateMock<ITcpListener>();
IAsyncResult asyncResult = MockRepository.GenerateMock<IAsyncResult>();
listener.Expect(x => x.BeginAcceptTcpClient(null, null)).IgnoreArguments().Return(asyncResult);
listener.Expect(x => x.EndAcceptTcpClient(asyncResult)).Return(client);
NetworkChannel networkChannel = new NetworkChannel(listener);
networkChannel.StartListening();
//Some more work... fake callback etc., verfify expectations,...
}
If I comment all the manualresetevents, the test passes just fine but this cannot be the solution because the server tries to create duplicate sockets continuously ;-)
Any hints for me? Would be very much appreciated!
Regards, Martin