public void Server2() { var server = new TcpRpcServer(); server.Main = new TestInterfaceImpl2(); var tracer = new FrameTracing.RpcFrameTracer(Console.Out); server.OnConnectionChanged += (s, a) => { server.Dispose(); }; (var addr, int port) = TcpManager.Instance.GetLocalAddressAndPort(); server.StartAccepting(addr, port); var client1 = new TcpRpcClient(addr.ToString(), port); Assert.IsTrue(client1.WhenConnected.Wait(MediumNonDbgTimeout), "Did not connect"); Assert.IsTrue(SpinWait.SpinUntil(() => client1.State == ConnectionState.Down, MediumNonDbgTimeout), $"Connection did not go down: {client1.State}"); }
public void Server1() { var cbb = new BufferBlock <IConnection>(); var server = new TcpRpcServer(); server.Main = new TestInterfaceImpl2(); bool init = true; var tracer = new FrameTracing.RpcFrameTracer(Console.Out); (var addr, int port) = TcpManager.Instance.GetLocalAddressAndPort(); server.OnConnectionChanged += (s, a) => { var c = a.Connection; if (init) { Assert.ThrowsException <ArgumentNullException>(() => c.AttachTracer(null)); c.AttachTracer(tracer); Assert.ThrowsException <ArgumentNullException>(() => c.InjectMidlayer(null)); c.InjectMidlayer(_ => _); Assert.IsFalse(c.IsComputing); Assert.IsFalse(c.IsWaitingForData); Assert.AreEqual(ConnectionState.Initializing, c.State); Assert.IsNotNull(c.RemotePort); Assert.AreEqual(port, c.LocalPort); Assert.AreEqual(0L, c.RecvCount); Assert.AreEqual(0L, c.SendCount); } else { Assert.ThrowsException <InvalidOperationException>(() => c.AttachTracer(tracer)); Assert.ThrowsException <InvalidOperationException>(() => c.InjectMidlayer(_ => _)); Assert.AreEqual(ConnectionState.Down, c.State); } cbb.Post(c); }; Assert.ThrowsException <InvalidOperationException>(() => server.StopListening()); server.StartAccepting(addr, port); Assert.IsTrue(server.IsAlive); Assert.ThrowsException <InvalidOperationException>(() => server.StartAccepting(addr, port)); var server2 = new TcpRpcServer(); Assert.ThrowsException <SocketException>(() => server2.StartAccepting(addr, port)); var client1 = new TcpRpcClient(addr.ToString(), port); var c1 = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c1); Assert.AreEqual(1, server.ConnectionCount); Assert.AreEqual(c1, server.Connections[0]); Assert.AreEqual(ConnectionState.Active, c1.State); var proxy = client1.GetMain <ITestInterface>(); Assert.IsTrue(proxy is IResolvingCapability r && r.WhenResolved.WrappedTask.Wait(MediumNonDbgTimeout)); Assert.IsTrue(c1.RecvCount > 0); Assert.IsTrue(c1.SendCount > 0); var client2 = new TcpRpcClient(addr.ToString(), port); var c2 = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c2); Assert.AreEqual(2, server.ConnectionCount); Assert.AreEqual(c2, server.Connections[1]); init = false; client1.Dispose(); var c1d = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c1d); Assert.AreEqual(1, server.ConnectionCount); Assert.AreEqual(c2, server.Connections[0]); Assert.IsTrue(SpinWait.SpinUntil(() => c1d.State == ConnectionState.Down, MediumNonDbgTimeout)); client2.Dispose(); var c2d = cbb.Receive(TimeSpan.FromMilliseconds(MediumNonDbgTimeout)); Assert.IsNotNull(c2d); Assert.AreEqual(0, server.ConnectionCount); Assert.IsTrue(SpinWait.SpinUntil(() => c2d.State == ConnectionState.Down, MediumNonDbgTimeout)); server.StopListening(); Assert.IsFalse(server.IsAlive); Assert.ThrowsException <InvalidOperationException>(() => server.StopListening()); for (int i = 0; i < 100; i++) { server.StartAccepting(addr, port); Assert.IsTrue(server.IsAlive); server.StopListening(); Assert.IsFalse(server.IsAlive); } server.Dispose(); }
void ITestbed.CloseServer() { _prematurelyClosed = true; _server.Dispose(); }