public async Task RunRunsInitializationServices(
            TestLambdaMessage expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            JsonSerializer serializer,
            ILogger logger,
            [Substitute] ILambdaInitializationService initializationService,
            [Substitute] TestLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            lambda.Handle(Any <TestLambdaMessage>(), Any <CancellationToken>()).Returns(expectedResponse);

            collection.AddSingleton <ISerializer>(serializer);
            collection.AddSingleton(initializationService);
            collection.AddSingleton(lambda);
            collection.AddSingleton(scope);

            using var inputStream = await CreateStreamForRequest(request);

            var provider = collection.BuildServiceProvider();

            await using var host = new TestLambdaHost(lambdaHost =>
            {
                lambdaHost.ServiceProvider           = provider;
                lambdaHost.RunInitializationServices = true;
                lambdaHost.Logger = logger;
            });

            var cancellationToken = new CancellationToken(false);
            await host.Run(inputStream, context, cancellationToken);

            await initializationService.Received().Initialize(Is(cancellationToken));
        }
        public async Task DisposeAsyncIsPreferred(
            string expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            ISerializer serializer,
            ILogger logger,
            [Substitute] ILambdaInitializationService initializationService,
            [Substitute] MultiDisposableLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            collection.AddSingleton(serializer);
            collection.AddSingleton(initializationService);
            collection.AddSingleton <TestLambda>(lambda);
            collection.AddSingleton(scope);

            using var inputStream = await CreateStreamForRequest(request);

            var provider = collection.BuildServiceProvider();

            await using (var host = new TestLambdaHost(lambdaHost =>
            {
                lambdaHost.ServiceProvider = provider;
                lambdaHost.RunInitializationServices = false;
                lambdaHost.Logger = logger;
            }))
            {
                await host.Run(inputStream, context);
            }

            await lambda.Received().DisposeAsync();

            lambda.DidNotReceive().Dispose();
        }
        public async Task RunSetsLambdaContextOnScope(
            TestLambdaMessage expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            ISerializer serializer,
            ILogger logger,
            [Substitute] ILambdaInitializationService initializationService,
            [Substitute] TestLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            lambda.Handle(Any <TestLambdaMessage>()).Returns(expectedResponse);

            collection.AddSingleton(serializer);
            collection.AddSingleton(initializationService);
            collection.AddSingleton(lambda);
            collection.AddSingleton(scope);

            using var inputStream = await CreateStreamForRequest(request);

            var provider = collection.BuildServiceProvider();

            await using var host = new TestLambdaHost(lambdaHost =>
            {
                lambdaHost.ServiceProvider           = provider;
                lambdaHost.RunInitializationServices = true;
                lambdaHost.Logger = logger;
            });

            await host.Run(inputStream, context);

            scope.LambdaContext.Should().Be(context);
        }
        public async Task ShouldLogExceptions(
            string expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            ISerializer serializer,
            TestLambda lambda,
            ILogger logger,
            Exception exception,
            [Substitute] Action <object> suppressor,
            [Substitute] ILambdaInitializationService initializationService,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            collection.AddSingleton(serializer);
            collection.AddSingleton(initializationService);
            collection.AddSingleton(lambda);
            collection.AddSingleton(scope);

            lambda.HandleAction = () => throw exception;

            using var inputStream = await CreateStreamForRequest(request);

            var provider = collection.BuildServiceProvider();

            await using var host = new TestLambdaHost(lambdaHost =>
            {
                lambdaHost.ServiceProvider           = provider;
                lambdaHost.RunInitializationServices = false;
                lambdaHost.SuppressFinalize          = suppressor;
                lambdaHost.Logger = logger;
            });

            Func <Task> func = () => host.Run(inputStream, context);

            await func.Should().ThrowAsync <Exception>();

            logger.ReceivedCalls().Should().Contain(call => (LogLevel)call.GetArguments()[0] == LogLevel.Critical);
        }
        public async Task FinalizationIsSuppressed(
            string expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            ISerializer serializer,
            ILogger logger,
            [Substitute] Action <object> suppressor,
            [Substitute] ILambdaInitializationService initializationService,
            [Substitute] MultiDisposableLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            collection.AddSingleton(serializer);
            collection.AddSingleton(initializationService);
            collection.AddSingleton <TestLambda>(lambda);
            collection.AddSingleton(scope);

            TestLambdaHost host;

            using var inputStream = await CreateStreamForRequest(request);

            var provider = collection.BuildServiceProvider();

            await using (host = new TestLambdaHost(lambdaHost =>
            {
                lambdaHost.ServiceProvider = provider;
                lambdaHost.RunInitializationServices = false;
                lambdaHost.SuppressFinalize = suppressor;
                lambdaHost.Logger = logger;
            }))
            {
                await host.Run(inputStream, context);
            }

            suppressor.Received()(Is(host));
        }