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 RunCreatesLambdaAndCallsHandle(
            TestLambdaMessage expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            JsonSerializer serializer,
            ILogger logger,
            [Substitute] TestLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            lambda.Handle(Any <TestLambdaMessage>(), Any <CancellationToken>()).Returns(expectedResponse);
            collection.AddSingleton <ISerializer>(serializer);
            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.Logger          = logger;
            });

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

            var response = await SystemTextJsonSerializer.DeserializeAsync <TestLambdaMessage>(responseStream);

            response.Should().NotBeNull();
            response !.Id.Should().Be(expectedResponse.Id);
            await lambda.Received().Handle(Is <TestLambdaMessage>(req => req.Id == request.Id), Is(cancellationToken));
        }
        public async Task RunDisposesInitializationServicesAsync(
            TestLambdaMessage expectedResponse,
            TestLambdaMessage request,
            ServiceCollection collection,
            ISerializer serializer,
            ILogger logger,
            [Substitute] TestLambda lambda,
            [Substitute] LambdaScope scope,
            [Substitute] ILambdaContext context
            )
        {
            var initializationService = Substitute.For <ILambdaInitializationService, IAsyncDisposable>();

            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);

            await initializationService.As <IAsyncDisposable>().Received().DisposeAsync();
        }
        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);
        }