-
Notifications
You must be signed in to change notification settings - Fork 504
Expand file tree
/
Copy pathAddAWSLambdaBeforeSnapshotRequestTests.cs
More file actions
63 lines (50 loc) · 2.29 KB
/
Copy pathAddAWSLambdaBeforeSnapshotRequestTests.cs
File metadata and controls
63 lines (50 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using Amazon.Lambda.AspNetCoreServer.Test;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Amazon.Lambda.AspNetCoreServer.Hosting.Tests;
/// <summary>
/// Tests for <see cref="ServiceCollectionExtensions.AddAWSLambdaBeforeSnapshotRequest"/>
/// </summary>
public class AddAWSLambdaBeforeSnapshotRequestTests
{
[Theory]
[InlineData(LambdaEventSource.HttpApi)]
[InlineData(LambdaEventSource.RestApi)]
[InlineData(LambdaEventSource.ApplicationLoadBalancer)]
public async Task VerifyCallbackIsInvoked(LambdaEventSource hostingType)
{
using var e1 = new EnvironmentVariableHelper("AWS_LAMBDA_FUNCTION_NAME", nameof(VerifyCallbackIsInvoked));
using var e2 = new EnvironmentVariableHelper("AWS_LAMBDA_INITIALIZATION_TYPE", "snap-start");
var callbackDidTheCallback = false;
var builder = WebApplication.CreateSlimBuilder(new string[0]);
builder.Services.AddAWSLambdaHosting(hostingType);
// Initialize asp.net pipeline before Snapshot
builder.Services.AddAWSLambdaBeforeSnapshotRequest(
new HttpRequestMessage(HttpMethod.Get, "/test")
);
var app = builder.Build();
app.MapGet($"/test",
() =>
{
callbackDidTheCallback = true;
return "Success";
});
var serverTask = app.RunAsync();
// Poll for the before-snapshot callback to fire rather than racing a single fixed delay.
// A fixed 500 ms window is flaky under load (observed intermittently in CI): the
// before-snapshot request may not have completed yet, leaving the callback unset. Wait up
// to 10 seconds, checking frequently, and stop as soon as the callback has run.
var timeout = TimeSpan.FromSeconds(10);
var sw = System.Diagnostics.Stopwatch.StartNew();
while (!callbackDidTheCallback && sw.Elapsed < timeout && !serverTask.IsCompleted)
{
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
// shut down server
await app.StopAsync();
Assert.True(callbackDidTheCallback);
}
}