-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathRuntimeApiTests.cs
More file actions
153 lines (132 loc) · 6.16 KB
/
RuntimeApiTests.cs
File metadata and controls
153 lines (132 loc) · 6.16 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using Amazon.Runtime;
using Amazon.Lambda.Model;
using Amazon.Lambda.TestTool.Services;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestTool.Processes;
using Amazon.Lambda.TestTool.Commands.Settings;
using Amazon.Lambda.TestTool.Tests.Common.Helpers;
using Amazon.Lambda.TestTool.Tests.Common.Retries;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Environment = System.Environment;
using Xunit.Abstractions;
using Amazon.Lambda.TestTool.Tests.Common;
namespace Amazon.Lambda.TestTool.UnitTests;
public class RuntimeApiTests(ITestOutputHelper testOutputHelper)
{
#if DEBUG
[Fact]
#else
[Fact(Skip = "Skipping this test as it is not working properly.")]
#endif
public async Task AddEventToDataStore()
{
const string functionName = "FunctionFoo";
var lambdaPort = TestHelpers.GetNextLambdaRuntimePort();
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(15_000);
var options = new RunCommandSettings();
options.LambdaEmulatorPort = lambdaPort;
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
var testToolProcess = TestToolProcess.Startup(options, cancellationTokenSource.Token);
try
{
Assert.True(await TestHelpers.WaitForApiToStartAsync($"{testToolProcess.ServiceUrl}/lambda-runtime-api/healthcheck"));
var lambdaClient = ConstructLambdaServiceClient(testToolProcess.ServiceUrl);
var invokeFunction = new InvokeRequest
{
FunctionName = functionName,
Payload = "\"hello\"",
InvocationType = InvocationType.Event
};
await lambdaClient.InvokeAsync(invokeFunction, cancellationTokenSource.Token);
var dataStoreManager = testToolProcess.Services.GetRequiredService<IRuntimeApiDataStoreManager>();
var dataStore = dataStoreManager.GetLambdaRuntimeDataStore(functionName);
Assert.NotNull(dataStore);
Assert.Single(dataStore.QueuedEvents);
Assert.Single(dataStoreManager.GetListOfFunctionNames());
Assert.Equal(functionName, dataStoreManager.GetListOfFunctionNames().First());
var handlerCalled = false;
var handler = (string input, ILambdaContext context) =>
{
handlerCalled = true;
Thread.Sleep(1000); // Add a sleep to prove the LambdaRuntimeApi waited for the completion.
return input.ToUpper();
};
_ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
.ConfigureOptions(x => x.RuntimeApiEndpoint = $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}")
.Build()
.RunAsync(cancellationTokenSource.Token);
await Task.Delay(2_000, cancellationTokenSource.Token);
Assert.True(handlerCalled);
}
finally
{
await cancellationTokenSource.CancelAsync();
}
}
[RetryFact]
public async Task InvokeRequestResponse()
{
const string functionName = "FunctionFoo";
var lambdaPort = TestHelpers.GetNextLambdaRuntimePort();
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(15_000);
var options = new RunCommandSettings();
options.LambdaEmulatorPort = lambdaPort;
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
var testToolProcess = TestToolProcess.Startup(options, cancellationTokenSource.Token);
try
{
Assert.True(await TestHelpers.WaitForApiToStartAsync($"{testToolProcess.ServiceUrl}/lambda-runtime-api/healthcheck"));
var handler = (string input, ILambdaContext context) =>
{
Thread.Sleep(1000); // Add a sleep to prove the LambdaRuntimeApi waited for the completion.
return input.ToUpper();
};
_ = LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
.ConfigureOptions(x => x.RuntimeApiEndpoint = $"{options.LambdaEmulatorHost}:{options.LambdaEmulatorPort}/{functionName}")
.Build()
.RunAsync(cancellationTokenSource.Token);
var lambdaClient = ConstructLambdaServiceClient(testToolProcess.ServiceUrl);
// Test with relying on the default value of InvocationType
var invokeFunction = new InvokeRequest
{
FunctionName = functionName,
Payload = "\"hello\""
};
var response = await lambdaClient.InvokeAsync(invokeFunction, cancellationTokenSource.Token);
var responsePayloadString = System.Text.Encoding.Default.GetString(response.Payload.ToArray());
Assert.Equal("\"HELLO\"", responsePayloadString);
// Test with InvocationType explicilty set
invokeFunction = new InvokeRequest
{
FunctionName = functionName,
Payload = "\"hello\"",
InvocationType = InvocationType.RequestResponse
};
response = await lambdaClient.InvokeAsync(invokeFunction, cancellationTokenSource.Token);
responsePayloadString = System.Text.Encoding.Default.GetString(response.Payload.ToArray());
Assert.Equal("\"HELLO\"", responsePayloadString);
}
finally
{
await cancellationTokenSource.CancelAsync();
}
}
private IAmazonLambda ConstructLambdaServiceClient(string url)
{
var config = new AmazonLambdaConfig
{
ServiceURL = url,
MaxErrorRetry = 0
};
// We don't need real credentials because we are not calling the real Lambda service.
var credentials = new BasicAWSCredentials("accessKeyId", "secretKey");
return new AmazonLambdaClient(credentials, config);
}
}