public async Task ProcessMessageContextAsync_Failed_Abandon_Test()
        {
            //Arrange
            var message = CreateMessage();
            var context = new TestMessageContext(message, _messageReceiver, _messageSender);

            _failurePolicy.CanHandle(Arg.Any <Exception>()).Returns(false);
            var exception = new Exception();

            _messageProcessor.ProcessMessageAsync(Arg.Any <Message>(), Arg.Any <CancellationToken>())
            .Throws(exception);

            //Act
            await _sut.ProcessMessageContextAsync(context, CancellationToken.None).ConfigureAwait(false);

            //Assert
            await _messageProcessor.Received().ProcessMessageAsync(Arg.Is(message), Arg.Is(CancellationToken.None))
            .ConfigureAwait(false);

            await _messageReceiver.DidNotReceiveWithAnyArgs().CompleteAsync(null).ConfigureAwait(false);

            await _messageReceiver.Received().AbandonAsync(Arg.Is(message.SystemProperties.LockToken)).ConfigureAwait(false);

            _failurePolicy.Received().CanHandle(Arg.Is(exception));
            await _failurePolicy.DidNotReceiveWithAnyArgs()
            .HandleFailureAsync(Arg.Any <MessageContext>(), Arg.Any <CancellationToken>()).ConfigureAwait(false);
        }
        public void CanHandle_Test()
        {
            //Act
            var result  = _sut.CanHandle(new Exception());
            var result2 = _sut.CanHandle(new InvalidOperationException());

            //Assert
            Assert.IsFalse(result);
            Assert.IsTrue(result2);
        }
示例#3
0
        public void CanHandle_Test()
        {
            //Act
            var result = _sut.CanHandle(new Exception());

            //Assert
            Assert.IsFalse(result);
        }
示例#4
0
        private async Task <bool> TryAbandonOnExceptionAsync(MessageContext context, Exception exception)
        {
            if (_failurePolicy.CanHandle(exception))
            {
                return(false);
            }

            await context.MessageReceiver.AbandonAsync(context.Message.SystemProperties.LockToken)
            .ConfigureAwait(false);

            return(true);
        }
示例#5
0
        public async Task ProcessMessageContextAsync(MessageContext context, CancellationToken cancellationToken = default)
        {
            try
            {
                await CheckForDeferredMessageAsync(context).ConfigureAwait(false);

                await _messageProcessor.ProcessMessageAsync(context.Message, cancellationToken).ConfigureAwait(false);

                await context.MessageReceiver.CompleteAsync(context.Message.SystemProperties.LockToken)
                .ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                if (!_failurePolicy.CanHandle(exception))
                {
                    await context.MessageReceiver.AbandonAsync(context.Message.SystemProperties.LockToken)
                    .ConfigureAwait(false);

                    return;
                }

                await _failurePolicy.HandleFailureAsync(context, cancellationToken).ConfigureAwait(false);
            }
        }