コード例 #1
0
        public void Main_Disposes_Others_On_Error()
        {
            var us1 = new MonocastSubject <int>();
            var us2 = new MonocastSubject <int>();

            us1.Emit(10);
            us2.Emit(100);

            var source = new PublishSubject <int>();

            var to = source.WithLatestFrom((a, bs) =>
            {
                foreach (var i in bs)
                {
                    a += i;
                }
                return(a);
            }, us1, us2).Test();

            Assert.True(us1.HasObserver());
            Assert.True(us2.HasObserver());

            to.AssertEmpty();

            source.EmitError(new InvalidOperationException(), 1, 2, 3, 4, 5);

            Assert.False(us1.HasObserver());
            Assert.False(us2.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException), 111, 112, 113, 114, 115);
        }
コード例 #2
0
        public void Switch_Outer_Completes_First()
        {
            var source = new PublishSubject <IObservableSource <int> >();

            var to = source.Switch().Test();

            to.AssertEmpty();

            var us1 = new MonocastSubject <int>();

            source.OnNext(us1);

            Assert.True(us1.HasObserver());

            var us2 = new MonocastSubject <int>();

            source.OnNext(us2);

            Assert.True(us2.HasObserver());
            Assert.False(us1.HasObserver());

            source.OnCompleted();

            Assert.True(us2.HasObserver());

            to.AssertEmpty();

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

            to.AssertResult(1, 2, 3, 4, 5);
        }
コード例 #3
0
        public void Mapper_Crashes()
        {
            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;
                }

                if (s == 33)
                {
                    throw new InvalidOperationException();
                }
                return(s);
            },
                                          us1,
                                          us2
                                          ).Test();

            us1.Emit(1, 2, 3);

            Assert.True(us1.HasObserver());
            Assert.True(us2.HasObserver());

            us2.Emit(10, 20, 30, 40);

            Assert.False(us1.HasObserver());
            Assert.False(us2.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException), 11, 22);
        }
コード例 #4
0
        public void Error_Delayed()
        {
            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);
            }, true,
                                          us1,
                                          us2
                                          ).Test();

            us1.EmitError(new InvalidOperationException(), 1, 2, 3);

            Assert.True(us2.HasObserver(), "us2: No observers!");

            us2.Emit(10, 20, 30, 40);

            Assert.False(us2.HasObserver(), "us2: Observers present!");

            to.AssertFailure(typeof(InvalidOperationException), 11, 22, 33);
        }
コード例 #5
0
        public void Main_Disposes_Others_On_Completion()
        {
            var us1 = new MonocastSubject <int>();
            var us2 = new MonocastSubject <int>();

            us1.Emit(10);
            us2.Emit(100);

            var source = new PublishSubject <int>();

            var to = source.WithLatestFrom((a, bs) =>
            {
                foreach (var i in bs)
                {
                    a += i;
                }
                return(a);
            }, us1, us2).Test();

            Assert.True(us1.HasObserver());
            Assert.True(us2.HasObserver());

            to.AssertEmpty();

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

            Assert.False(us1.HasObserver());
            Assert.False(us2.HasObserver());

            to.AssertResult(111, 112, 113, 114, 115);
        }
