예제 #1
0
        public void Predicate_Dispose()
        {
            var count = 0;
            var us    = new CompletableSubject();

            var to = CompletableSource.Defer(() =>
            {
                if (count++ < 5)
                {
                    return(CompletableSource.Error(new InvalidOperationException()));
                }
                return(us);
            })
                     .Retry((e, c) => true)
                     .Test();

            to.AssertEmpty();

            Assert.True(us.HasObserver());

            to.Dispose();

            Assert.False(us.HasObserver());

            Assert.AreEqual(6, count);
        }
예제 #2
0
        public void ReferenceCount()
        {
            var cs = new CompletableSubject(true);

            var bd = new BooleanDisposable();

            cs.OnSubscribe(bd);

            Assert.False(bd.IsDisposed());

            var to1 = cs.Test();

            Assert.False(bd.IsDisposed());

            var to2 = cs.Test();

            to1.Dispose();

            Assert.False(bd.IsDisposed());

            to2.Dispose();

            Assert.True(bd.IsDisposed());

            cs.Test().AssertFailure(typeof(OperationCanceledException));
        }
예제 #3
0
        public IDisposable Subscribe(ICompletableObserver observer)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(Lapse));
            }

            if (_subject == null)
            {
                _subject = new CompletableSubject();
            }

            if (!_isSubscribed)
            {
                _isSubscribed = true;
                _action?.Invoke(this);

                if (_isDisposed)
                {
                    observer.OnCompleted();
                    return(Disposable.Empty);
                }
            }

            return(_subject.Subscribe(observer));
        }
        public void Dispose()
        {
            var count = 0;
            var us    = new CompletableSubject();

            var to = CompletableSource.Defer(() =>
            {
                if (count++ < 5)
                {
                    return(CompletableSource.Empty());
                }
                return(us);
            })
                     .Repeat()
                     .Test();

            to.AssertEmpty();

            Assert.True(us.HasObserver());

            to.Dispose();

            Assert.False(us.HasObserver());

            Assert.AreEqual(6, count);
        }
예제 #5
0
        public void Race_Error()
        {
            var ex = new InvalidOperationException();

            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var s = new Subject <ICompletableSource>();

                var cs1 = new CompletableSubject();


                var to = s
                         .SwitchMap(v => v)
                         .Test();

                TestHelper.Race(() =>
                {
                    cs1.OnCompleted();
                },
                                () =>
                {
                    s.OnNext(CompletableSource.Error(ex));
                    s.OnCompleted();
                }
                                );

                to.AssertFailure(typeof(InvalidOperationException));
            }
        }
        public void Timeout_Fallback_Error()
        {
            var ts = new TestScheduler();
            var us = new CompletableSubject();

            var count = 0;
            var fb    = CompletableSource.FromAction(() =>
            {
                ++count;
                throw new InvalidOperationException();
            });

            var to = us
                     .Timeout(TimeSpan.FromSeconds(1), ts, fb)
                     .Test();

            to.AssertEmpty();

            ts.AdvanceTimeBy(100);

            Assert.True(us.HasObserver());

            ts.AdvanceTimeBy(900);

            Assert.False(us.HasObserver());

            to.AssertFailure(typeof(InvalidOperationException));

            Assert.AreEqual(1, count);
        }
예제 #7
0
        public void Enumerable_Race_MaxConcurrent()
        {
            for (int k = 1; k < 4; k++)
            {
                for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
                {
                    var cs1 = new CompletableSubject();
                    var cs2 = new CompletableSubject();

                    var to = CompletableSource.Merge(new List <ICompletableSource>()
                    {
                        cs1, cs2
                    }, maxConcurrency: k)
                             .Test();

                    TestHelper.Race(() =>
                    {
                        cs1.OnCompleted();
                    }, () =>
                    {
                        cs2.OnCompleted();
                    });

                    to.AssertResult();
                }
            }
        }
        public void Timeout_Fallback()
        {
            var ts = new TestScheduler();
            var us = new CompletableSubject();

            var count = 0;
            var fb    = CompletableSource.FromAction(() => count++);

            var to = us
                     .Timeout(TimeSpan.FromSeconds(1), ts, fb)
                     .Test();

            to.AssertEmpty();

            ts.AdvanceTimeBy(100);

            Assert.True(us.HasObserver());

            ts.AdvanceTimeBy(900);

            Assert.False(us.HasObserver());

            to.AssertResult();

            Assert.AreEqual(1, count);
        }
예제 #9
0
        public void Observer_Dispose()
        {
            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var cs = new CompletableSubject();

                var to = new TestObserver <object>();

                var cdl = new CountdownEvent(1);

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

                cs.BlockingSubscribe(to);

                cdl.Wait();

                Assert.False(cs.HasObserver());
            }
        }
예제 #10
0
        public void Cancel_Wait()
        {
            var cs  = new CompletableSubject();
            var cts = new CancellationTokenSource();

            try
            {
                Task.Factory.StartNew(() =>
                {
                    while (!cs.HasObserver())
                    {
                        ;
                    }

                    Thread.Sleep(100);

                    cts.Cancel();
                });

                cs
                .Wait(cts: cts);
                Assert.Fail();
            }
            catch (OperationCanceledException)
            {
                // expected
            }

            Assert.False(cs.HasObserver());
        }
        public void Dispose()
        {
            var d = new IDisposable[1];

            var cs = new CompletableSubject();

            var source = cs.Cache(c => d[0] = c);

            Assert.IsNull(d[0]);
            Assert.IsFalse(cs.HasObserver());

            var to1 = source.Test();

            Assert.IsNotNull(d[0]);
            Assert.IsTrue(cs.HasObserver());

            source.Test(true).AssertEmpty();

            d[0].Dispose();

            Assert.IsFalse(cs.HasObserver());

            to1.AssertFailure(typeof(OperationCanceledException));

            source.Test().AssertFailure(typeof(OperationCanceledException));
        }
        public void Single_Dispose_Inner()
        {
            var ss = new SingleSubject <int>();
            var cs = new CompletableSubject();

            var to = ss
                     .FlatMap(v => cs)
                     .Test();

            to.AssertSubscribed();

            Assert.True(ss.HasObserver());
            Assert.False(cs.HasObserver());

            ss.OnSuccess(1);

            Assert.True(cs.HasObserver());
            Assert.False(ss.HasObserver());

            to.AssertEmpty();

            cs.OnCompleted();

            to.AssertResult();
        }
