public async Task ThreeMiddlewares_ShouldExecuteInCorrectOrder()
        {
            var callRecord = new List <string>();

            void Before(string id) => callRecord.Add($"Before_{id}");
            void After(string id) => callRecord.Add($"After_{id}");

            var outer  = new TrackingMiddleware("outer", Before, After);
            var middle = new TrackingMiddleware("middle", Before, After);
            var inner  = new TrackingMiddleware("inner", Before, After);

            var middleware = new HandlerMiddlewareBuilder(_resolver, _resolver)
                             .Configure(pipe =>
            {
                pipe.Use(outer);
                pipe.Use(middle);
                pipe.Use(inner);
            }).Build();

            var context = new HandleMessageContext(new SimpleMessage(),
                                                   typeof(SimpleMessage),
                                                   "a-fake-queue");

            await middleware.RunAsync(context,
                                      ct =>
            {
                callRecord.Add("HandledMessage");
                return(Task.FromResult(true));
            },
                                      CancellationToken.None);

            var record = string.Join(Environment.NewLine, callRecord);

            record.ShouldMatchApproved(c => c.SubFolder("Approvals"));
        }
        public async Task WhenMessageIsLockedByAnotherHandler_MessageWillBeLeftInTheQueue()
        {
            var messageLock = new FakeMessageLock(false);

            var testResolver = new InMemoryServiceResolver(sc => sc
                .AddLogging(l =>
                    l.AddXUnit(_outputHelper))
                .AddSingleton<IMessageLockAsync>(messageLock));

            var monitor = new TrackingLoggingMonitor(LoggerFactory.Create(lf => lf.AddXUnit()).CreateLogger<TrackingLoggingMonitor>());
            var handler = new InspectableHandler<OrderAccepted>();

            var middleware = new HandlerMiddlewareBuilder(testResolver, testResolver)
                .UseExactlyOnce<OrderAccepted>(nameof(InspectableHandler<OrderAccepted>),
                    TimeSpan.FromSeconds(1))
                .UseHandler(ctx => handler)
                .Build();

            var context = TestHandleContexts.From<OrderAccepted>();

            var result = await middleware.RunAsync(context, null, CancellationToken.None);

            handler.ReceivedMessages.ShouldBeEmpty();
            result.ShouldBeFalse();
        }