public void AsyncCommandSmokeTest() { var sched = new TestScheduler(); IObservable<int> async_data; ReactiveAsyncCommand fixture; using (TestUtils.WithTestScheduler(sched)) { fixture = new ReactiveAsyncCommand(null, 1); async_data = fixture .Delay(TimeSpan.FromSeconds(5), RxApp.TaskpoolScheduler) .Select(_ => 5) .Do(_ => fixture.AsyncCompletedNotification.OnNext(new Unit())); } var inflight_results = new List<int>(); fixture.ItemsInflight.Subscribe(inflight_results.Add); var output = new List<int>(); async_data.Subscribe(output.Add); Assert.IsTrue(fixture.CanExecute(null)); fixture.Execute(null); sched.RunToMilliseconds(1005); Assert.IsFalse(fixture.CanExecute(null)); sched.RunToMilliseconds(5005); Assert.IsTrue(fixture.CanExecute(null)); new[] {0,1,0}.AssertAreEqual(inflight_results); new[] {5}.AssertAreEqual(output); }
public void RAFShouldActuallyRunOnTheTaskpool() { var deferred = RxApp.DeferredScheduler; var taskpool = RxApp.TaskpoolScheduler; try { var testDeferred = new CountingTestScheduler(Scheduler.Immediate); var testTaskpool = new CountingTestScheduler(Scheduler.NewThread); RxApp.DeferredScheduler = testDeferred; RxApp.TaskpoolScheduler = testTaskpool; var fixture = new ReactiveAsyncCommand(); var result = fixture.RegisterAsyncFunction(x => { Thread.Sleep(1000); return((int)x * 5); }); fixture.Execute(1); Assert.Equal(5, result.First()); Assert.True(testDeferred.ScheduledItems.Count >= 1); Assert.True(testTaskpool.ScheduledItems.Count >= 1); } finally { RxApp.DeferredScheduler = deferred; RxApp.TaskpoolScheduler = taskpool; } }
public void MultipleSubscribersShouldntDecrementRefcountBelowZero() { (new TestScheduler()).With(sched => { var fixture = new ReactiveAsyncCommand(); var results = new List <int>(); bool[] subscribers = new[] { false, false, false, false, false }; var output = fixture.RegisterAsyncObservable(_ => Observable.Return(5).Delay(TimeSpan.FromMilliseconds(5000), sched)); output.Subscribe(x => results.Add(x)); Enumerable.Range(0, 5).Run(x => output.Subscribe(_ => subscribers[x] = true)); Assert.True(fixture.CanExecute(null)); fixture.Execute(null); sched.AdvanceToMs(2000); Assert.False(fixture.CanExecute(null)); sched.AdvanceToMs(6000); Assert.True(fixture.CanExecute(null)); Assert.True(results.Count == 1); Assert.True(results[0] == 5); Assert.True(subscribers.All(x => x)); }); }
public void RAOShouldActuallyRunOnTheTaskpool() { var deferred = RxApp.DeferredScheduler; var taskpool = RxApp.TaskpoolScheduler; try { var testDeferred = new CountingTestScheduler(Scheduler.Immediate); var testTaskpool = new CountingTestScheduler(Scheduler.NewThread); RxApp.DeferredScheduler = testDeferred; RxApp.TaskpoolScheduler = testTaskpool; var fixture = new ReactiveAsyncCommand(); var result = fixture.RegisterAsyncObservable(x => Observable.Return((int)x * 5).Delay(TimeSpan.FromSeconds(1), RxApp.TaskpoolScheduler)); fixture.Execute(1); Assert.Equal(5, result.First()); Assert.True(testDeferred.ScheduledItems.Count >= 1); Assert.True(testTaskpool.ScheduledItems.Count >= 1); } finally { RxApp.DeferredScheduler = deferred; RxApp.TaskpoolScheduler = taskpool; } }
public MyPageViewModel(ITicketService ticketService, ITicketMapper mapper) { _ticketService = ticketService; _mapper = mapper; LoadTickets = new ReactiveAsyncCommand(null, 1, RxApp.DeferredScheduler); LoadTickets.RegisterAsyncFunction(x => loadTickets()) .ToProperty(this, x => x.Tickets); Observable.Interval(TimeSpan.FromSeconds(10), RxApp.DeferredScheduler) .InvokeCommand(LoadTickets); LoadTickets.Execute(null); _redmineBaseUrl = ConfigurationManager.AppSettings["Redmine.BaseRedmineUrl"]; SortBy = new List<SortByModel>() { new SortByModel("Project", c => c.Project), new SortByModel("Due date", c=> c.DueDate), new SortByModel("Priority", c => c.Priority), }; SortByCommand = new ReactiveCommand(this.WhenAny(c => c.Tickets, ((tickets) => tickets.Value != null && tickets.Value.Count > 0))); SortByCommand.Subscribe(c => sortTickets((SortByModel)c)); }
public MyPageViewModel(ITicketService ticketService, ITicketMapper mapper) { _ticketService = ticketService; _mapper = mapper; LoadTickets = new ReactiveAsyncCommand(null, 1, RxApp.DeferredScheduler); LoadTickets.RegisterAsyncFunction(x => loadTickets()) .ToProperty(this, x => x.Tickets); Observable.Interval(TimeSpan.FromSeconds(10), RxApp.DeferredScheduler) .InvokeCommand(LoadTickets); LoadTickets.Execute(null); _redmineBaseUrl = ConfigurationManager.AppSettings["Redmine.BaseRedmineUrl"]; SortBy = new List <SortByModel>() { new SortByModel("Project", c => c.Project), new SortByModel("Due date", c => c.DueDate), new SortByModel("Priority", c => c.Priority), }; SortByCommand = new ReactiveCommand(this.WhenAny(c => c.Tickets, ((tickets) => tickets.Value != null && tickets.Value.Count > 0))); SortByCommand.Subscribe(c => sortTickets((SortByModel)c)); }
public void AsyncCommandSmokeTest() { var inflight_results = new List<int>(); var fixture = new ReactiveAsyncCommand(null, 1, Scheduler.Immediate); var async_data = fixture .ObserveOn(Scheduler.NewThread) .Delay(new TimeSpan(0, 0, 5)) .Select(_ => 5) .Do(_ => fixture.AsyncCompletedNotification.OnNext(new Unit())); fixture.ItemsInflight.Subscribe(x => inflight_results.Add(x)); async_data.Subscribe(); Assert.IsTrue(fixture.CanExecute(null)); fixture.Execute(null); Assert.IsFalse(fixture.CanExecute(null)); Thread.Sleep(5500); Assert.IsTrue(fixture.CanExecute(null)); new[] {0,1,0}.Zip(inflight_results, (expected, actual) => new {expected, actual}) .Run(x => Assert.AreEqual(x.expected, x.actual)); }
public TrackReporter(IObservable <Mix> currentMixObservable, IObservable <double> currentPositionObservable, IRequestExecutor requestExecutor) { this.requestExecutor = new RequestExecutionAdapter(requestExecutor); IObservable <bool> reportingMarkReached = currentPositionObservable .BufferWithCount(2, 1) .Select(positions => (positions[0] < reportingMark && positions[1] > reportingMark)); currentMixObservable.Subscribe(mix => { currentMix = mix; }); ReactiveAsyncCommand reportTrack = new ReactiveAsyncCommand(); reportTrack.Subscribe(_ => { int mixId = currentMix.MixId; Track track = currentMix.GetCurrentTrack(); Report(track, mixId); }); reportingMarkReached .Where(x => x) // we crossed the mark .Where(_ => !currentMix.GetCurrentTrack().IsReported) //it's not previously reported .Subscribe(_ => reportTrack.Execute(null)); }
public void CanExecuteShouldChangeOnInflightOp() { (new TestScheduler()).With(sched => { var canExecute = sched.CreateHotObservable( sched.OnNextAt(0, true), sched.OnNextAt(250, false), sched.OnNextAt(500, true), sched.OnNextAt(750, false), sched.OnNextAt(1000, true), sched.OnNextAt(1100, false) ); var fixture = new ReactiveAsyncCommand(canExecute); int calculatedResult = -1; bool latestCanExecute = false; fixture.RegisterAsyncObservable(x => Observable.Return((int)x * 5).Delay(TimeSpan.FromMilliseconds(900), RxApp.DeferredScheduler)) .Subscribe(x => calculatedResult = x); fixture.CanExecuteObservable.Subscribe(x => latestCanExecute = x); // CanExecute should be true, both input observable is true // and we don't have anything inflight sched.AdvanceToMs(10); Assert.True(fixture.CanExecute(1)); Assert.True(latestCanExecute); // Invoke a command 10ms in fixture.Execute(1); // At 300ms, input is false sched.AdvanceToMs(300); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); // At 600ms, input is true, but the command is still running sched.AdvanceToMs(600); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); // After we've completed, we should still be false, since from // 750ms-1000ms the input observable is false sched.AdvanceToMs(900); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); Assert.Equal(-1, calculatedResult); sched.AdvanceToMs(1010); Assert.True(fixture.CanExecute(1)); Assert.True(latestCanExecute); Assert.Equal(calculatedResult, 5); sched.AdvanceToMs(1200); Assert.False(fixture.CanExecute(1)); Assert.False(latestCanExecute); }); }
public TranslatorViewModel() { TranslateText = new ReactiveAsyncCommand(); this.ObservableForProperty(x => x.EnglishText) .Throttle(TimeSpan.FromMilliseconds(1200)) .Subscribe(x => TranslateText.Execute(x.Value)); var client = new LanguageServiceClient() as LanguageService; var translation_func = Observable.FromAsyncPattern<string, string>( client.BeginTranslateToGerman, client.EndTranslateToGerman); var results = TranslateText.RegisterAsyncObservable(x => translation_func((string)x)); _TranslatedText = this.ObservableToProperty(results, x => x.TranslatedText); }
public TranslatorViewModel() { TranslateText = new ReactiveAsyncCommand(); this.ObservableForProperty(x => x.EnglishText) .Throttle(TimeSpan.FromMilliseconds(1200)) .Subscribe(x => TranslateText.Execute(x.Value)); var client = new LanguageServiceClient() as LanguageService; var translation_func = Observable.FromAsyncPattern <string, string>( client.BeginTranslateToGerman, client.EndTranslateToGerman); var results = TranslateText.RegisterAsyncObservable(x => translation_func((string)x)); _TranslatedText = this.ObservableToProperty(results, x => x.TranslatedText); }
public void MultipleResultsFromObservableShouldntDecrementRefcountBelowZero() { (new TestScheduler()).With(sched => { int latestInFlight = 0; var fixture = new ReactiveAsyncCommand(null, 1, sched); var results = fixture .RegisterAsyncObservable(_ => new[] { 1, 2, 3 }.ToObservable()) .CreateCollection(); fixture.ItemsInflight.Subscribe(x => latestInFlight = x); fixture.Execute(1); sched.Start(); Assert.Equal(3, results.Count); Assert.Equal(0, latestInFlight); }); }
public MainWindowViewModel() { CalculateTheAnswer = new ReactiveAsyncCommand(); CalculateTheAnswer.RegisterAsyncTask(_ => AnswerCalculator()) .ToProperty(this, x => x.TheAnswer); CalculateTheAnswer.ThrownExceptions .SelectMany(ex => UserError.Throw(ex.Message)) .Subscribe(result => { if (result == RecoveryOptionResult.RetryOperation) { _retryCalculate = true; CalculateTheAnswer.Execute(null); } else Application.Current.Shutdown(); }); }
public void MakeSureMemoizedReleaseFuncGetsCalled() { //Assert.True(false, "When an item gets evicted from the cache before it has a chance to complete, it deadlocks. Fix it."); var input = new[] { 1, 1, 2, 2, 1, 1, 3, 3 }; var sched = new EventLoopScheduler(); var fixture = new ReactiveAsyncCommand(); var results = new List <Timestamped <int> >(); var released = new List <int>(); fixture.RegisterMemoizedFunction(x => { Thread.Sleep(250); return(((int)x) * 5); }, 2, x => released.Add(x), sched) .Timestamp() .DebugObservable() .Subscribe(x => results.Add(x)); Assert.True(fixture.CanExecute(1)); var start = DateTimeOffset.Now; foreach (var i in input) { Assert.True(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(1000); this.Log().Info("Timestamp Deltas"); results.Select(x => x.Timestamp - start) .Run(x => this.Log().Info(x)); this.Log().Info("Release list"); released.Run(x => this.Log().Info(x)); Assert.True(results.Count == 8); Assert.True(released.Count == 1); Assert.True(released[0] == 2 * 5); }
public void RegisterMemoizedFunctionSmokeTest() { var input = new[] { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; var output = new[] { 5, 5, 5, 5, 5, 10, 10, 10, 10, 10 }; var sched = new EventLoopScheduler(); var results = new List <Timestamped <int> >(); var start = sched.Now; sched.With(_ => { var fixture = new ReactiveAsyncCommand(null, 5, sched); fixture.RegisterMemoizedFunction(x => { Thread.Sleep(1000); return(((int)x) * 5); }, 50, null, sched) .Timestamp() .Subscribe(x => results.Add(x)); Assert.True(fixture.CanExecute(1)); foreach (var i in input) { Assert.True(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(2500); }); Assert.Equal(10, results.Count); results.Select(x => x.Timestamp - start) .Run(x => { }); output.AssertAreEqual(results.Select(x => x.Value)); Assert.False(results.Any(x => x.Timestamp - start > new TimeSpan(0, 0, 3))); }
public void MakeSureMemoizedReleaseFuncGetsCalled() { var input = new[] { 1, 1, 2, 2, 1, 1, 3, 3 }; var output = new[] { 5, 5, 10, 10, 5, 5, 15, 15 }; var fixture = new ReactiveAsyncCommand(null, 0); var results = new List<Timestamped<int>>(); var released = new List<int>(); fixture.RegisterMemoizedFunction<int>(x => { Thread.Sleep(250); return ((int)x) * 5; }, 2, x => released.Add(x)) .Timestamp() .DebugObservable() .Subscribe(x => results.Add(x)); Assert.IsTrue(fixture.CanExecute(1)); var start = DateTimeOffset.Now; foreach(var i in input) { Assert.IsTrue(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(1000); this.Log().Info("Timestamp Deltas"); results.Select(x => x.Timestamp - start) .Run(x => this.Log().Info(x)); this.Log().Info("Release list"); released.Run(x => this.Log().Info(x)); output.Zip(results.Select(x => x.Value), (expected, actual) => new { expected, actual }) .Run(x => Assert.AreEqual(x.expected, x.actual)); Assert.IsTrue(results.Count == 8); Assert.IsTrue(released.Count == 1); Assert.IsTrue(released[0] == 2*5); }
public void RegisterAsyncFunctionSmokeTest() { (new TestScheduler()).With(sched => { var fixture = new ReactiveAsyncCommand(null, 1); ReactiveCollection <int> results; results = fixture.RegisterAsyncObservable(_ => Observable.Return(5).Delay(TimeSpan.FromSeconds(5), sched)).CreateCollection(); var inflightResults = fixture.ItemsInflight.CreateCollection(); sched.AdvanceToMs(10); Assert.True(fixture.CanExecute(null)); fixture.Execute(null); sched.AdvanceToMs(1005); Assert.False(fixture.CanExecute(null)); sched.AdvanceToMs(5100); Assert.True(fixture.CanExecute(null)); new[] { 0, 1, 0 }.AssertAreEqual(inflightResults); new[] { 5 }.AssertAreEqual(results); }); }
public void RegisterMemoizedFunctionSmokeTest() { var input = new[] { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; var output = new[] { 5, 5, 5, 5, 5, 10, 10, 10, 10, 10 }; var sched = new EventLoopScheduler(); var results = new List<Timestamped<int>>(); var start = sched.Now; sched.With(_ => { var fixture = new ReactiveAsyncCommand(null, 5, sched); fixture.RegisterMemoizedFunction(x => { Thread.Sleep(1000); return ((int) x) * 5; }, 50, null, sched) .Timestamp() .Subscribe(x => results.Add(x)); Assert.True(fixture.CanExecute(1)); foreach (var i in input) { Assert.True(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(2500); }); Assert.Equal(10, results.Count); results.Select(x => x.Timestamp - start) .Run(x => { }); output.AssertAreEqual(results.Select(x => x.Value)); Assert.False(results.Any(x => x.Timestamp - start > new TimeSpan(0, 0, 3))); }
public void RegisterAsyncFunctionSmokeTest() { var inflight_results = new List<int>(); var fixture = new ReactiveAsyncCommand(null, 1); var results = new List<int>(); fixture.RegisterAsyncFunction(_ => { Thread.Sleep(2000); return 5; }) .Subscribe(x => results.Add(x)); fixture.ItemsInflight.Subscribe(x => inflight_results.Add(x)); Assert.IsTrue(fixture.CanExecute(null)); fixture.Execute(null); Assert.IsFalse(fixture.CanExecute(null)); Thread.Sleep(6000); Assert.IsTrue(fixture.CanExecute(null)); new[] {0,1,0}.Zip(inflight_results, (expected, actual) => new {expected, actual}) .Run(x => Assert.AreEqual(x.expected, x.actual)); Assert.IsTrue(results.Count == 1); Assert.IsTrue(results[0] == 5); }
public MediaBrowserViewModel(IMediaLibraryBrowser mediaLibraryBrowser, Settings settings, LoginFormViewModel loginFormViewModel, IMixViewModelFactory mixViewModelFactory) { this.settings = settings; this.mixViewModelFactory = mixViewModelFactory; this.mediaLibraryBrowser = mediaLibraryBrowser; GetRecentlyMadeMixes = new ReactiveAsyncCommand(null, 1); GetRecentlyMadeMixes.RegisterAsyncFunction( _ => mediaLibraryBrowser.GetRecentlyMadeMixes(CreateMixFilter(Filter))) .Subscribe(SetBrowsedMixes); GetHotMixes = new ReactiveAsyncCommand(null, 1); GetHotMixes.RegisterAsyncFunction(_ => mediaLibraryBrowser.GetHotMixes(CreateMixFilter(Filter))).Subscribe( SetBrowsedMixes); GetPopularMixes = new ReactiveAsyncCommand(null, 1); GetPopularMixes.RegisterAsyncFunction(_ => mediaLibraryBrowser.GetPopularMixes(CreateMixFilter(Filter))) .Subscribe(SetBrowsedMixes); // command is enabled only if the user is logged in GetLikedMixes = new ReactiveAsyncCommand(loginFormViewModel.UserLoggedInObservable, 1); GetLikedMixes.RegisterAsyncFunction(_ => { var mixFilter = CreateMixFilter(Filter); string userSlug = loginFormViewModel.UserData.UserSlug; return(mediaLibraryBrowser.GetLikedMixes(mixFilter, userSlug)); }).Subscribe(SetBrowsedMixes); // command is enabled only if the user is logged in GetFeedMixes = new ReactiveAsyncCommand(loginFormViewModel.UserLoggedInObservable, 1); GetFeedMixes.RegisterAsyncFunction(_ => { var mixFilter = CreateMixFilter(Filter); string userSlug = loginFormViewModel.UserData.UserSlug; return(mediaLibraryBrowser.GetFeedMixes(mixFilter, userSlug)); }).Subscribe(SetBrowsedMixes); GetRecommendedMixes = new ReactiveAsyncCommand(loginFormViewModel.UserLoggedInObservable, 1); GetRecommendedMixes.RegisterAsyncFunction(_ => { var mixFilter = CreateMixFilter(Filter); string userSlug = loginFormViewModel.UserData.UserSlug; return(mediaLibraryBrowser.GetRecommendedMixes(mixFilter, userSlug)); }).Subscribe(SetBrowsedMixes); GetHistoryMixes = new ReactiveAsyncCommand(loginFormViewModel.UserLoggedInObservable, 1); GetHistoryMixes.RegisterAsyncFunction(_ => { var mixFilter = CreateMixFilter(Filter); string userSlug = loginFormViewModel.UserData.UserSlug; return(mediaLibraryBrowser.GetHistoryMixes(mixFilter, userSlug)); }).Subscribe(SetBrowsedMixes); BrowsedMixes = new DispatchedReactiveCollection <MixViewModel>(); ReplaySubject <bool> browsedMixesExistObservable = new ReplaySubject <bool>(1); BrowsedMixes.CollectionCountChanged.Select(count => count > 0).Subscribe(browsedMixesExistObservable.OnNext); browsedMixesExistObservable.OnNext(false); GetMoreMixes = new ReactiveAsyncCommand(browsedMixesExistObservable, 1); GetMoreMixes.RegisterAsyncFunction(_ => mediaLibraryBrowser.GetMoreMixes()) .Subscribe(AddBrowsedMixes); // set can execute only if there is something in the list SetMixDisplayMode = new ReactiveCommand(browsedMixesExistObservable); SetMixDisplayMode.Subscribe(mode => MixDisplayMode = (MixDisplayMode)mode); ApplyFilter = new ReactiveCommand(); ApplyFilter.Where(_ => Filter.Length > 0) .Subscribe(_ => { // if we only started up the application, by search for Hot mixes if (mediaLibraryBrowser.SessionData.IsEmpty) { GetHotMixes.Execute(null); return; } // otherwise, take the same type of mixes as they were before switch (mediaLibraryBrowser.SessionData.LastMixesRequest.ViewType) { case MixesViewType.Hot: GetHotMixes.Execute(null); break; case MixesViewType.Popular: GetPopularMixes.Execute(null); break; case MixesViewType.Recent: GetRecentlyMadeMixes.Execute(null); break; case MixesViewType.Liked: GetLikedMixes.Execute(null); break; case MixesViewType.Feed: GetFeedMixes.Execute(null); break; case MixesViewType.Listened: GetHistoryMixes.Execute(null); break; case MixesViewType.Recommended: GetRecommendedMixes.Execute(null); break; default: // throw new ArgumentOutOfRangeException(); break; } }); MixDisplayMode mixDisplayMode; string storedMixDisplayModeString = (string)settings[mixdisplayModeKey]; Enum.TryParse <MixDisplayMode>(storedMixDisplayModeString, out mixDisplayMode); MixDisplayMode = mixDisplayMode; MixesViewTypeAsString = String.Empty; Filter = String.Empty; MixBrowsingMessage = String.Empty; }
public DropRepoViewModel(IScreen hostScreen, IAppState appState, IRepoAnalysisProvider analyzeFunc) { HostScreen = hostScreen; AnalyzeRepo = new ReactiveAsyncCommand(); CoreUtility.ExtractLibGit2(); var scanResult = AnalyzeRepo.RegisterAsyncObservable(x => analyzeFunc.AnalyzeRepo((string) x)); scanResult.Select(x => x.Item1).ToProperty(this, x => x.CurrentRepoPath); scanResult .Select(x => x.Item2.Select(y => (IBranchInformationViewModel)new BranchInformationViewModel(y.Key, y.Value))) .Select(x => new ReactiveCollection<IBranchInformationViewModel>(x)) .ToProperty(this, x => x.BranchInformation); this.WhenAny(x => x.BranchInformation, x => x.Value != null ? Visibility.Visible : Visibility.Hidden) .ToProperty(this, x => x.RepairButtonVisibility); RepairButton = new ReactiveCommand(); RepairButton.Subscribe(_ => { appState.BranchInformation = BranchInformation.Where(x => x.BranchName != Constants.WorkingDirectory).ToArray(); appState.WorkingDirectoryInformation = BranchInformation.First(x => x.BranchName == Constants.WorkingDirectory).Model; appState.CurrentRepo = CurrentRepoPath; HostScreen.Router.Navigate.Execute(RxApp.GetService<IRepairViewModel>()); }); var viewStates = Observable.Merge( AnalyzeRepo.ItemsInflight.Where(x => x > 0).Select(_ => "Analyzing"), scanResult.Select(_ => "RepoAdded")); MessageBus.Current.RegisterMessageSource(viewStates, "DropRepoViewState"); this.WhenNavigatedTo(() => MessageBus.Current.Listen<string>("DropFolder").Subscribe(path => AnalyzeRepo.Execute(path))); }
public void MultipleSubscribersShouldntDecrementRefcountBelowZero() { var fixture = new ReactiveAsyncCommand(null, 1); var results = new List<int>(); bool[] subscribers = new[] { false, false, false, false, false }; var output = fixture.RegisterAsyncFunction(_ => { Thread.Sleep(2000); return 5; }); output.Subscribe(x => results.Add(x)); Enumerable.Range(0, 5).Run(x => output.Subscribe(_ => subscribers[x] = true)); Assert.IsTrue(fixture.CanExecute(null)); fixture.Execute(null); Assert.IsFalse(fixture.CanExecute(null)); Thread.Sleep(6000); Assert.IsTrue(fixture.CanExecute(null)); Assert.IsTrue(results.Count == 1); Assert.IsTrue(results[0] == 5); Assert.IsTrue(subscribers.All(x => x == true)); }
public DropRepoViewModel(IScreen hostScreen, IAppState appState, IRepoAnalysisProvider analyzeFunc) { HostScreen = hostScreen; AnalyzeRepo = new ReactiveAsyncCommand(); CoreUtility.ExtractLibGit2(); var scanResult = AnalyzeRepo.RegisterAsyncObservable(x => analyzeFunc.AnalyzeRepo((string)x)); scanResult.Select(x => x.Item1).ToProperty(this, x => x.CurrentRepoPath); scanResult .Select(x => x.Item2.Select(y => (IBranchInformationViewModel) new BranchInformationViewModel(y.Key, y.Value))) .Select(x => new ReactiveCollection <IBranchInformationViewModel>(x)) .ToProperty(this, x => x.BranchInformation); this.WhenAny(x => x.BranchInformation, x => x.Value != null ? Visibility.Visible : Visibility.Hidden) .ToProperty(this, x => x.RepairButtonVisibility); RepairButton = new ReactiveCommand(); RepairButton.Subscribe(_ => { appState.BranchInformation = BranchInformation.Where(x => x.BranchName != Constants.WorkingDirectory).ToArray(); appState.WorkingDirectoryInformation = BranchInformation.First(x => x.BranchName == Constants.WorkingDirectory).Model; appState.CurrentRepo = CurrentRepoPath; HostScreen.Router.Navigate.Execute(RxApp.GetService <IRepairViewModel>()); }); var viewStates = Observable.Merge( AnalyzeRepo.ItemsInflight.Where(x => x > 0).Select(_ => "Analyzing"), scanResult.Select(_ => "RepoAdded")); MessageBus.Current.RegisterMessageSource(viewStates, "DropRepoViewState"); this.WhenNavigatedTo(() => MessageBus.Current.Listen <string>("DropFolder").Subscribe(path => AnalyzeRepo.Execute(path))); }
public void RegisterAsyncFunctionSmokeTest() { (new TestScheduler()).With(sched => { var fixture = new ReactiveAsyncCommand(null, 1); ReactiveCollection<int> results; results = fixture.RegisterAsyncObservable(_ => Observable.Return(5).Delay(TimeSpan.FromSeconds(5), sched)).CreateCollection(); var inflightResults = fixture.ItemsInflight.CreateCollection(); sched.AdvanceToMs(10); Assert.True(fixture.CanExecute(null)); fixture.Execute(null); sched.AdvanceToMs(1005); Assert.False(fixture.CanExecute(null)); sched.AdvanceToMs(5100); Assert.True(fixture.CanExecute(null)); new[] {0,1,0}.AssertAreEqual(inflightResults); new[] {5}.AssertAreEqual(results); }); }
public void MakeSureMemoizedReleaseFuncGetsCalled() { //Assert.True(false, "When an item gets evicted from the cache before it has a chance to complete, it deadlocks. Fix it."); var input = new[] { 1, 1, 2, 2, 1, 1, 3, 3 }; var sched = new EventLoopScheduler(); var fixture = new ReactiveAsyncCommand(); var results = new List<Timestamped<int>>(); var released = new List<int>(); fixture.RegisterMemoizedFunction(x => { Thread.Sleep(250); return ((int)x) * 5; }, 2, x => released.Add(x), sched) .Timestamp() .DebugObservable() .Subscribe(x => results.Add(x)); Assert.True(fixture.CanExecute(1)); var start = DateTimeOffset.Now; foreach(var i in input) { Assert.True(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(1000); this.Log().Info("Timestamp Deltas"); results.Select(x => x.Timestamp - start) .Run(x => this.Log().Info(x)); this.Log().Info("Release list"); released.Run(x => this.Log().Info(x)); Assert.True(results.Count == 8); Assert.True(released.Count == 1); Assert.True(released[0] == 2*5); }
public void Execute(object a_parameter) { m_subCommand.Execute(a_parameter); }
public void RegisterMemoizedFunctionSmokeTest() { var input = new[] { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; var output = new[] { 5, 5, 5, 5, 5, 10, 10, 10, 10, 10 }; var fixture = new ReactiveAsyncCommand(null, 0); var results = new List<Timestamped<int>>(); fixture.RegisterMemoizedFunction<int>(x => { Thread.Sleep(1000); return ((int)x) * 5; }) .Timestamp() .DebugObservable() .Subscribe(x => results.Add(x)); Assert.IsTrue(fixture.CanExecute(1)); var start = DateTimeOffset.Now; foreach(var i in input) { Assert.IsTrue(fixture.CanExecute(i)); fixture.Execute(i); } Thread.Sleep(2500); Assert.IsTrue(results.Count == 10); this.Log().Info("Timestamp Deltas"); results.Select(x => x.Timestamp - start) .Run(x => this.Log().Info(x)); output.AssertAreEqual(results.Select(x => x.Value)); Assert.IsFalse(results.Any(x => x.Timestamp - start > new TimeSpan(0, 0, 3))); }
public void MultipleResultsFromObservableShouldntDecrementRefcountBelowZero() { (new TestScheduler()).With(sched => { int latestInFlight = 0; var fixture = new ReactiveAsyncCommand(null, 1, sched); var results = fixture .RegisterAsyncObservable(_ => new[] {1, 2, 3}.ToObservable()) .CreateCollection(); fixture.ItemsInflight.Subscribe(x => latestInFlight = x); fixture.Execute(1); sched.Start(); Assert.Equal(3, results.Count); Assert.Equal(0, latestInFlight); }); }
public void MultipleSubscribersShouldntDecrementRefcountBelowZero() { var sched = new TestScheduler(); var fixture = new ReactiveAsyncCommand(null, 1, sched); var results = new List<int>(); bool[] subscribers = new[] { false, false, false, false, false }; var output = fixture.RegisterAsyncObservable(_ => Observable.Return(5).Delay(TimeSpan.FromMilliseconds(5000), sched)); output.Subscribe(x => results.Add(x)); Enumerable.Range(0, 5).Run(x => output.Subscribe(_ => subscribers[x] = true)); Assert.IsTrue(fixture.CanExecute(null)); fixture.Execute(null); sched.RunToMilliseconds(2000); Assert.IsFalse(fixture.CanExecute(null)); sched.RunToMilliseconds(6000); Assert.IsTrue(fixture.CanExecute(null)); Assert.IsTrue(results.Count == 1); Assert.IsTrue(results[0] == 5); Assert.IsTrue(subscribers.All(x => x == true)); }
public void RAFShouldActuallyRunOnTheTaskpool() { var deferred = RxApp.DeferredScheduler; var taskpool = RxApp.TaskpoolScheduler; try { var testDeferred = new CountingTestScheduler(Scheduler.Immediate); var testTaskpool = new CountingTestScheduler(Scheduler.NewThread); RxApp.DeferredScheduler = testDeferred; RxApp.TaskpoolScheduler = testTaskpool; var fixture = new ReactiveAsyncCommand(); var result = fixture.RegisterAsyncFunction(x => { Thread.Sleep(1000); return (int)x * 5; }); fixture.Execute(1); Assert.Equal(5, result.First()); Assert.True(testDeferred.ScheduledItems.Count >= 1); Assert.True(testTaskpool.ScheduledItems.Count >= 1); } finally { RxApp.DeferredScheduler = deferred; RxApp.TaskpoolScheduler = taskpool; } }