public StopwatchMiddlewareTests(ITestOutputHelper outputHelper)
        {
            var loggerFactory = LoggerFactory.Create(lf => lf.AddXUnit(outputHelper));

            _handler = new InspectableHandler <OrderAccepted>();
            _monitor = new TrackingLoggingMonitor(loggerFactory.CreateLogger <TrackingLoggingMonitor>());
            var serviceResolver = new FakeServiceResolver(c =>
                                                          c.AddSingleton <IHandlerAsync <OrderAccepted> >(_handler)
                                                          .AddSingleton <IMessageMonitor>(_monitor));

            _middleware = new HandlerMiddlewareBuilder(serviceResolver, serviceResolver)
                          .UseHandler <OrderAccepted>()
                          .UseStopwatch(_handler.GetType())
                          .Build();
        }
Пример #2
0
            protected override void Given()
            {
                base.Given();

                var handler = new InspectableHandler <SimpleMessage>();

                var testResolver = new InMemoryServiceResolver(_outputHelper,
                                                               _messageMonitor,
                                                               sc => sc.AddSingleton <IHandlerAsync <SimpleMessage> >(handler));

                var middleware = new HandlerMiddlewareBuilder(testResolver, testResolver)
                                 .UseBackoff(MessageBackoffStrategy)
                                 .UseDefaults <SimpleMessage>(handler.GetType())
                                 .Build();

                _middlewareMap.Add <OrderAccepted>(_queue.QueueName, middleware);
            }
        public async Task Then_Both_Handlers_Receive_The_Message()
        {
            var messageLock = new MessageLockStore();

            var handler1 = new InspectableHandler <SimpleMessage>();
            var handler2 = new InspectableHandler <SimpleMessage>();

            var services = GivenJustSaying()
                           .AddSingleton <IMessageLockAsync>(messageLock)
                           .ConfigureJustSaying((builder) =>
                                                builder.WithLoopbackTopic <SimpleMessage>(UniqueName,
                                                                                          t => t.WithMiddlewareConfiguration(m =>
            {
                m.UseExactlyOnce <SimpleMessage>("some-key");
                m.UseDefaults <SimpleMessage>(handler1.GetType());
            })))
                           .AddJustSayingHandlers(new[] { handler1, handler2 });

            await WhenAsync(
                services,
                async (publisher, listener, serviceProvider, cancellationToken) =>
            {
                await listener.StartAsync(cancellationToken);
                await publisher.StartAsync(cancellationToken);

                var message = new SimpleMessage();

                // Act
                await publisher.PublishAsync(message, cancellationToken);

                // Assert
                await Patiently.AssertThatAsync(OutputHelper,
                                                () =>
                {
                    handler1.ReceivedMessages.ShouldHaveSingleItem().Id.ShouldBe(message.Id);
                    handler2.ReceivedMessages.ShouldHaveSingleItem().Id.ShouldBe(message.Id);
                });
            });
        }
        public async Task Then_The_Builder_Should_Put_User_Middlewares_In_The_Correct_Order()
        {
            // Arrange
            var handler         = new InspectableHandler <SimpleMessage>();
            var outerMiddleware = new OuterTestMiddleware();
            var innerMiddleware = new InnerTestMiddleware();
            var afterMiddleware = new AfterTestMiddleware();

            var services = GivenJustSaying()
                           .ConfigureJustSaying((builder) =>
                                                builder.WithLoopbackTopic <SimpleMessage>(UniqueName,
                                                                                          c => c.WithMiddlewareConfiguration(m =>
            {
                m.Use(outerMiddleware);
                m.Use(innerMiddleware);
                m.UseDefaults <SimpleMessage>(handler.GetType());
                m.Use(afterMiddleware);
            })))
                           .AddJustSayingHandlers(new[] { handler });

            string json = "";

            await WhenAsync(
                services,
                (_, listener, _, _) =>
            {
                dynamic interrogation = listener.Interrogate();
                dynamic middlewares   = interrogation.Data.Middleware;

                json = JsonConvert.SerializeObject(middlewares, Formatting.Indented)
                       .Replace(UniqueName, "TestQueueName");

                return(Task.CompletedTask);
            });

            json.ShouldMatchApproved(c => c.SubFolder($"Approvals"));
        }
        public async Task Then_The_Handler_Only_Receives_The_Message_Once()
        {
            // Arrange
            var messageLock = new MessageLockStore();
            var handler     = new InspectableHandler <SimpleMessage>();

            var services = GivenJustSaying()
                           .AddSingleton <IMessageLockAsync>(messageLock)
                           .ConfigureJustSaying((builder) =>
                                                builder.WithLoopbackTopic <SimpleMessage>(UniqueName,
                                                                                          c => c.WithMiddlewareConfiguration(m =>
                                                                                                                             m.UseExactlyOnce <SimpleMessage>("lock-simple-message")
                                                                                                                             .UseDefaults <SimpleMessage>(handler.GetType()))))
                           .AddJustSayingHandlers(new[] { handler });

            var    message = new SimpleMessage();
            string json    = "";

            await WhenAsync(
                services,
                async (publisher, listener, serviceProvider, cancellationToken) =>
            {
                await listener.StartAsync(cancellationToken);
                await publisher.StartAsync(cancellationToken);

                // Act
                await publisher.PublishAsync(message, cancellationToken);
                await publisher.PublishAsync(message, cancellationToken);

                dynamic middlewares = ((dynamic)listener.Interrogate().Data).Middleware;
                json = JsonConvert.SerializeObject(middlewares, Formatting.Indented)
                       .Replace(UniqueName, "TestQueueName");

                await Patiently.AssertThatAsync(() =>
                {
                    handler.ReceivedMessages.Where(m => m.Id.ToString() == message.UniqueKey())
                    .ShouldHaveSingleItem();
                });
            });

            json.ShouldMatchApproved(c => c.SubFolder("Approvals"));
        }