Example #1
0
        public async Task TestListenOnceAsync()
        {
            BehaviorSink <int> b = Behavior.CreateSink(9);
            int result           = await Transaction.Run(() => Operational.Value(b).ListenOnceAsync());

            b.Send(2);
            b.Send(7);
            Assert.AreEqual(9, result);
        }
Example #2
0
        public void TestListenOnce()
        {
            BehaviorSink <int> b    = Behavior.CreateSink(9);
            List <int>         @out = new List <int>();
            IListener          l    = Transaction.Run(() => Operational.Value(b).ListenOnce(@out.Add));

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Example #3
0
        public void TestUpdates()
        {
            BehaviorSink <int> b    = Behavior.CreateSink(9);
            List <int>         @out = new List <int>();
            IListener          l    = Operational.Updates(b).Listen(@out.Add);

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Example #4
0
        public void TestGate()
        {
            StreamSink <char?>  sc    = Stream.CreateSink <char?>();
            BehaviorSink <bool> cGate = Behavior.CreateSink(true);
            List <char?>        @out  = new List <char?>();
            IListener           l     = sc.Gate(cGate).Listen(@out.Add);

            sc.Send('H');
            cGate.Send(false);
            sc.Send('O');
            cGate.Send(true);
            sc.Send('I');
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out);
        }
Example #5
0
        public void FunctionalBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Behavior.Loop <int>().WithoutCaptures(l => Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior());

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Example #6
0
        public void TestSnapshot()
        {
            BehaviorSink <int> b       = Behavior.CreateSink(0);
            StreamSink <long>  trigger = Stream.CreateSink <long>();
            List <string>      @out    = new List <string>();
            IListener          l       = trigger.Snapshot(b, (x, y) => x + " " + y).Listen(@out.Add);

            trigger.Send(100L);
            b.Send(2);
            trigger.Send(200L);
            b.Send(9);
            b.Send(1);
            trigger.Send(300L);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out);
        }
Example #7
0
        public void FunctionalBehaviorLoopWithCaptures()
        {
            BehaviorSink <int> s = Behavior.CreateSink(0);

            (Behavior <int> result, Behavior <int> s2) = Behavior.Loop <int>()
                                                         .WithCaptures(l => (Behavior: Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior(), Captures: s.Map(v => 2 * v)));

            List <int> @out = new List <int>();
            List <int> out2 = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
                using (Transaction.Run(() => Operational.Value(s2).Listen(out2.Add)))

                {
                    s.Send(1);
                    s.Send(2);
                    s.Send(3);
                }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
            CollectionAssert.AreEqual(new[] { 0, 2, 4, 6 }, out2);
        }
Example #8
0
        public void ImperativeBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Transaction.Run(
                () =>
            {
                BehaviorLoop <int> l       = new BehaviorLoop <int>();
                Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                l.Loop(resultLocal);
                return(resultLocal);
            });

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Example #9
0
        public void ImperativeBehaviorLoopFailsWhenLoopedInSeparateTransaction()
        {
            InvalidOperationException actual = null;

            BehaviorLoop <int> l = null;

            new Thread(
                () =>
                Transaction.RunVoid(
                    () =>
            {
                l = new BehaviorLoop <int>();
                Thread.Sleep(500);
            })).Start();

            try
            {
                BehaviorSink <int> s = Behavior.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    Thread.Sleep(250);
                    Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                    l.Loop(resultLocal);
                });
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Thread.Sleep(500);

            Assert.IsNotNull(actual);
            Assert.AreEqual("Loop must be looped in the same transaction that it was created in.", actual.Message);
        }
Example #10
0
        public void ImperativeBehaviorLoopFailsWhenLoopedTwice()
        {
            InvalidOperationException actual = null;

            try
            {
                BehaviorSink <int> s = Behavior.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    BehaviorLoop <int> l       = new BehaviorLoop <int>();
                    Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                    l.Loop(resultLocal);
                    l.Loop(resultLocal);
                });
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("Loop was looped more than once.", actual.Message);
        }