private void TestDisconnectBase(Action <List <int>, int> advise) { var timeout = TimeSpan.FromSeconds(1); Lifetime.Using(lifetime => { SynchronousScheduler.Instance.SetActive(lifetime); var serverProtocol = Server(lifetime, null); var clientProtocol = Client(lifetime, serverProtocol); var sp = new RdSignal <int>().Static(1); sp.Bind(lifetime, serverProtocol, Top); var cp = new RdSignal <int>().Static(1); cp.Bind(lifetime, clientProtocol, Top); var log = new List <int>(); sp.Advise(lifetime, i => advise(log, i)); cp.Fire(1); cp.Fire(2); Assert.True(SpinWaitEx.SpinUntil(timeout, () => log.Count == 2)); Assert.AreEqual(new List <int> { 1, 2 }, log); CloseSocket(clientProtocol); cp.Fire(3); cp.Fire(4); Assert.True(SpinWaitEx.SpinUntil(timeout, () => log.Count == 4)); Assert.AreEqual(new List <int> { 1, 2, 3, 4 }, log); CloseSocket(serverProtocol); cp.Fire(5); cp.Fire(6); Assert.True(SpinWaitEx.SpinUntil(timeout, () => log.Count == 6)); Assert.AreEqual(new List <int> { 1, 2, 3, 4, 5, 6 }, log); }); }
public void TestDisconnect() { Lifetime.Using(lifetime => { SynchronousScheduler.Instance.SetActive(lifetime); var serverProtocol = Server(lifetime, null); var clientProtocol = Client(lifetime, serverProtocol); var sp = new RdSignal <int>().Static(1); sp.Bind(lifetime, serverProtocol, Top); var cp = new RdSignal <int>().Static(1); cp.Bind(lifetime, clientProtocol, Top); var log = new List <int>(); sp.Advise(lifetime, i => log.Add(i)); cp.Fire(1); cp.Fire(2); SpinWait.SpinUntil(() => log.Count == 2); Assert.AreEqual(new List <int> { 1, 2 }, log); (clientProtocol.Wire as SocketWire.Base)?.Socket.Shutdown(SocketShutdown.Both); (clientProtocol.Wire as SocketWire.Base)?.Socket.Close(); cp.Fire(3); cp.Fire(4); SpinWait.SpinUntil(() => log.Count == 4); Assert.AreEqual(new List <int> { 1, 2, 3, 4 }, log); (serverProtocol.Wire as SocketWire.Base)?.Socket.Shutdown(SocketShutdown.Both); (serverProtocol.Wire as SocketWire.Base)?.Socket.Close(); cp.Fire(5); cp.Fire(6); SpinWait.SpinUntil(() => log.Count == 6); Assert.AreEqual(new List <int> { 1, 2, 3, 4, 5, 6 }, log); }); }
public void TestSimple() { // using var factory = Log.UsingLogFactory(new TextWriterLogFactory(Console.Out, LoggingLevel.TRACE)); Lifetime.Using(lifetime => { var proxyLifetimeDefinition = lifetime.CreateNested(); var proxyLifetime = proxyLifetimeDefinition.Lifetime; { SynchronousScheduler.Instance.SetActive(lifetime); var serverProtocol = SocketWireTest.Server(lifetime); var proxy = new SocketProxy("TestProxy", proxyLifetime, serverProtocol).With(socketProxy => socketProxy.Start()); Thread.Sleep(SocketWireTest.DefaultTimeout); var clientProtocol = SocketWireTest.Client(lifetime, proxy.Port); var sp = new RdSignal <int>().Static(1); sp.Bind(lifetime, serverProtocol, SocketWireTest.Top); var cp = new RdSignal <int>().Static(1); cp.Bind(lifetime, clientProtocol, SocketWireTest.Top); var serverLog = new List <int>(); var clientLog = new List <int>(); sp.Advise(lifetime, i => serverLog.Add(i)); cp.Advise(lifetime, i => clientLog.Add(i)); //Connection is established for now sp.Fire(1); SpinWaitEx.SpinUntil(() => serverLog.Count == 1); SpinWaitEx.SpinUntil(() => clientLog.Count == 1); Assert.AreEqual(new List <int> { 1 }, serverLog); Assert.AreEqual(new List <int> { 1 }, clientLog); cp.Fire(2); SpinWaitEx.SpinUntil(() => serverLog.Count == 2); SpinWaitEx.SpinUntil(() => clientLog.Count == 2); Assert.AreEqual(new List <int> { 1, 2 }, serverLog); Assert.AreEqual(new List <int> { 1, 2 }, clientLog); proxy.StopServerToClientMessaging(); cp.Advise(lifetime, i => Assert.AreNotSame(3, i, "Value {0} mustn't be received", 3)); sp.Fire(3); SpinWaitEx.SpinUntil(() => serverLog.Count == 3); Assert.AreEqual(new List <int> { 1, 2, 3 }, serverLog); proxy.StopClientToServerMessaging(); sp.Advise(lifetime, i => Assert.AreNotSame(4, i, "Value {0} mustn't be received", 4)); cp.Fire(4); SpinWaitEx.SpinUntil(() => clientLog.Count == 3); Assert.AreEqual(new List <int> { 1, 2, 4 }, clientLog); //Connection is broken for now proxy.StartServerToClientMessaging(); sp.Fire(5); SpinWaitEx.SpinUntil(() => serverLog.Count == 4); SpinWaitEx.SpinUntil(() => clientLog.Count == 4); Assert.AreEqual(new List <int> { 1, 2, 3, 5 }, serverLog); Assert.AreEqual(new List <int> { 1, 2, 4, 5 }, clientLog); proxy.StartClientToServerMessaging(); cp.Fire(6); SpinWaitEx.SpinUntil(() => serverLog.Count == 5); SpinWaitEx.SpinUntil(() => clientLog.Count == 5); Assert.AreEqual(new List <int> { 1, 2, 3, 5, 6 }, serverLog); Assert.AreEqual(new List <int> { 1, 2, 4, 5, 6 }, clientLog); //Connection is established for now proxyLifetimeDefinition.Terminate(); cp.Advise(lifetime, i => Assert.AreNotSame(7, i, "Value {0} mustn't be received", 7)); sp.Fire(7); SpinWaitEx.SpinUntil(() => serverLog.Count == 6); Assert.AreEqual(new List <int> { 1, 2, 3, 5, 6, 7 }, serverLog); sp.Advise(lifetime, i => Assert.AreNotSame(8, i, "Value {0} mustn't be received", 8)); cp.Fire(8); SpinWaitEx.SpinUntil(() => clientLog.Count == 6); Assert.AreEqual(new List <int> { 1, 2, 4, 5, 6, 8 }, clientLog); //Connection is broken for now, proxy is not alive } }); }