コード例 #1
0
        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));
            });
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
            });
        }
コード例 #5
0
        public void ReactiveAsyncCommandInitialConditionDefaultBehavior()
        {
            (new TestScheduler()).With(sched => {
                var canExecute = sched.CreateHotObservable(
                    sched.OnNextAt(0, false),
                    sched.OnNextAt(250, true)
                    );

                var fixture = new ReactiveAsyncCommand(canExecute);

                fixture.RegisterAsyncTask(_ => DummyTestFunction());

                Assert.True(fixture.CanExecute(null));

                sched.AdvanceToMs(10);
                Assert.False(fixture.CanExecute(null));

                sched.AdvanceToMs(255);
                Assert.True(fixture.CanExecute(null));
            });
        }
コード例 #6
0
        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)));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
            });
        }
コード例 #10
0
        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);
            });
        }
コード例 #11
0
        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)));
        }
コード例 #12
0
        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);
            });
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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));
        }
コード例 #15
0
        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);
        }
コード例 #16
0
 public bool CanExecute(object a_parameter)
 {
     return(m_subCommand.CanExecute(a_parameter));
 }
コード例 #17
0
        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));
        }
コード例 #18
0
        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)));
        }