Exemplo n.º 1
0
        public void When_limiting_account_concurrency_exceeded_then_should_not_get_an_instance()
        {
            var lambdaFunctionInfos = new Dictionary <string, LambdaFunctionInfo>();
            var lambdaFunctionInfo  = new LambdaFunctionInfo(
                "test",
                typeof(ReverseStringFunction),
                nameof(ReverseStringFunction.Reverse));

            lambdaFunctionInfos.Add(lambdaFunctionInfo.Name, lambdaFunctionInfo);
            var lambdaAccountPool = new LambdaAccountPool(1, lambdaFunctionInfos);
            var lambdaInstance1   = lambdaAccountPool.Get("test");

            var lambdaInstance2 = lambdaAccountPool.Get("test");

            lambdaInstance1.ShouldNotBeNull();
            lambdaInstance2.ShouldBeNull();
        }
Exemplo n.º 2
0
        public void If_function_does_not_exist_then_should_return_null()
        {
            var lambdaFunctionInfos = new Dictionary <string, LambdaFunctionInfo>();
            var lambdaAccountPool   = new LambdaAccountPool(1, lambdaFunctionInfos);

            var lambdaInstance = lambdaAccountPool.Get("test");

            lambdaInstance.ShouldBeNull();
        }
Exemplo n.º 3
0
        public void Can_return_an_instance()
        {
            var lambdaFunctionInfos = new Dictionary <string, LambdaFunctionInfo>();
            var lambdaFunctionInfo  = new LambdaFunctionInfo(
                "test",
                typeof(ReverseStringFunction),
                nameof(ReverseStringFunction.Reverse));

            lambdaFunctionInfos.Add(lambdaFunctionInfo.Name, lambdaFunctionInfo);
            var lambdaAccountPool = new LambdaAccountPool(1, lambdaFunctionInfos);
            var lambdaInstance1   = lambdaAccountPool.Get("test");

            lambdaAccountPool.Return(lambdaInstance1 !);

            var lambdaInstance2 = lambdaAccountPool.Get("test");

            lambdaInstance2.ShouldBe(lambdaInstance1);
        }
Exemplo n.º 4
0
            private async Task HandleInvocation(HttpContext ctx)
            {
                var logger       = ctx.RequestServices.GetRequiredService <ILoggerFactory>().CreateLogger <LambdaTestHost>();
                var functionName = (string)ctx.Request.RouteValues["functionName"] !;

                if (!_settings.Functions.TryGetValue(functionName, out var lambdaFunction))
                {
                    ctx.Response.StatusCode = 404;
                    return;
                }

                try
                {
                    using var streamReader = new StreamReader(ctx.Request.Body, Encoding.UTF8);
                    var payload = await streamReader.ReadToEndAsync();

                    var lambdaInstance = _lambdaAccountPool.Get(functionName);
                    if (lambdaInstance == null)
                    {
                        ctx.Response.StatusCode = 429;
                        return;
                    }

                    var settings = ctx.RequestServices.GetRequiredService <LambdaTestHostSettings>();

                    var context = settings.CreateContext();

                    var parameters = BuildParameters(lambdaFunction, context, payload);

                    _settings.InvocationOnStart.Set();
                    var lambdaReturnObject = lambdaFunction.HandlerMethod.Invoke(lambdaInstance !.FunctionInstance, parameters);
                    var responseBody       = await ProcessReturnAsync(lambdaFunction, lambdaReturnObject);

                    ctx.Response.StatusCode = 200;
                    if (responseBody != null)
                    {
                        await ctx.Response.WriteAsync(responseBody);
                    }

                    _lambdaAccountPool.Return(lambdaInstance);
                }
                catch (TargetInvocationException ex)
                {
                    logger.LogError(ex, "Error invoking function");
                    ctx.Response.StatusCode = 500;
                    // TODO response body error
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Error invoking function");
                    ctx.Response.StatusCode = 500;
                    // TODO response body error
                }
            }
Exemplo n.º 5
0
        public void Can_get_lambda_instance()
        {
            var lambdaFunctionInfos = new Dictionary <string, LambdaFunctionInfo>();
            var lambdaFunctionInfo  = new LambdaFunctionInfo(
                "test",
                typeof(ReverseStringFunction),
                nameof(ReverseStringFunction.Reverse));

            lambdaFunctionInfos.Add(lambdaFunctionInfo.Name, lambdaFunctionInfo);
            var lambdaAccountPool = new LambdaAccountPool(1000, lambdaFunctionInfos);

            var lambdaInstance = lambdaAccountPool.Get("test");

            lambdaInstance.ShouldNotBeNull();
        }