Beispiel #1
0
        public void each_subscriber_is_scheduled_individually()
        {
            ManualScheduler scheduler = new ManualScheduler();

            AsyncSubject <int> subject = new AsyncSubject <int>(scheduler);

            StatsObserver <int> statsA = new StatsObserver <int>();
            StatsObserver <int> statsB = new StatsObserver <int>();

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

            subject.Subscribe(statsA);
            subject.Subscribe(statsB);


            scheduler.RunNext();

            Assert.IsTrue(statsA.NextCalled);
            Assert.IsFalse(statsB.NextCalled);

            scheduler.RunNext();

            Assert.IsTrue(statsB.NextCalled);
        }
Beispiel #2
0
        public void time_offset_excludes_values_of_exact_offset()
        {
            var stats = new StatsObserver <IList <int> >();

            var valueScheduler  = new ManualScheduler();
            var bufferScheduler = new ManualScheduler();

            DateTimeOffset startTime = DateTimeOffset.UtcNow;

            Observable.Range(0, 5, valueScheduler)
            .BufferWithTime(TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(20), bufferScheduler)
            .Subscribe(stats);

            Assert.IsFalse(stats.NextCalled);

            bufferScheduler.Now = startTime.AddMilliseconds(10);
            valueScheduler.RunNext();

            bufferScheduler.Now = startTime.AddMilliseconds(20); // exact offset value
            valueScheduler.RunNext();

            bufferScheduler.Now = startTime.AddMilliseconds(30);
            valueScheduler.RunNext();

            bufferScheduler.RunNext();
            bufferScheduler.RunNext();

            Assert.AreEqual(2, stats.NextCount);
            Assert.AreEqual(1, stats.NextValues[1].Count);
        }
Beispiel #3
0
        public void live_values_are_sent_through_scheduler()
        {
            ManualScheduler scheduler = new ManualScheduler();

            BehaviorSubject <int> subject = new BehaviorSubject <int>(0, scheduler);

            StatsObserver <int> stats = new StatsObserver <int>();

            subject.Subscribe(stats);

            subject.OnNext(1);
            subject.OnNext(2);
            subject.OnCompleted();

            Assert.IsFalse(stats.NextCalled);

            scheduler.RunNext();
            Assert.AreEqual(1, stats.NextCount);
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 0 }));
            Assert.IsFalse(stats.CompletedCalled);

            scheduler.RunNext();
            Assert.AreEqual(2, stats.NextCount);
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 0, 1 }));
            Assert.IsFalse(stats.CompletedCalled);

            scheduler.RunNext();
            Assert.AreEqual(3, stats.NextCount);
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 0, 1, 2 }));
            Assert.IsFalse(stats.CompletedCalled);

            scheduler.RunNext();
            Assert.IsTrue(stats.CompletedCalled);
        }
Beispiel #4
0
        public void values_cannot_be_replayed_out_of_order()
        {
            ManualScheduler scheduler = new ManualScheduler();

            ReplaySubject <int> subject = new ReplaySubject <int>(scheduler);

            StatsObserver <int> stats = new StatsObserver <int>();

            subject.OnNext(1);
            subject.OnNext(2);

            subject.Subscribe(stats);

            Assert.IsFalse(stats.NextCalled);

            scheduler.RunNext();
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 1 }));

            subject.OnNext(3);

            scheduler.RunNext();
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 1, 2 }));

            scheduler.RunNext();
            Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 1, 2, 3 }));

            subject.OnCompleted();

            scheduler.RunNext();
            Assert.IsTrue(stats.CompletedCalled);
        }
Beispiel #5
0
        public void no_value_is_emitted_if_sampled_value_hasnt_changed()
        {
            var subject = new Subject <int>();

            var stats = new StatsObserver <int>();

            var scheduler = new ManualScheduler();

            subject
            .Sample(TimeSpan.FromSeconds(1), scheduler)
            .Subscribe(stats);

            stats.OnNext(0);

            scheduler.RunNext();
            scheduler.RunNext();

            Assert.AreEqual(1, stats.NextCount);
        }
