示例#1
0
        public async Task ChecksShortCircuitMiddlewareExecutionAsync()
        {
            #region Arrange

            var services    = new ServiceCollection();
            var registrator = new MiddlewareRegistrator();

            var messageMiddlewareRegistrator =
                registrator.AddFor <TestHandler, TestMessageDto>()
                .Add <ShortCircuitMiddleware>();

            var executor = new MiddlewareExecutor(registrator, services.BuildServiceProvider());

            #endregion Arrange

            #region Act

            var acknowledgement = await executor.ExecuteAsync(
                typeof(TestHandler),
                new MessageContext <TestMessageDto>(new TestMessageDto(), new MqMessageData()));

            #endregion Act

            #region Assert

            Assert.Multiple(() =>
            {
                Assert.IsInstanceOf <Reject>(acknowledgement);
                Assert.AreEqual("stop", (acknowledgement as Reject)?.Reason);
                Assert.AreEqual(typeof(TestMessageDto), messageMiddlewareRegistrator.MessageType);
            });

            #endregion Assert
        }
示例#2
0
        public async Task ChecksGeneralMiddlewareExecutionPipelineAsync()
        {
            #region Arrange

            var services    = new ServiceCollection();
            var registrator = new MiddlewareRegistrator();

            registrator.AddFor <TestHandler, TestMessageDto>().Add <TestMiddleware>();
            var sp = services.BuildServiceProvider();

            var executor       = new MiddlewareExecutor(registrator, sp);
            var testMessageDto = new TestMessageDto();

            #endregion Arrange

            #region Act

            var acknowledgement = await executor.ExecuteAsync(
                typeof(TestHandler),
                new MessageContext <TestMessageDto>(testMessageDto, new MqMessageData()));

            #endregion Act

            #region Assert

            Assert.Multiple(() =>
            {
                var middleware = sp.GetService <TestMiddleware>();

                Assert.IsNull(middleware, "Middleware не должен быть в DI.");
                Assert.IsNotNull(testMessageDto.HandlerExecutedAt, "Handler не был выполнен.");
                Assert.IsNotNull(testMessageDto.MiddlewareExecutedAt, "Middleware не был выполнен.");

                Assert.IsTrue(testMessageDto.MiddlewareExecutedAt < testMessageDto.HandlerExecutedAt, "Middleware должен выполниться раньше, чем хендер.");
                Assert.IsInstanceOf <Ack>(acknowledgement);
            });

            #endregion Assert
        }