コード例 #6
0
        public void Disposed()
        {
            var us = new MonocastSubject <int>();

            var to = new TestObserverLocal();

            bool[] disposed = { false };

            var d = us.ConcatMap(v => new OnDispose <int>(Enumerable.Range(v * 100, 5), disposed))
                    .SubscribeWith(to);

            Assert.True(us.HasObserver());

            to.AssertEmpty();

            us.OnNext(1);

            to.AssertValuesOnly(100, 101, 102, 103, 104);

            Assert.True(us.HasObserver());

            us.OnNext(2);

            Assert.False(us.HasObserver());

            Assert.True(disposed[0]);

            to.AssertResult(100, 101, 102, 103, 104, 200, 201);
        }
        public void Dispose()
        {
            var us = new MonocastSubject <int>();

            var en = us.BlockingEnumerable().GetEnumerator();

            Assert.True(us.HasObserver());

            en.Dispose();

            Assert.False(us.HasObserver());

            Assert.False(en.MoveNext());
        }
        public void Basic_Observer_Dispose()
        {
            var to  = new TestObserver <int>();
            var cdl = new CountdownEvent(2);
            var us  = new MonocastSubject <int>();

            Task.Factory.StartNew(() =>
            {
                while (to.ItemCount != 5)
                {
                    ;
                }
                to.Dispose();
                cdl.Signal();
            });

            Task.Factory.StartNew(() =>
            {
                us.Emit(1, 2, 3, 4, 5);
                cdl.Signal();
            });

            us.BlockingSubscribe(to);

            to.AssertValuesOnly(1, 2, 3, 4, 5);

            Assert.True(cdl.Wait(3000));

            Assert.False(us.HasObserver());
        }
        public void Mapper_Crash()
        {
            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;
                }
                if (v == 22)
                {
                    throw new InvalidOperationException();
                }
                return(v);
            }, us1, us2).Test();

            to.AssertEmpty();

            us1.OnNext(1);
            us2.OnNext(10);

            us1.OnNext(2);
            us2.OnNext(20);

            Assert.False(us1.HasObserver());
            Assert.False(us2.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException), 11, 12);
        }
コード例 #10
0
        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();
        }
コード例 #11
0
        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 Basic_While_Dispose()
        {
            var to  = new TestObserver <int>();
            var sad = new SingleAssignmentDisposable();
            var cdl = new CountdownEvent(2);
            var us  = new MonocastSubject <int>();

            Task.Factory.StartNew(() =>
            {
                while (to.ItemCount != 5)
                {
                    ;
                }
                sad.Dispose();
                cdl.Signal();
            });

            Task.Factory.StartNew(() =>
            {
                us.Emit(1, 2, 3, 4, 5);
                cdl.Signal();
            });

            us.BlockingSubscribeWhile(v => {
                to.OnNext(v);
                return(true);
            }, to.OnError, to.OnCompleted, d => sad.Disposable = d);

            to.AssertValuesOnly(1, 2, 3, 4, 5);

            Assert.True(cdl.Wait(3000));

            Assert.False(us.HasObserver());
        }
コード例 #13
0
        public void Main_Disposed_Handler_Completes()
        {
            var us = new MonocastSubject <int>();

            us.RepeatWhen(v => ObservableSource.Empty <int>())
            .Test()
            .AssertResult();

            Assert.False(us.HasObserver());
        }
コード例 #14
0
        public void Main_Disposed_Handler_Errors()
        {
            var us = new MonocastSubject <int>();

            us.RepeatWhen(v => ObservableSource.Error <int>(new InvalidOperationException()))
            .Test()
            .AssertFailure(typeof(InvalidOperationException));

            Assert.False(us.HasObserver());
        }
コード例 #15
0
        public void Handler_Disposed()
        {
            var us = new MonocastSubject <int>();

            ObservableSource.Range(1, 5)
            .RetryWhen(v => us)
            .Test()
            .AssertResult(1, 2, 3, 4, 5);

            Assert.False(us.HasObserver());
        }
コード例 #16
0
        public void Handler_Completes_Immediately()
        {
            var us = new MonocastSubject <int>();

            us
            .RetryWhen(v => ObservableSource.Empty <int>())
            .Test()
            .AssertResult();

            Assert.False(us.HasObserver());
        }
コード例 #17
0
        public void Handler_Disposed()
        {
            var us = new MonocastSubject <int>();

            ObservableSource.Range(1, 5).ConcatError(new InvalidOperationException())
            .RepeatWhen(v => us)
            .Test()
            .AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);

            Assert.False(us.HasObserver());
        }
コード例 #18
0
        public void Dispose()
        {
            var d = default(IDisposable);

            var us = new MonocastSubject <int>();

            var o = us.Cache(capacityHint: 16, cancel: v => d = v);

            Assert.IsNull(d);
            Assert.False(us.HasObserver());

            var to = o.Test();

            Assert.IsNotNull(d);
            Assert.True(us.HasObserver());

            d.Dispose();

            Assert.False(us.HasObserver());

            to.AssertFailure(typeof(OperationCanceledException));
        }
コード例 #19
0
        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));
        }
コード例 #20
0
        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);
        }