Beispiel #1
0
        public void TestMultipleUnlisten()
        {
            StreamSink <int> s = Stream.CreateSink <int>();

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

            ((Action)(() =>
            {
                // ReSharper disable once UnusedVariable
                IStrongListener l = s.Listen(@out.Add);

                s.Send(1);

                l.Unlisten();
                l.Unlisten();

                s.Send(2);

                l.Unlisten();
            }))();

            s.Send(3);
            s.Send(4);

            Assert.AreEqual(1, @out.Count);
        }
Beispiel #2
0
 public void TestCoalesce2()
 {
     StreamSink<int> s = new StreamSink<int>((x, y) => x + y);
     List<int> @out = new List<int>();
     using (s.Listen(@out.Add))
     {
         Transaction.RunVoid(() =>
         {
             s.Send(1);
             s.Send(2);
             s.Send(3);
             s.Send(4);
             s.Send(5);
         });
         Transaction.RunVoid(() =>
         {
             s.Send(6);
             s.Send(7);
             s.Send(8);
             s.Send(9);
             s.Send(10);
         });
     }
     CollectionAssert.AreEqual(new[] { 15, 40 }, @out.ToArray());
 }
Beispiel #3
0
        public void TestCoalesce2()
        {
            StreamSink <int> s    = Stream.CreateSink <int>((x, y) => x + y);
            List <int>       @out = new List <int>();
            IListener        l    = s.Listen(@out.Add);

            Transaction.RunVoid(() =>
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
                s.Send(4);
                s.Send(5);
            });
            Transaction.RunVoid(() =>
            {
                s.Send(6);
                s.Send(7);
                s.Send(8);
                s.Send(9);
                s.Send(10);
            });
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 15, 40 }, @out.ToArray());
        }
Beispiel #4
0
        public void TestBaseSend1()
        {
            StreamSink <string> s    = Stream.CreateSink <string>();
            List <string>       @out = new List <string>();
            IListener           l    = s.Listen(@out.Add);

            s.Send("a");
            s.Send("b");
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "a", "b" }, @out);
        }
Beispiel #5
0
 public void TestBaseSend1()
 {
     StreamSink<string> s = new StreamSink<string>();
     List<string> @out = new List<string>();
     using (s.Listen(@out.Add))
     {
         s.Send("a");
         s.Send("b");
     }
     CollectionAssert.AreEqual(new[] { "a", "b" }, @out);
 }
Beispiel #6
0
        public void TestStreamSend()
        {
            StreamSink <int> s    = Stream.CreateSink <int>();
            List <int>       @out = new List <int>();
            IListener        l    = s.Listen(@out.Add);

            s.Send(5);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 5 }, @out);
            s.Send(6);
            CollectionAssert.AreEqual(new[] { 5 }, @out);
        }
Beispiel #7
0
        public void TestBaseSend1()
        {
            StreamSink <string> s    = new StreamSink <string>();
            List <string>       @out = new List <string>();

            using (s.Listen(@out.Add))
            {
                s.Send("a");
                s.Send("b");
            }
            CollectionAssert.AreEqual(new[] { "a", "b" }, @out);
        }
Beispiel #8
0
        public void TestStreamSend()
        {
            StreamSink <int> s    = new StreamSink <int>();
            List <int>       @out = new List <int>();

            using (s.Listen(@out.Add))
            {
                s.Send(5);
            }
            CollectionAssert.AreEqual(new[] { 5 }, @out);
            s.Send(6);
            CollectionAssert.AreEqual(new[] { 5 }, @out);
        }
Beispiel #9
0
            public void Run()
            {
                StreamSink <int> sX = Stream.CreateSink <int>();
                StreamSink <int> sY = Stream.CreateSink <int>();
                // Should throw an exception because you're not allowed to use Send() inside
                // a callback.
                IListener l = Listener.CreateComposite(new[]
                {
                    sX.Listen(x => sY.Send(x)),
                    sY.Listen(Console.WriteLine)
                });

                sX.Send(1);
                sX.Send(2);
                sX.Send(3);
                l.Unlisten();
            }
Beispiel #10
0
            public void Run()
            {
                StreamSink <int> sX = new StreamSink <int>();
                StreamSink <int> sY = new StreamSink <int>();

                // Should throw an exception because you're not allowed to use Send() inside
                // a callback.
                using (new ImmutableCompositeListener(new[]
                {
                    sX.Listen(x => sY.Send(x)),
                    sY.Listen(Console.WriteLine)
                }))
                {
                    sX.Send(1);
                    sX.Send(2);
                    sX.Send(3);
                }
            }
Beispiel #11
0
        public void TestCoalesce1()
        {
            StreamSink <int> s    = new StreamSink <int>((x, y) => x + y);
            List <int>       @out = new List <int>();

            using (s.Listen(@out.Add))
            {
                Transaction.RunVoid(() =>
                {
                    s.Send(2);
                });
                Transaction.RunVoid(() =>
                {
                    s.Send(8);
                    s.Send(40);
                });
            }
            CollectionAssert.AreEqual(new[] { 2, 48 }, @out.ToArray());
        }
Beispiel #12
0
        public void TestListen()
        {
            StreamSink <int> s = Stream.CreateSink <int>();

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

            ((Action)(() =>
            {
                // ReSharper disable once UnusedVariable
                IListener l = s.Listen(@out.Add);

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

            GC.Collect(0, GCCollectionMode.Forced);
            s.Send(3);
            s.Send(4);

            Assert.AreEqual(2, @out.Count);
        }
Beispiel #13
0
        public void TestStreamSendInCallbackThrowsException()
        {
            InvalidOperationException actual = null;

            StreamSink <int> s  = Stream.CreateSink <int>();
            StreamSink <int> s2 = Stream.CreateSink <int>();

            using (s.Listen(s2.Send))
            {
                try
                {
                    s.Send(5);
                }
                catch (InvalidOperationException e)
                {
                    actual = e;
                }
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("Send may not be called inside a Sodium callback.", actual.Message);
        }
Beispiel #14
0
        public async Task PostSeeInside()
        {
            OperationCanceledException actual = null;
            AutoResetEvent             re     = new AutoResetEvent(false);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Task task = Task.Run(
                    () =>
                {
                    Transaction.Post(
                        () =>
                    {
                        re.Set();

                        Thread.Sleep(500);

                        cts.Token.ThrowIfCancellationRequested();
                    });
                });

                re.WaitOne();

                StreamSink <Unit> sink2 = Stream.CreateSink <Unit>();
                sink2.Listen(_ => cts.Cancel());
                sink2.Send(Unit.Value);

                try
                {
                    await task;
                }
                catch (OperationCanceledException e)
                {
                    actual = e;
                }
            }

            Assert.IsNotNull(actual);
        }
Beispiel #15
0
 public void TestStreamSend()
 {
     StreamSink<int> s = new StreamSink<int>();
     List<int> @out = new List<int>();
     using (s.Listen(@out.Add))
     {
         s.Send(5);
     }
     CollectionAssert.AreEqual(new[] { 5 }, @out);
     s.Send(6);
     CollectionAssert.AreEqual(new[] { 5 }, @out);
 }
Beispiel #16
0
 public void Run()
 {
     StreamSink<int> sX = new StreamSink<int>();
     StreamSink<int> sY = new StreamSink<int>();
     // Should throw an exception because you're not allowed to use Send() inside
     // a callback.
     using (new ImmutableCompositeListener(new[]
     {
         sX.Listen(x => sY.Send(x)),
         sY.Listen(Console.WriteLine)
     }))
     {
         sX.Send(1);
         sX.Send(2);
         sX.Send(3);
     }
 }