public async Task Run_Func_ExecutesTwice()
        {
            // arrange
            var next1    = new NextFunc();
            var next2    = new NextFunc();
            int executed = 0;

            Task Middleware(DispatchMiddlewareContext context, Func <Task> next)
            {
                executed++;
                return(next());
            }

            var descriptor = new TaskMiddlewareDescriptor(Middleware);
            var context    = CreateContext();

            // act
            await TaskMiddlewareRunner.RunAsync(descriptor, context, next1.InvokeAsync);

            await TaskMiddlewareRunner.RunAsync(descriptor, context, next2.InvokeAsync);

            // assert
            executed.Should().Be(2);
            next1.ExecutionCount.Should().Be(1);
            next2.ExecutionCount.Should().Be(1);
        }
        public async Task Run_Type_Executes(bool addMiddleware)
        {
            // arrange
            var next       = new NextFunc();
            var descriptor = TaskMiddlewareDescriptor.Create <TestMiddleware>();

            IServiceProvider serviceProvider = CreateServiceProvider(addMiddleware);
            var context = CreateContext(serviceProvider);

            // act
            await TaskMiddlewareRunner.RunAsync(descriptor, context, next.InvokeAsync);

            // assert
            var tracker = serviceProvider.GetRequiredService <ExecutionTracker>();

            tracker.ExecutionCount.Should().Be(1);
            next.ExecutionCount.Should().Be(1);
        }
 public async Task Run_NullNext_Throws()
 {
     var         descriptor = new TaskMiddlewareDescriptor((c, n) => Task.CompletedTask);
     Func <Task> act        = () => TaskMiddlewareRunner.RunAsync(descriptor, CreateContext(), null);
     await act.Should().ThrowExactlyAsync <ArgumentNullException>();
 }
 public async Task Run_NullDescriptor_Throws()
 {
     Func <Task> act = () => TaskMiddlewareRunner.RunAsync(null, CreateContext(), () => Task.CompletedTask);
     await act.Should().ThrowExactlyAsync <ArgumentNullException>();
 }
 private static Func <DispatchMiddlewareContext, Func <Task>, Task> WrapMiddleware(
     TaskMiddlewareDescriptor descriptor)
 {
     return((context, next) => TaskMiddlewareRunner.RunAsync(descriptor, context, next));
 }