コード例 #1
0
        public void GetsRejectUpdateInsertThrottlingConditionFromSqlError()
        {
            SqlError            sqlError  = SqlExceptionCreator.GenerateFakeSqlError(ThrottlingCondition.ThrottlingErrorNumber, "Code: 12345 SQL Error");
            ThrottlingCondition condition = ThrottlingCondition.FromError(sqlError);

            Assert.AreEqual(ThrottlingMode.RejectUpdateInsert, condition.ThrottlingMode);
        }
        public void RetriesWhenSqlExceptionIsThrownWithNetworkLevelError()
        {
            int executeCount = 0;

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

                    SqlException ex = SqlExceptionCreator.CreateSqlException("A network-related or instance-specific error occurred while establishing a connection to SQL Server.", 10054);
                    throw ex;
                });

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

            Assert.AreEqual(6, executeCount);
        }
        public void DoesNotRetryWhenSqlExceptionIsThrownWithSqlQueryError()
        {
            int executeCount = 0;

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

                    SqlException ex = SqlExceptionCreator.CreateSqlException("ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.", 104);
                    throw ex;
                });

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

            Assert.AreEqual(1, executeCount);
        }
        public void RetriesWhenSqlExceptionIsThrownWithTransportLevelError()
        {
            int executeCount = 0;

            try
            {
                RetryPolicy <SqlDatabaseTransientErrorDetectionStrategy> retryPolicy = this.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);
        }
コード例 #5
0
        public Task Handle(PlaceOrder message, IMessageHandlerContext context)
        {
            logger.Info($"Received PlaceOrder, OrderId = {message.OrderId}");

            LogTrace(message);

            // This is normally where some business logic would occur

            // 25% of messages always fail with a missing parameter error - persistent
            // all messages fail 10% of the time with a deadlock - transient
            // all messages fail 10% of the time with a FraudDetectionUnavailableException - transient
            var d = message.GetHashCode();

            if (d > (int.MaxValue / 4) * 3)
            {
                throw SqlExceptionCreator.NewSqlException(137); // Must declare the scalar variable - PERSISTENT
            }
            else if (new Random(Guid.NewGuid().GetHashCode()).NextDouble() > 0.90)
            {
                throw SqlExceptionCreator.NewSqlException(1205); // deadlock - TRANSIENT
            }
            else if (new Random(Guid.NewGuid().GetHashCode()).NextDouble() > 0.80)
            {
                throw new FraudDetectionUnavailableException("Fraud detection system is down for a short time"); // SEMI TRANSIENT
            }

            var orderPlaced = new OrderPlaced
            {
                OrderId = message.OrderId
            };

            return(context.Publish(orderPlaced));
        }
 public LevyTransactionBatchStorageServiceFixture With_RepositoryThrowingSqlException(int number)
 {
     mockLevyTransactionRepository
     .Setup(x => x.SaveLevyTransactions(It.IsAny <List <LevyTransactionModel> >(),
                                        It.IsAny <CancellationToken>()))
     .ThrowsAsync(SqlExceptionCreator.NewSqlException(number));
     return(this);
 }
コード例 #7
0
        public void Create_ShouldNotReturnNull()
        {
            var fixture   = new Fixture();
            var message   = fixture.Create <string>();
            var errorCode = fixture.Create <int>();

            System.Data.SqlClient.SqlException exception = SqlExceptionCreator.Create(message, errorCode);

            Assert.That(exception, Is.Not.Null);
        }
コード例 #8
0
        public void Create_ShouldCreateSqlException()
        {
            var fixture   = new Fixture();
            var message   = fixture.Create <string>();
            var errorCode = fixture.Create <int>();

            System.Data.SqlClient.SqlException exception = SqlExceptionCreator.Create(message, errorCode);

            Assert.That(exception.Message, Is.EqualTo(message));
            Assert.That(exception.Number, Is.EqualTo(errorCode));
        }
コード例 #9
0
        public void MonitoringMiddleware_Returns_JsonDetail_And_404_On_Specific_SqlException()
        {
            int expectedHttpCode = 404;
            Mock <IFrameworkTracer> frameworkTracerMock = new Mock <IFrameworkTracer>();
            SqlException            expectedException   = SqlExceptionCreator.NewSqlException(60000);

            _requestDelegateMock.Setup(rd => rd(It.IsAny <HttpContext>())).Throws(expectedException);
            var httpResponseMock = GetHttpResponseMock();
            var httpContextMock  = GetHttpContextMock(responseMock: httpResponseMock);

            var systemUnderTest = new MonitoringMiddleware(_requestDelegateMock.Object, frameworkTracerMock.Object);

            systemUnderTest.InvokeAsync(httpContextMock.Object).GetAwaiter().GetResult();

            httpResponseMock.VerifySet(t => t.StatusCode = expectedHttpCode);
        }
        public Task Handle(CancelOrder message, IMessageHandlerContext context)
        {
            logger.Info($"Received CancelOrder, OrderId = {message.OrderId}");

            // This is normally where some business logic would occur

            // SqlException is: Must declare the scalar variable - this will happen everytime
            // but the policy related to thse messages is a Transient By Default
            // and OrderNotFoundException are treated as persistent
            // so these messages will be retried up to the limit
            SqlExceptionCreator.NewSqlException(137);

            var orderCancelled = new OrderCancelled
            {
                OrderId = message.OrderId
            };

            return(context.Publish(orderCancelled));
        }