public void OnTerminate_Error()
        {
            var count = 0;

            var ms = new MonocastSubject <int>(onTerminate: () => count++);

            ms.OnError(new InvalidOperationException());
            ms.OnError(new IndexOutOfRangeException());

            Assert.AreEqual(1, count);
        }
        public void Async_Regular_Error_Delayed()
        {
            for (int i = 0; i < 2; i++)
            {
                var ts   = new TestScheduler();
                var subj = new MonocastSubject <int>();

                var to = subj.ObserveOn(ts, true, fair: i == 0)
                         .Test()
                         .WithTag($"fair={i == 0}");

                Assert.True(subj.HasObservers);

                to.AssertEmpty();

                subj.OnNext(1);
                subj.OnNext(2);
                subj.OnError(new InvalidOperationException());

                to.AssertEmpty();

                ts.AdvanceTimeBy(1);

                to.AssertFailure(typeof(InvalidOperationException), 1, 2);
            }
        }
        public void Offline_Error_Fused()
        {
            var ms = new MonocastSubject <int>();
            var ex = new IndexOutOfRangeException();

            ms.OnNext(1);
            ms.OnNext(2);
            ms.OnNext(3);
            ms.OnError(ex);

            Assert.False(ms.HasObservers);
            Assert.True(ms.HasException());
            Assert.False(ms.HasCompleted());
            Assert.AreEqual(ex, ms.GetException());

            var to = ms.Test(fusionMode: FusionSupport.Any);

            Assert.False(ms.HasObservers);

            to.AssertFuseable()
            .AssertFusionMode(FusionSupport.Async);

            to.AssertFailure(typeof(IndexOutOfRangeException), 1, 2, 3);


            ms.Test().AssertFailure(typeof(InvalidOperationException));
        }
        public void Error_Delayed()
        {
            var us1 = new MonocastSubject <int>();
            var us2 = new MonocastSubject <int>();

            var to = ObservableSource.CombineLatest(a =>
            {
                int v = 0;
                foreach (var e in a)
                {
                    v += e;
                }
                return(v);
            }, true, us1, us2).Test();

            to.AssertEmpty();

            us1.OnNext(1);
            us1.OnError(new InvalidOperationException());

            Assert.True(us2.HasObserver());

            us2.EmitAll(10, 20, 30, 40, 50);

            to.AssertFailure(typeof(InvalidOperationException), 11, 21, 31, 41, 51);
        }
        public void Online_Error()
        {
            var ms = new MonocastSubject <int>();

            Assert.False(ms.HasObservers);
            Assert.False(ms.HasException());
            Assert.False(ms.HasCompleted());
            Assert.Null(ms.GetException());

            var to = ms.Test();

            ms.Test().AssertFailure(typeof(InvalidOperationException));

            Assert.True(ms.HasObservers);
            Assert.False(ms.HasException());
            Assert.False(ms.HasCompleted());
            Assert.Null(ms.GetException());

            to.AssertEmpty();

            ms.OnNext(1);

            to.AssertValuesOnly(1);

            ms.OnNext(2);

            to.AssertValuesOnly(1, 2);

            ms.OnNext(3);

            to.AssertValuesOnly(1, 2, 3);

            var ex = new IndexOutOfRangeException();

            ms.OnError(ex);

            to.AssertFailure(typeof(IndexOutOfRangeException), 1, 2, 3);

            Assert.False(ms.HasObservers);
            Assert.True(ms.HasException());
            Assert.False(ms.HasCompleted());
            Assert.AreEqual(ex, ms.GetException());

            ms.Test().AssertFailure(typeof(InvalidOperationException));
        }
        public void Error()
        {
            var us1 = new MonocastSubject <int>();
            var us2 = new MonocastSubject <int>();

            var to = ObservableSource.CombineLatest(a =>
            {
                int v = 0;
                foreach (var e in a)
                {
                    v += e;
                }
                return(v);
            }, us1, us2).Test();

            to.AssertEmpty();

            us1.OnError(new InvalidOperationException());

            Assert.False(us2.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException));
        }
        public void Race_With_Error()
        {
            var exc = new InvalidOperationException();

            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var us = new MonocastSubject <int>();

                var to = new TestObserver <int>();

                TestHelper.Race(() =>
                {
                    for (int j = 0; j < 1000; j++)
                    {
                        us.OnNext(j);
                    }
                    us.OnError(exc);
                },
                                () =>
                {
                    try
                    {
                        foreach (var v in us.BlockingEnumerable())
                        {
                            to.OnNext(v);
                        }
                    } catch (Exception ex)
                    {
                        to.OnError(ex);
                    }
                });

                to.AssertValueCount(1000)
                .AssertError(typeof(InvalidOperationException))
                .AssertNotCompleted();
            }
        }
        public void Error_Second_Source()
        {
            var us1 = new MonocastSubject <int>();
            var us2 = new MonocastSubject <int>();

            var to = ObservableSource.Zip(a =>
            {
                int s = 0;
                foreach (var v in a)
                {
                    s += v;
                }
                return(s);
            },
                                          us1,
                                          us2
                                          ).Test();

            us1.OnNext(1);

            to.AssertEmpty();

            us1.OnNext(2);

            to.AssertEmpty();

            us2.OnNext(10);

            to.AssertValuesOnly(11);

            us2.OnError(new InvalidOperationException());

            Assert.False(us1.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException), 11);
        }