コード例 #1
0
    public void TestDefaultRetryPolicyWithRetryableError()
    {
        RetryPolicy defaultPolicy = RetryPolicyFactory.GetDefaultSqlConnectionRetryPolicy();

        RetryManagerOptions  retryManagerOptions  = RetryConfiguration.GetConfiguration().GetSection(nameof(RetryManager)).Get <RetryManagerOptions>(buildOptions => buildOptions.BindNonPublicProperties = true);
        FixedIntervalOptions retryStrategyOptions = retryManagerOptions.RetryStrategy !.GetSection(retryManagerOptions.DefaultSqlConnectionRetryStrategy).Get <FixedIntervalOptions>(buildOptions => buildOptions.BindNonPublicProperties = true);

        int execCount = 0;

        try
        {
            defaultPolicy.ExecuteAction(() =>
            {
                execCount++;

                throw new TimeoutException("Forced Exception");
            });
        }
        catch (TimeoutException ex)
        {
            Assert.AreEqual("Forced Exception", ex.Message);
        }

        Assert.IsNotNull(retryStrategyOptions);
        Assert.AreEqual(retryStrategyOptions.RetryCount, execCount - 1, "The action was not retried using the expected amount of times");
    }
        public void NonExist()
        {
            const string          Key     = "NonExist";
            IConfigurationSection section = RetryConfiguration.GetConfiguration().GetSection("causeError").GetSection(Key);

            Assert.IsNotNull(section);
            Assert.IsFalse(section.Exists());
            try
            {
                section.GetFixedInterval();
                Assert.Fail();
            }
            catch (ArgumentException exception)
            {
                StringAssert.Contains(exception.Message, Key);
            }
            try
            {
                section.GetIncremental();
                Assert.Fail();
            }
            catch (ArgumentException exception)
            {
                StringAssert.Contains(exception.Message, Key);
            }
            try
            {
                section.GetExponentialBackoff();
                Assert.Fail();
            }
            catch (ArgumentException exception)
            {
                StringAssert.Contains(exception.Message, Key);
            }
        }
    public void NegativeMinBackoff()
    {
        const string          Key     = "NegativeMinBackoff";
        IConfigurationSection section = RetryConfiguration.GetConfiguration().GetSection("causeError").GetSection(Key);

        Assert.IsNotNull(section);
        Assert.IsTrue(section.Exists());
        try
        {
            section.GetExponentialBackoff();
            Assert.Fail();
        }
        catch (TargetInvocationException exception)
        {
            Assert.IsTrue(exception.InnerException is ArgumentOutOfRangeException {
                ParamName: "value"
            });
        public void ExponentialInterval_MissingDeltaBackoff()
        {
            const string          Key     = "ExponentialInterval_MissingDeltaBackoff";
            IConfigurationSection section = RetryConfiguration.GetConfiguration().GetSection("causeError").GetSection(Key);

            Assert.IsNotNull(section);
            Assert.IsTrue(section.Exists());
            try
            {
                section.GetExponentialBackoff();
                Assert.Fail();
            }
            catch (ArgumentException exception)
            {
                StringAssert.Contains(exception.Message, Key);
            }
        }
        public void FixedInterval_MissingRetryInterval()
        {
            const string          Key     = "FixedInterval_MissingRetryInterval";
            IConfigurationSection section = RetryConfiguration.GetConfiguration().GetSection("causeError").GetSection(Key);

            Assert.IsNotNull(section);
            Assert.IsTrue(section.Exists());
            try
            {
                section.GetFixedInterval();
                Assert.Fail();
            }
            catch (ArgumentException exception)
            {
                StringAssert.Contains(exception.Message, Key);
            }
        }
        public void MinBackoffGreaterThanMax()
        {
            const string          Key     = "MinBackoffGreaterThanMax";
            IConfigurationSection section = RetryConfiguration.GetConfiguration().GetSection("causeError").GetSection(Key);

            Assert.IsNotNull(section);
            Assert.IsTrue(section.Exists());
            try
            {
                section.GetExponentialBackoff();
                Assert.Fail();
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Assert.IsTrue(exception.Message.Contains(nameof(ExponentialBackoffOptions.MinBackOff), StringComparison.InvariantCultureIgnoreCase));
            }
        }
        public static string GetSetting(string settingName, string fileName = RetryConfiguration.DefaultConfigurationFile)
        {
            IConfiguration configuration = RetryConfiguration.GetConfiguration(fileName);

            return(configuration[settingName]);
        }
 public void Initialize() =>
 this.retryManagerOptions = RetryConfiguration.GetConfiguration().GetSection(nameof(RetryManager)).Get <RetryManagerOptions>();