Esempio n. 1
0
        public static void RaiseExceptionAndOrCancellation <TException>(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute, Func <int, TException> exceptionFactory) where TException : Exception
        {
            int counter = 0;

            CancellationToken cancellationToken = cancellationTokenSource.Token;

            policy.Execute(ct =>
            {
                onExecute();

                counter++;

                if (scenario.AttemptDuringWhichToCancel.HasValue && counter >= scenario.AttemptDuringWhichToCancel.Value)
                {
                    cancellationTokenSource.Cancel();
                }

                if (scenario.ActionObservesCancellation)
                {
                    ct.ThrowIfCancellationRequested();
                }

                if (counter <= scenario.NumberOfTimesToRaiseException)
                {
                    throw exceptionFactory(counter);
                }
            }, cancellationToken);
        }
Esempio n. 2
0
 public static TResult RaiseExceptionAndOrCancellation <TException, TResult>(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute, TResult successResult) where TException : Exception, new()
 {
     return(policy.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute,
                                                   _ => new TException(), successResult));
 }
Esempio n. 3
0
        public static Task <TResult> RaiseExceptionAndOrCancellationAsync <TException, TResult>(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute, TResult successResult) where TException : Exception, new()
        {
            int counter = 0;

            CancellationToken cancellationToken = cancellationTokenSource.Token;

            return(policy.ExecuteAsync(ct =>
            {
                onExecute();

                counter++;

                if (scenario.AttemptDuringWhichToCancel.HasValue && counter >= scenario.AttemptDuringWhichToCancel.Value)
                {
                    cancellationTokenSource.Cancel();
                }

                if (scenario.ActionObservesCancellation)
                {
                    ct.ThrowIfCancellationRequested();
                }

                if (counter <= scenario.NumberOfTimesToRaiseException)
                {
                    throw new TException();
                }

                return Task.FromResult(successResult);
            }, cancellationToken));
        }
Esempio n. 4
0
 public static void RaiseExceptionAndOrCancellation <TException>(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute) where TException : Exception, new()
 {
     policy.RaiseExceptionAndOrCancellation <TException>(scenario, cancellationTokenSource, onExecute, _ => new TException());
 }
Esempio n. 5
0
 public static Task RaiseExceptionAndOrCancellationAsync <TException>(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute) where TException : Exception, new()
 {
     return(policy.RaiseExceptionAndOrCancellationAsync <TException, int>(scenario, cancellationTokenSource, onExecute, 0));
 }