public void TestClientConnectionRetryDifferentServer() { var connectionEstablished = new ManualResetEventSlim(); var connectionLost = new ManualResetEventSlim(); int server1ListenPort = GetAvailablePort(9997); int server2ListenPort = GetAvailablePort(server1ListenPort + 1); IPEndPoint[] clientEndPoints = SecureTransport.ParseConnectionString(string.Format("127.0.0.1:{0},127.0.0.1:{1}", server1ListenPort, server2ListenPort)); SecureTransport serverTransport1 = null; SecureTransport clientTransport = null; try { serverTransport1 = CreateTransport(); serverTransport1.StartServer(server1ListenPort); clientTransport = CreateTransport(); clientTransport.OnNewConnection = _ => connectionEstablished.Set(); clientTransport.OnConnectionLost = () => connectionLost.Set(); clientTransport.StartClient(clientEndPoints); connectionEstablished.Wait(); // Now stop the server to break the established connection serverTransport1.Stop(); connectionLost.Wait(); connectionEstablished.Reset(); connectionLost.Reset(); using (var serverTransport2 = CreateTransport()) { // Start a new server at a different port and verify that the client automatically connects to it serverTransport2.StartServer(server2ListenPort); connectionEstablished.Wait(); } } finally { try { serverTransport1.Dispose(); Assert.IsTrue(connectionLost.Wait(5000)); } finally { clientTransport?.Dispose(); } } }
public void TestClientConnectionRetrySameServer() { var connectionEstablished = new ManualResetEventSlim(); var connectionLost = new ManualResetEventSlim(); SecureTransport serverTransport = null; SecureTransport clientTransport = null; try { serverTransport = CreateTransport(); serverTransport.StartServer(this.serverListenPort); clientTransport = CreateTransport(); clientTransport.OnNewConnection = _ => connectionEstablished.Set(); clientTransport.OnConnectionLost = () => connectionLost.Set(); clientTransport.StartClient(this.clientEndPoints); connectionEstablished.Wait(); // Now stop the server to break the established connection serverTransport.Stop(); connectionLost.Wait(); connectionEstablished.Reset(); connectionLost.Reset(); // Restart the server and verify that the client automatically connects to it serverTransport.StartServer(this.serverListenPort); connectionEstablished.Wait(); } finally { try { serverTransport.Dispose(); Assert.IsTrue(connectionLost.Wait(5000)); } finally { clientTransport?.Dispose(); } } }