|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | 4 | using System.IO.Compression; |
|
9 | 9 | using System.Threading.Tasks; |
10 | 10 |
|
11 | 11 | using Amazon.Lambda.APIGatewayEvents; |
| 12 | +using Amazon.Lambda.RuntimeSupport; |
| 13 | +using Amazon.Lambda.Serialization.SystemTextJson; |
12 | 14 | using Amazon.Lambda.TestUtilities; |
13 | 15 |
|
14 | 16 | using Newtonsoft.Json; |
15 | 17 | using Newtonsoft.Json.Linq; |
16 | 18 |
|
17 | 19 | using TestWebApp; |
18 | | - |
| 20 | +using TestWebApp.Controllers; |
19 | 21 | using Xunit; |
20 | 22 |
|
21 | 23 |
|
@@ -282,6 +284,30 @@ public async Task TestTraceIdSetFromLambdaContext() |
282 | 284 | } |
283 | 285 | } |
284 | 286 |
|
| 287 | + #if NET8_0_OR_GREATER |
| 288 | + /// <summary> |
| 289 | + /// Verifies that <see cref="HttpV2LambdaFunction.GetBeforeSnapshotRequests"/> is invoked during startup. |
| 290 | + /// </summary> |
| 291 | + /// <returns></returns> |
| 292 | + [Fact] |
| 293 | + public async Task TestSnapStartInitialization() |
| 294 | + { |
| 295 | + using var e1 = new EnvironmentVariableHelper("AWS_LAMBDA_FUNCTION_NAME", nameof(TestSnapStartInitialization)); |
| 296 | + using var e2 = new EnvironmentVariableHelper("AWS_LAMBDA_INITIALIZATION_TYPE", "snap-start"); |
| 297 | + using var e3 = new EnvironmentVariableHelper("AWS_LAMBDA_RUNTIME_API", "localhost:123"); |
| 298 | + |
| 299 | + var t = LambdaBootstrapBuilder.Create<APIGatewayHttpApiV2ProxyRequest>( |
| 300 | + new TestWebApp.HttpV2LambdaFunction().FunctionHandlerAsync, |
| 301 | + new DefaultLambdaJsonSerializer()) |
| 302 | + .Build() |
| 303 | + .RunAsync(); |
| 304 | + |
| 305 | + await Task.Delay(1000); |
| 306 | + |
| 307 | + Assert.True(SnapStartController.Invoked); |
| 308 | + } |
| 309 | + #endif |
| 310 | + |
285 | 311 | private async Task<APIGatewayHttpApiV2ProxyResponse> InvokeAPIGatewayRequest(string fileName, bool configureApiToReturnExceptionDetail = false) |
286 | 312 | { |
287 | 313 | return await InvokeAPIGatewayRequestWithContent(new TestLambdaContext(), GetRequestContent(fileName), configureApiToReturnExceptionDetail); |
@@ -310,4 +336,19 @@ private string GetRequestContent(string fileName) |
310 | 336 | return requestStr; |
311 | 337 | } |
312 | 338 | } |
| 339 | + |
| 340 | + public class EnvironmentVariableHelper : IDisposable |
| 341 | + { |
| 342 | + private string _name; |
| 343 | + private string? _oldValue; |
| 344 | + public EnvironmentVariableHelper(string name, string value) |
| 345 | + { |
| 346 | + _name = name; |
| 347 | + _oldValue = Environment.GetEnvironmentVariable(name); |
| 348 | + |
| 349 | + Environment.SetEnvironmentVariable(name, value); |
| 350 | + } |
| 351 | + |
| 352 | + public void Dispose() => Environment.SetEnvironmentVariable(_name, _oldValue); |
| 353 | + } |
313 | 354 | } |
0 commit comments