Beispiel #6
0
        public void scheduler_is_used_for_subsequent_subscriptions()
        {
            var sourceA = Observable.Empty <int>();
            var sourceB = new StatsSubject <int>();

            var scheduler = new ManualScheduler();
            var stats     = new StatsObserver <int>();

            sourceA.Concat(sourceB, scheduler).Subscribe(stats);

            scheduler.RunNext();

            Assert.AreEqual(0, sourceB.SubscriptionCount);
            Assert.AreEqual(1, scheduler.QueueSize);

            scheduler.RunNext();

            Assert.AreEqual(1, sourceB.SubscriptionCount);
            Assert.AreEqual(0, scheduler.QueueSize);
        }
Beispiel #7
0
        public void scheduler_used_to_subscribe_to_each_sequence()
        {
            var stats = new StatsObserver <int>();

            var scheduler = new ManualScheduler();

            var subjectA = new StatsSubject <int>();
            var subjectB = new StatsSubject <int>();
            var subjectC = new StatsSubject <int>();

            Observable.Merge(
                scheduler,
                subjectA,
                subjectB,
                subjectC
                )
            .Subscribe(stats);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();

            Assert.IsTrue(subjectA.HasSubscriptions);
            Assert.IsFalse(subjectB.HasSubscriptions);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();

            Assert.IsTrue(subjectB.HasSubscriptions);
            Assert.IsFalse(subjectC.HasSubscriptions);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();

            Assert.IsTrue(subjectC.HasSubscriptions);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();

            Assert.AreEqual(0, scheduler.QueueSize);
        }
Beispiel #8
0
        public void catch_with_observer_subscribes_to_next_through_scheduler()
        {
            ManualScheduler scheduler = new ManualScheduler();

            StatsObserver <IEvent <EventArgs> > observer = new StatsObserver <IEvent <EventArgs> >();

            EventOwner owner = new EventOwner();

            var eventObs = Observable.FromEvent <EventArgs>(x => owner.Event += x, x => owner.Event -= x);

            Observable.Throw <IEvent <EventArgs> >(new ApplicationException())
            .Catch(eventObs, scheduler)
            .Subscribe(observer);

            scheduler.RunNext();

            Assert.IsFalse(owner.HasSubscriptions);

            scheduler.RunNext();

            Assert.IsTrue(owner.HasSubscriptions);
        }
Beispiel #9
0
        public void completion_occurs_after_interval()
        {
            var scheduler = new ManualScheduler();
            var stats     = new StatsObserver <int>();

            Observable.Empty <int>()
            .Sample(TimeSpan.FromSeconds(1), scheduler)
            .Subscribe(stats);

            Assert.IsFalse(stats.CompletedCalled);

            scheduler.RunNext();
            Assert.IsTrue(stats.CompletedCalled);
        }
Beispiel #10
0
        public void scheduler_is_used_for_completion_when_take_is_zero()
        {
            var scheduler = new ManualScheduler();

            var stats = new StatsObserver <int>();

            new Subject <int>().Take(0, scheduler).Subscribe(stats);

            Assert.IsFalse(stats.CompletedCalled);

            scheduler.RunNext();

            Assert.IsTrue(stats.CompletedCalled);
        }
Beispiel #11
0
        public void last_value_is_taken_for_each_sample()
        {
            var subject = new Subject <int>();

            var stats = new StatsObserver <int>();

            var scheduler = new ManualScheduler();

            subject
            .Sample(TimeSpan.FromSeconds(1), scheduler)
            .Subscribe(stats);

            subject.OnNext(0);
            subject.OnNext(1);
            scheduler.RunNext();

            subject.OnNext(2);
            subject.OnNext(3);
            scheduler.RunNext();

            Assert.AreEqual(2, stats.NextCount);
            Assert.AreEqual(1, stats.NextValues[0]);
            Assert.AreEqual(3, stats.NextValues[1]);
        }
Beispiel #12
0
        public void next_and_complete_are_scheduled_separately()
        {
            ManualScheduler scheduler = new ManualScheduler();

            AsyncSubject <int> subject = new AsyncSubject <int>(scheduler);

            StatsObserver <int> statsA = new StatsObserver <int>();

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

            subject.Subscribe(statsA);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();
            Assert.IsTrue(statsA.NextCalled);
            Assert.IsFalse(statsA.CompletedCalled);

            Assert.AreEqual(1, scheduler.QueueSize);
            scheduler.RunNext();
            Assert.IsTrue(statsA.CompletedCalled);
        }
