public void ReceiveItemsAfterAnExceptionIsEncountered() { TestScheduler scheduler = new TestScheduler(); IObservable<int> observableA = scheduler.CreateColdObservable<int>(new[] { new Recorded<Notification<int>>(10, Notification.CreateOnError<int>(new InvalidOperationException())) }); IObservable<int> observableB = scheduler.CreateColdObservable<int>(new[] { new Recorded<Notification<int>>(10, Notification.CreateOnNext<int>(314)) }); Queue<IObservable<int>> observables = new Queue<IObservable<int>>(new [] { observableA, observableB }); IObservable<int> observable = A.Fake<IObservable<int>>(); A.CallTo(() => observable.Subscribe(A<IObserver<int>>.Ignored)) .Invokes(call => observables.Dequeue().Subscribe(call.GetArgument<IObserver<int>>(0))); IObserver<Exception> errors = A.Fake<IObserver<Exception>>(); IObserver<int> values = A.Fake<IObserver<int>>(); observable.Retry(errors).Subscribe(values); scheduler.AdvanceBy(20); A.CallTo(() => values.OnNext(314)).MustHaveHappened(Repeated.Exactly.Once); }
public void Iterate_Complete() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnNext(30, 3), OnNext(40, 4), OnCompleted<int>(50) ); var ys = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnCompleted<int>(30) ); var zs = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnNext(30, 3), OnNext(40, 4), OnNext(50, 5), OnCompleted<int>(60) ); var res = scheduler.Start(() => ObservableEx.Create<int>(observer => ToIterate_Complete(xs, ys, zs, observer))); res.Messages.AssertEqual( OnNext(200, 1), OnNext(250, 2), OnNext(280, 3), OnCompleted<int>(280) ); xs.Subscriptions.AssertEqual( Subscribe(200, 250) ); ys.Subscriptions.AssertEqual( Subscribe(250, 280) ); zs.Subscriptions.AssertEqual( Subscribe(280, 280) ); }
public void DoWhile_AlwaysFalse() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(50, 1), OnNext(100, 2), OnNext(150, 3), OnNext(200, 4), OnCompleted <int>(250) ); var results = scheduler.Start(() => Observable.DoWhile(xs, () => false)); results.Messages.AssertEqual( OnNext(250, 1), OnNext(300, 2), OnNext(350, 3), OnNext(400, 4), OnCompleted <int>(450) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450) ); }
public void FilterBurstsInColdObservable() { var scheduler = new TestScheduler(); // A cold observable will begin emitting when the observer subscribes // in this case, each emission defined for the observable will be realtive to the observer subscription time // which by default is 200 (defined in ReactiveTest.Subscribed) var xs = scheduler.CreateColdObservable( OnNext(250, 1), OnNext(258, 2), OnNext(262, 3), OnNext(450, -1), OnNext(451, -2), OnNext(460, -3), OnCompleted<int>(500) ); var res = scheduler.Start(() => xs.FilterBursts(TimeSpan.FromTicks(10), scheduler)); res.Messages.AssertEqual( OnNext(450, 1), OnNext(650, -1), OnCompleted<int>(700)); xs.Subscriptions.AssertEqual( Subscribe(ReactiveTest.Subscribed, 700)); }
public void CreatColdObservable_ShortWay() { var testScheduler = new TestScheduler(); ITestableObservable <int> coldObservable = testScheduler.CreateColdObservable <int>( // Inheritting your test class from ReactiveTest opens the following // factory methods that make your code much more fluent OnNext(20, 1), OnNext(40, 2), OnNext(60, 2), OnCompleted <int>(900) ); // Creating an observer that captures the emission it recieves var testableObserver = testScheduler.CreateObserver <int>(); // Subscribing the observer, but until TestSchduler is started, emissions // are not be emitted coldObservable .Subscribe(testableObserver); // Starting the TestScheduler means that only now emissions that were configured // will be emitted testScheduler.Start(); // Asserting that every emitted value was recieved by the observer at the // same time it was emitted coldObservable.Messages .AssertEqual(testableObserver.Messages); // Asserting that the observer was subscribed at Scheduler inital time coldObservable.Subscriptions.AssertEqual( Subscribe(0)); }
public void CreatColdObservable_ShortWay() { var testScheduler = new TestScheduler(); ITestableObservable<int> coldObservable = testScheduler.CreateColdObservable<int>( // Inheritting your test class from ReactiveTest opens the following // factory methods that make your code much more fluent OnNext(20, 1), OnNext(40, 2), OnNext(60, 2), OnCompleted<int>(900) ); // Creating an observer that captures the emission it recieves var testableObserver = testScheduler.CreateObserver<int>(); // Subscribing the observer, but until TestSchduler is started, emissions // are not be emitted coldObservable .Subscribe(testableObserver); // Starting the TestScheduler means that only now emissions that were configured // will be emitted testScheduler.Start(); // Asserting that every emitted value was recieved by the observer at the // same time it was emitted coldObservable.Messages .AssertEqual(testableObserver.Messages); // Asserting that the observer was subscribed at Scheduler inital time coldObservable.Subscriptions.AssertEqual( Subscribe(0)); }
public void TakeUntil_Predicate_Error() { var scheduler = new TestScheduler(); var ex = new InvalidOperationException(); var source = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnNext(30, 3), OnError <int>(40, ex) ); var result = scheduler.Start(() => source.TakeUntil(v => false)); result.Messages.AssertEqual( OnNext(210, 1), OnNext(220, 2), OnNext(230, 3), OnError <int>(240, ex) ); source.Subscriptions.AssertEqual( Subscribe(200, 240) ); }
public void Timeout_DateTimeOffset_TimeoutOccurs() { var scheduler = new TestScheduler(); var xs = scheduler.CreateHotObservable( OnNext(410, 1) ); var ys = scheduler.CreateColdObservable( OnNext(100, -1) ); var res = scheduler.Start(() => xs.Timeout(new DateTimeOffset(new DateTime(400), TimeSpan.Zero), ys, scheduler) ); res.Messages.AssertEqual( OnNext(500, -1) ); xs.Subscriptions.AssertEqual( Subscribe(200, 400) ); ys.Subscriptions.AssertEqual( Subscribe(400, 1000) ); }
public void RetryWhen_Observable_Handler_Completes() { var scheduler = new TestScheduler(); var ex = new Exception(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnNext(200, 3), OnError <int>(250, ex) ); var res = scheduler.Start(() => xs.RetryWhen(v => v.Take(1).Skip(1)) ); res.Messages.AssertEqual( OnNext(300, 1), OnNext(350, 2), OnNext(400, 3), OnCompleted <int>(450) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450) ); }
public void ObservableToTaskException() { var scheduler = new TestScheduler(); var ex = new InvalidOperationException(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnError <int>(200, ex) ); var continuation = xs.ToTask(); scheduler.Start(); Assert.IsTrue(continuation.IsFaulted); var ag = continuation.Exception; Assert.IsNotNull(ag); Assert.AreEqual(1, ag.InnerExceptions.Count); Assert.AreEqual(ex, ag.InnerExceptions[0]); xs.Subscriptions.AssertEqual( Subscribe(0, 200) ); }
public void FilterBurstsInColdObservable() { var scheduler = new TestScheduler(); // A cold observable will begin emitting when the observer subscribes // in this case, each emission defined for the observable will be realtive to the observer subscription time // which by default is 200 (defined in ReactiveTest.Subscribed) var xs = scheduler.CreateColdObservable( OnNext(250, 1), OnNext(258, 2), OnNext(262, 3), OnNext(450, -1), OnNext(451, -2), OnNext(460, -3), OnCompleted <int>(500) ); var res = scheduler.Start(() => xs.FilterBursts(TimeSpan.FromTicks(10), scheduler)); res.Messages.AssertEqual( OnNext(450, 1), OnNext(650, -1), OnCompleted <int>(700)); xs.Subscriptions.AssertEqual( Subscribe(ReactiveTest.Subscribed, 700)); }
public void Retry_Observable_RetryCount_Completed() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnNext(200, 3), OnCompleted <int>(250) ); var res = scheduler.Start(() => xs.Retry(3) ); res.Messages.AssertEqual( OnNext(300, 1), OnNext(350, 2), OnNext(400, 3), OnCompleted <int>(450) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450) ); }
public void Retry_Observable_RetryCount_Dispose() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(5, 1), OnNext(10, 2), OnNext(15, 3), OnError <int>(20, new Exception()) ); var res = scheduler.Start(() => xs.Retry(3), 231 ); res.Messages.AssertEqual( OnNext(205, 1), OnNext(210, 2), OnNext(215, 3), OnNext(225, 1), OnNext(230, 2) ); xs.Subscriptions.AssertEqual( Subscribe(200, 220), Subscribe(220, 231) ); }
public void Expand_Error() { var scheduler = new TestScheduler(); var ex = new Exception(); var xs = scheduler.CreateHotObservable( OnError <int>(300, ex) ); var res = scheduler.Start(() => xs.Expand(x => scheduler.CreateColdObservable <int>( OnNext(100 + x, 2 * x), OnNext(200 + x, 3 * x), OnCompleted <int>(300 + x) ), scheduler) ); res.Messages.AssertEqual( OnError <int>(300, ex) ); xs.Subscriptions.AssertEqual( Subscribe(201, 300) ); }
public void DownloadFile_DownloadRemovedOnFailure() { TestScheduler testScheduler = new TestScheduler(); ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnError <double>(new TestException())), new Recorded <Notification <double> >(2, Notification.CreateOnError <double>(new TestException())), new Recorded <Notification <double> >(3, Notification.CreateOnError <double>(new TestException()))); IStorage storage = Substitute.For <IStorage>(); IHttpService httpService = Substitute.For <IHttpService>(); httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>()) .Returns(testObservable); var downloadManager = new Managers.DownloadManager(httpService, storage); downloadManager.MaxRetryCount = 3; string url = "http://www.someplace.com/file.bin"; var download = downloadManager.DownloadFile(url); testScheduler.AdvanceBy(1); testScheduler.AdvanceBy(1); testScheduler.AdvanceBy(1); var queueDownload = downloadManager.DownloadQueue.FirstOrDefault(); Assert.IsNull(queueDownload); }
public void Timeout_TimeoutOccurs_Completed() { var scheduler = new TestScheduler(); var xs = scheduler.CreateHotObservable( OnCompleted <int>(500) ); var ys = scheduler.CreateColdObservable( OnNext(100, -1) ); var res = scheduler.Start(() => xs.Timeout(TimeSpan.FromTicks(100), ys, scheduler) ); res.Messages.AssertEqual( OnNext(400, -1) ); xs.Subscriptions.AssertEqual( Subscribe(200, 300) ); ys.Subscriptions.AssertEqual( Subscribe(300, 1000) ); }
public void RetryWhen_Observable_Handler_Errors() { var scheduler = new TestScheduler(); var ex = new Exception(); var ex2 = new Exception(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnNext(200, 3), OnError <int>(250, ex) ); var res = scheduler.Start(() => xs.RetryWhen(v => v.SelectMany(w => Observable.Throw <int>(ex2))) ); res.Messages.AssertEqual( OnNext(300, 1), OnNext(350, 2), OnNext(400, 3), OnError <int>(450, ex2) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450) ); }
public void Sut_ShouldReturnCorrectValue( [Frozen]Mock<ISettingsService> settingsService, ConfigurationObservable sut, TestScheduler scheduler, MusicMirrorConfiguration expected) { //arrange settingsService.Setup(s => s.ObserveValue(ConfigurationObservable.SourcePathKey, It.IsAny<Func<string>>())) .Returns(scheduler.CreateColdObservable(OnNext(0, expected.SourcePath.FullName))); settingsService.Setup(s => s.ObserveValue(ConfigurationObservable.TargetPathKey, It.IsAny<Func<string>>())) .Returns(scheduler.CreateColdObservable(OnNext(0, expected.TargetPath.FullName))); //act var result = scheduler.Start(() => sut); //assert result.Values().First().Should().Be(expected); }
public void ExampleCreateColdObservable() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( new Recorded <Notification <long> >(10000000, Notification.CreateOnNext(0L)), new Recorded <Notification <long> >(20000000, Notification.CreateOnNext(1L)), new Recorded <Notification <long> >(30000000, Notification.CreateOnNext(2L)), new Recorded <Notification <long> >(40000000, Notification.CreateOnNext(3L)), new Recorded <Notification <long> >(40000000, Notification.CreateOnCompleted <long>()) ); var testObserver = scheduler.Start( () => source, 0, 0, TimeSpan.FromSeconds(5).Ticks); Console.WriteLine("Time is {0} ticks", scheduler.Clock); Console.WriteLine("Received {0} notifications", testObserver.Messages.Count); foreach (Recorded <Notification <long> > message in testObserver.Messages) { Console.WriteLine(" {0} @ {1}", message.Value, message.Time); } //Time is 50000000 ticks //Received 5 notifications //OnNext(0) @ 10000001 //OnNext(1) @ 20000001 //OnNext(2) @ 30000001 //OnNext(3) @ 40000001 //OnCompleted() @ 40000001 }
public void TakeUntil_Predicate_Crash() { var scheduler = new TestScheduler(); var ex = new InvalidOperationException(); var source = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnNext(30, 3), OnNext(240, 4), OnNext(250, 5), OnCompleted <int>(260) ); var result = scheduler.Start(() => source.TakeUntil(v => { if (v == 3) { throw ex; } return(false); })); result.Messages.AssertEqual( OnNext(210, 1), OnNext(220, 2), OnNext(230, 3), OnError <int>(230, ex) ); source.Subscriptions.AssertEqual( Subscribe(200, 230) ); }
public void RepeatWhen_Basic() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnNext(200, 3), OnCompleted <int>(250) ); var res = scheduler.Start(() => xs.RepeatWhen(v => v) ); res.Messages.AssertEqual( OnNext(300, 1), OnNext(350, 2), OnNext(400, 3), OnNext(550, 1), OnNext(600, 2), OnNext(650, 3), OnNext(800, 1), OnNext(850, 2), OnNext(900, 3) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450), Subscribe(450, 700), Subscribe(700, 950), Subscribe(950, 1000) ); }
public async Task AreReloadedWhenANewWorkspaceIdIsObserved() { var initialWorkspaceId = 10L; var newlySelectedWorkspaceId = 11L; var observable = TestScheduler.CreateColdObservable( OnNext(0, initialWorkspaceId), OnNext(1, newlySelectedWorkspaceId) ); var workspace10 = Observable.Return(new MockWorkspace(initialWorkspaceId)); var workspace11 = Observable.Return(new MockWorkspace(newlySelectedWorkspaceId)); InteractorFactory.GetWorkspaceById(initialWorkspaceId).Execute().Returns(workspace10); InteractorFactory.GetWorkspaceById(newlySelectedWorkspaceId).Execute().Returns(workspace11); InteractorFactory.ObserveDefaultWorkspaceId().Execute().Returns(observable); TimeService.CurrentDateTime.Returns(DateTimeOffset.Now); await ViewModel.Initialize(); ViewModel.ViewAppeared(); TestScheduler.AdvanceTo(100); TestScheduler.Start(); InteractorFactory.Received(1).GetProjectSummary( initialWorkspaceId, Arg.Any <DateTimeOffset>(), Arg.Any <DateTimeOffset>()); InteractorFactory.Received(1).GetProjectSummary( newlySelectedWorkspaceId, Arg.Any <DateTimeOffset>(), Arg.Any <DateTimeOffset>()); }
public void RepeatWhen_RepeatCount_Dispose() { var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(5, 1), OnNext(10, 2), OnNext(15, 3), OnCompleted <int>(20) ); var res = scheduler.Start(() => xs.RepeatWhen(v => { var count = 0; return(v.TakeWhile(w => ++ count < 3)); }), 231 ); res.Messages.AssertEqual( OnNext(205, 1), OnNext(210, 2), OnNext(215, 3), OnNext(225, 1), OnNext(230, 2) ); xs.Subscriptions.AssertEqual( Subscribe(200, 220), Subscribe(220, 231) ); }
public void CreatColdObservable_LongWay() { var testScheduler = new TestScheduler(); ITestableObservable <int> coldObservable = testScheduler.CreateColdObservable <int>( // This is the long way to configure emissions. see below for a shorter one new Recorded <Notification <int> >(20, Notification.CreateOnNext <int>(1)), new Recorded <Notification <int> >(40, Notification.CreateOnNext <int>(2)), new Recorded <Notification <int> >(60, Notification.CreateOnCompleted <int>()) ); // Creating an observer that captures the emission it recieves var testableObserver = testScheduler.CreateObserver <int>(); // Subscribing the observer, but until TestSchduler is started, emissions // are not be emitted coldObservable .Subscribe(testableObserver); // Starting the TestScheduler means that only now emissions that were configured // will be emitted testScheduler.Start(); // Asserting that every emitted value was recieved by the observer at the // same time it was emitted coldObservable.Messages .AssertEqual(testableObserver.Messages); // Asserting that the observer was subscribed at Scheduler inital time coldObservable.Subscriptions.AssertEqual( Subscribe(0)); }
public void RepeatWhen_RepeatCount_Error() { var scheduler = new TestScheduler(); var ex = new Exception(); var xs = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(150, 2), OnNext(200, 3), OnError <int>(250, ex) ); var res = scheduler.Start(() => xs.RepeatWhen(v => { var count = 0; return(v.TakeWhile(w => ++ count < 3)); }) ); res.Messages.AssertEqual( OnNext(300, 1), OnNext(350, 2), OnNext(400, 3), OnError <int>(450, ex) ); xs.Subscriptions.AssertEqual( Subscribe(200, 450) ); }
public void values_can_be_triggered_by_time_or_count() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( // start #1 (@0) new Recorded<Notification<int>>(10, OnNext(1)), new Recorded<Notification<int>>(12, OnNext(2)), new Recorded<Notification<int>>(14, OnNext(3)), // start #2 (@ 15) new Recorded<Notification<int>>(16, OnNext(4)), new Recorded<Notification<int>>(20, OnNext(5)), new Recorded<Notification<int>>(24, OnNext(6)), new Recorded<Notification<int>>(28, OnNext(7)), // start #3 (@ 28) // start #4 (@ 43) new Recorded<Notification<int>>(50, OnNext(8)), new Recorded<Notification<int>>(51, OnCompleted()) ); var stats = new StatsObserver<IList<int>>(); source.BufferWithTimeOrCount(new TimeSpan(15), 4, scheduler) .Subscribe(stats); scheduler.Run(); Assert.AreEqual(4, stats.NextCount); Assert.AreEqual(3, stats.NextValues[0].Count); Assert.AreEqual(4, stats.NextValues[1].Count); Assert.AreEqual(0, stats.NextValues[2].Count); Assert.AreEqual(1, stats.NextValues[3].Count); }
public void MessageTimeoutIsStartedAgainIfMessageIsUpdated() { var expected = messageA.Merge(new Message(A)); var messageSequence = testScheduler.CreateColdObservable( ReactiveTest.OnNext(MessageAOnNextTick, messageA), ReactiveTest.OnNext(3.Seconds(), expected) ); var sut = CreateKeysController(messageSequence); sut.Start(); testScheduler.AdvanceBy(6.Seconds()); messages.Single().IsDeleting.ShouldBe(false); messages.Single().ShouldBe(expected); }
public void DelaySubscription_TimeSpan_Error() { var ex = new Exception(); var scheduler = new TestScheduler(); var xs = scheduler.CreateColdObservable( OnNext(50, 42), OnNext(60, 43), OnError <int>(70, ex) ); var res = scheduler.Start(() => xs.DelaySubscription(TimeSpan.FromTicks(30), scheduler) ); res.Messages.AssertEqual( OnNext(280, 42), OnNext(290, 43), OnError <int>(300, ex) ); xs.Subscriptions.AssertEqual( Subscribe(230, 300) ); }
public async Task WorksIfProviderHasMultipleSuggestions() { var scheduler = new TestScheduler(); var provider = Substitute.For <ISuggestionProvider>(); var timeEntries = new[] { new TimeEntry { Description = "te1" }, new TimeEntry { Description = "te2" }, new TimeEntry { Description = "te3" } }; var observableContent = timeEntries .Select(te => new Recorded <Notification <ITimeEntry> >(1, Notification.CreateOnNext <ITimeEntry>(te))) .ToArray(); var observable = scheduler .CreateColdObservable(observableContent); provider.GetSuggestion().Returns(observable); var container = new SuggestionProviderContainer(provider); var viewmodel = new SuggestionsViewModel(container); await viewmodel.Initialize(); scheduler.AdvanceTo(1); viewmodel.Suggestions.Should().HaveCount(timeEntries.Length) .And.Contain(timeEntries); }
public void T03_Concurrent() { var testScheduler = new TestScheduler(); var observer1 = testScheduler.CreateObserver <string>(); var observer2 = testScheduler.CreateObserver <string>(); var observable = testScheduler.CreateColdObservable( OnNext(100, "1"), OnNext(200, "2"), OnNext(300, "3"), OnNext(400, "4"), OnNext(500, "5"), OnCompleted <string>(600)); var sharedObservable = observable.ToShareSource(); sharedObservable.SubscribeOn(testScheduler).Subscribe(observer1); sharedObservable.SubscribeOn(testScheduler).Subscribe(observer2); testScheduler.Start(); var expected = new[] { OnNext(101, "1"), OnNext(201, "2"), OnNext(301, "3"), OnNext(401, "4"), OnNext(501, "5"), OnCompleted <string>(601) }; Assert.Equal(expected.ToList(), observer1.Messages); Assert.Equal(expected.ToList(), observer2.Messages); Assert.Equal(1, observable.Subscriptions.Count); }
public void Should_Unsubscribe_From_Activator_When_All_Subscriptions_Disposed() { var scheduler = new TestScheduler(); var activator1 = scheduler.CreateColdObservable<bool>(); var activator2 = scheduler.CreateColdObservable<bool>(); var activator = StyleActivator.And(new[] { activator1, activator2 }); var target = new ActivatedValue(activator, 1, string.Empty); var subscription = target.Subscribe(_ => { }); Assert.Equal(1, activator1.Subscriptions.Count); Assert.Equal(Subscription.Infinite, activator1.Subscriptions[0].Unsubscribe); subscription.Dispose(); Assert.Equal(1, activator1.Subscriptions.Count); Assert.Equal(0, activator1.Subscriptions[0].Unsubscribe); }
public void Timeout_TimeoutOccurs_Never() { var scheduler = new TestScheduler(); var xs = scheduler.CreateHotObservable( OnNext(70, 1), OnNext(130, 2), OnNext(240, 3), OnNext(310, 4), OnNext(430, 5), OnCompleted <int>(500) ); var ys = scheduler.CreateColdObservable <int>( ); var res = scheduler.Start(() => xs.Timeout(TimeSpan.FromTicks(100), ys, scheduler) ); res.Messages.AssertEqual( OnNext(240, 3), OnNext(310, 4) ); xs.Subscriptions.AssertEqual( Subscribe(200, 410) ); ys.Subscriptions.AssertEqual( Subscribe(410, 1000) ); }
public void SameAsRetry_OnError() { var scheduler = new TestScheduler(); var ex = new Exception(); var source = scheduler.CreateColdObservable <int>( OnNext(0, 1), OnNext(0, 2), OnError <int>(0, ex)); var called = 0; var retryRecorder = scheduler.CreateObserver <int>(); source.Retry().Take(5).Subscribe(retryRecorder); var onErrorRecorder = scheduler.CreateObserver <int>(); source.OnErrorRetry((Exception e) => { ++called; }).Take(5).Subscribe(onErrorRecorder); scheduler.Start(); retryRecorder.Messages.Is( OnNext(1, 1), OnNext(1, 2), OnNext(2, 1), OnNext(2, 2), OnNext(3, 1), OnCompleted <int>(3)); var retryResult = retryRecorder.Messages.ToArray(); onErrorRecorder.Messages.Is(retryRecorder.Messages); called.Is(2); }
public void Timeout_TimeoutNotOccurs_Error() { var scheduler = new TestScheduler(); var ex = new Exception(); var xs = scheduler.CreateHotObservable( OnError <int>(250, ex) ); var ys = scheduler.CreateColdObservable( OnNext(100, -1) ); var res = scheduler.Start(() => xs.Timeout(TimeSpan.FromTicks(100), ys, scheduler) ); res.Messages.AssertEqual( OnError <int>(250, ex) ); xs.Subscriptions.AssertEqual( Subscribe(200, 250) ); ys.Subscriptions.AssertEqual( ); }
public void TakeUntil_Predicate_True() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( OnNext(10, 1), OnNext(20, 2), OnNext(30, 3), OnNext(40, 4), OnNext(50, 5), OnNext(60, 6), OnNext(70, 7), OnNext(80, 8), OnNext(90, 9), OnCompleted <int>(100) ); var result = scheduler.Start(() => source.TakeUntil(v => true)); result.Messages.AssertEqual( OnNext(210, 1), OnCompleted <int>(210) ); source.Subscriptions.AssertEqual( Subscribe(200, 210) ); }
public void SameAsRetry_OnError() { var scheduler = new TestScheduler(); var ex = new Exception(); var source = scheduler.CreateColdObservable<int>( OnNext(0, 1), OnNext(0, 2), OnError<int>(0, ex)); var called = 0; var retryRecorder = scheduler.CreateObserver<int>(); source.Retry().Take(5).Subscribe(retryRecorder); var onErrorRecorder = scheduler.CreateObserver<int>(); source.OnErrorRetry((Exception e) => { ++called; }).Take(5).Subscribe(onErrorRecorder); scheduler.Start(); retryRecorder.Messages.Is( OnNext(1, 1), OnNext(1, 2), OnNext(2, 1), OnNext(2, 2), OnNext(3, 1), OnCompleted<int>(3)); var retryResult = retryRecorder.Messages.ToArray(); onErrorRecorder.Messages.Is(retryRecorder.Messages); called.Is(2); }
public void UpToMaxEventsAreBuffered() { TimeSpan maxInterval = TimeSpan.FromTicks(200); const int maxBufferSize = 2; var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(200, 2), OnNext(300, 3)); IList <int>[] expectedBuffers = { new[] { 1, 2 }, new[] { 3 } }; var expectedTimes = new[] { 200, /* Buffer cap reached */ maxInterval.Ticks + 300 }; var results = scheduler.CreateObserver <IList <int> >(); source.BufferNearEvents(maxInterval, maxBufferSize, scheduler) .Subscribe(results); scheduler.AdvanceTo(10000); results.Messages.AssertEqual( OnNext <IList <int> >(expectedTimes[0], buffer => CheckBuffer(expectedBuffers[0], buffer)), OnNext <IList <int> >(expectedTimes[1], buffer => CheckBuffer(expectedBuffers[1], buffer))); }
public void CreatColdObservable_LongWay() { var testScheduler = new TestScheduler(); ITestableObservable<int> coldObservable = testScheduler.CreateColdObservable<int>( // This is the long way to configure emissions. see below for a shorter one new Recorded<Notification<int>>(20, Notification.CreateOnNext<int>(1)), new Recorded<Notification<int>>(40, Notification.CreateOnNext<int>(2)), new Recorded<Notification<int>>(60, Notification.CreateOnCompleted<int>()) ); // Creating an observer that captures the emission it recieves var testableObserver = testScheduler.CreateObserver<int>(); // Subscribing the observer, but until TestSchduler is started, emissions // are not be emitted coldObservable .Subscribe(testableObserver); // Starting the TestScheduler means that only now emissions that were configured // will be emitted testScheduler.Start(); // Asserting that every emitted value was recieved by the observer at the // same time it was emitted coldObservable.Messages .AssertEqual(testableObserver.Messages); // Asserting that the observer was subscribed at Scheduler inital time coldObservable.Subscriptions.AssertEqual( Subscribe(0)); }
public void RetryWithDelay_Errors_RetriesForLongerAndLongerPeriodsThenCapsOutAtMaximum() { var initialSubscription = 100; var initialDelay = 50; var delayIncrease = 10; var maxDelay = initialDelay + delayIncrease * 2 + 5; int subscription1 = initialSubscription, subscription2 = subscription1 + initialDelay + 1, subscription3 = subscription2 + initialDelay + delayIncrease + 1, subscription4 = subscription3 + initialDelay + delayIncrease * 2 + 1, subscription5 = subscription4 + maxDelay + 1, subscription6 = subscription5 + maxDelay + 1; var source = _scheduler.CreateColdObservable(OnError <int>(0)); _scheduler.Start(() => source.RetryWithDelay(TimeSpan.FromTicks(initialDelay), TimeSpan.FromTicks(delayIncrease), TimeSpan.FromTicks(maxDelay), _scheduler), 0, initialSubscription, subscription6 + 1); source.Subscriptions.ShouldMatch(new[] { new Subscription(subscription1, subscription1 + 1), new Subscription(subscription2, subscription2 + 1), new Subscription(subscription3, subscription3 + 1), new Subscription(subscription4, subscription4 + 1), new Subscription(subscription5, subscription5 + 1), new Subscription(subscription6, subscription6 + 1) }); }
public void values_are_buffered_in_specified_time() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( // start #1 (@0) new Recorded<Notification<int>>(10, OnNext(1)), new Recorded<Notification<int>>(12, OnNext(2)), // start #2 (@ 15) // start #3 (@ 30) new Recorded<Notification<int>>(40, OnNext(3)), // start #4 (@ 45) new Recorded<Notification<int>>(50, OnNext(4)), new Recorded<Notification<int>>(51, OnCompleted()) ); var stats = new StatsObserver<IList<int>>(); source.BufferWithTime(new TimeSpan(15), scheduler) .Subscribe(stats); scheduler.Run(); Assert.AreEqual(4, stats.NextCount); Assert.AreEqual(2, stats.NextValues[0].Count); Assert.AreEqual(0, stats.NextValues[1].Count); Assert.AreEqual(1, stats.NextValues[2].Count); Assert.AreEqual(1, stats.NextValues[3].Count); }
public void SelectDisposableShouldWork() { var scheduler = new TestScheduler(); var disposables = new List<BooleanDisposable>(); var list = new CompositeDisposable(); scheduler.CreateColdObservable( new Recorded<Notification<long>>(100, Notification.CreateOnNext(0L)), new Recorded<Notification<long>>(200, Notification.CreateOnNext(1L)), new Recorded<Notification<long>>(300, Notification.CreateOnNext(2L)), new Recorded<Notification<long>>(400, Notification.CreateOnNext(3L)), new Recorded<Notification<long>>(400, Notification.CreateOnCompleted<long>()) ) .SelectDisposable(list, i => { var d = new BooleanDisposable(); disposables.Add(d); return d; }, (i, _) => i) .Subscribe() .DisposeWith(list); scheduler.AdvanceTo(300); disposables.Count.Should().Be(3); disposables.Select(d => d.IsDisposed).Should().NotContain(true); list.Dispose(); disposables.Select(d => d.IsDisposed).Should().NotContain(false); }
private static IObservable<int> IntervalZeroToTwo(TestScheduler scheduler) { return scheduler.CreateColdObservable( OnNext(100, 0), OnNext(200, 1), OnNext(300, 2), OnCompleted<int>(300)); }
private static IObservable<int> RangeOneToThree(TestScheduler scheduler) { return scheduler.CreateColdObservable( OnNext(100, 1), OnNext(100, 2), OnNext(100, 3), OnCompleted<int>(100)); }
public void when_user_login_fails_too_fast_then_locks_account () { var seconds = TimeSpan.FromSeconds(1).Ticks; var events = new EventStream(); // Here we use the test scheduler to simulate time passing by // because we have a dependency on time because of the Buffer // method. var scheduler = new TestScheduler(); var observable = scheduler.CreateColdObservable( // Two users attempt to log in, 4 times in a row new Recorded<Notification<LoginFailure>>(10 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 1 })), new Recorded<Notification<LoginFailure>>(10 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 2 })), new Recorded<Notification<LoginFailure>>(20 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 1 })), new Recorded<Notification<LoginFailure>>(20 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 2 })), new Recorded<Notification<LoginFailure>>(30 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 1 })), new Recorded<Notification<LoginFailure>>(30 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 2 })), new Recorded<Notification<LoginFailure>>(40 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 1 })), new Recorded<Notification<LoginFailure>>(40 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 2 })), // User 2 attempts one more time within the 1' window new Recorded<Notification<LoginFailure>>(45 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 2 })), // User 1 pulls out the paper where he wrote his pwd ;), so he takes longer new Recorded<Notification<LoginFailure>>(75 * seconds, Notification.CreateOnNext(new LoginFailure { UserId = 1 })) ); // This subscription bridges the scheduler-driven // observable with our event stream, causing us // to publish events as they are "raised" by the // test scheduler. observable.Subscribe (failure => events.Push (failure)); var query = events.Of<LoginFailure>() // Sliding windows 1' long, every 10'' .Buffer(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10), scheduler) // From all failure values .SelectMany(failures => failures // Group the failures by user .GroupBy(failure => failure.UserId) // Only grab those failures with more than 5 in the 1' window .Where(group => group.Count() >= 5) // Return the user id that failed to log in .Select(group => group.Key)); var blocked = new List<int>(); using (var subscription = query.Subscribe (userId => blocked.Add (userId))) { // Here we could advance the scheduler half way and test intermediate // state if needed. We go all the way past the end of our login failures. scheduler.AdvanceTo (100 * seconds); } // We should have only user # 2 in the list. Assert.False (blocked.Contains (1)); Assert.True (blocked.Contains (2)); }
public void Activator_Should_Subscribe_To_Inputs_On_First_Subscription() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable<bool>(); var target = StyleActivator.And(new[] { source }); Assert.Equal(0, source.Subscriptions.Count); target.Subscribe(_ => { }); Assert.Equal(1, source.Subscriptions.Count); }
public void Delta_WithNoValue_ShouldReturnCorrectValue( TestScheduler scheduler) { //arrange var source = scheduler.CreateColdObservable( OnNext(0, 0)); //act var actual = scheduler.Start(() => source.Delta((previous, current) => previous - current)); //assert actual.Values().Should().BeEmpty(); }
public void recursively_expands_values_and_merges() { var scheduler = new TestScheduler(); var observer = new MockObserver<int>(scheduler); scheduler.CreateColdObservable( OnNext(5, 0), OnNext(10, 1), OnNext(15, 2), OnCompleted(15) ) .Expand(i => (i < 300) ? scheduler.CreateColdObservable( OnNext(5, i + 100), OnCompleted(5)) : Observable.Empty<int>() ) .Subscribe(observer); scheduler.Run(); observer.AssertEqual( OnNext(5, 0), OnNext(10, 1), OnNext(10, 100), OnNext(15, 2), OnNext(15, 101), OnNext(15, 200), OnNext(20, 102), OnNext(20, 201), OnNext(20, 300), OnNext(25, 202), OnNext(25, 301), OnNext(30, 302), OnCompleted(30) ); }
public void Activator_Should_Unsubscribe_From_Inputs_After_Last_Subscriber_Completes() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable<bool>(); var target = StyleActivator.And(new[] { source }); var dispose = target.Subscribe(_ => { }); Assert.Equal(1, source.Subscriptions.Count); Assert.Equal(Subscription.Infinite, source.Subscriptions[0].Unsubscribe); dispose.Dispose(); Assert.Equal(1, source.Subscriptions.Count); Assert.Equal(0, source.Subscriptions[0].Unsubscribe); }
public void CallOnErrorWhenAnExceptionIsEncountered() { TestScheduler scheduler = new TestScheduler(); IObservable<int> observable = scheduler.CreateColdObservable<int>(new [] { new Recorded<Notification<int>>(10, Notification.CreateOnError<int>(new InvalidOperationException())) }); IObserver<Exception> errors = A.Fake<IObserver<Exception>>(); observable.Retry(errors).Subscribe(); scheduler.AdvanceBy(10); A.CallTo(() => errors.OnNext(A<Exception>.Ignored)).MustHaveHappened(Repeated.Exactly.Once); }
public void Should_Unsubscribe_From_Update_Observable() { var scheduler = new TestScheduler(); var update = scheduler.CreateColdObservable<Unit>(); var target = new ExpressionObserver(() => new { Foo = "foo" }, "Foo", update); var result = new List<object>(); using (target.Subscribe(x => result.Add(x))) using (target.Subscribe(_ => { })) { scheduler.Start(); } Assert.Equal(new[] { "foo" }, result); Assert.All(update.Subscriptions, x => Assert.NotEqual(Subscription.Infinite, x.Unsubscribe)); }
public void Should_Unsubscribe_From_Source_Observable() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( OnNext(1, new { Foo = "foo" })); var target = new ExpressionObserver(source, "Foo"); var result = new List<object>(); using (target.Subscribe(x => result.Add(x))) using (target.Subscribe(_ => { })) { scheduler.Start(); } Assert.Equal(new[] { AvaloniaProperty.UnsetValue, "foo" }, result); Assert.All(source.Subscriptions, x => Assert.NotEqual(Subscription.Infinite, x.Unsubscribe)); }
public void RetryCountAndDelay() { var scheduler = new TestScheduler(); var ex = new Exception(); var source = scheduler.CreateColdObservable<int>( OnNext(0, 1), OnNext(0, 2), OnError<int>(0, ex)); var called = 0; var retryRecorder = scheduler.CreateObserver<int>(); source.Retry(3).Subscribe(retryRecorder); var onErrorRecorder = scheduler.CreateObserver<int>(); source.OnErrorRetry((Exception e) => { ++called; }, 3, TimeSpan.FromSeconds(3), scheduler) .Subscribe(onErrorRecorder); scheduler.Start(); retryRecorder.Messages.Is( OnNext(1, 1), OnNext(1, 2), // error OnNext(2, 1), OnNext(2, 2), // error OnNext(3, 1), OnNext(3, 2), OnError<int>(3, ex)); onErrorRecorder.Messages.Is( OnNext(1, 1), OnNext(1, 2), // error OnNext(TimeSpan.FromSeconds(3).Ticks + 2, 1), OnNext(TimeSpan.FromSeconds(3).Ticks + 2, 2), // error OnNext(TimeSpan.FromSeconds(6).Ticks + 3, 1), OnNext(TimeSpan.FromSeconds(6).Ticks + 3, 2), OnError<int>(TimeSpan.FromSeconds(6).Ticks + 3, ex)); called.Is(3); }
public void Delta_ShouldReturnCorrectValue( TestScheduler scheduler) { //arrange var source = scheduler.CreateColdObservable( OnNext(0, 0), OnNext(1, 10), OnNext(2, 8), OnNext(3, 16), OnNext(4, 20), OnNext(5, 10)); //act var actual = scheduler.Start(() => source.Delta((previous, current) => previous - current)); //assert var expected = new[] {-10, 2, -8, -4, 10}; actual.Values().ShouldAllBeEquivalentTo(expected); }
public void time_shifted_values_make_it_into_the_next_window() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( // start #1 (@0) // start #2 (@10) <-- happens before subscribe so its before the first value new Recorded<Notification<int>>(10, OnNext(1)), new Recorded<Notification<int>>(12, OnNext(2)), // end #1 (@15) // start #3 (@20) // end #2 (@25) // start #4 (@30) // end #3 (@35) new Recorded<Notification<int>>(40, OnNext(3)), // start #5 (@40) // end #4 (@45) new Recorded<Notification<int>>(50, OnNext(4)), // start #6 (@50) new Recorded<Notification<int>>(51, OnCompleted()) ); var stats = new StatsObserver<IList<int>>(); source.BufferWithTime(new TimeSpan(15), new TimeSpan(4), scheduler) .Subscribe(stats); scheduler.Run(); Assert.AreEqual(6, stats.NextCount); Assert.AreEqual(2, stats.NextValues[0].Count); Assert.AreEqual(2, stats.NextValues[1].Count); Assert.AreEqual(0, stats.NextValues[2].Count); Assert.AreEqual(1, stats.NextValues[3].Count); Assert.AreEqual(1, stats.NextValues[4].Count); Assert.AreEqual(0, stats.NextValues[5].Count); Assert.IsTrue(stats.NextValues[0].SequenceEqual(new int[] { 1, 2 })); Assert.IsTrue(stats.NextValues[1].SequenceEqual(new int[] { 1, 2 })); Assert.IsTrue(stats.NextValues[3].SequenceEqual(new int[] { 3 })); Assert.IsTrue(stats.NextValues[4].SequenceEqual(new int[] { 4 })); }
public void ObservableFromPool_ShouldReturnCorrectValue( ViewModelTest viewModel, TestScheduler scheduler) { //arrange var observable = scheduler.CreateColdObservable( OnNext(200, 1), OnNext(201, 2), OnNext(210, 3)); //act var actual = scheduler.Start(() => { var obs = viewModel.ObservableFromPool(() => observable, scheduler, "Test"); return obs; }); //assert actual.Values().ShouldAllBeEquivalentTo(new[] { 1, 2, 3 }); }
public void IsCorrect() { var scheduler = new TestScheduler(); var source = scheduler.CreateColdObservable( OnNext(100, 1), OnNext(200, 2), OnNext(300, 3), OnCompleted<int>(400)); var results = scheduler.CreateObserver<Tuple<int,int>>(); source.CombineWithPreceding(Tuple.Create).Subscribe(results); scheduler.Start(); results.Messages.AssertEqual( OnNext(100, Tuple.Create(1, 0)), OnNext(200, Tuple.Create(2, 1)), OnNext(300, Tuple.Create(3, 2)), OnCompleted<Tuple<int,int>>(400)); }
public void CombineLatest() { var scheduler = new TestScheduler(); var first = scheduler.CreateHotObservable( OnNext(10, 'a'), OnNext(20, 'b'), OnNext(40, 'c'), OnCompleted<char>(70)); var second = scheduler.CreateHotObservable( OnNext(30, 'd'), OnNext(60, 'e'), OnNext(100, 'f'), OnCompleted<char>(110)); var third = scheduler.CreateHotObservable( OnNext(50, 'g'), OnNext(80, 'h'), OnCompleted<char>(90)); var input = scheduler.CreateColdObservable( OnNext(0, first), OnNext(25, second), OnNext(45, third), OnCompleted<ITestableObservable<char>>(55)); var result = scheduler.Start(() => input.CombineLatest(), 0, 0, 200).Messages; result.AssertEqual( OnNext<IList<char>>(10, new[] { 'a' }.SequenceEqual), OnNext<IList<char>>(20, new[] { 'b' }.SequenceEqual), OnNext<IList<char>>(30, new[] { 'b', 'd' }.SequenceEqual), OnNext<IList<char>>(40, new[] { 'c', 'd' }.SequenceEqual), OnNext<IList<char>>(50, new[] { 'c', 'd', 'g' }.SequenceEqual), OnNext<IList<char>>(60, new[] { 'c', 'e', 'g' }.SequenceEqual), OnNext<IList<char>>(80, new[] { 'c', 'e', 'h' }.SequenceEqual), OnNext<IList<char>>(100, new[] { 'c', 'f', 'h' }.SequenceEqual), OnCompleted<IList<char>>(110)); }
public void When_Source_Observable_Completes_Then_Completes() { // Arrange var scheduler = new TestScheduler(); var sourceObs = scheduler.CreateColdObservable( OnCompleted<IPropertyValueChanged<TestObservableObject>>(40) ); var observer = scheduler.CreateObserver<IPropertyChanged<TestObservableObject>>(); var expected = new[] { OnCompleted<IPropertyChanged<TestObservableObject>>(40) }; var sut = new NotifyPropertyChangedObservable<TestObservableObject>(sourceObs); // Act sut.Subscribe(observer); scheduler.Start(); // Assert Assert.AreEqual(expected, observer.Messages); }
public void When_Errored_Observable_ToRex_Then_Projects_Error_Into_Rex() { // Arrange var scheduler = new TestScheduler(); var expectedException = new Exception(); var observable = scheduler.CreateColdObservable( OnNext(10, 10), OnError<int>(30, expectedException) ); var observer = scheduler.CreateObserver<Rex<int>>(); // Act observable.ToRex().Subscribe(observer); scheduler.Start(); // Assert observer.Messages.AssertEqual( OnNext(10, Rex.Result(10)), OnNext(30, Rex.Error<int>(expectedException)), OnCompleted<Rex<int>>(30) ); }
public void When_Source_Observable_Errors_Then_Errors() { // Arrange var scheduler = new TestScheduler(); var expectedException = new Exception("Expected"); var sourceObs = scheduler.CreateColdObservable( OnError<IPropertyValueChanged<TestObservableObject>>(40, expectedException) ); var observer = scheduler.CreateObserver<IPropertyValueChanged<int>>(); var expected = new[] { OnError<IPropertyValueChanged<int>>(40, expectedException) }; var sut = new NotifyPropertyValueChangedObservable<TestObservableObject, int>(sourceObs, "Age", x => x.Age); // Act sut.Subscribe(observer); scheduler.Start(); // Assert Assert.AreEqual(expected, observer.Messages); }