public void CanHandle_WithMaxFailedAttempts_ExpectedResultReturned( int failedAttempts, bool expectedResult) { var envelope = new InboundEnvelope( new MemoryStream(), new[] { new MessageHeader( DefaultMessageHeaders.FailedAttempts, failedAttempts.ToString(CultureInfo.InvariantCulture)) }, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name); var policy = new TestErrorPolicy() .MaxFailedAttempts(3) .Build(Substitute.For<IServiceProvider>()); var canHandle = policy.CanHandle( ConsumerPipelineContextHelper.CreateSubstitute(envelope), new InvalidOperationException()); canHandle.Should().Be(expectedResult); }
public async Task HandleError_WithPublish_MessagePublished() { var publisher = Substitute.For <IPublisher>(); var serviceProvider = new ServiceCollection().AddScoped(_ => publisher) .BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true }); var policy = new TestErrorPolicy() .Publish(_ => new TestEventTwo { Content = "aaa" }) .Build(serviceProvider); var envelope = new InboundEnvelope( new MemoryStream(), new[] { new MessageHeader(DefaultMessageHeaders.FailedAttempts, "3") }, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name); await policy.HandleErrorAsync( ConsumerPipelineContextHelper.CreateSubstitute(envelope, serviceProvider), new ArgumentNullException()); await publisher.Received().PublishAsync(Arg.Any <TestEventTwo>()); }
public async Task HandleErrorAsync_RetryWithMaxFailedAttempts_AppliedAccordingToMaxFailedAttempts(int failedAttempts) { var rawMessage = new MemoryStream(); var headers = new[] { new MessageHeader( DefaultMessageHeaders.FailedAttempts, failedAttempts.ToString(CultureInfo.InvariantCulture)) }; var testPolicy = new TestErrorPolicy(); var chain = new ErrorPolicyChain( new[] { new RetryErrorPolicy().MaxFailedAttempts(3), testPolicy }) .Build(_serviceProvider); await chain.HandleErrorAsync( ConsumerPipelineContextHelper.CreateSubstitute( new InboundEnvelope( rawMessage, headers, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name)), new InvalidOperationException("test")); testPolicy.Applied.Should().Be(failedAttempts > 3); }
public void CanHandle_Whatever_TrueReturned(int failedAttempts) { var rawMessage = new MemoryStream(); var headers = new[] { new MessageHeader( DefaultMessageHeaders.FailedAttempts, failedAttempts.ToString(CultureInfo.InvariantCulture)) }; var testPolicy = new TestErrorPolicy(); var chain = new ErrorPolicyChain( new RetryErrorPolicy().MaxFailedAttempts(3), testPolicy) .Build(_serviceProvider); var result = chain.CanHandle( ConsumerPipelineContextHelper.CreateSubstitute( new InboundEnvelope( rawMessage, headers, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name)), new InvalidOperationException("test")); result.Should().BeTrue(); }
public void ApplyTo_Exception_CanHandleReturnsExpectedResult(Exception exception, bool mustApply) { var policy = new TestErrorPolicy() .ApplyTo <ArgumentException>() .ApplyTo <InvalidCastException>(); var canHandle = policy.CanHandle(new InboundMessage { Message = new TestEventOne(), FailedAttempts = 99 }, exception); canHandle.Should().Be(mustApply); }
public void HandleError_RetryWithMaxFailedAttempts_AppliedAccordingToMaxFailedAttempts(int failedAttempts) { var testPolicy = new TestErrorPolicy(); var chain = _errorPolicyBuilder.Chain( _errorPolicyBuilder.Retry().MaxFailedAttempts(3), testPolicy); chain.HandleError(new InboundMessage { Message = new TestEventOne(), FailedAttempts = failedAttempts }, new Exception("test")); testPolicy.Applied.Should().Be(failedAttempts > 3); }
public void ApplyTo_Exception_CanHandleReturnsExpectedResult(Exception exception, bool mustApply) { var policy = new TestErrorPolicy() .ApplyTo <ArgumentException>() .ApplyTo <InvalidCastException>(); var canHandle = policy.CanHandle( new InboundMessage( new byte[1], new[] { new MessageHeader(MessageHeader.FailedAttemptsKey, "99") }, null, TestEndpoint.GetDefault(), true), exception); canHandle.Should().Be(mustApply); }
public void CanHandle_WithApplyWhen_ExpectedResultReturned( IInboundEnvelope envelope, Exception exception, bool mustApply) { var policy = new TestErrorPolicy() .ApplyWhen( (msg, ex) => msg.Headers.GetValue <int>(DefaultMessageHeaders.FailedAttempts) <= 5 && ex.Message != "no") .Build(Substitute.For <IServiceProvider>()); var canHandle = policy.CanHandle( ConsumerPipelineContextHelper.CreateSubstitute(envelope), exception); canHandle.Should().Be(mustApply); }
public void HandleError_RetryWithMaxFailedAttempts_AppliedAccordingToMaxFailedAttempts(int failedAttempts) { var testPolicy = new TestErrorPolicy(); var chain = _errorPolicyBuilder.Chain( _errorPolicyBuilder.Retry().MaxFailedAttempts(3), testPolicy); chain.HandleError(new[] { new InboundMessage( new byte[1], new[] { new MessageHeader(MessageHeader.FailedAttemptsKey, failedAttempts.ToString()) }, null, TestEndpoint.GetDefault(), true) }, new Exception("test")); testPolicy.Applied.Should().Be(failedAttempts > 3); }
public void CanHandle_WithApplyTo_ExpectedResultReturned(Exception exception, bool mustApply) { var policy = new TestErrorPolicy() .ApplyTo <ArgumentException>() .ApplyTo <InvalidCastException>() .Build(Substitute.For <IServiceProvider>()); var canHandle = policy.CanHandle( ConsumerPipelineContextHelper.CreateSubstitute( new InboundEnvelope( new MemoryStream(), new[] { new MessageHeader(DefaultMessageHeaders.FailedAttempts, "99") }, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name)), exception); canHandle.Should().Be(mustApply); }