Exemplo n.º 1
0
        public void TestBindable()
        {
            ClientWire.AutoTransmitMode = true;
            ServerWire.AutoTransmitMode = true;


            var call1 = new RdCall <Unit, RdSignal <int> >(Serializers.ReadVoid, Serializers.WriteVoid, RdSignal <int> .Read, RdSignal <int> .Write);
            var call2 = new RdCall <Unit, RdSignal <int> >(Serializers.ReadVoid, Serializers.WriteVoid, RdSignal <int> .Read, RdSignal <int> .Write);

            var respSignal           = new RdSignal <int>();
            var endpointLfTerminated = false;

            call2.Set((endpointLf, _) =>
            {
                endpointLf.OnTermination(() => endpointLfTerminated = true);
                return(RdTask <RdSignal <int> > .Successful(respSignal));
            });

            var serverEntity = BindToServer(LifetimeDefinition.Lifetime, call1, ourKey);
            var clientEntity = BindToClient(LifetimeDefinition.Lifetime, call2, ourKey);

            var ld     = new LifetimeDefinition();
            var lf     = ld.Lifetime;
            var signal = call1.Start(lf, Unit.Instance).AsTask().GetOrWait(lf);
            var log    = new List <int>();

            signal.Advise(Lifetime.Eternal, v =>
            {
                log.Add(v);
                Console.WriteLine(v);
            });

            respSignal.Fire(1);
            respSignal.Fire(2);
            respSignal.Fire(3);
            ld.Terminate();
            Assert.False(respSignal.IsBound);

            SpinWaitEx.SpinUntil(() => log.Count >= 3);
            Thread.Sleep(100);
            Assert.AreEqual(new [] { 1, 2, 3 }, log.ToArray());

            Assert.True(endpointLfTerminated);
        }
Exemplo n.º 2
0
        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);
            });
        }
Exemplo n.º 3
0
        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);
            });
        }
Exemplo n.º 4
0
        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
                }
            });
        }