public async Task HandleErrorAsync_Whatever_ConsumerRolledBackAndTransactionAborted() { var policy = new RetryErrorPolicy().MaxFailedAttempts(3).Build(_serviceProvider); var envelope = new InboundEnvelope( "hey oh!", new MemoryStream(), null, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name); var transactionManager = Substitute.For <IConsumerTransactionManager>(); await policy.HandleErrorAsync( ConsumerPipelineContextHelper.CreateSubstitute( envelope, _serviceProvider, transactionManager), new InvalidOperationException("test")); await transactionManager.Received(1).RollbackAsync( Arg.Any <InvalidOperationException>(), false, true, false); }
public void CanHandle_WithDifferentFailedAttemptsCount_ReturnReflectsMaxFailedAttempts( int failedAttempts, bool expectedResult) { var policy = new RetryErrorPolicy().MaxFailedAttempts(3).Build(_serviceProvider); var rawMessage = new MemoryStream(); var headers = new[] { new MessageHeader( DefaultMessageHeaders.FailedAttempts, failedAttempts.ToString(CultureInfo.InvariantCulture)) }; var envelope = new InboundEnvelope( rawMessage, headers, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name); var canHandle = policy.CanHandle( ConsumerPipelineContextHelper.CreateSubstitute(envelope, _serviceProvider), new InvalidOperationException("test")); canHandle.Should().Be(expectedResult); }
public void OnError_ErrorPolicy_ErrorPolicySet() { var builder = new TestConsumerEndpointBuilder(); var errorPolicy = new RetryErrorPolicy(); var endpoint = builder.OnError(errorPolicy).Build(); endpoint.ErrorPolicy.Should().BeSameAs(errorPolicy); }
public async Task HandleErrorAsync_Whatever_TrueReturned() { var policy = new RetryErrorPolicy().MaxFailedAttempts(3).Build(_serviceProvider); var envelope = new InboundEnvelope( "hey oh!", new MemoryStream(), null, new TestOffset(), TestConsumerEndpoint.GetDefault(), TestConsumerEndpoint.GetDefault().Name); var result = await policy.HandleErrorAsync( ConsumerPipelineContextHelper.CreateSubstitute(envelope, _serviceProvider), new InvalidOperationException("test")); result.Should().BeTrue(); }
/// <inheritdoc cref="IErrorPolicyChainBuilder.ThenRetry(int?, TimeSpan?, TimeSpan?, Action{RetryErrorPolicy}?)" /> public IErrorPolicyChainBuilder ThenRetry( int?retriesCount = null, TimeSpan?initialDelay = null, TimeSpan?delayIncrement = null, Action <RetryErrorPolicy>?policyConfigurationAction = null) { var policy = new RetryErrorPolicy(initialDelay, delayIncrement); if (retriesCount.HasValue) { policy.MaxFailedAttempts(retriesCount); } policyConfigurationAction?.Invoke(policy); _errorPolicies.Add(policy); return(this); }