Exemplo n.º 1
0
        public void RetryPolicyFactoryTest()
        {
            Assert.NotNull(RetryPolicyFactory.NoRetryPolicy);
            Assert.NotNull(RetryPolicyFactory.PrimaryKeyViolationRetryPolicy);

            RetryPolicy noRetyPolicy = RetryPolicyFactory.CreateDefaultSchemaCommandRetryPolicy(useRetry: false);

            var retryState = new RetryStateEx();

            retryState.LastError = new Exception();
            RetryPolicyFactory.DataConnectionFailureRetry(retryState);
            RetryPolicyFactory.CommandFailureRetry(retryState, "command");
            RetryPolicyFactory.CommandFailureIgnore(retryState, "command");
            RetryPolicyFactory.ElementCommandFailureIgnore(retryState);
            RetryPolicyFactory.ElementCommandFailureRetry(retryState);
            RetryPolicyFactory.CreateDatabaseCommandFailureIgnore(retryState);
            RetryPolicyFactory.CreateDatabaseCommandFailureRetry(retryState);
            RetryPolicyFactory.CommandFailureIgnore(retryState);
            RetryPolicyFactory.CommandFailureRetry(retryState);

            var transientPolicy = new RetryPolicyFactory.TransientErrorIgnoreStrategy();

            Assert.False(transientPolicy.CanRetry(new Exception()));
            Assert.False(transientPolicy.ShouldIgnoreError(new Exception()));
        }
Exemplo n.º 2
0
            protected override bool ShouldRetryImpl(RetryState retryStateObj)
            {
                Contract.Assert(retryStateObj is RetryStateEx);
                RetryStateEx retryState = (RetryStateEx)retryStateObj;

                // Calculate the delay as exponential value based on the number of retries.
                retryState.Delay =
                    RetryPolicyUtils.CalcExponentialRetryDelay(
                        retryState.RetryCount,
                        _intervalFactor,
                        _minInterval,
                        _maxInterval);

                // Add the delay to the total retry time
                retryState.TotalRetryTime = retryState.TotalRetryTime + retryState.Delay;

                // Calculate the maximum total retry time depending on how long ago was the task (this retry policy) started.
                // Longer running tasks are less eager to abort since, more work is has been done.
                TimeSpan totalRetryTimeLimit = checked (TimeSpan.FromMilliseconds(
                                                            Math.Max(
                                                                Math.Min(
                                                                    _stopwatch.ElapsedMilliseconds * _totalRetryTimeLimitRate,
                                                                    _maxTotalRetryTimeLimit.TotalMilliseconds),
                                                                _minTotalRetryTimeLimit.TotalMilliseconds)));

                if (retryState.TotalRetryTime <= totalRetryTimeLimit)
                {
                    return(true);
                }

                retryState.Delay = TimeSpan.Zero;
                return(false);
            }
Exemplo n.º 3
0
        public void FixedDelayPolicyTest()
        {
            TestFixedDelayPolicy policy = new TestFixedDelayPolicy(
                strategy: new NetworkConnectivityErrorDetectionStrategy(),
                maxRetryCount: 3,
                intervalBetweenRetries: TimeSpan.FromMilliseconds(100));
            var  retryState = new RetryStateEx();
            bool shouldRety = policy.InvokeShouldRetryImpl(retryState);

            policy.DoOnIgnoreErrorOccurred(retryState);
            Assert.True(shouldRety);
        }