public void OnTerminate_Completed()
        {
            var count = 0;

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

            ms.OnCompleted();
            ms.OnCompleted();

            Assert.AreEqual(1, count);
        }
        public void Race()
        {
            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.OnCompleted();
                },
                                () =>
                {
                    foreach (var v in us.BlockingEnumerable())
                    {
                        to.OnNext(v);
                    }
                    to.OnCompleted();
                });

                to.AssertValueCount(1000)
                .AssertNoError()
                .AssertCompleted();
            }
        }
        public void Race_While()
        {
            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.OnCompleted();
                },
                                () =>
                {
                    us.BlockingSubscribeWhile(v =>
                    {
                        to.OnNext(v);
                        return(true);
                    }, to.OnError, to.OnCompleted);
                });

                to.AssertValueCount(1000)
                .AssertNoError()
                .AssertCompleted();
            }
        }
        public void Offline_Basic_Fused()
        {
            var ms = new MonocastSubject <int>();

            ms.OnNext(1);
            ms.OnNext(2);
            ms.OnNext(3);
            ms.OnCompleted();

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

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

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

            Assert.False(ms.HasObservers);

            to.AssertResult(1, 2, 3);

            ms.Test().AssertFailure(typeof(InvalidOperationException));
        }
        public void No_Combinations_Complete_After_All()
        {
            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.OnCompleted();

            Assert.True(us2.HasObserver(), "Other source disposed?");

            us2.EmitAll(1, 2, 3, 4, 5);

            to.AssertResult();
        }
        public void Basic()
        {
            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.OnNext(1);

            to.AssertEmpty();

            us1.OnNext(2);

            to.AssertEmpty();

            us2.OnNext(10);

            to.AssertValuesOnly(12);

            us2.OnNext(20);

            to.AssertValuesOnly(12, 22);

            us2.OnCompleted();

            to.AssertValuesOnly(12, 22);

            us1.OnNext(3);

            to.AssertValuesOnly(12, 22, 23);

            us1.OnCompleted();

            to.AssertResult(12, 22, 23);
        }
        public void Online_Basic()
        {
            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);

            ms.OnCompleted();

            to.AssertResult(1, 2, 3);

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

            ms.Test().AssertFailure(typeof(InvalidOperationException));
        }
        public void Race_DelayErrors()
        {
            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var us1 = new MonocastSubject <int>();
                var us2 = new MonocastSubject <int>();

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

                TestHelper.Race(() => {
                    for (int j = 0; j < 1000; j++)
                    {
                        us1.OnNext(j);
                    }
                    us1.OnCompleted();
                }, () => {
                    for (int j = 1000; j < 2000; j++)
                    {
                        us2.OnNext(j);
                    }
                    us2.OnCompleted();
                });

                to.AssertValueCount(1000)
                .AssertNoError()
                .AssertCompleted();

                var list = to.Items;
                for (int j = 0; j < 1000; j++)
                {
                    Assert.AreEqual(j + (1000 + j), list[j]);
                }
            }
        }
        public void Race_DelayErrors()
        {
            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var us1 = new MonocastSubject <int>();
                var us2 = new MonocastSubject <int>();

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

                us1.OnNext(0);
                us2.OnNext(1000);

                TestHelper.Race(() => {
                    for (int j = 1; j < 1000; j++)
                    {
                        us1.OnNext(j);
                    }
                    us1.OnCompleted();
                }, () => {
                    for (int j = 1001; j < 2000; j++)
                    {
                        us2.OnNext(j);
                    }
                    us2.OnCompleted();
                });

                to.AssertValueCount(1999)
                .AssertNoError()
                .AssertCompleted();
            }
        }