public async Task Function_Can_Process_Multiple_Requests()
        {
            // Arrange
            void Configure(IServiceCollection services)
            {
                services.AddLogging((builder) => builder.AddXUnit(this));
            }

            using var server = new LambdaTestServer(Configure);
            using var cts    = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            await server.StartAsync(cts.Token);

            var channels = new List <(int expected, ChannelReader <LambdaTestResponse> reader)>();

            for (int i = 0; i < 10; i++)
            {
                var request = new MyRequest()
                {
                    Values = Enumerable.Range(1, i + 1).ToArray(),
                };

                string json = JsonSerializer.Serialize(request);

                channels.Add((request.Values.Sum(), await server.EnqueueAsync(json)));
            }

            _ = Task.Run(async() =>
            {
                foreach ((var _, var reader) in channels)
                {
                    await reader.WaitToReadAsync(cts.Token);
                }

                if (!cts.IsCancellationRequested)
                {
                    cts.Cancel();
                }
            });

            using var httpClient = server.CreateClient();

            // Act
            await MyFunctionEntrypoint.RunAsync(httpClient, cts.Token);

            // Assert
            foreach ((int expected, var channel) in channels)
            {
                channel.TryRead(out var response).ShouldBeTrue();

                response.ShouldNotBeNull();
                response.IsSuccessful.ShouldBeTrue();
                response.Content.ShouldNotBeNull();

                var deserialized = JsonSerializer.Deserialize <MyResponse>(response.Content);
                deserialized.Sum.ShouldBe(expected);
            }
        }
Esempio n. 2
0
        public static async Task Function_Can_Process_Request()
        {
            // Arrange - Create a test server for the Lambda runtime to use
            using var server = new LambdaTestServer();

            // Create a cancellation token that stops the server listening for new requests.
            // Auto-cancel the server after 2 seconds in case something goes wrong and the request is not handled.
            using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            // Start the test server so it is ready to listen for requests from the Lambda runtime
            await server.StartAsync(cancellationTokenSource.Token);

            // Create a test request for the Lambda function being tested
            var value = new MyRequest()
            {
                Values = new[] { 1, 2, 3 }, // The function returns the sum of the specified numbers
            };

            // Queue the request with the server to invoke the Lambda function and
            // store the ChannelReader into a variable to use to read the response.
            ChannelReader <LambdaTestResponse> reader = await server.EnqueueAsync(value);

            // Queue a task to stop the test server from listening as soon as the response is available
            _ = Task.Run(async() =>
            {
                await reader.WaitToReadAsync(cancellationTokenSource.Token);

                if (!cancellationTokenSource.IsCancellationRequested)
                {
                    cancellationTokenSource.Cancel();
                }
            });

            // Create an HttpClient for the Lambda to use with LambdaBootstrap
            using var httpClient = server.CreateClient();

            // Act - Start the Lambda runtime and run until the cancellation token is signalled
            await MyFunctionEntrypoint.RunAsync(httpClient, cancellationTokenSource.Token);

            // Assert - The channel reader should have the response available
            reader.TryRead(out LambdaTestResponse response).ShouldBeTrue("No Lambda response is available.");

            response.ShouldNotBeNull("The Lambda response is null.");
            response.IsSuccessful.ShouldBeTrue("The Lambda function failed to handle the request.");
            response.Content.ShouldNotBeNull("The Lambda function did not return any content.");

            string json   = Encoding.UTF8.GetString(response.Content);
            var    actual = JsonConvert.DeserializeObject <MyResponse>(json);

            actual.Sum.ShouldBe(6, "The Lambda function returned an incorrect response.");
        }
        public async Task Function_Can_Process_Request_With_Mobile_Sdk_Headers()
        {
            // Arrange
            void Configure(IServiceCollection services)
            {
                services.AddLogging((builder) => builder.AddXUnit(this));
            }

            using var server = new LambdaTestServer(Configure);
            using var cts    = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            await server.StartAsync(cts.Token);

            byte[] content = Encoding.UTF8.GetBytes(@"{""Values"": [ 1, 2, 3 ]}");

            var request = new LambdaTestRequest(content)
            {
                ClientContext   = "{}",
                CognitoIdentity = "{}",
            };

            var reader = await server.EnqueueAsync(request);

            _ = Task.Run(async() =>
            {
                await reader.WaitToReadAsync(cts.Token);

                if (!cts.IsCancellationRequested)
                {
                    cts.Cancel();
                }
            });

            using var httpClient = server.CreateClient();

            // Act
            await MyFunctionEntrypoint.RunAsync(httpClient, cts.Token);

            // Assert
            reader.TryRead(out var response).ShouldBeTrue();

            response.ShouldNotBeNull();
            response.IsSuccessful.ShouldBeTrue();
        }
        public async Task Function_Can_Handle_Failed_Request()
        {
            // Arrange
            void Configure(IServiceCollection services)
            {
                services.AddLogging((builder) => builder.AddXUnit(this));
            }

            using var server = new LambdaTestServer(Configure);
            using var cts    = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            await server.StartAsync(cts.Token);

            var reader = await server.EnqueueAsync(@"{""Values"": null}");

            _ = Task.Run(async() =>
            {
                await reader.WaitToReadAsync(cts.Token);

                if (!cts.IsCancellationRequested)
                {
                    cts.Cancel();
                }
            });

            using var httpClient = server.CreateClient();

            // Act
            await MyFunctionEntrypoint.RunAsync(httpClient, cts.Token);

            // Assert
            reader.TryRead(out var response).ShouldBeTrue();

            response.ShouldNotBeNull();
            response.IsSuccessful.ShouldBeFalse();
            response.Content.ShouldNotBeNull();
        }