public void data_is_saved_immediately_upon_deactivation_even_if_two_seconds_has_not_elapsed_since_last_change() { var scheduler = new TestScheduler(); var api = new ApiMock(); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); sut.Name = "Barney"; scheduler.AdvanceByMs(1); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasNotCalled(); sut .Activator .Deactivate(); scheduler.AdvanceByMs(1); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasCalledExactlyOnce(); }
public void saves_are_throttled_to_two_seconds() { var scheduler = new TestScheduler(); var api = new ApiMock(); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); scheduler.AdvanceByMs(2000); sut.Name = "Barney"; scheduler.AdvanceByMs(500); sut.Name = "Barney the Dinosaur"; scheduler.AdvanceByMs(500); sut.Weight = "42"; scheduler.AdvanceByMs(1999); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasNotCalled(); scheduler.AdvanceByMs(1); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasCalledExactlyOnce(); }
public void CancelButton_IsAvailableUntil_TwoSeconds() { var actual = false; var scheduler = new TestScheduler(); LoginViewModel sut = new LoginViewModelBuilder() .WithScheduler(scheduler) .WithUserName("coolusername") .WithPassword("excellentpassword"); sut.Cancel.CanExecute.Subscribe(x => { actual = x; }); Observable.Return(Unit.Default).InvokeCommand(sut.Login); actual.Should().Be(false); // 50ms scheduler.AdvanceByMs(50); actual.Should().Be(true); // 1sec 50ms scheduler.AdvanceByMs(TimeSpan.FromSeconds(1).TotalMilliseconds); actual.Should().Be(true); // 2sec 50sms scheduler.AdvanceByMs(TimeSpan.FromSeconds(1).TotalMilliseconds); actual.Should().Be(false); }
public void CancelButton_Cancels_Login() { var scheduler = new TestScheduler(); LoginViewModel sut = new LoginViewModelBuilder() .WithScheduler(scheduler) .WithUserName("coolusername") .WithPassword("excellentpassword"); scheduler.AdvanceByMs(TimeSpan.FromSeconds(1).Milliseconds); sut.Login.Subscribe(x => x.Should().Be(true)); Observable .Return(Unit.Default) .InvokeCommand(sut.Login); sut.Cancel.CanExecute.Subscribe(x => x.Should().Be(true)); scheduler.AdvanceByMs(1000); Observable .Return(Unit.Default) .InvokeCommand(sut.Cancel); }
public void busy_flag_remains_true_whilst_saving_data() { var scheduler = new TestScheduler(); var api = new ApiMock(); var saveDinosaur = new Subject <Unit>(); api .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .Return(saveDinosaur); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); sut.Name = "Barney"; sut.Weight = "42"; scheduler.AdvanceByMs(2001); Assert.True(sut.IsBusy); saveDinosaur.OnCompleted(); scheduler.AdvanceByMs(1); Assert.False(sut.IsBusy); }
public void errors_saving_data_are_surfaced() { var scheduler = new TestScheduler(); var api = new ApiMock(); api .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .Return(Observable.Throw <Unit>(new InvalidOperationException("foo"))); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); sut.Name = "Barney"; sut.Weight = "42"; scheduler.AdvanceByMs(2001); Assert.NotNull(sut.Error); Assert.Equal("foo", sut.Error.Exception.Message); api .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .Return(Observable.Return(Unit.Default)); sut .Error .RetryCommand .Execute(null); scheduler.AdvanceByMs(1); Assert.Null(sut.Error); }
public void Should_Resume_After_Stopped() { // Given var testScheduler = new TestScheduler(); var schedulerProvider = new SchedulerProviderFixture().WithTestScheduler(testScheduler); DecrementTimer sut = new DecrementTimerFixture().WithProvider(schedulerProvider); var timer = TimeSpan.Zero; sut.Subscribe(x => { timer = x; }); sut.Start(TimeSpan.FromMinutes(1)); testScheduler.AdvanceByMs(InitialMilliseconds); sut.Stop(); testScheduler.AdvanceByMs(OneThousandMilliseconds); // When sut.Start(); testScheduler.AdvanceByMs(OneThousandMilliseconds); // Then timer.Should().Be(TimeSpan.FromSeconds(58)); }
public void data_is_saved_two_seconds_after_image_data_is_modified() { var scheduler = new TestScheduler(); var api = new ApiMock(); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); scheduler.AdvanceByMs(2000); sut.Weight = "42"; sut.ImageData = new byte[] { 1, 2, 3 }; scheduler.AdvanceByMs(1999); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasNotCalled(); scheduler.AdvanceByMs(1); api .Verify(x => x.SaveDinosaur(It.IsAny <Dinosaur>())) .WasCalledExactlyOnce(); }
public void Track_new() { current.Value = new Mail(); current.Value.IsNew = false; current.Value.IsNew = true; scheduler.AdvanceByMs(10000); Assert.IsTrue(current.Value.IsNew); }
public void ShouldAdvanceNextValueByOne() { var value = (long?)null; _clock.Tick.Subscribe(next => value = next); value.Should().BeNull(); _scheduler.AdvanceByMs(1000); value.Should().Be(0L); _scheduler.AdvanceByMs(1000); value.Should().Be(1L); }
public void test_that_when_threshold_is_exceeded_during_the_buffer_period__video_record_request_is_generated() { const string testStationId = "5677"; const string testStationArea = "Area1"; const string testKMPayLoad = "AAAADDD5677ZZZZ"; //set the date and time for the test DateTime testDateTime = new DateTime(2020, 1, 1, 1, 0, 0); _mockLogger = new Mock <ILogger <PIRDetectionSink> >(); _mockConfig = new Mock <IOptions <PIRDetectionSinkConfig> >(); _mockStationConfig = new Mock <IOptions <StationConfig> >(); _mockTimeProvider = new Mock <ITimeProvider>(); _mockInfluxClient = new Mock <IInfluxClient>(); _mockStationConfig.Setup(x => x.Value).Returns(new StationConfig() { Stations = new[] { new Station() { Id = testStationId, Description = testStationArea, Enabled = true } } }); _mockTimeProvider.Setup(x => x.GetCurrentTimeUtc()).Returns(testDateTime); var testScheduler = new TestScheduler(); var kmStream = testScheduler.CreateColdObservable <KafkaMessage>( testScheduler.OnNextAt(2000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(2000))), testScheduler.OnNextAt(3000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(3000))), testScheduler.OnNextAt(4000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(4000))), testScheduler.OnNextAt(7000, GenerateTestKM(testKMPayLoad, testDateTime.AddMilliseconds(7000))) ); //setup _mockConfig.Setup(x => x.Value).Returns(new PIRDetectionSinkConfig { Enabled = true }); var filter = new PIRDetectionSink(_mockLogger.Object, _mockConfig.Object, _mockStationConfig.Object, _mockTimeProvider.Object, _mockInfluxClient.Object, testScheduler); filter.Observe(kmStream); testScheduler.AdvanceByMs(10500); testScheduler.AdvanceByMs(1000); _mockLogger.VerifyLogInfo($"Activity detected in station {testStationId}"); _mockInfluxClient.Verify(x => x.WritePirDetectEvent(It.IsAny <string>(), testStationId, testStationArea, It.IsAny <DateTime>()), Times.AtLeastOnce); }
public void Connectivity_Should_Connect_And_Disconnect() { // Given NetworkAccess access = NetworkAccess.None; var testScheduler = new TestScheduler(); MockConnectivityEssential connectivity = new MockConnectivityEssentialFixture() .WithScheduler(testScheduler) .WithAccesses(NetworkAccesses); connectivity .ConnectivityChanged .Subscribe(_ => { access = _.NetworkAccess; }); // When testScheduler.AdvanceToMs(TimeSpan.FromSeconds(2.5).TotalMilliseconds); // Then access.Should().Be(NetworkAccess.Internet); // When testScheduler.AdvanceByMs(TimeSpan.FromSeconds(2).TotalMilliseconds); // Then access.Should().Be(NetworkAccess.Unknown); }
public void retrieved_data_is_used_to_populate_the_initial_values() { var scheduler = new TestScheduler(); var dinosaur = new Dinosaur("Barney", 13, new byte[] { 1, 2, 3 }); var api = new ApiMock(); api .When(x => x.GetDinosaur(42)) .Return(Observable.Return(dinosaur)); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), api, scheduler); sut .Activator .Activate(); scheduler.AdvanceByMs(1); Assert.Equal("Barney", sut.Name); Assert.Equal("13", sut.Weight); Assert.Equal(13, sut.ValidatedWeight.Value); Assert.NotNull(sut.Image); }
public void valid_weights_are_accepted() { var scheduler = new TestScheduler(); var sut = new DinosaurDetailsViewModel( 42, new BitmapLoaderMock(), new ApiMock(), scheduler); sut .Activator .Activate(); scheduler.AdvanceByMs(1); sut.Weight = "42"; scheduler.AdvanceByMs(1); Assert.True(sut.ValidatedWeight.IsValid); Assert.Equal(42, sut.ValidatedWeight.Value); }
public void Should_Advance_When_Started() { // Given var testScheduler = new TestScheduler(); var schedulerProvider = new SchedulerProviderFixture().WithTestScheduler(testScheduler); IncrementTimer sut = new IncrementTimerFixture().WithProvider(schedulerProvider); var timer = TimeSpan.Zero; sut.Subscribe(x => { timer = x; }); // When sut.Start(); testScheduler.AdvanceByMs(InitialMilliseconds); testScheduler.AdvanceByMs(OneThousandMilliseconds); // Then timer.Should().Be(TimeSpan.FromSeconds(2)); }
public void Should_Be_Running_When_Started() { // Given var testScheduler = new TestScheduler(); SchedulerProvider schedulerProvider = new SchedulerProviderFixture().WithTestScheduler(testScheduler); DecrementTimer sut = new DecrementTimerFixture().WithProvider(schedulerProvider); // When sut.Start(TimeSpan.FromMinutes(25)); testScheduler.AdvanceByMs(InitialMilliseconds); // Then sut.IsRunning.Should().BeTrue(); }
public void SmokeTest() { var scheduler = new TestScheduler(); var throttleDuration = TimeSpan.FromSeconds(5); var source = new Subject <int>(); var throttler = new Subject <Unit>(); var output = source.ThrottleWhenIncoming(throttler, throttleDuration, scheduler).CreateCollection(); source.OnNext(1); source.OnNext(2); scheduler.AdvanceByMs(1000); Assert.Equal(1, output[0]); Assert.Equal(2, output[1]); throttler.OnNext(Unit.Default); source.OnNext(3); scheduler.AdvanceByMs(1000); Assert.Equal(2, output.Count); scheduler.AdvanceByMs(5000); Assert.Equal(2, output.Count); source.OnNext(4); scheduler.AdvanceByMs(1000); Assert.Equal(4, output[2]); }
public void Should_Notify_When_Operations_Complete() { // Given var result = false; var testScheduler = new TestScheduler(); ApplicationStartup sut = new ApplicationStartupFixture().WithStartupOperations(new TestOperation(testScheduler, TimeSpan.FromSeconds(3))); // When sut.Startup() .Select(_ => true) .Subscribe( x => { // Then result = x; } ); testScheduler.AdvanceByMs(1000); result.Should().BeFalse(); testScheduler.AdvanceByMs(2001); result.Should().BeTrue(); }
public void CanLogin_TicksCorrectly() { var scheduler = new TestScheduler(); LoginViewModel sut = new LoginViewModelBuilder() .WithScheduler(scheduler) .WithUserName("coolusername") .WithPassword("Mr. Goodbytes"); sut.Cancel.CanExecute.ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var collection).Subscribe(); Observable.Return(Unit.Default).InvokeCommand(sut.Login); scheduler.AdvanceByMs(TimeSpan.FromSeconds(5).TotalMilliseconds); collection.ToList().Should().BeEquivalentTo(new[] { false, true, false }); }
public void RemainingTime() { // Given TimeSpan result = TimeSpan.Zero; var something = DateTimeOffset.Parse("2020-08-15").AddHours(2); var testScheduler = new TestScheduler(); Subject <DateTimeOffset> fixture = new Subject <DateTimeOffset>(); fixture.RemainingTime(testScheduler).Subscribe(x => result = x); // When fixture.OnNext(something); testScheduler.AdvanceByMs(2001); // Then result.Should().Be(TimeSpan.Zero); }
public void User_CannotLogin_WithIncorrect_Password() { var scheduler = new TestScheduler(); LoginViewModel sut = new LoginViewModelBuilder() .WithScheduler(scheduler) .WithUserName("coolusername") .WithPassword("incorrectpassword"); bool?value = null; sut.Login.Subscribe(x => value = x); Observable.Return(Unit.Default).InvokeCommand(sut.Login); scheduler.AdvanceByMs(TimeSpan.FromSeconds(3).TotalMilliseconds); value.Should().Be(false); }
public void test_that_ack_filter_responds_to_a_valid_message(string message) { _mockLogger = new(); _mockStationConfig = new(); _mockInfluxClient = new(); _mockConfig = new(); _mockTimeProvider = new(); _mockStationConfig.Setup(x => x.Value).Returns(new StationConfig() { Stations = new Station[] { new Station { Id = testStationId, Description = testStationArea, Enabled = true } } }); var testScheduler = new TestScheduler(); DateTime testDateTime = new DateTime(2020, 1, 1, 1, 0, 0); var kmStream = testScheduler.CreateColdObservable <KafkaMessage>( testScheduler.OnNextAt(2000, GenerateTestKM(message, testDateTime.AddMilliseconds(2000))) ); _mockConfig.Setup(x => x.Value).Returns(new AckSinkConfig() { Enabled = true }); _ackSink = new(_mockLogger.Object, _mockStationConfig.Object, _mockInfluxClient.Object, _mockConfig.Object, _mockTimeProvider.Object, testScheduler); _ackSink.Observe(kmStream); testScheduler.AdvanceByMs(5000); _mockInfluxClient.Verify(x => x.WritePirVoltage(It.IsAny <string>(), testStationId, It.IsAny <float>(), It.IsAny <DateTime>())); }
public void Commit_edit() { var scheduler = new TestScheduler(); var viewModel = new ViewModel(); var items = new List <Item> { new Item() }; var grid = new DataGrid2(); grid.DataContext = viewModel; grid.ItemsSource = items; grid.SelectedItem = items[0]; new Editable(scheduler).Attach(grid); grid.RaiseEvent(WpfTestHelper.TextArgs("1")); Assert.IsTrue(viewModel.updated); Assert.IsFalse(viewModel.commited); scheduler.AdvanceByMs(2000); Assert.IsTrue(viewModel.commited); Assert.AreEqual(1, items[0].Value); }
public async Task CachingTest() { var testScheduler = new TestScheduler(); var cache = new InMemoryBlobCache(testScheduler); var cacheTimeout = TimeSpan.FromSeconds(10); var someApi = new Mock <ISomeApi>(); someApi.Setup(s => s.GetSomeStrings()) .Returns(Task.FromResult("helloworld")).Verifiable(); var apiWrapper = new SomeApiWrapper(someApi.Object, cache, cacheTimeout); var string1 = await apiWrapper.GetSomeStrings(); someApi.Verify(s => s.GetSomeStrings(), Times.Once()); StringAssert.AreEqualIgnoringCase("helloworld", string1); testScheduler.AdvanceToMs(5000); var observer = testScheduler.CreateObserver <string>(); apiWrapper.GetSomeStrings().Subscribe(observer); testScheduler.AdvanceByMs(cacheTimeout.TotalMilliseconds); someApi.Verify(s => s.GetSomeStrings(), Times.Once()); StringAssert.AreEqualIgnoringCase("helloworld", observer.Messages[0].Value.Value); }
public static void Advance(this TestScheduler scheduler) { scheduler.AdvanceByMs(1); }