예제 #1
0
        public void Should_report_cancellation_during_faulting_last_retry_execution_when_user_delegate_does_observe_cancellationtoken()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .RetryAsync(3);

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            int    attemptsInvoked = 0;
            Action onExecute       = () => attemptsInvoked++;

            Scenario scenario = new Scenario
            {
                AttemptDuringWhichToCancel = 1 + 3,
                ActionObservesCancellation = true
            };

            policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Good))
            .ShouldThrow <TaskCanceledException>()
            .And.CancellationToken.Should().Be(cancellationToken);

            attemptsInvoked.Should().Be(1 + 3);
        }
예제 #2
0
        public void Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .RetryAsync(3, (_, __) =>
            {
                cancellationTokenSource.Cancel();
            });

            int    attemptsInvoked = 0;
            Action onExecute       = () => attemptsInvoked++;

            Scenario scenario = new Scenario
            {
                AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above.
                ActionObservesCancellation = false
            };

            policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Good))
            .ShouldThrow <TaskCanceledException>()
            .And.CancellationToken.Should().Be(cancellationToken);

            attemptsInvoked.Should().Be(1);
        }
예제 #3
0
        public void Should_not_execute_action_when_cancellationtoken_cancelled_before_execute()
        {
            RetryPolicy <ResultPrimitive> policy = Policy
                                                   .HandleResult(ResultPrimitive.Fault)
                                                   .RetryAsync(3);

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            int    attemptsInvoked = 0;
            Action onExecute       = () => attemptsInvoked++;

            Scenario scenario = new Scenario
            {
                AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution.
            };

            cancellationTokenSource.Cancel();

            policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Fault,
                                                                                         ResultPrimitive.Good))
            .ShouldThrow <TaskCanceledException>()
            .And.CancellationToken.Should().Be(cancellationToken);

            attemptsInvoked.Should().Be(0);
        }