Ejemplo n.º 1
0
 public void ConsiderRemainingLockTimeRatioGreaterThanRiskFactorAsASafeFinish()
 {
     behaviour = new BatchSizeGoverningBehaviour(0.5, NullLogger <BatchSizeGoverningBehaviour> .Instance, initialBatchSize: 1);
     Assert.IsTrue(
         behaviour.FinishedWithinSafeTime(
             IncomingMessageBuilder.BuildDefault()));
 }
Ejemplo n.º 2
0
 public void ConsiderRemainingLockTimeRatioLessThanRiskFactorAsAnUnsafeFinish()
 {
     behaviour = new BatchSizeGoverningBehaviour(0.5, NullLogger <BatchSizeGoverningBehaviour> .Instance, initialBatchSize: 1);
     Assert.IsFalse(
         behaviour.FinishedWithinSafeTime(
             IncomingMessageBuilder.BuildExpired()));
 }
        public async Task Setup()
        {
            cancellationTokenSource = new CancellationTokenSource();

            pipeline = new IncomingPipeline(
                mockMessageSource.Object,
                new[] { mockBehaviour.Object },
                mockServiceScopeFactory.Object,
                mockHandlerInvoker.Object,
                NullLogger <IncomingPipeline> .Instance,
                retryOptions);

            mockBehaviour
            .Setup(
                m => m.Process(
                    It.Is <IncomingMessage>(message => ((string)message.Body) == "throw this"),
                    It.IsAny <Context>(),
                    It.IsAny <IncomingPipelineAction>()))
            .Throws(new InvalidOperationException());

            messageCausingException = IncomingMessageBuilder.BuildWithBody("throw this");
            normalMessage           = IncomingMessageBuilder.BuildDefault();

            await pipeline
            .Initialise(CancellationToken.None)
            .ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public async Task InvokeTheNextBehaviourInTheChain()
        {
            await behaviour
            .Process(
                IncomingMessageBuilder.BuildDefault(),
                new Context(null),
                NextAction)
            .ConfigureAwait(false);

            Assert.IsTrue(nextActionWasCalled);
        }
 private async Task Process(int count)
 {
     for (var index = 0; index < count; index++)
     {
         await behaviour
         .Process(
             IncomingMessageBuilder.BuildDefault(),
             new Context(null),
             NextAction)
         .ConfigureAwait(false);
     }
 }
Ejemplo n.º 6
0
        public async Task GenerateNewCorrelationIdIfIncomingMessageDoesNotIncludeOne()
        {
            var message = IncomingMessageBuilder
                          .BuildDefault();

            await behaviour.Process(message, new Context(null), NextAction).ConfigureAwait(false);

            Assert.AreEqual(1, capturedHeaders.Length);
            Assert.AreEqual(SharedConstants.CorrelationIdHeaderName, capturedHeaders.First().HeaderName);
            Assert.IsTrue(Guid.TryParse(capturedHeaders.First().Value, out var generatedId));
            Assert.AreNotEqual(Guid.Empty, generatedId);
        }
        public async Task CallNextAction(bool loggingEnabled)
        {
            behaviour = new LoggingIncomingBehaviour(mockLogger.Object, LogLevel.Warning);

            var message = IncomingMessageBuilder.BuildDefault();

            mockLogger.Setup(m => m.IsEnabled(LogLevel.Warning)).Returns(loggingEnabled);

            await behaviour.Process(message, new Context(null), NextAction).ConfigureAwait(false);

            Assert.IsTrue(nextActionWasCalled);
        }
        public async Task LogMessageAtConfiguredLevel(LogLevel level)
        {
            behaviour = new LoggingIncomingBehaviour(mockLogger.Object, level);

            var message = IncomingMessageBuilder.BuildDefault();

            mockLogger.Setup(m => m.IsEnabled(level)).Returns(true);

            await behaviour.Process(message, new Context(null), NextAction).ConfigureAwait(false);

            mockLogger.VerifyLoggedMessageContains(level, "HANDLING ");
        }
Ejemplo n.º 9
0
        public async Task ReadCorrelationIdFromIncomingMessageHeader()
        {
            var message = IncomingMessageBuilder
                          .New()
                          .WithHeader(SharedConstants.CorrelationIdHeaderName, "expected id")
                          .Build();

            await behaviour.Process(message, new Context(null), NextAction).ConfigureAwait(false);

            Assert.AreEqual(1, capturedHeaders.Length);
            Assert.AreEqual(SharedConstants.CorrelationIdHeaderName, capturedHeaders.First().HeaderName);
            Assert.AreEqual("expected id", capturedHeaders.First().Value);
        }
        public async Task DeadLetterMessageIfHandlerThrowsExceptionMoreThanConfiguredDeferredRetriesCount()
        {
            var message = IncomingMessageBuilder.New()
                          .WithBody("throw this")
                          .WithDequeuedCount(retryOptions.MaximumImmediateAttempts + retryOptions.MaximumDeferredAttempts)
                          .Build();

            await Process(message).ConfigureAwait(false);

            mockMessageSource.VerifyDeadLetterCalledOnce(message, "Exception handling message");
            mockMessageSource.VerifyAbandonCalledNever(message);
            mockMessageSource.VerifyDeferCalledNever(message);
        }
        private List <Task> StartLongRunning(int count)
        {
            var tasks = new List <Task>();

            for (var index = 0; index < count; index++)
            {
                tasks.Add(
                    behaviour
                    .Process(
                        IncomingMessageBuilder.BuildDefault(),
                        new Context(null),
                        LongRunningNextAction));
            }

            return(tasks);
        }
        public async Task InvokeHandlersForMessage()
        {
            using (var serviceScope = serviceProvider.CreateScope())
            {
                var message = IncomingMessageBuilder.BuildWithBody(new ExampleEvent());

                await behaviour
                .Process(
                    message,
                    new Context(serviceScope))
                .ConfigureAwait(false);

                Assert.IsNotNull(mockHandler.CalledWith);
                Assert.AreSame(message.Body, mockHandler.CalledWith);
            }
        }
        public async Task NotLogWhenLoggingIsDisabled()
        {
            behaviour = new LoggingIncomingBehaviour(mockLogger.Object, LogLevel.Warning);

            var message = IncomingMessageBuilder.BuildDefault();

            mockLogger.Setup(m => m.IsEnabled(LogLevel.Warning)).Returns(false);

            await behaviour.Process(message, new Context(null), NextAction).ConfigureAwait(false);

            mockLogger
            .Verify(
                m => m.Log <object>(
                    It.IsAny <LogLevel>(),
                    It.IsAny <EventId>(), It.IsAny <object>(), It.IsAny <Exception>(),
                    It.IsAny <Func <object, Exception, string> >()),
                Times.Never);
        }
        public async Task ThrowExceptionIfMessageExceedsLockTimeDuringWaitPeriod()
        {
            var allowedTasks = StartLongRunning(MaximumConcurrency);
            var blockedTask  = behaviour.Process(
                IncomingMessageBuilder
                .New()
                .WithLockExpiry(DateTime.UtcNow, DateTime.UtcNow + TimeSpan.FromSeconds(0.25))
                .Build(),
                new Context(null),
                LongRunningNextAction);

            await Task.WhenAll(allowedTasks).ConfigureAwait(false);

            var exception = await Assert
                            .ThrowsExceptionAsync <MessageConcurrencyException>(() => blockedTask)
                            .ConfigureAwait(false);

            Assert.AreEqual($"The concurrent-processing limit of {MaximumConcurrency} messages was reached and a slot did not become available before the remaining lock time for this message was exceeded.", exception.Message);
        }
        public async Task NotWrapExceptionsInHandlersWithAnInvocationException()
        {
            using (var serviceScope = serviceProvider.CreateScope())
            {
                var message = IncomingMessageBuilder.BuildWithBody(new ExampleEvent());

                mockHandler.ThrowException = true;

                var exception = await Assert
                                .ThrowsExceptionAsync <InvalidOperationException>(
                    () => behaviour
                    .Process(
                        message,
                        new Context(serviceScope)))
                                .ConfigureAwait(false);

                Assert.AreEqual("Was told to throw.", exception.Message);
            }
        }