예제 #1
0
        public void Cancel_Loop_That_Is_Executing_An_Action()
        {
            "Given a loop that executes a long running action every 1 tick"
            ._(() =>
            {
                var loop     = new RxLoopCancellable(_testSchedulers, _longRunningAction, TimeSpan.FromTicks(1));
                _tokenSource = new CancellationTokenSource();
                loop.StartLoop(_tokenSource.Token);
            });

            "Given the loop starts but we cancel the loop after a 0.2 second delay"
            ._(() =>
            {
                var timerToStopLoop      = new Timer(200);
                timerToStopLoop.Elapsed += (sender, args) => _tokenSource.Cancel();
                timerToStopLoop.Start();

                _testSchedulers.AdvanceAllBy(20.Ticks());
            });

            "The long running action should gracefully exit and the loop should stop"
            ._(() =>
            {
                _longRunningActionCancelled.Should().BeTrue();
                _callCount.Should().BeGreaterThan(1000);
            });
        }
예제 #2
0
        public void AsTimePasses_TheLoopAction_ShouldBePerformedMultipleTimes()
        {
            "Given a loop that increments a call counter every 5 ticks"
            ._(() =>
            {
                var loop     = new RxLoopCancellable(_testSchedulers, _action, TimeSpan.FromTicks(5));
                _tokenSource = new CancellationTokenSource();
                loop.StartLoop(_tokenSource.Token);
            });

            "Given an initial call count of 0"
            ._(() =>
            {
                _callCount.Should().Be(0);
            });

            "Then the call count should be one after 4 ticks"
            ._(() =>
            {
                _testSchedulers.AdvanceAllBy(TimeSpan.FromTicks(4));
                _callCount.Should().Be(1);
            });

            "Then the call count should be 2 after 5 ticks"
            ._(() =>
            {
                _testSchedulers.AdvanceAllBy(TimeSpan.FromTicks(2));
                _callCount.Should().Be(2);
            });

            "Then the call count should be 3 after 10 ticks"
            ._(() =>
            {
                _testSchedulers.AdvanceAllBy(TimeSpan.FromTicks(5));
                _callCount.Should().Be(3);
            });

            "Then the call count should be 11 after 50 ticks"
            ._(() =>
            {
                _testSchedulers.AdvanceAllBy(TimeSpan.FromTicks(40));
                _callCount.Should().Be(11);
            });

            "Then let's cancel the loop"
            ._(() =>
            {
                _tokenSource.Cancel();
            });

            "Then the call count should not change while time elapses"
            ._(() =>
            {
                _testSchedulers.AdvanceAllBy(1000.Ticks());
                _callCount.Should().Be(11);
            });
        }