public LambdaBootstrapTests()
 {
     _environmentVariables = new TestEnvironmentVariables();
     _testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables);
     _testInitializer      = new TestInitializer();
     _testFunction         = new TestHandler();
 }
 public LambdaBootstrapTests()
 {
     _environmentVariables = new TestEnvironmentVariables();
     _testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables);
     _testInitializer      = new TestInitializer();
     _testFunction         = new TestHandler();
     _testWrapper          = HandlerWrapper.GetHandlerWrapper(_testFunction.HandlerVoidVoidSync);
 }
Exemplo n.º 3
0
        private async Task <Exception> TestHandlerFailAsync(string handler, string expectedPartialMessage, bool?expectAggregateException = null)
        {
            _output.WriteLine($"Testing handler {handler}");

            var testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables, _headers);

            var userCodeLoader = new UserCodeLoader(handler, _internalLogger);
            var initializer    = new UserCodeInitializer(userCodeLoader, _internalLogger);
            var handlerWrapper = HandlerWrapper.GetHandlerWrapper(userCodeLoader.Invoke);
            var bootstrap      = new LambdaBootstrap(handlerWrapper, initializer.InitializeAsync)
            {
                Client = testRuntimeApiClient
            };

            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                var exceptionWaiterTask = Task.Run(() =>
                {
                    _output.WriteLine($"Waiting for an exception.");
                    while (testRuntimeApiClient.LastRecordedException == null)
                    {
                    }
                    _output.WriteLine($"Exception available.");
                    cancellationTokenSource.Cancel();
                    return(testRuntimeApiClient.LastRecordedException);
                });

                await Record.ExceptionAsync(async() =>
                {
                    await bootstrap.RunAsync(cancellationTokenSource.Token);
                });

                var exception = await exceptionWaiterTask;
                Assert.NotNull(exception);

                Common.CheckException(exception, expectedPartialMessage);
                Common.CheckForAggregateException(exception, expectAggregateException);
                return(exception);
            }
        }
Exemplo n.º 4
0
        public LambdaBootstrapTests()
        {
            _environmentVariables = new TestEnvironmentVariables();
            var headers = new Dictionary <string, IEnumerable <string> >
            {
                {
                    RuntimeApiHeaders.HeaderAwsRequestId, new List <string> {
                        "request_id"
                    }
                },
                {
                    RuntimeApiHeaders.HeaderInvokedFunctionArn, new List <string> {
                        "invoked_function_arn"
                    }
                }
            };

            _testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables, headers);
            _testInitializer      = new TestInitializer();
            _testFunction         = new TestHandler();
            _testWrapper          = HandlerWrapper.GetHandlerWrapper(_testFunction.HandlerVoidVoidSync);
        }
Exemplo n.º 5
0
        private async Task <ExecutionInfo> ExecHandlerAsync(string handler, string dataIn, string assertLoggedByInitialize)
        {
            // The actionWriter
            using (var actionWriter = new StringWriter())
            {
                var testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables, _headers);
                var loggerAction         = actionWriter.ToLoggingAction();
                var assembly             = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(UserCodeLoader.LambdaCoreAssemblyName));
                UserCodeLoader.SetCustomerLoggerLogAction(assembly, loggerAction, _internalLogger);

                var userCodeLoader = new UserCodeLoader(handler, _internalLogger);
                var handlerWrapper = HandlerWrapper.GetHandlerWrapper(userCodeLoader.Invoke);
                var initializer    = new UserCodeInitializer(userCodeLoader, _internalLogger);
                var bootstrap      = new LambdaBootstrap(handlerWrapper, initializer.InitializeAsync)
                {
                    Client = testRuntimeApiClient
                };

                if (assertLoggedByInitialize != null)
                {
                    Assert.False(actionWriter.ToString().Contains($"^^[{assertLoggedByInitialize}]^^"));
                }

                await bootstrap.InitializeAsync();

                if (assertLoggedByInitialize != null)
                {
                    Assert.True(actionWriter.ToString().Contains($"^^[{assertLoggedByInitialize}]^^"));
                }

                var dataOut = await InvokeAsync(bootstrap, dataIn, testRuntimeApiClient);

                var actionText = actionWriter.ToString();

                return(new ExecutionInfo(bootstrap, dataIn, dataOut, actionText, null, userCodeLoader));
            }
        }
Exemplo n.º 6
0
        private async Task <string> InvokeAsync(LambdaBootstrap bootstrap, string dataIn, TestRuntimeApiClient testRuntimeApiClient)
        {
            testRuntimeApiClient.FunctionInput = dataIn != null?Encoding.UTF8.GetBytes(dataIn) : new byte[0];

            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                var exceptionWaiterTask = Task.Run(async() =>
                {
                    _output.WriteLine($"Waiting for an output.");
                    while (testRuntimeApiClient.LastOutputStream == null)
                    {
                    }
                    _output.WriteLine($"Output available.");
                    cancellationTokenSource.Cancel();
                    using (var reader = new StreamReader(testRuntimeApiClient.LastOutputStream))
                    {
                        return(await reader.ReadToEndAsync());
                    }
                });

                await bootstrap.RunAsync(cancellationTokenSource.Token);

                return(await exceptionWaiterTask);
            }
        }