Exemplo n.º 1
0
        public void SubscribableBase_Assert_StoppedAfterOnError()
        {
            var sub = new MySubscribable();

            var res = -1;

            var o = Observer.Create <int>(
                x =>
            {
                res *= x;
            },
                ex =>
            {
                Assert.IsTrue(ex.Message == "foo");
                res *= -1;
            },
                () =>
            {
                Assert.Fail();
            }
                );

            sub.Subscribe(o);

            sub.Observer.OnNext(42);
            sub.Observer.OnError(new Exception("foo"));

            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnNext(42), ex => ex.Message.Contains("terminated"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnError(new Exception()), ex => ex.Message.Contains("terminated"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnCompleted(), ex => ex.Message.Contains("terminated"));

            Assert.AreEqual(42, res);
        }
Exemplo n.º 2
0
        public void SubscribableBase_Assert_ThrowWhileBusy_OnCompleted()
        {
            var sub = new MySubscribable();

            var s = new ManualResetEvent(false);
            var e = new ManualResetEvent(false);

            var o = Observer.Create <int>(
                x =>
            {
                e.WaitOne();
            },
                ex =>
            {
                e.WaitOne();
            },
                () =>
            {
                s.Set();
                e.WaitOne();
            }
                );

            sub.Subscribe(o);

            var t = Task.Run(() => sub.Observer.OnCompleted());

            s.WaitOne();

            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnNext(43), ex => ex.Message.Contains("processing"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnError(new Exception()), ex => ex.Message.Contains("processing"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnCompleted(), ex => ex.Message.Contains("processing"));

            e.Set();
            t.Wait();

            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnNext(42), ex => ex.Message.Contains("terminated"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnError(new Exception()), ex => ex.Message.Contains("terminated"));
            AssertEx.ThrowsException <InvalidOperationException>(() => sub.Observer.OnCompleted(), ex => ex.Message.Contains("terminated"));
        }