public void ZeroMinBackoff()
        {
            RetryPolicy retryPolicy = RetryPolicyFactory.GetRetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>("ZeroMinBackoff");
            int         execCount   = 0;

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    execCount++;
                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual <int>(4, execCount, "The action was not executed the expected amount of times");
        }
        public void DefaultRetryPolicyIsNoRetry()
        {
            int         count      = 0;
            RetryPolicy connPolicy = RetryPolicyFactory.GetRetryPolicy <MockErrorDetectionStrategy>();

            try
            {
                connPolicy.ExecuteAction(() =>
                {
                    count++;
                    throw new ApplicationException();
                });
            }
            catch (ApplicationException)
            {
                Assert.AreEqual(1, count);
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
Пример #3
0
        [Ignore]    // REVIEW
        public void ZeroRetryCount()
        {
            RetryPolicy retryPolicy   = RetryPolicyFactory.GetRetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>("ZeroRetryCount");
            int         execCount     = 0;
            double      totalDuration = 0;

            retryPolicy.Retrying += (sender, args) => totalDuration += args.Delay.TotalMilliseconds;

            try
            {
                retryPolicy.ExecuteAction(() =>
                {
                    execCount++;
                    throw new TimeoutException("Forced Exception");
                });
            }
            catch (TimeoutException ex)
            {
                Assert.AreEqual("Forced Exception", ex.Message);
            }

            Assert.AreEqual <int>(1, execCount, "The action was not executed the expected amount of times");
            Assert.AreEqual(0, totalDuration, "Unexpected duration of retry block");
        }
 protected override void Act()
 {
     this.retryPolicy = RetryPolicyFactory.GetRetryPolicy <AlwaysTransientErrorDetectionStrategy>("other");
 }
Пример #5
0
        private void ExecuteQueryButton_Click(object sender, EventArgs e)
        {
            int productId, attempts;

            if (!int.TryParse(this.ProductId.Text, out productId))
            {
                this.Log("Invalid product id " + this.ProductId.Text);
            }

            if (!int.TryParse(this.Attempts.Text, out attempts))
            {
                this.Log("Invalid attempts " + this.Attempts.Text);
            }

            var sessionId = Guid.NewGuid().ToString();

            try
            {
                this.ExecuteQueryButton.Enabled = false;
                Application.UseWaitCursor       = true;

                this.ClearLog();

                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ProductsSampleDB"].ConnectionString))
                {
                    connection.Open();

                    var command = new SqlCommand("dbo.GetProductDetails", connection)
                    {
                        CommandType = CommandType.StoredProcedure
                    };

                    // Add the input parameters
                    command.Parameters.AddWithValue("@ProductID", productId);
                    command.Parameters.AddWithValue("@SessionID", sessionId);
                    command.Parameters.AddWithValue("@NumberOfTriesBeforeSucceeding", attempts);

                    var policy = RetryPolicyFactory.GetRetryPolicy <HolSqlTransientErrorDetectionStrategy>("HOL Strategy");

                    policy.Retrying += (s, a) =>
                                       this.Log(string.Format(CultureInfo.CurrentCulture,
                                                              "Retry attempt {1} delay {2}: {0}",
                                                              a.LastException.Message,
                                                              a.CurrentRetryCount,
                                                              a.Delay
                                                              )
                                                );


                    // Execute the command
                    policy.ExecuteAction(
                        () =>
                    {
                        using (var reader = command.ExecuteReader())
                        {
                            // For each record
                            while (reader.Read())
                            {
                                // Log the result
                                this.Log(string.Format("Product name : {0} ", reader["ProductName"]));
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                this.Log(string.Format(CultureInfo.CurrentCulture, "Exception has occurred: {0}", ex.Message));
            }
            finally
            {
                this.ResetUi();
            }
        }
 public void ExceptionIsThrownWhenRetryStrategyIsNotDefinedInConfiguration()
 {
     RetryPolicyFactory.GetRetryPolicy <MockErrorDetectionStrategy>(retryStrategyName: "someinstancewhichdoesnotexist");
 }