예제 #13
0
        public void Race()
        {
            for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
            {
                var s = new Subject <ICompletableSource>();

                var cs1 = new CompletableSubject();

                var to = s
                         .SwitchMap(v => v)
                         .Test();

                TestHelper.Race(() =>
                {
                    cs1.OnCompleted();
                },
                                () =>
                {
                    s.OnNext(CompletableSource.Empty());
                    s.OnCompleted();
                }
                                );

                to.AssertResult();
            }
        }
예제 #14
0
        public void Race_MaxConcurrent()
        {
            for (int k = 1; k < 4; k++)
            {
                for (int i = 0; i < TestHelper.RACE_LOOPS; i++)
                {
                    var cs1 = new CompletableSubject();
                    var cs2 = new CompletableSubject();

                    var to = new[] { cs1, cs2 }
                    .ToObservable()
                    .Merge(maxConcurrency: k)
                    .Test();

                    TestHelper.Race(() =>
                    {
                        cs1.OnCompleted();
                    }, () =>
                    {
                        cs2.OnCompleted();
                    });

                    to.AssertResult();
                }
            }
        }
        public void Time_Dispose_Other()
        {
            var ts = new TestScheduler();

            var cs = new CompletableSubject();

            var to = cs
                     .DelaySubscription(TimeSpan.FromSeconds(1), ts)
                     .Test();

            Assert.False(cs.HasObserver());

            ts.AdvanceTimeBy(500);

            Assert.False(cs.HasObserver());

            ts.AdvanceTimeBy(500);

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());

            to.AssertEmpty();
        }
        public void Crash_OnSubscribe()
        {
            var cs = new CompletableSubject();

            cs.SubscribeSafe(new FailingCompletableObserver(true, true, true));

            Assert.False(cs.HasObserver());
        }
예제 #17
0
        public void Dispose()
        {
            var cs = new CompletableSubject();

            cs.ObserveOn(NewThreadScheduler.Default)
            .Test(true)
            .AssertEmpty();

            Assert.False(cs.HasObserver());
        }
        public void Crash_OnError()
        {
            var cs = new CompletableSubject();

            cs.SubscribeSafe(new FailingCompletableObserver(false, true, true));

            Assert.True(cs.HasObserver());

            cs.OnError(new InvalidOperationException("main"));
        }
        public void Completable_Error()
        {
            var us = new CompletableSubject();

            CompletableSource.Error(new InvalidOperationException())
            .AndThen(us)
            .Test()
            .AssertFailure(typeof(InvalidOperationException));

            Assert.False(us.HasObserver());
        }
예제 #20
0
        public void Handler_Errors_Immediately()
        {
            var us = new CompletableSubject();

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

            Assert.False(us.HasObserver());
        }
예제 #21
0
        public void Handler_Completes_Immediately()
        {
            var us = new CompletableSubject();

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

            Assert.False(us.HasObserver());
        }
        public void Dispose()
        {
            var cs = new CompletableSubject();

            var to = cs.Hide().Test();

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());
        }
예제 #23
0
        public void Complete_Dispose_Other()
        {
            var cs = new CompletableSubject();

            var to = new TestObserver <object>();

            cs.Subscribe(() => { to.Dispose(); });
            cs.Subscribe(to);

            cs.OnCompleted();

            to.AssertEmpty();
        }
예제 #24
0
        public void Dispose()
        {
            var cs = new CompletableSubject();

            var to = cs.Delay(TimeSpan.FromMinutes(5), NewThreadScheduler.Default)
                     .Test();

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());
        }
        public void Normal_Dispose()
        {
            var cs = new CompletableSubject();
            var to = new TestObserver <object>();

            cs.SubscribeSafe(to);

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());
        }
예제 #26
0
        public void Observer_Disposes_Upfront()
        {
            var cs = new CompletableSubject();

            var to = cs.Test();

            var to1 = cs.Test(true);

            cs.OnCompleted();

            to.AssertResult();

            to1.AssertEmpty();
        }
예제 #27
0
        public void Disposed()
        {
            var up = new CompletableSubject();

            IObservable <int> o = up.ToObservable <int>();

            var to = o.Test();

            Assert.True(up.HasObserver());

            to.Dispose();

            Assert.False(up.HasObserver());
        }
        public void Single_Dispose_Main()
        {
            var cs = new CompletableSubject();

            var to = cs
                     .AndThen(SingleSource.Just(1))
                     .Test();

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());
        }
        public void Observable_Dispose_Main()
        {
            var cs = new CompletableSubject();

            var to = cs
                     .AndThen(Observable.Range(1, 5))
                     .Test();

            Assert.True(cs.HasObserver());

            to.Dispose();

            Assert.False(cs.HasObserver());
        }
        public void Completable_Dispose_Next()
        {
            var us = new CompletableSubject();

            var to = CompletableSource.Empty()
                     .AndThen(us)
                     .Test();

            Assert.True(us.HasObserver());

            to.Dispose();

            Assert.False(us.HasObserver());
        }