Esempio n. 1
0
    public void RetriesWhenSqlExceptionIsThrownWithTransportLevelError()
    {
        int executeCount = 0;

        try
        {
            RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy> retryPolicy = RetryManager.GetRetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>("Retry 5 times");
            retryPolicy.ExecuteAction(() =>
            {
                executeCount++;

                SqlException ex = SqlExceptionCreator.CreateSqlException("A transport-level error has occurred when sending the request to the server", 10053);
                throw ex;
            });

            Assert.Fail("Should have thrown SqlException");
        }
        catch (SqlException)
        { }
        catch (Exception)
        {
            Assert.Fail("Should have thrown SqlException");
        }

        Assert.AreEqual(6, executeCount);
    }
    public void OpensConnectionWithRetryPolicy()
    {
        using ReliableSqlConnection reliableConnection = new(TestDatabase.TransientFaultHandlingTestDatabase);

        try
        {
            RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy> retryPolicy = RetryManager.GetRetryPolicy <SqlDatabaseTransientErrorDetectionStrategy>("Retry 5 times");
            retryPolicy.ExecuteAction(() => reliableConnection.Open(retryPolicy));
        }
        catch (SqlException)
        { }
        catch (Exception)
        {
            Assert.Fail();
        }
    }
        public void ExceptionIsThrownWhenAllRetriesFail()
        {
            var policy = retryManager.GetRetryPolicy <MockErrorDetectionStrategy>("Exponential Backoff, 5 retries");

            int count  = 0;
            int count1 = 0;

            policy.Retrying += (s, args) =>
            {
                Assert.AreEqual(count, args.CurrentRetryCount);
                Assert.IsInstanceOfType(args.LastException, typeof(InvalidCastException));
                count1 = args.CurrentRetryCount;
            };

            try
            {
                policy.ExecuteAction(() =>
                {
                    ++count;
                    throw new InvalidCastException();
                });
            }
            catch (Exception)
            {
                Assert.AreEqual <int>(6, count);
                Assert.AreEqual <int>(5, count1);
                throw;
            }

            Assert.Fail("Test should throw");
        }
Esempio n. 4
0
 public static RetryPolicy GetRetryPolicy(this RetryManager retryManager)
 {
     if (retryManager == null)
     {
         throw new ArgumentNullException("retryManager");
     }
     return(retryManager.GetRetryPolicy <AcceptAllErrorsDetectionStrategy>());
 }
    public void TransactionIsCommittedWhenSomeRetriesFailAndThenSucceeds()
    {
        this.DeleteAllOnTransactionScopeTestTable();

        int retryTransactionCount = 0;
        RetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy> policyForTransaction = RetryManager.GetRetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy>("Retry 5 times");

        policyForTransaction.Retrying += (_, _) => retryTransactionCount++;

        int retrySqlCommandCount = 0;
        RetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy> policyForSqlCommand = RetryManager.GetRetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy>("Retry 2 times, first retry is fast");

        policyForSqlCommand.Retrying += (_, _) => retrySqlCommandCount++;

        int    transactionActionExecutedCount = 0;
        Action action = () =>
        {
            transactionActionExecutedCount++;

            using SqlConnection connection = new(TestDatabase.TransientFaultHandlingTestDatabase);
            using SqlCommand command1      = connection.CreateCommand();
            command1.CommandType           = CommandType.Text;
            command1.CommandText           = "Insert Into TranscationScopeTestTable (rowId) Values (@rowId);";
            command1.Connection            = connection;
            command1.Parameters.Add(new SqlParameter("rowId", SqlDbType.UniqueIdentifier)
            {
                Value = Guid.NewGuid()
            });
            command1.ExecuteNonQueryWithRetry(policyForSqlCommand);

            if (retryTransactionCount < 4)
            {
                using SqlCommand command2 = connection.CreateCommand();
                command2.CommandType      = CommandType.StoredProcedure;
                command2.CommandText      = "ErrorRaisingForExecuteNonQuery";
                command2.Connection       = connection;
                command2.Parameters.Add(new SqlParameter("rowId", SqlDbType.UniqueIdentifier)
                {
                    Value = Guid.NewGuid()
                });
                command2.Parameters.Add(new SqlParameter("maxCountToRaiseErrors", SqlDbType.Int)
                {
                    Value = 10
                });
                command2.Parameters.Add(new SqlParameter("error", SqlDbType.Int)
                {
                    Value = 60000
                });
                command2.ExecuteNonQueryWithRetry(policyForSqlCommand);
            }
        };

        using (TransactionRetryScope scope = new(policyForTransaction, action))
        {
            try
            {
                scope.InvokeUnitOfWork();
                scope.Complete();
            }
            catch (Exception)
            {
                Assert.Fail("Should not throw");
            }
        }

        Assert.AreEqual(1, this.GetCountOnTransactionScopeTestTable());
        Assert.AreEqual(5, transactionActionExecutedCount);
        Assert.AreEqual(4, retryTransactionCount);
        Assert.AreEqual(8, retrySqlCommandCount);
    }
    public void ThrowsExceptionWhenAllRetriesFailDuringReaderExecutionWithRetryPolicy()
    {
        using SqlConnection connection = new(TestDatabase.TransientFaultHandlingTestDatabase);
        RetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy> policy = RetryManager.GetRetryPolicy <FakeSqlAzureTransientErrorDetectionStrategy>("Retry 5 times");

        connection.Open();
        SqlCommand command = new();
        int        count   = 0;

        policy.Retrying += (_, args) => count = args.CurrentRetryCount;

        try
        {
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "ErrorRaisingReader";
            command.Connection  = connection;
            command.Parameters.Add(new SqlParameter("rowId", SqlDbType.UniqueIdentifier)
            {
                Value = Guid.NewGuid()
            });
            command.Parameters.Add(new SqlParameter("maxCountToRaiseErrors", SqlDbType.Int)
            {
                Value = 7
            });
            command.Parameters.Add(new SqlParameter("error", SqlDbType.Int)
            {
                Value = 60000
            });
            using SqlDataReader reader = command.ExecuteReaderWithRetry(policy);
            while (reader.Read())
            {
            }
        }
        catch (Exception)
        {
            Assert.AreEqual(5, count);
            connection.Close();
            Assert.AreEqual(ConnectionState.Closed, command.Connection.State);
            throw;
        }

        Assert.Fail("Test should throw");
    }