Пример #1
0
        public void RefCount_ConnectsOnFirst()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnCompleted <int>(250)
                );

            var subject = new MySubject();

            var conn = new ConnectableObservable <int>(xs, subject);

            var res = scheduler.Start(() =>
                                      conn.RefCount()
                                      );

            res.Messages.AssertEqual(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnCompleted <int>(250)
                );

            Assert.True(subject.Disposed);
        }
Пример #2
0
        public void LazyRefCount_NotConnected()
        {
            var scheduler    = new TestScheduler();
            var disconnected = false;
            var count        = 0;
            var xs           = Observable.Defer(() =>
            {
                count++;
                return(Observable.Create <int>(obs =>
                {
                    return () => { disconnected = true; };
                }));
            });

            var subject = new MySubject();

            var conn = new ConnectableObservable <int>(xs, subject);
            var refd = conn.RefCount(TimeSpan.FromTicks(20), scheduler);

            var dis1 = refd.Subscribe();

            Assert.Equal(1, count);
            Assert.Equal(1, subject.SubscribeCount);
            Assert.False(disconnected);

            var dis2 = refd.Subscribe();

            Assert.Equal(1, count);
            Assert.Equal(2, subject.SubscribeCount);
            Assert.False(disconnected);

            dis1.Dispose();
            Assert.False(disconnected);
            dis2.Dispose();
            Assert.False(disconnected);

            scheduler.AdvanceBy(19);
            Assert.False(disconnected);

            scheduler.AdvanceBy(1);
            Assert.True(disconnected);
            disconnected = false;

            var dis3 = refd.Subscribe();

            Assert.Equal(2, count);
            Assert.Equal(3, subject.SubscribeCount);
            Assert.False(disconnected);

            dis3.Dispose();
            scheduler.AdvanceBy(20);
            Assert.True(disconnected);
        }
Пример #3
0
        public void RefCount_NotConnected()
        {
            var disconnected = false;
            var count        = 0;
            var xs           = Observable.Defer(() =>
            {
                count++;
                return(Observable.Create <int>(obs =>
                {
                    return () => { disconnected = true; };
                }));
            });

            var subject = new MySubject();

            var conn = new ConnectableObservable <int>(xs, subject);
            var refd = conn.RefCount();

            var dis1 = refd.Subscribe();

            Assert.Equal(1, count);
            Assert.Equal(1, subject.SubscribeCount);
            Assert.False(disconnected);

            var dis2 = refd.Subscribe();

            Assert.Equal(1, count);
            Assert.Equal(2, subject.SubscribeCount);
            Assert.False(disconnected);

            dis1.Dispose();
            Assert.False(disconnected);
            dis2.Dispose();
            Assert.True(disconnected);
            disconnected = false;

            var dis3 = refd.Subscribe();

            Assert.Equal(2, count);
            Assert.Equal(3, subject.SubscribeCount);
            Assert.False(disconnected);

            dis3.Dispose();
            Assert.True(disconnected);
        }