Beispiel #13
0
        public void scheduler_is_not_used_to_sent_complete()
        {
            Subject <int> subject = new Subject <int>();

            var scheduler = new ManualScheduler();

            var stats = new StatsObserver <int>();

            var obs        = subject.Prune(scheduler);
            var connection = obs.Connect();

            subject.OnNext(1);
            subject.OnCompleted();

            obs.Subscribe(stats);

            scheduler.RunNext(); // only run one

            Assert.IsTrue(stats.NextCalled);

            scheduler.RunNext();                  // only run one

            Assert.IsTrue(stats.CompletedCalled); // complete is called anyway
        }
Beispiel #14
0
        public void zero_length_list_is_emitted_when_no_values_are_available()
        {
            var stats = new StatsObserver <IList <int> >();

            var scheduler = new ManualScheduler();

            var manObs = Observable.Never <int>()
                         .BufferWithTime(TimeSpan.FromMilliseconds(1), scheduler)
                         .Take(1)
                         .Subscribe(stats);

            scheduler.RunNext();

            Assert.AreEqual(1, stats.NextCount);
            Assert.AreEqual(0, stats.NextValues[0].Count);
        }
Beispiel #15
0
        public void errors_are_sent_through_scheduler()
        {
            ManualScheduler scheduler = new ManualScheduler();

            BehaviorSubject <int> subject = new BehaviorSubject <int>(0, scheduler);

            StatsObserver <int> stats = new StatsObserver <int>();

            subject.OnError(new Exception());

            subject.Subscribe(stats);

            Assert.IsFalse(stats.ErrorCalled);

            scheduler.RunNext();
            Assert.IsTrue(stats.ErrorCalled);
        }
Beispiel #16
0
        public void complete_is_sent_through_scheduler()
        {
            ManualScheduler scheduler = new ManualScheduler();

            BehaviorSubject <int> subject = new BehaviorSubject <int>(0, scheduler);

            StatsObserver <int> stats = new StatsObserver <int>();

            subject.OnCompleted();

            subject.Subscribe(stats);

            Assert.IsFalse(stats.NextCalled);

            scheduler.RunNext();
            Assert.AreEqual(0, stats.NextCount);
            Assert.IsTrue(stats.CompletedCalled);
        }
Beispiel #17
0
        public void interval_is_cancelled_on_completion()
        {
            var subject = new Subject <int>();

            var stats = new StatsObserver <int>();

            var scheduler = new ManualScheduler();

            subject
            .Sample(TimeSpan.FromSeconds(1), scheduler)
            .Subscribe(stats);

            subject.OnNext(0);
            subject.OnCompleted();
            scheduler.RunNext();

            Assert.AreEqual(0, scheduler.QueueSize);
        }
Beispiel #18
0
        public void next_and_complete_are_scheduled_together()
        {
            ManualScheduler scheduler = new ManualScheduler();

            AsyncSubject <int> subject = new AsyncSubject <int>(scheduler);

            StatsObserver <int> statsA = new StatsObserver <int>();

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

            subject.Subscribe(statsA);

            scheduler.RunNext();

            Assert.IsTrue(statsA.NextCalled);
            Assert.IsTrue(statsA.CompletedCalled);
        }
Beispiel #19
0
        public void uses_scheduler_to_schedule_samples()
        {
            var subject = new Subject <int>();

            var stats = new StatsObserver <int>();

            var scheduler = new ManualScheduler();

            subject
            .Sample(TimeSpan.FromSeconds(1), scheduler)
            .Subscribe(stats);

            Assert.AreEqual(1, scheduler.QueueSize);

            subject.OnNext(0);
            scheduler.RunNext();

            Assert.AreEqual(1, scheduler.QueueSize);
            Assert.AreEqual(1, stats.NextCount);
            Assert.AreEqual(0, stats.NextValues[0]);
        }
Beispiel #20
0
        public void scheduler_is_used_to_distribute_values()
        {
            ManualScheduler scheduler = new ManualScheduler();

            AsyncSubject <int> subject = new AsyncSubject <int>(scheduler);

            StatsObserver <int> statsA = new StatsObserver <int>();

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

            subject.Subscribe(statsA);

            Assert.IsFalse(statsA.NextCalled);

            scheduler.RunNext();

            Assert.IsTrue(statsA.NextCalled);
        }