コード例 #1
0
        public async Task TriggerOnceSetsIsActiveFalseAfterActionExecutionThrows(int?durationSecs)
        {
            //Arrange
            var triggered = false;
            var action    = Get.CustomBuilderFor.MockAnarchyAction
                            .ThatIsSchedulable()
                            .ThatExecutesTask(ct =>
            {
                triggered = true;
                throw new Exception("this shouldn't affect things");
            })
                            .Build();
            var schedulerFactory = Get.CustomBuilderFor.MockSchedulerFactory.Build();
            var sut = new ActionOrchestrator <ICauseAnarchy>(action, schedulerFactory);

            var duration = durationSecs.HasValue ? TimeSpan.FromSeconds(durationSecs.Value) : (TimeSpan?)null;

            //Act
            sut.TriggerOnce(duration);
            await Wait.Until(() => triggered, 1);

            await Wait.Until(() => sut.IsActive == false, 1);

            //Assert
            sut.IsActive.Should().BeFalse();
        }
コード例 #2
0
        public async Task StopKillsUnscheduledExecutions()
        {
            //Arrange
            var cts        = new CancellationTokenSource();
            var ctFromTest = cts.Token;
            CancellationToken linkedCancellationToken;

            var action = Get.CustomBuilderFor.MockAnarchyAction
                         .ThatIsSchedulable()
                         .ThatExecutesTask(async ctFromOrchestrator =>
            {
                linkedCancellationToken =
                    CancellationTokenSource.CreateLinkedTokenSource(ctFromOrchestrator, ctFromTest).Token;
                await Block.UntilCancelled(linkedCancellationToken);
            })
                         .Build();
            var schedulerFactory = Get.CustomBuilderFor.MockSchedulerFactory.Build();
            var sut = new ActionOrchestrator <ICauseAnarchy>(action, schedulerFactory);

            sut.TriggerOnce(null);

            //Act
#pragma warning disable CS4014 // Intentionally not awaiting Stop as we want to check the task triggered
            sut.Stop();
#pragma warning restore CS4014 // We instead wait until our test state is triggered before asserting
            await Wait.Until(() => linkedCancellationToken.IsCancellationRequested, 1);

            var stopCancelledTheTask = !ctFromTest.IsCancellationRequested;
            cts.Cancel();

            //Assert
            Assert.That(stopCancelledTheTask);
        }
コード例 #3
0
        public void Start_ActionOrchestratorStopping_Throws()
        {
            //Arrange
            var ctsFromTest      = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            var initialExecution = true;

            var action = Get.CustomBuilderFor.MockAnarchyAction
                         .ThatIsSchedulable()
                         .ThatExecutesTask(async ctFromOrchestrator =>
            {
                // the goal of this is to block the action execution on the first call,
                // this will lead to an active task in _executionInstances that will need cancelling
                if (initialExecution)
                {
                    initialExecution = false;
                    await Block.UntilCancelled(ctsFromTest.Token);
                }
            })
                         .Build();

            var schedulerFactory = Get.CustomBuilderFor.MockSchedulerFactory.Build();
            var sut = new ActionOrchestrator <ICauseAnarchy>(action, schedulerFactory);

#pragma warning disable 4014                             // explicitly not awaiting here as we need to set separate tasks running that are blocked to trigger test state
            sut.TriggerOnce(TimeSpan.FromMinutes(1000)); // block the stop action to ensure we have a token that is cancelled but not replaced
            sut.Stop();
#pragma warning restore 4014

            //Act
            var exception = Assert.Catch(() => sut.Start());
            ctsFromTest.Cancel();

            //Assert
            exception.Should().BeOfType <ActionStoppingException>();
        }
コード例 #4
0
        public void TriggerOnceCallsActionExecuteAsync(int?durationSecs)
        {
            //Arrange
            var action           = (ICauseScheduledAnarchy)Get.CustomBuilderFor.MockAnarchyAction.ThatIsSchedulable().Build();
            var schedulerFactory = Get.CustomBuilderFor.MockSchedulerFactory.Build();
            var sut      = new ActionOrchestrator <ICauseAnarchy>(action, schedulerFactory);
            var duration = durationSecs.HasValue ? TimeSpan.FromSeconds(durationSecs.Value) : (TimeSpan?)null;

            //Act
            sut.TriggerOnce(duration);

            //Assert
            action.Received(1).ExecuteAsync(duration, Arg.Any <CancellationToken>());
        }
コード例 #5
0
        public void TriggerOnceSetsIsActive(int?durationSecs)
        {
            //Arrange
            var cts    = new CancellationTokenSource();
            var action = Get.CustomBuilderFor.MockAnarchyAction
                         .ThatIsSchedulable()
                         .ThatExecutesTask(async ct => await Block.UntilCancelled(cts.Token))
                         .Build();
            var schedulerFactory = Get.CustomBuilderFor.MockSchedulerFactory.Build();
            var sut = new ActionOrchestrator <ICauseAnarchy>(action, schedulerFactory);

            var duration = durationSecs.HasValue ? TimeSpan.FromSeconds(durationSecs.Value) : (TimeSpan?)null;

            //Act
            sut.TriggerOnce(duration);
            var runningIsActiveState = sut.IsActive;

            cts.Cancel();

            //Assert
            runningIsActiveState.Should().BeTrue();
        }